## Gaussian copula - toy example

For posting your own programs to share with others

Moderators: EViews Gareth, EViews Moderator

joff
Posts: 6
Joined: Thu Jan 04, 2018 4:20 am

### Gaussian copula - toy example

This is just a simple example of Gaussian copula written for educational purposes but maybe someone will find it usefull.

Code: Select all

`'Example of Gaussian copula with arbitrary marginal distributions (simulation and estimation)'wfcreate u 5000scalar n = @obssmplscalar k = 3'Simulate k series using standard normal distribution and put them in matrix B_mat:series _x = nrndseries _y = nrndseries _z = nrndstom(_x,x)stom(_y,y)stom(_z,z)matrix(n,k) B_matcolplace(B_mat,x,1)colplace(B_mat,y,2)colplace(B_mat,z,3)'Calibrate the covariance (correlation) matrix A_sym:matrix(k,k) corr_matcorr_mat.fill 1, 0.7,-0.4,0.7,1, 0.1, -0.4, 0.1,1sym A_sym = @implode(corr_mat)'Simulate from multivariate normal using B_mat and Cholesky factor of A_sym:matrix chol=@cholesky(A_sym)matrix MVN_mat = chol*@transpose(B_mat)matrix MVN_mat = @transpose(MVN_mat)'Create uniform marginals for each variable (u_*) and map them to arbitrary marginals (m_*)'(here Beta for x, Gamma for y and Normal for z) vector(n) u_xvector(n) u_yvector(n) u_zvector(n) m_xvector(n) m_yvector(n) m_zfor !k = 1 to nu_x(!k) = 1-@cnorm(MVN_mat(!k,1))m_x(!k) = @qbeta(u_x(!k),2,2)u_y(!k) = 1-@cnorm(MVN_mat(!k,2))m_y(!k) = @qgamma(u_y(!k),2,1)u_z(!k) = 1-@cnorm(MVN_mat(!k,3))m_z(!k) = @qnorm(u_z(!k))next'Put results in U_mat and XYZ_mat:matrix(n,k) U_matcolplace(U_mat,u_x,1)colplace(U_mat,u_y,2)colplace(U_mat,u_z,3)matrix(n,k) XYZ_matcolplace(XYZ_mat,m_x,1)colplace(XYZ_mat,m_y,2)colplace(XYZ_mat,m_z,3)'This shows that that correlation structure is preserved for arbitrary marginals:freeze(MVN_correlation) MVN_mat.cov(outfmt=sheet) tauafreeze(U_correlation) U_mat.cov(outfmt=sheet) tauafreeze(xyz_correlation) xyz_mat.cov(outfmt=sheet) taua'Estimating Gaussian copula, i.e. Pearson correlation matrix (coefficients), using Kendall's tau calculated from the simulated data (marginals)scalar pi = 3.1416scalar ktau12 = @val(xyz_correlation(4,2))scalar rho12  = @sin((pi/2)*ktau12)scalar ktau13 = @val(xyz_correlation(5,2))scalar rho13  = @sin((pi/2)*ktau13)scalar ktau23 = @val(xyz_correlation(5,3))scalar rho23  = @sin((pi/2)*ktau23)`