 # Get covariance matrix in middle of iteration in cmlmt module?

HI there

How can I get covariance matrix of estimated parameters in the middle of iteration  in CMLMT modul?

0

The covariance matrix is computed only at the end of iterations, so this is what I suggest:  set the MaxIters member of the cmlmtControl structure to some limited amount, five say, then print (or save) the covariance matrix, the covPar member of the cmlmtResults structure.  Also save the estimates, the Par member of the cmlmtResults structure and use as start values for another run.  This could be put in a loop and you could print every fifth covariance matrix of the parameters up to convergence.

``````library cmlmt;

y = // endogenous data

x = // exogenous data

struct PV p0;

po = pvPack(pvcreate,@starting point@,"b");

proc fct(p0,y,x,ind);

......   likelihood calculation

endp;

struct cmlmtControl c0;

c0.maxiters = 5;

convergence = 1;

do until convergence == 0;

struct cmlmtResults  = out;

out = cmlmt(&fct,p0,y,x,c0);

print out.covpar;

p0 = out.par;

convergence = out.retcode;

endo;`````` 0

Hi Mr Ron Schoenberg

Thank you for answering my question. Unfortunately again errors which already happened, still remain and I can not solve it. if it is possible, I send you code and data.  and guide me how I solve this dilemma

``````// Libraries
library cmlmt,pgraph;

// Data

y=x-meanc(x)';

n=rows(y);

k=cols(y);

struct DS d0;

d0=dsCreate;

d0.DataMatrix=y;

// Declarations

varred=vcx(y);

epsilon=y';

avec=zeros(k,n);

lnvar=zeros(k,n);

lnvar[.,1]=ln(diag(varred));

var=diagrv(eye(k),exp(lnvar[.,1]));

ind=1|1|1;

// Parameters

struct PV p;

a12=0;

b12=0;

b122=0;

b123=0;

a21=0;

b21=0;

b212=0;

b213=0;

A=1~avec[1,1]|avec[2,1]~1;

g=eye(k)*0.95;

d=eye(k)*0.15;

f=eye(k)*(-0.09);

c=lnvar[.,1].*(1-diag(g));

uncs=A*varred*A';

rho=uncs[1,2]/sqrt(uncs[1,1]*uncs[2,2]);

p=pvPack(pvCreate,a12,"a12");

p=pvPack(p,b12,"b12");

p=pvPack(p,b122,"b122");

p=pvPack(p,b123,"b123");

p=pvPack(p,a21,"a21");

p=pvPack(p,b21,"b21");

p=pvPack(p,b212,"b212");

p=pvPack(p,b213,"b213");

p=pvPack(p,c,"c");

p=pvPack(p,g,"g");

p=pvPack(p,d,"d");

p=pvPack(p,f,"f");

p=pvPack(p,rho,"rho");

// Algorithm settings

struct cmlmtControl c0;

c0=cmlmtControlCreate;

c0.Algorithm=4;

c0.LineSearch=4;

c0.CovParType=2;

c0.printIters=1;

c0.DirTol=0.0001;

//c0.Switch=1|10000000|0;

//c0.Switch={3 1, 0.1 0.1, 1 1, .001 .001};

// Start likelihood procedure

proc likproc(struct PV p, struct DS d0, ind);

// Declarations

local i,llik,a12,b12,a21,b21,c,g,d,f,A,trans,b122,b123,b212,b213,rho;

llik=zeros(n,1);

A=1~avec[1,1]|avec[2,1]~1;

a12=pvUnpack(p,"a12");

b12=pvUnpack(p,"b12");

b122=pvUnpack(p,"b122");

b123=pvUnpack(p,"b123");

a21=pvUnpack(p,"a21");

b21=pvUnpack(p,"b21");

b212=pvUnpack(p,"b212");

b213=pvUnpack(p,"b213");

c=pvUnpack(p,"c");

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

rho=pvUnpack(p,"rho");

y=d0.DataMatrix;

// Loop of likelihood for each observation

i=2;

do while i<=n;

// EGARCH for structural conditional variance process

lnvar[.,i]=c+g*lnvar[.,i-1]+d*(abs(epsilon[.,i-1]./sqrt(exp(lnvar[.,i-1])))-sqrt(2/pi))+f*(epsilon[.,i-1]./sqrt(exp(lnvar[.,i-1])));

// Transition variable = demeaned sqrt-variance

trans=sqrt(exp(lnvar[.,i]));
//-sqrt(exp(c./(1-diag(g))));

