I'm playing around with copulas and wanted to generate some sample based on copula techniques in R. For this purpose I applied the following algorithm:

  1. Generate three sample vectors coming from different distributions (normal, t, uniform), i.e. $X=(X_1,\dots,X_{100})$,$Y=(Y_1,\dots,Y_{100})$,$Z=(Z_1,\dots,Z_{100})$
  2. Apply empirical distribution function to each of the three assets. I take the empirical distribution function, since in reality you may not know the actual marginal distribution. In math terms we estimate $F_X,F_Y,F_Z$ based on $X,Y,Z$ and apply it: $$U_1=F_X(X),U_2=F_Y(Y),U_3=F_Z(Z)$$
  3. Fit a t-copula to the resulting pseudo-unfiromly distributed samples, i.e. we fit a copula $c$ to $c(U_1,U_2,U_3)$
  4. generate $100$ sample matrices $A_i$, each of size $200$ per marginal sample, i.e. we generate $100$ matrices $A_i=[S_1^i,S_2^i,S_3^i]$ with $200$ rows and $3$ column vector $S_1^i,S^i_2,S^i_3$ containing generated samples coming from $c$.
  5. retransform back to obtain actual marginal distribution sample coming from a t-copula model, i.e. for each of the $A_i$ we look at $$B_i=[F_X^{-1}(S^i_1),F_Y^{-1}(S^i_2),F_Z^{-1}(S^i_3)] $$

here is a sample R code

a <- rnorm(1000,1,0.3)
b <- rt(1000,4)
c <- runif(1000)

data <- cbind(a,b,c)

#calculate emprical distribution

for (i in 1:ncol(data)){
  emp <- apply(data,2,ecdf)


data.unif <- cbind(emp[[1]](data[,1]),emp[[2]](data[,2]),emp[[3]](data[,3]))

#fit copula

fitCopulat <- fit.tcopula(Udata = data.unif,method = "Kendall")

l1 <- list()

for(i in 1:100){
  l1[[i]] <- rcopula.t(200,df = fitCopulat$nu,Sigma = fitCopulat$P)

#transform back to get right marginal distributions

l2 <- l1
for(i in 1:100){
  for(j in 1:ncol(data)){
    l2[[i]][,j] <- quantile(data[,j],probs = l1[[i]][,j])

I have the following two questions:

  1. Does the algorithm make sense?
  2. Is step 5. correctly implemented? I'm unsure if I used the function quantile correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.