I am not able to execute the program because of this error. When I use another variable then there is no issue. Please help.
The program:
/*
** THIS PROCEDURE ESTIMATES THE VAR-BEKK-GARCH-M MODEL;
**
*/
new;
cls;
@===========call different library=============@
library maxlik, pgraph;
#include maxlik.ext;
maxset;
graphset;
@===============================================@
format /ld 8,6;
@==================set output file==============@
output file=varmgarchm_manuf_lag2.out reset;
@===============================================@
@============= load data file===================@
nor0=358; @=====number of rows=================@
noc=2; @========number of columns===========@
//load data[nor0,noc]=ukbr.txt;
data1mg = loadd("A:/1_research21/1.1_obj1/1_MGARCH_part/analysis/model1/manuf/data1mg.xlsx", "rt + manufgrowth");
z0=data1mg[.,"rt" "manufgrowth"];@==level values========@
//z0=(ln(z0[2:nor0,.])-ln(z0[1:nor0-1,.]))*100;
nor=rows(z0);
@==========================================@
n=noc; @===number of equations/number of dependent variable=======@
p=1; @====lags in VAR part=======@
q=1; @====lags in ARCH part======@
@===============================================@
/* Procedure definition area */
/********************** PROC VARLAGS *****************************
** last update: 5 Dec 95 previous: 15 June 94
** AUTHOR
** Alan G. Isaac
** FORMAT
** {x,xlags} = varlags(var,lags)
** INPUT
** var - T x K matrix
** lags - scalar, number of lags of var (a positive integer)
** OUTPUT
** x - (T - lags) x K matrix, the last T-lags rows of var
** xlags - (T - lags) x lags*cols(var) matrix,
** being the 1st through lags-th
** values of var corresponding to the values in x
** i.e, the appropriate rows of x(-1)~x(-2)~etc.
** GLOBAL VARIABLES: none
**********************************************************************/
proc(2)=varlags(var,lags);
local xlags;
xlags = shiftr((ones(1,lags) .*. var)',seqa(1-lags,1,lags)
.*. ones(cols(var),1),miss(0,0))';
retp(trimr(var,lags,0),trimr(xlags,0,lags));
endp;
@==========================================@
{y,ylag}=varlags(z0,p);
t=rows(y~ylag);
c=ones(t,1); @===========constant in VAR ============== @
@=========================================================@
data=y~c~ylag;
/* **************set initial parameters***************** */
/*theta=ones(23,1).*0;
theta[1:2]=0.1|0.1; theta[3:6]=ones(4,1).*0.06; theta[7:12]=ones(6,1).*0.13;
theta[13:15]=ones(3,1).*0.03; theta[16:19]=ones(4,1).*0.14; theta[20:23]=ones(4,1).*0.7; */
theta={0.0056
-0.0169
-0.0702
0.2558
0.0114
0.0921
0.0414
-0.0382
-0.0153
0.1139
0.0023
0.0446
-0.0954
0.54
0.9092
0.2554
0.0158
0.1033
0.6264
0.9065
-0.008
0.1609
0.3898
};
/* ******************************************************* */
@======================cml setting=======================@
__title="VAR WITH BEKK-GARCH IN MEAN MODEL UK-BR_LAG1";
_max_DirTol=1.e-5;
_max_Algorithm=2;
_max_CovPar=0;
_max_MaxIters=1000;
__output=1;
/*_cml_Bounds=*/
/*_cml_ParNames="b10"|(0$+"b1"$+ftocv(seqa(1,1,cols(lagp)*neqn),1,0))|
"b20"|(0$+"b2"$+ftocv(seqa(1,1,cols(lagp)*neqn),1,0))|
"b30"|(0$+"b3"$+ftocv(seqa(1,1,cols(lagp)*neqn),1,0));*/
/* ********************************************************************** */
{th,f,g,covmat,retcode}=maxprt(maxlik(data,0,&varmgarchmll,theta));
/* ********************************************************************** */
/* ***************************fit of the model********************************* */
{yhatt,et,est,Ht}=varmgarchmfit(th,data);
"";
//"fit1 fit2 err1 err2 stderr1 stderr2 h1 h2";
//yhatt~et~est~Ht;
@================== log likeltihood value================@
llv=f*t;
"Loglikelihood Value";
llv;
"number of observation";
t;
/* ************* likelihood ratio test ************* */
llv1=-9110.751367;
np1=17;
lr=-2*(llv1-llv);
lrpval=cdfchic(lr,(rows(th)-np1));
"LIKELIHOOD RATIO TEEST";"";
"LR LRPVAL";
lr~lrpval;
/* ******************** AIC/BIC ***************** */
@====================== AIC/BIC===================@
"==========================================";
"AIC BIC";"";
AIC= -2*f+2*rows(th)/t;
BIC=-2*f+2*rows(th)*ln(ln(t))/t;
AIC~BIC;
@===============residual test=========@
varres=est;
smax=10;
Numvars = 2;
lags=1; /* v is the number of variables in the VAR model. */
INVCOV00 = INVPD(((varres[1:T,.])'*(varres[1:T,.]))/(T));
LBS = 0;
teststat = zeros(1,Smax);
pvalu = zeros(1,Smax);
S=1;
do until S > Smax;
{LBS} = LBPort (S, varres,T,LBS,INVCOV00);
LBSpv = cdfchic(LBS,numvars^2*(S-lags));
teststat[.,S] = LBS;
pvalu[.,S] = LBSpv;
S = S +1;
endo;
"teststat";
teststat;
"pvalu";
pvalu;
/* ******************** IN-SAMPLE RESULT *************** */
RMSE=sqrt(meanc(et.^2));
"RMSE";
RMSE;
/* *******************plot *************** */
/* Make graphs of actual and fitted series, transition function over time,
residuals and transition function versus transition variable */
graphset;
_pdate="";
_pnum=2;
_ptitlht=0.3;
_paxht=0.15;
_ptek="varmgarchm_ukbr_lag1.tkf";
begwind;
window(3,2,0);
_ptitle="Series1+Fit1";
xy(0,data[.,1]~yhatt[.,1]);
nextwind;
_ptitle="Series2+Fit2";
xy(0,data[.,2]~yhatt[.,2]);
nextwind;
_ptitle="H1 vs. time";
xy(0,ht[.,1]);
nextwind;
_ptitle="H2 vs. time";
plotxy(seqa(1,1,rows(ht)),ht[.,2]);
nextwind;
_plctrl = 0;
_ptitle="H1 vs e1(t-1)";
temp1=sortc(et[1:t-1,1]~ht[2:t,1],1);
xy(temp1[.,1],temp1[.,2]);
nextwind;
_plctrl = 0 ;
_ptitle="H2 vs e2(t-1)";
temp2=sortc(et[1:t-1,2]~ht[2:t,2],1);
xy(temp2[.,1],temp2[.,2]);
endwind;
/********************** PROCEDURE DEFINITION AREA ************************** */
/* ************* computes the likelihood function ************************ */
proc(1)=varmgarchmll(theta,data);
local tc,tn,y1,y,x1,x,tb,b1,b,d1,d,o1,o2,o,a1,a,bg1,bg,i,m,ht,e,za,oldt,ll;
tc=cols(data);
tn=rows(data);
y1=data[.,1:n];
x1=data[.,n+1:tc];
@===========CREATING PARAMETER MATRIX========================@
tb=rows(theta);
b1=theta[1:n*(1+p*n)]; @=====var parameters in vech=======@
b=reshape(b1,(1+p*n),n)'; @=====var parameters=======@
d1=theta[n*(1+p*n)+1:(n*(1+p*n)+n*(n*(n+1)/2))]; @====in-mean parameter in vech=====@
d=reshape(d1,(n*(n+1)/2),n)'; @============in-mean parameter================@
o1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+1:(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)];
o2=xpnd(o1);
o=lowmat(o2);
a1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+1:(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+n^2];
a=reshape(a1,n,n);
bg1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+n^2+1:tb];
bg=reshape(bg1,n,n);
@============= COMPLETE PARAMETER MATRIX==========================@
y=y1';
x=x1';
/* ************ initialize the matrics ************************ */
m=zeros(n,tn);
ht=arrayinit(tn|n|n,0);
e=zeros(n,tn);
ll=zeros(tn,1);
@=====================================@
/* ************* 1st observation of the matrics ****************** */
m[.,1]=meanc(y');
e[.,1]=y[.,1]-m[.,1];
ht[1,.,.]=vcx(y1);
/******************************************************** */
i=2;
do while i<=tn;
ht[i,.,.]=o*o'+a*(e[.,i-1]*e[.,i-1]')*a'+bg*(arraytomat(ht[i-1,.,.]))*bg';
m[.,i]=b*x[.,i]+d*(vech(arraytomat(ht[i,.,.])));
e[.,i]=y[.,i]-m[.,i];
oldt=trapchk(1);
trap 1,1;
za=solpd(e[.,i],arraytomat(ht[i,.,.]));
trap oldt,1;
if not scalmiss(za);
ll[i]=-0.5*n*ln(2*pi)-0.5*ln(det(arraytomat(ht[i,.,.])))-0.5*(e[.,i]')*za;
else ;
ll[i]=error(0);
endif;
i=i+1;
endo;
retp(ll);
endp;
@==========================================================@
@================ fitting the model==========================@
/* ************* COMPUTES THE IN-SAMPLE FIITED VALUE ************************ */
proc(4)=varmgarchmfit(theta,data);
local tc,tn,y1,y,x1,x,tb,b1,b,d1,d,o1,o2,o,a1,a,bg1,bg,i,m,ht,e,hht,est;
tc=cols(data);
tn=rows(data);
y1=data[.,1:n];
x1=data[.,n+1:tc];
@===========CREATING PARAMETER MATRIX========================@
tb=rows(theta);
b1=theta[1:n*(1+p*n)]; @=====var parameters in vech=======@
b=reshape(b1,(1+p*n),n)'; @=====var parameters=======@
d1=theta[n*(1+p*n)+1:(n*(1+p*n)+n*(n*(n+1)/2))]; @====in-mean parameter in vech=====@
d=reshape(d1,(n*(n+1)/2),n)'; @============in-mean parameter================@
o1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+1:(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)];
o2=xpnd(o1);
o=lowmat(o2);
a1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+1:(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+n^2];
a=reshape(a1,n,n);
bg1=theta[(n*(1+p*n)+n*(n*(n+1)/2))+(n*(n+1)/2)+n^2+1:tb];
bg=reshape(bg1,n,n);
@============= COMPLETE PARAMETER MATRIX==========================@
y=y1';
x=x1';
/* ************ initialize the matrics ************************ */
m=zeros(n,tn);
ht=arrayinit(tn|n|n,0);
hht=zeros(n,tn);
e=zeros(n,tn);
est=zeros(n,tn);
@=====================================@
/* ************* 1st observation of the matrics ****************** */
ht[1,.,.]=vcx(y1);
hht[.,1]=diag(arraytomat(ht[1,.,.]));
m[.,1]=meanc(y');
e[.,1]=y[.,1]-m[.,1];
est[.,1]=e[.,1]./hht[.,1];
/******************************************************** */
i=2;
do while i<=tn;
ht[i,.,.]=o*o'+a*(e[.,i-1]*e[.,i-1]')*a'+bg*(arraytomat(ht[i-1,.,.]))*bg';
hht[.,i]=diag(arraytomat(ht[i,.,.]));
m[.,i]=b*x[.,i]+d*(vech(arraytomat(ht[i,.,.])));
e[.,i]=y[.,i]-m[.,i];
est[.,i]=e[.,1]./hht[.,i];
i=i+1;
endo;
retp(m',e',est',hht');
endp;
/*************************************************************************
----proc LBPort
----AUTHOR: Scott Hacker (in cooperation with Hatemi-J)
----INPUT:
S - order of autocorrelation to test for
varres - residuals from VAR regression
T - number of observations
LBS - LBS statistic for S -1
INVCOV00 - var-cov matrix for residuals
----OUTPUT: Ljeung-Box Statistic:
LBS = T(T) x sum (j=1 to S) of (1/(T-j)) x trace(C(0,j)inv[C(0,0)]C'(0,j)inv[C(0,0)])
where C(0,j) = sum(t = j+1 to T) for e(t)e'(t-j), and
e(t) is the residual from the estimation of
y(t) = v + A(1)y(t-1) + . . . + A(k)y(t-k) + error(t)
Note this definition is used rather than
LBS = T(T+2) x sum (j=1 to S) of (1/(T-j)) x trace(C(0,j)inv[C(0,0)]C'(0,j)inv[C(0,0)])
as the first definition matches equation 4.4.23 in Lutkepohl's textbook on
Multivariate Time Series.
----GLOBAL VARIABLES: none
----NB: none.
************************************************************************/
proc (1) = LBPort(S, varres,T,LBS,INVCOV00);
local cov0S,tracematrix,pvalue, RESNOW, RESLAGGED, j, COV0J;
COV0S = ( (varres[S+1:T,.])'*(varres[1:T-S,.]) )/T;
j = S;
RESNOW = varRES[j+1:T,.];
/*print "line 662 RESNOW=";; RESNOW;*/
RESLAGGED = varRES[1:T-j,.];
COV0j = (RESNOW'*RESLAGGED)/T;
/*format /rd 12,7; print "line 654 COV0S";; COV0S;" S=";; S; "COV0J=";; cov0j;*/
tracematrix = COV0S*INVCOV00*COV0S'*INVCOV00;
LBS = LBS + (T*(T)/(T-S))*ones(1,rows(tracematrix))*diag(tracematrix);
/* print " line 667! LBS=";LBS; */
retp(LBS);
endp;
ERROR::
G0041 : Argument must be scalar [maxlik.src, line 767] Currently active call: File maxlik.src, line 767, in maxprt print "Mean log-likelihood " ftos(f,"%#.lG",15,6); Traceback: File mgarch1.gss, line 111, in <main> {th,f,g,covmat,retcode}=maxprt(maxlik(data,0,&varmgarchmll,theta));
1 Answer
0
The most likely problem is that the return value from varmgarchmll()
is a complex number.
You can check this using the iscplx()
procedure. Then if it is complex, you can check to see if the complex data is just round-off error with the hasimag()
procedure. You can do that by changing the last part of varmgarchmll()
from:
i=i+1;
endo;
retp(ll);
endp;
to
i=i+1;
endo;
// Is the log-likelihood value complex?
if iscplx(ll);
// Is the complex part, just round-off error?
if hasimag(ll);
print "The log likelihood is complex:";
print "ll = " ll;
print "The current parameters are:";
print theta;
else;
// If the complex part is just round-off, remove it
ll = real(ll);
endif;
endif;
retp(ll);
endp;
Your Answer
1 Answer
The most likely problem is that the return value from varmgarchmll()
is a complex number.
You can check this using the iscplx()
procedure. Then if it is complex, you can check to see if the complex data is just round-off error with the hasimag()
procedure. You can do that by changing the last part of varmgarchmll()
from:
i=i+1;
endo;
retp(ll);
endp;
to
i=i+1;
endo;
// Is the log-likelihood value complex?
if iscplx(ll);
// Is the complex part, just round-off error?
if hasimag(ll);
print "The log likelihood is complex:";
print "ll = " ll;
print "The current parameters are:";
print theta;
else;
// If the complex part is just round-off, remove it
ll = real(ll);
endif;
endif;
retp(ll);
endp;