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

Postby joff » Fri Mar 16, 2018 4:09 am

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 5000

scalar n = @obssmpl
scalar k = 3

'Simulate k series using standard normal distribution and put them in matrix B_mat:

series _x = nrnd
series _y = nrnd
series _z = nrnd

stom(_x,x)
stom(_y,y)
stom(_z,z)

matrix(n,k) B_mat

colplace(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_mat
corr_mat.fill 1, 0.7,-0.4,0.7,1, 0.1, -0.4, 0.1,1
sym 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_x
vector(n) u_y
vector(n) u_z

vector(n) m_x
vector(n) m_y
vector(n) m_z

for !k = 1 to n
u_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_mat

colplace(U_mat,u_x,1)
colplace(U_mat,u_y,2)
colplace(U_mat,u_z,3)

matrix(n,k) XYZ_mat

colplace(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) taua
freeze(U_correlation) U_mat.cov(outfmt=sheet) taua
freeze(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.1416

scalar 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)

Return to “Program Repository”

Who is online

Users browsing this forum: No registered users and 7 guests