/* GAUSS Sample program for the Grouped T copula simulation and estimation Built by Dean Fantazzini Email: deanfa@eco.unipv.it Web page: http://economia.unipv.it/pagp/pagine_personali/dean/ 14-09-05 */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS for A PARTICULAR PURPOSE. See the GNU General Public License for more details*/ new; cls; //True Correlation matrix for a sample of 30 assets matrixr ={ 1.0000000 0.20837582 0.32662575 0.21571619 0.35714766 0.30297373 0.36648780 0.34124000 0.30516635 0.47274791 0.26796437 0.30428628 0.40859254 0.33028488 0.22521741 0.30685596 0.34070398 0.23552303 0.23398736 0.40876130 0.30502877 0.27171844 0.22925382 0.25995795 0.20578923 0.30586138 0.23887214 0.39015609 0.32975026 0.26345153 , 0.20837582 1.0000000 0.20371453 0.14970417 0.27250201 0.18331431 0.17658945 0.30790898 0.19542824 0.20888312 0.17096046 0.20360301 0.32446558 0.26863965 0.23459439 0.25521488 0.22486840 0.26208333 0.23819618 0.21003508 0.26301295 0.18776885 0.16960385 0.20931388 0.25233474 0.15195626 0.32432228 0.18055400 0.24005497 0.26924820 , 0.32662575 0.20371453 1.0000000 0.16217538 0.32053491 0.28284379 0.40171543 0.32911091 0.17407081 0.42462877 0.24832344 0.26102187 0.37289615 0.32869143 0.23469045 0.28873022 0.35479728 0.23208838 0.24356670 0.47109788 0.32573504 0.17429358 0.22185235 0.19670900 0.22938030 0.15713486 0.20954507 0.36967095 0.23680455 0.28779786 , 0.21571619 0.14970417 0.16217538 1.0000000 0.19505988 0.16079767 0.17917986 0.20340659 0.26994426 0.19604188 0.16792459 0.19503437 0.22880130 0.16855936 0.13575515 0.17994736 0.17199619 0.13101947 0.15960818 0.16261413 0.17790084 0.24633780 0.21018213 0.25501076 0.16259061 0.25903101 0.21292931 0.15825045 0.18658501 0.19171034 , 0.35714766 0.27250201 0.32053491 0.19505988 1.0000000 0.32034543 0.32556237 0.55322738 0.32510489 0.34767021 0.26790173 0.26018138 0.52534651 0.36218654 0.29984165 0.40068866 0.34825619 0.32277154 0.32189713 0.30858443 0.53058060 0.32111689 0.28130928 0.30388691 0.32365714 0.26735321 0.29858580 0.37727953 0.39441215 0.36665462 , 0.30297373 0.18331431 0.28284379 0.16079767 0.32034543 1.0000000 0.28322854 0.31509043 0.26030896 0.31039866 0.20739138 0.23238587 0.37560515 0.27586542 0.22794457 0.30954137 0.35163629 0.24975535 0.21635903 0.26991986 0.28197784 0.23986673 0.22221643 0.25005195 0.23769070 0.20720449 0.20114568 0.42815735 0.28437413 0.28502843 , 0.36648780 0.17658945 0.40171543 0.17917986 0.32556237 0.28322854 1.0000000 0.34964409 0.22708448 0.40398507 0.26697723 0.22266130 0.39327333 0.35954360 0.23851006 0.31992124 0.35021622 0.23661756 0.25170669 0.42197439 0.31640630 0.19844938 0.23005022 0.20442451 0.23667386 0.22015265 0.18844489 0.37855345 0.28516009 0.27446576 , 0.34124000 0.30790898 0.32911091 0.20340659 0.55322738 0.31509043 0.34964409 1.0000000 0.30766600 0.35421894 0.27168964 0.27331523 0.51631915 0.36983917 0.30624946 0.41807938 0.36065002 0.34844395 0.34301444 0.29334268 0.57429121 0.31135592 0.26740195 0.31822865 0.35374122 0.27682535 0.29393954 0.38408566 0.38082306 0.35721051 , 0.30516635 0.19542824 0.17407081 0.26994426 0.32510489 0.26030896 0.22708448 0.30766600 1.0000000 0.29665791 0.20082987 0.31087705 0.38261241 0.21869855 0.17296547 0.30716256 0.23274819 0.17487034 0.16483484 0.20888099 0.25233388 0.38707478 0.31777145 0.35794588 0.21854181 0.42007955 0.28894477 0.26535890 0.34668377 0.25621486 , 0.47274791 0.20888312 0.42462877 0.19604188 0.34767021 0.31039866 0.40398507 0.35421894 0.29665791 1.0000000 0.28295485 0.32865113 0.42184051 0.36104049 0.23637639 0.30603650 0.37295697 0.21569215 0.23706144 0.46129982 0.31113731 0.25376030 0.23379586 0.27191325 0.22046698 0.29090032 0.23860262 0.38506896 0.33500476 0.27163982 , 0.26796437 0.17096046 0.24832344 0.16792459 0.26790173 0.20739138 0.26697723 0.27168964 0.20082987 0.28295485 1.0000000 0.18225112 0.30089884 0.25408849 0.22885412 0.23302130 0.25025722 0.22753036 0.21475525 0.24899567 0.27494562 0.19134956 0.18261343 0.20383850 0.21700549 0.18731448 0.18180087 0.26122599 0.22582690 0.23813472 , 0.30428628 0.20360301 0.26102187 0.19503437 0.26018138 0.23238587 0.22266130 0.27331523 0.31087705 0.32865113 0.18225112 1.0000000 0.32113777 0.20754958 0.16393841 0.21772535 0.26354188 0.17252388 0.19288095 0.22258146 0.22228531 0.29021843 0.20076632 0.26336763 0.20664155 0.20057120 0.28355222 0.27381113 0.26309856 0.21729237 , 0.40859254 0.32446558 0.37289615 0.22880130 0.52534651 0.37560515 0.39327333 0.51631915 0.38261241 0.42184051 0.30089884 0.32113777 1.0000000 0.41235168 0.35881206 0.44791949 0.42564067 0.37816682 0.38304250 0.37726064 0.48060934 0.37373245 0.33384947 0.36075648 0.39452983 0.34801797 0.33029073 0.45679534 0.46217096 0.42744642 , 0.33028488 0.26863965 0.32869143 0.16855936 0.36218654 0.27586542 0.35954360 0.36983917 0.21869855 0.36104049 0.25408849 0.20754958 0.41235168 1.0000000 0.28132968 0.33840641 0.32670725 0.32324526 0.28469966 0.34847523 0.37719720 0.21884818 0.24107749 0.24929649 0.29438149 0.19713714 0.22477502 0.35843161 0.30505420 0.30574563 , 0.22521741 0.23459439 0.23469045 0.13575515 0.29984165 0.22794457 0.23851006 0.30624946 0.17296547 0.23637639 0.22885412 0.16393841 0.35881206 0.28132968 1.0000000 0.29153176 0.27419559 0.46639727 0.42632483 0.18992196 0.33309432 0.16123768 0.19950093 0.18750231 0.39916445 0.12861989 0.17269609 0.26555470 0.25156840 0.29804945 , 0.30685596 0.25521488 0.28873022 0.17994736 0.40068866 0.30954137 0.31992124 0.41807938 0.30716256 0.30603650 0.23302130 0.21772535 0.44791949 0.33840641 0.29153176 1.0000000 0.30029639 0.30734768 0.28636193 0.31593226 0.37872299 0.27662916 0.29913763 0.28260351 0.34886872 0.25553586 0.21878746 0.33652373 0.53889584 0.34488003 , 0.34070398 0.22486840 0.35479728 0.17199619 0.34825619 0.35163629 0.35021622 0.36065002 0.23274819 0.37295697 0.25025722 0.26354188 0.42564067 0.32670725 0.27419559 0.30029639 1.0000000 0.25459273 0.26245042 0.36109142 0.32881891 0.20952335 0.21079769 0.22121532 0.27149767 0.21024834 0.23379648 0.43593784 0.27018775 0.30836731 , 0.23552303 0.26208333 0.23208838 0.13101947 0.32277154 0.24975535 0.23661756 0.34844395 0.17487034 0.21569215 0.22753036 0.17252388 0.37816682 0.32324526 0.46639727 0.30734768 0.25459273 1.0000000 0.42903872 0.20451046 0.33735630 0.19001030 0.19006285 0.19211447 0.53836605 0.15516264 0.18888313 0.24083597 0.27824731 0.28429512 , 0.23398736 0.23819618 0.24356670 0.15960818 0.32189713 0.21635903 0.25170669 0.34301444 0.16483484 0.23706144 0.21475525 0.19288095 0.38304250 0.28469966 0.42632483 0.28636193 0.26245042 0.42903872 1.0000000 0.19636117 0.32810120 0.20102981 0.20309388 0.21534669 0.38636987 0.15631602 0.20694850 0.25685071 0.25866668 0.28445071 , 0.40876130 0.21003508 0.47109788 0.16261413 0.30858443 0.26991986 0.42197439 0.29334268 0.20888099 0.46129982 0.24899567 0.22258146 0.37726064 0.34847523 0.18992196 0.31593226 0.36109142 0.20451046 0.19636117 1.0000000 0.28948145 0.18967087 0.20895873 0.20439568 0.21984154 0.19554775 0.18457561 0.36273477 0.26878883 0.26274995 , 0.30502877 0.26301295 0.32573504 0.17790084 0.53058060 0.28197784 0.31640630 0.57429121 0.25233388 0.31113731 0.27494562 0.22228531 0.48060934 0.37719720 0.33309432 0.37872299 0.32881891 0.33735630 0.32810120 0.28948145 1.0000000 0.24696739 0.24884304 0.28843970 0.34219097 0.21607494 0.28771153 0.33983802 0.34373990 0.35529357 , 0.27171844 0.18776885 0.17429358 0.24633780 0.32111689 0.23986673 0.19844938 0.31135592 0.38707478 0.25376030 0.19134956 0.29021843 0.37373245 0.21884818 0.16123768 0.27662916 0.20952335 0.19001030 0.20102981 0.18967087 0.24696739 1.0000000 0.27776731 0.54568304 0.24277630 0.38892223 0.27503501 0.23458636 0.32238335 0.24556123 , 0.22925382 0.16960385 0.22185235 0.21018213 0.28130928 0.22221643 0.23005022 0.26740195 0.31777145 0.23379586 0.18261343 0.20076632 0.33384947 0.24107749 0.19950093 0.29913763 0.21079769 0.19006285 0.20309388 0.20895873 0.24884304 0.27776731 1.0000000 0.25708367 0.19178998 0.30316380 0.23114759 0.25329427 0.30283765 0.24605707 , 0.25995795 0.20931388 0.19670900 0.25501076 0.30388691 0.25005195 0.20442451 0.31822865 0.35794588 0.27191325 0.20383850 0.26336763 0.36075648 0.24929649 0.18750231 0.28260351 0.22121532 0.19211447 0.21534669 0.20439568 0.28843970 0.54568304 0.25708367 1.0000000 0.24605545 0.34711507 0.25650426 0.24034722 0.32000644 0.24117934 , 0.20578923 0.25233474 0.22938030 0.16259061 0.32365714 0.23769070 0.23667386 0.35374122 0.21854181 0.22046698 0.21700549 0.20664155 0.39452983 0.29438149 0.39916445 0.34886872 0.27149767 0.53836605 0.38636987 0.21984154 0.34219097 0.24277630 0.19178998 0.24605545 1.0000000 0.16111090 0.21374165 0.26355134 0.31632476 0.29172660 , 0.30586138 0.15195626 0.15713486 0.25903101 0.26735321 0.20720449 0.22015265 0.27682535 0.42007955 0.29090032 0.18731448 0.20057120 0.34801797 0.19713714 0.12861989 0.25553586 0.21024834 0.15516264 0.15631602 0.19554775 0.21607494 0.38892223 0.30316380 0.34711507 0.16111090 1.0000000 0.25695541 0.23846409 0.31537402 0.19210170 , 0.23887214 0.32432228 0.20954507 0.21292931 0.29858580 0.20114568 0.18844489 0.29393954 0.28894477 0.23860262 0.18180087 0.28355222 0.33029073 0.22477502 0.17269609 0.21878746 0.23379648 0.18888313 0.20694850 0.18457561 0.28771153 0.27503501 0.23114759 0.25650426 0.21374165 0.25695541 1.0000000 0.23484455 0.28833827 0.22789356 , 0.39015609 0.18055400 0.36967095 0.15825045 0.37727953 0.42815735 0.37855345 0.38408566 0.26535890 0.38506896 0.26122599 0.27381113 0.45679534 0.35843161 0.26555470 0.33652373 0.43593784 0.24083597 0.25685071 0.36273477 0.33983802 0.23458636 0.25329427 0.24034722 0.26355134 0.23846409 0.23484455 1.0000000 0.30507380 0.32832785 , 0.32975026 0.24005497 0.23680455 0.18658501 0.39441215 0.28437413 0.28516009 0.38082306 0.34668377 0.33500476 0.22582690 0.26309856 0.46217096 0.30505420 0.25156840 0.53889584 0.27018775 0.27824731 0.25866668 0.26878883 0.34373990 0.32238335 0.30283765 0.32000644 0.31632476 0.31537402 0.28833827 0.30507380 1.0000000 0.30414937 , 0.26345153 0.26924820 0.28779786 0.19171034 0.36665462 0.28502843 0.27446576 0.35721051 0.25621486 0.27163982 0.23813472 0.21729237 0.42744642 0.30574563 0.29804945 0.34488003 0.30836731 0.28429512 0.28445071 0.26274995 0.35529357 0.24556123 0.24605707 0.24117934 0.29172660 0.19210170 0.22789356 0.32832785 0.30414937 1.0000000} ; //Identifiers of the Groups (for example, rating classes): 1=AAA, 2=AA, 3=A, 4=BBB, 5=BB groups_all={ 2 5 3 4 3 3 3 2 3 2 5 1 1 5 3 2 3 3 3 4 3 1 3 2 1 2 3 3 2 3}; //Degrees of freedom of the different groups dof={ 3 5 10 14 6}; print "True Degrees of Freedom (Grouped T copula)="; dof'; //Other examples //matrixr = { 1 0.3 -0.2 0.5, 0.3 1 -0.25 0.4, -0.2 -0.25 1 0.1, 0.5 0.4 0.1 1}; //groups_all={1 1 2 2}; //dof={ 4 8 }; print "True Degrees of Freedom (Grouped T copula)="; dof'; //number of scenarios scenarios=2000; //Number of d.o.f. at which I compute the T-copula likelihood, starting from 2:the higher the slower the estimation process precision_dof=25; mat=GTcop_simul(matrixr,dof,groups_all, scenarios); {rho_est,gradi}=const_gtcop(mat,groups_all,precision_dof);// rho_est is the estimated correlation matrix print "Estimated Degrees of Freedom (Grouped T copula)="; gradi; /*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @ PROCEDURES @-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ /*GROUPED T Copula Simulation*/ proc(1)=GTcop_simul(rho,dof, gru, scenarios); local gru_type,i,matrixr, randomunif,xx,uu,zz,t,yy,j,serie; gru_type=gruppi(gru); matrixr =rho; randomunif=rndn(cols(rho),scenarios); yy =chol(matrixr)'*randomunif; t = cdfchii(rndu(cols(gru_type),scenarios),dof'); xx=zeros(rows(yy),cols(yy)); uu=zeros(rows(yy),cols(yy)); for i(1,cols(gru_type),1); for j(1,rows(gru_type),1); if gru_type[j,i] .ne 0; serie= gru_type[j,i]; xx[serie,.]=(sqrt(dof[i]).*yy[serie,.])./sqrt(t[i,.]); uu[serie,.] = 1-cdftc(xx[serie,.], dof[i]); endif; endfor; endfor; retp(uu'); endp; /* ========================================================================================================= @ @ ========================================================================================================== */ /*GROUPED T Copula Estimation*/ proc(2)= const_gtcop(mat, gru, precision); local gru_type,inv_cdf,rho_par,gr,mat_gt,inverse,rho, somma,loglik,tdf,i,j,serie,nu,invcdf,rhodet,rhoinv,k,cl,t,indice,gradi; gru_type=gruppi(gru); inverse = cdfni(mat); /*1-step: Normal copula estimation of the correlation matrix*/ rho= corrx(inverse); somma={}; for i(1,cols(gru_type),1); /*2-step-a: T-copula estimation for each group*/ mat_gt={}; for j(1,rows(gru_type),1); if gru_type[j,i] .ne 0; serie= gru_type[j,i]; mat_gt=mat_gt~mat[.,serie]; endif; endfor; {rho_par,gr}=const_tcop(mat_gt,precision); somma=somma|gr; endfor; inv_cdf=zeros(rows(mat),cols(mat)); for i(1,cols(gru_type),1); /*2-step-b: creation of inverse cdfs with different d.o.f. */ for j(1,rows(gru_type),1); if gru_type[j,i] .ne 0; serie= gru_type[j,i]; inv_cdf[.,serie]=cdftci(1-mat[.,serie],somma[i]); endif; endfor; endfor; rho=corrx(inv_cdf); /*3-step: Normal copula estimation with studentized inverse cdfs*/ retp(rho,somma); endp; /* T Copula Estimation*/ proc(2)= const_tcop(mat, precision); local inverse,rho, loglik,tdf,i,nu,invcdf,rhodet,rhoinv,k,cl,t,indice,gradi; inverse = cdfni(mat); rho= corrx(inverse); loglik=zeros(precision,1); tdf=seqa(2, 1, precision); for i(1,precision,1); nu=tdf[i,1]; invcdf=cdftci(1-mat,nu); RHOdet = det(RHO); RHOinv = inv(RHO); k=cols(mat); CL = zeros(rows(mat),1); for t(1,rows(mat),1); CL[t,1] = ln(gamma((NU+k)/2)) + (k-1)*ln(gamma(NU/2)) - k*ln(gamma((NU+1)/2)) - 0.5*ln(RHOdet)- (NU+k)/2*ln(1+(invcdf[t,1:k]*RHOinv*(invcdf[t,1:k]'))/NU) + (NU+1)/2*sumc( ln( 1+(invcdf[t,1:k].^2)/NU)' ); endfor; loglik[i,1] = sumc(CL); endfor; indice=maxindc(loglik); gradi=tdf[indice,1]; invcdf=cdftci(1-mat,gradi); rho=corrx(invcdf); /*print "Correlation Matrix (T_cop)="; rho; print "Degrees of Freedom (T_cop)="; gradi;*/ retp(rho,gradi); endp; /*Create the identifiers for the different series, signalling their groups*/ proc gruppi(gru); /*WARNING: the gru matrix must have the group number expressed in the first row*/ local m,aa,gru_min,gru_max,i,j,gru_fin; gru_min =minc(gru'); gru_max =maxc(gru'); aa=gru'; gru_fin=zeros(rows(aa),gru_max); for i(1,rows(aa),1); for j(1,gru_max,1); if aa[i,1]==j; gru_fin[i,j]= i; endif; endfor; endfor; retp(gru_fin); endp;