tnagler / VineCopula

Statistical inference of vine copulas
87 stars 32 forks source link

substitution of dducopula? #90

Closed chienyutseng closed 10 months ago

chienyutseng commented 10 months ago

Hi, I am trying to do the decomposition(pairwise) conditional sampling from vine distributions. I did found the example from VC2copula:

H <- rnorm(100)
D <- rnorm(100)
I <- rnorm(100)

u_H <- pobs(H)
u_D <- pobs(D) 
u_I <- pobs(I)

C_HD <- BB1Copula()
C_HIgivenD <- BB1Copula()  

k <- ddvCopula(cbind(u_H, u_D), C_HD)
m <- dduCopula(cbind(u_D, u_I), C_DI)
cond_prob <- dduCopula(cbind(k, m), C_HIgivenD)

but I encountered a problem, if I change the pair-copula "BB1Copula()" to "BiCop(family = 1, par =0.36)", the ddvCopula function won't work. so I tried to build the variables "k" and "m" by "BiCopHinv()", however, got stuck at the " _dduCopula(cbind(k, m), CHIgivenD)", when trying to derivative multiple variables. I am wondering if I failed to find the substitution of dduCopula in VineCopula or if there's some issue with my current understanding.

tnagler commented 10 months ago

Hi! Your example doesn't run, C_DI is not defined. Please also provide code for the cases where an error occurs, so I don't have to guess what to replace.

chienyutseng commented 10 months ago

Apologize for the typo, i did defined C_DI at my local side and the example can worked. What couldn't work is when i try to replace BB1Copula() to BiCop(), as code show below

H <- rnorm(100)
D <- rnorm(100)
I <- rnorm(100)

u_H <- pobs(H)
u_D <- pobs(D) 
u_I <- pobs(I)

C_HD <- BiCop(family = 1, par =0.36)
C_DI <- BiCop(family = 1, par =0.53)
C_HIgivenD <- BiCop(family = 1, par =0.54)

k <- ddvCopula(cbind(u_H, u_D), C_HD)
m <- dduCopula(cbind(u_D, u_I), C_DI)
cond_prob <- dduCopula(cbind(k, m), C_HIgivenD)
tnagler commented 10 months ago

OK, a few things:

  1. The code you're writing is not for conditional sampling, it computes a conditional probability. Conditional sampling is the inverse of the transformation you're computing.
  2. You cannot use copula::ddvCopula() with VineCopula::BiCop objects. You have to replace the BiCop() calls with BiCop2copula().
  3. Even then, the corresponding ddvCopula()and dduCopula() methods were not defined in the VC2copula package (my bad). This is fixed in the dev branch. You can update using remotes::install_github("tnagler/VC2copula@dev")
chienyutseng commented 10 months ago

Thank you for your supplement. According to what you mentioned first, what i trying to do is conditional sampling, as the formula below, which is what i am having trouble with.

x1 = w1, x2 = F^-1(w2|x1), x3 = F^-1(w3|x1, x2), with F(x j |x1, . . . , x j−1) = ∂ C j, j−1|1,..., j−2 {F(x j |x1, . . . , x j−2), F(x j−1|x1, . . . , x j−2)} / ∂ F(x j−1|x1, . . . , x j−2)

if i wasn't wrong,i can get x2 from VineCopula::BiCopHinv, but is there any function for getting x3 from given x1 and x2?

tnagler commented 10 months ago
chienyutseng commented 10 months ago

The following snapshot(Kjersti et al., 2007), is what I trying to do. i have gotten a fitted 3D CVine Copula(for variable X,Y,Z) from my observation:

** Tree: 0
2,1 <-> Gaussian, parameters = 0.485697
1,3 <-> Gaussian, parameters = 2.10785
** Tree: 1
2,3 | 1 <-> Joe 90°, parameters = -1.08

The algorithm I planned to build is getting X from another Copula and viewed as observation, then representing as conditional value for the 3D CVine copula, and getting Y & Z by the pair-copulae decomposed model.

image
tnagler commented 10 months ago

OK, the three dimensional example is fairly easy:

RVM <- C2RVine(1:3, family = c(1, 4, 6), par = c(0.3, 2.4, 1.5))
W <- cbind(w1 = 0.2, runif(100), runif(100))
RVineSim(100, RVM, U = W)

Here we're simulating x1, x2, x3 conditional on x2 = 0.2. You can adapt this to your liking.

chienyutseng commented 10 months ago

thanks a alot! i think this is what i want. but please allow me to ask futher questions.

I am seeking clarification on the specific scenarios in which the pair-copula decomposition model is most appropriately applied. Many journals refer to this method in the context of vine-copula simulations, and I was under the impression that this methodology should be applied for my needs. Could you please provide some insight on this?

tnagler commented 10 months ago

I can't really answer this question without knowing much more about your application context. But if you want to simulate two variables conditional on one other variable, this is a viable method. Closing this now, since the software part seems to be solved.

chienyutseng commented 10 months ago

Really appreciate for the assistance!