// Linear Variance Spillover vs. Taylor vs. Smooth Transition

avec[.,i]=a12+b12*trans|a21+b21*trans;

//avec[.,i]=a12+b12*trans+b122*trans^2+b123*trans^3|a21+b21*trans+b212*trans^2+b213*trans^3;

// Structural coefficients matrix at time i

A=1~avec[1,i]|avec[2,i]~1;

// Structural Shocks

epsilon[.,i]=A*y[i,.]';

// Log-Likelihood

var=(1~rho*sqrt(exp(lnvar[1,i]+lnvar[2,i]))/exp(lnvar[2,i])|rho*sqrt(exp(lnvar[1,i]+lnvar[2,i]))/exp(lnvar[1,i])~1)*diagrv(eye(k),exp(lnvar[.,i]));

llik[i]=-0.5*(k*ln(2*pi)+ln(det(var))-2*ln(det(A))+y[i,.]*(A'*inv(var)*A)*y[i,.]');

i=i+1;

endo;

struct modelResults mm;

mm.Function=llik;

retp(mm);

endp;

// Inequality restrictions

proc ineqp(struct PV p, struct DS d0);

local g,d,f;

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

retp(1-g[1,1]|1-g[2,2]|d[1,1]|d[2,2]);

endp;

c0.IneqProc=&ineqp;

// Equality restrictions ... restrict spillover from 1 to 2 (1 to source) to zero

proc eqp(struct PV p, struct DS d0);

local g,d,f,b12,b122,b123,b21,b212,b213;

b21=pvUnpack(p,"b21");

b212=pvUnpack(p,"b212");

b213=pvUnpack(p,"b213");

b12=pvUnpack(p,"b12");

b122=pvUnpack(p,"b122");

b123=pvUnpack(p,"b123");

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

retp(b212|b213|b122|b123); // adjust to desired rstrictions

endp;

//c0.eqProc=&eqp; // uncomment for activating equality restrictions

// Output

struct cmlmtResults res;

res=cmlmt(&likproc,p,d0,c0);

cmlmtprt(res);``````

thank you 0

If I want to get covariance matrix after some certain amount of iteration even if cmlmt doesn't  get normal convergence. for example, after 100 iterations I want to print out the covpar member of the cmlmtResults structure . 0

The covariance matrix is computed only at the end of iterations, so this is what I suggest:  set the MaxIters member of the cmlmtControl structure to some limited amount, five say, then print (or save) the covariance matrix, the covPar member of the cmlmtResults structure.  Also save the estimates, the Par member of the cmlmtResults structure and use as start values for another run.  This could be put in a loop and you could print every fifth covariance matrix of the parameters up to convergence.

``````library cmlmt;

y = // endogenous data

x = // exogenous data

struct PV p0;

po = pvPack(pvcreate,@starting point@,"b");

proc fct(p0,y,x,ind);

......   likelihood calculation

endp;

struct cmlmtControl c0;

c0.maxiters = 5;

convergence = 1;

do until convergence == 0;

struct cmlmtResults  = out;

out = cmlmt(&fct,p0,y,x,c0);

print out.covpar;

p0 = out.par;

convergence = out.retcode;

endo;`````` 0

Hi Mr Ron Schoenberg

Thank you for answering my question. Unfortunately again errors which already happened, still remain and I can not solve it. if it is possible, I send you code and data.  and guide me how I solve this dilemma

``````// Libraries
library cmlmt,pgraph;

// Data

y=x-meanc(x)';

n=rows(y);

k=cols(y);

struct DS d0;

d0=dsCreate;

d0.DataMatrix=y;

// Declarations

varred=vcx(y);

epsilon=y';

avec=zeros(k,n);

lnvar=zeros(k,n);

lnvar[.,1]=ln(diag(varred));

var=diagrv(eye(k),exp(lnvar[.,1]));

ind=1|1|1;

// Parameters

struct PV p;

a12=0;

b12=0;

b122=0;

b123=0;

a21=0;

b21=0;

b212=0;

b213=0;

A=1~avec[1,1]|avec[2,1]~1;

g=eye(k)*0.95;

d=eye(k)*0.15;

f=eye(k)*(-0.09);

c=lnvar[.,1].*(1-diag(g));

uncs=A*varred*A';

rho=uncs[1,2]/sqrt(uncs[1,1]*uncs[2,2]);

p=pvPack(pvCreate,a12,"a12");

p=pvPack(p,b12,"b12");

p=pvPack(p,b122,"b122");

p=pvPack(p,b123,"b123");

p=pvPack(p,a21,"a21");

p=pvPack(p,b21,"b21");

p=pvPack(p,b212,"b212");

p=pvPack(p,b213,"b213");

p=pvPack(p,c,"c");

p=pvPack(p,g,"g");

p=pvPack(p,d,"d");

p=pvPack(p,f,"f");

p=pvPack(p,rho,"rho");

// Algorithm settings

struct cmlmtControl c0;

c0=cmlmtControlCreate;

c0.Algorithm=4;

c0.LineSearch=4;

c0.CovParType=2;

c0.printIters=1;

c0.DirTol=0.0001;

//c0.Switch=1|10000000|0;

//c0.Switch={3 1, 0.1 0.1, 1 1, .001 .001};

// Start likelihood procedure

proc likproc(struct PV p, struct DS d0, ind);

// Declarations

local i,llik,a12,b12,a21,b21,c,g,d,f,A,trans,b122,b123,b212,b213,rho;

llik=zeros(n,1);

A=1~avec[1,1]|avec[2,1]~1;

a12=pvUnpack(p,"a12");

b12=pvUnpack(p,"b12");

b122=pvUnpack(p,"b122");

b123=pvUnpack(p,"b123");

a21=pvUnpack(p,"a21");

b21=pvUnpack(p,"b21");

b212=pvUnpack(p,"b212");

b213=pvUnpack(p,"b213");

c=pvUnpack(p,"c");

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

rho=pvUnpack(p,"rho");

y=d0.DataMatrix;

// Loop of likelihood for each observation

i=2;

do while i<=n;

// EGARCH for structural conditional variance process

lnvar[.,i]=c+g*lnvar[.,i-1]+d*(abs(epsilon[.,i-1]./sqrt(exp(lnvar[.,i-1])))-sqrt(2/pi))+f*(epsilon[.,i-1]./sqrt(exp(lnvar[.,i-1])));

// Transition variable = demeaned sqrt-variance

trans=sqrt(exp(lnvar[.,i]));
//-sqrt(exp(c./(1-diag(g))));

// Linear Variance Spillover vs. Taylor vs. Smooth Transition

avec[.,i]=a12+b12*trans|a21+b21*trans;

//avec[.,i]=a12+b12*trans+b122*trans^2+b123*trans^3|a21+b21*trans+b212*trans^2+b213*trans^3;

// Structural coefficients matrix at time i

A=1~avec[1,i]|avec[2,i]~1;

// Structural Shocks

epsilon[.,i]=A*y[i,.]';

// Log-Likelihood

var=(1~rho*sqrt(exp(lnvar[1,i]+lnvar[2,i]))/exp(lnvar[2,i])|rho*sqrt(exp(lnvar[1,i]+lnvar[2,i]))/exp(lnvar[1,i])~1)*diagrv(eye(k),exp(lnvar[.,i]));

llik[i]=-0.5*(k*ln(2*pi)+ln(det(var))-2*ln(det(A))+y[i,.]*(A'*inv(var)*A)*y[i,.]');

i=i+1;

endo;

struct modelResults mm;

mm.Function=llik;

retp(mm);

endp;

// Inequality restrictions

proc ineqp(struct PV p, struct DS d0);

local g,d,f;

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

retp(1-g[1,1]|1-g[2,2]|d[1,1]|d[2,2]);

endp;

c0.IneqProc=&ineqp;

// Equality restrictions ... restrict spillover from 1 to 2 (1 to source) to zero

proc eqp(struct PV p, struct DS d0);

local g,d,f,b12,b122,b123,b21,b212,b213;

b21=pvUnpack(p,"b21");

b212=pvUnpack(p,"b212");

b213=pvUnpack(p,"b213");

b12=pvUnpack(p,"b12");

b122=pvUnpack(p,"b122");

b123=pvUnpack(p,"b123");

g=pvUnpack(p,"g");

d=pvUnpack(p,"d");

f=pvUnpack(p,"f");

retp(b212|b213|b122|b123); // adjust to desired rstrictions

endp;

//c0.eqProc=&eqp; // uncomment for activating equality restrictions

// Output

struct cmlmtResults res;

res=cmlmt(&likproc,p,d0,c0);

cmlmtprt(res);``````

thank you 0

If I want to get covariance matrix after some certain amount of iteration even if cmlmt doesn't  get normal convergence. for example, after 100 iterations I want to print out the covpar member of the cmlmtResults structure . ### Have a Specific Question?

Get a real answer from a real person

### Need Support?

Get help from our friendly experts.