全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 MATLAB等数学软件专版
2489 0
2011-03-30
这是RATS的程式码
我要分析一个Regime-switch VAR-GARCH-M的模型
在极大化概似函数时,会出现
## MAT15. Subscripts Too Large or Non-Positive
Error was evaluating entry 1
的错误讯息
希望大家可以帮我看一下问题在哪里


RATS CODE
*open the file (m)代表月 data後面接起始日與結束日
open data TD5_final.xls
CALENDAR(irregular)
data(format=xls,org=columns) / sp f1m
set dsp = sp - sp{1}
set df1m = f1m - f1m{1}
*graph SP(畫好多張圖) vfields = 幾張 footer = 標題名稱
print / dsp

spgraph(vfields=2,$
     footer="Figure 2. the changes")
graph(vlabel="spot freight rate")
# dSP
graph(vlabel="1-month forward freight rate")
# dF1M

spgraph(done)






******************************************************************************
***********************************VAR-GARCH-M Model**************************

******************************************************************************
set dSP1 = dSP{1}

set Lam1 2 1578 = (dsp1>-22.and.dsp1<4.)
set Lam2 2 1578 = (dsp1>4.)
print / Lam1


dec series[vect] La1
dec series[vect] La2
gset La1 = ||Lam1||
gset La2 = ||Lam2||


compute n=2
compute gstart=1,gend=1578
*
dec series[vect] Y
dec frml[vect] R1 R2 R3
dec vect[series] U1(n) U2(n) U3(n)
*
*
* The paths of the covariance matrices and uu' are saved in the
* SERIES[SYMM] named H and UU. UX and HX are used for the current values
* of the residual vector and H matrices
*
declare series[symm] G1 G2 G3 uu1 uu2 uu3
*
* ux is used when extracting a u vector
*
declare symm Gx1(n,n) Gx2(n,n) Gx3(n,n)
declare vect ux1(n) ux2(n) ux3(n)
*
* These will be the parameters for the mean equations. These are adjusted to add
* variance or covariance terms as needed.
*
dec vect A1(n) A2(n) A3(n)
dec rect B1(n,n) B2(n,n) B3(n,n) C1(n,n) C2(n,n) C3(n,n)
nonlin(parmset=meanparms) A1 B1 C1 A2 B2 C2 A3 B3 C3
*
* Mean model = VAR(1) with sqrt(G) "M" term
*
frml R1 = Y-A1-A2*La1-A3*La2-B1*Y{1}-B2*La1*Y{1}-B3*La2*Y{1}-C1*%xdiag(G1)-C2*La1*%xdiag(G2)-C3*La2*%xdiag(G3)

*
gset Y = ||dsp,df1m||

*
* Run preliminary VAR(4) to get estimates of residuals
*
*
linreg(smpl=dsp1<-22) dsp / u1(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>-22.and.dsp1<4.) dsp / u2(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>4) dsp / u3(1)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1<-22) df1m / u1(2)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=-22<dsp1<4) df1m / u2(2)
# constant dsp{1 to 4} df1m{1 to 4}
linreg(smpl=dsp1>4) df1m / u3(2)
# constant dsp{1 to 4} df1m{1 to 4}
*
linreg(smpl=dsp1<-22) dsp
# constant dsp{1} df1m{1}  u2(1){1} u2(2){1}
compute A1(1)=%beta(1),B1(1,1)=%beta(2),B1(1,2)=%beta(3),$
                      C1(1,1)=%beta(4),C1(1,2)=%beta(5)
linreg(smpl=dsp1>-22.and.dsp1<4.) dsp
# constant dsp{1} df1m{1}  u1(1){1} u1(2){1}
compute A2(1)=%beta(1),B2(1,1)=%beta(2),B2(1,2)=%beta(3),$
                      C2(1,1)=%beta(4),C2(1,2)=%beta(5)
linreg(smpl=dsp1>4) dsp
# constant dsp{1} df1m{1}  u3(1){1} u3(2){1}
compute A3(1)=%beta(1),B3(1,1)=%beta(2),B3(1,2)=%beta(3),$
                      C3(1,1)=%beta(4),C3(1,2)=%beta(5)
linreg(smpl=dsp1<-22) df1m
# constant dsp{1} df1m{1}  u2(1){1} u2(2){1}
compute A1(1)=%beta(1),B1(1,1)=%beta(2),B1(1,2)=%beta(3),$
                      C1(1,1)=%beta(4),C1(1,2)=%beta(5)
linreg(smpl=dsp1>-22.and.dsp1<4.) df1m
# constant dsp{1} df1m{1}  u1(1){1} u1(2){1}
compute A2(1)=%beta(1),B2(1,1)=%beta(2),B2(1,2)=%beta(3),$
                      C2(1,1)=%beta(4),C2(1,2)=%beta(5)
linreg(smpl=dsp1>4) df1m
# constant dsp{1} df1m{1}  u3(1){1} u3(2){1}
compute A3(1)=%beta(1),B3(1,1)=%beta(2),B3(1,2)=%beta(3),$
                      C3(1,1)=%beta(4),C3(1,2)=%beta(5)


vcv(matrix=WWW1)
# u1
vcv(matrix=WWW2)
# u2
vcv(matrix=WWW3)
# u3

*
* These are used to initialize pre-sample variances.
*
gset G1  * gend = WWW1
gset G2  * gend = WWW2
gset G3  * gend = WWW3
gset uu1 * gend = WWW1
gset uu2 * gend = WWW2
gset uu3 * gend = WWW3
set u1(1) = 0.0
set u2(1) = 0.0
set u3(1) = 0.0
set u1(2) = 0.0
set u2(2) = 0.0
set u3(2) = 0.0
*
declare frml[symm] Gf1 Gf2 Gf3
*
frml logl1 = $
    Gx1    = Gf1(t) , $
    G1(t)  = Gx1, $
    ux1    = R1 , $
    uu1(t) = %outerxx(ux1), $
    %pt(u1,t,ux1),$
    %logdensity(Gx1,ux1)
frml logl2 = $
    Gx2    = Gf2(t) , $
    G2(t)  = Gx2, $
    ux2    = R2 , $
    uu2(t) = %outerxx(ux2), $
    %pt(u2,t,ux2),$
    %logdensity(Gx2,ux2)
frml logl3 = $
    Gx3    = Gf3(t) , $
    G3(t)  = Gx3, $
    ux3    = R3 , $
    uu3(t) = %outerxx(ux3), $
    %pt(u3,t,ux3),$
    %logdensity(Gx3,ux3)

*
* This does a simple GARCH(1,1) model for the variance.
*
dec symm VA1(n,n) VA2(n,n) VA3(n,n) VB1(n,n) VB2(n,n) VB3(n,n) VC1(n,n) VC2(n,n) VC3(n,n)

compute VA1=WWW1,VA2=WWW2,VA3=WWW3,VB1=%const(0.50),VB2=%const(0.50),$
VB3=%const(0.50),VC1=%const(0.05),VC2=%const(0.05),VC3=%const(0.05)


nonlin(parmset=garchparms) VA1 VB1 VC1 VA2 VB2 VC2 VA3 VB3 VC3

frml Gf1 = VA1+VC1.*G1{1}+VB1.*uu1{1}

frml Gf2 = VA2+VC2.*G2{1}+VB2.*uu2{1}

frml Gf3 = VA3+VC3.*G3{1}+VB3.*uu3{1}

gset LOGLL = logl1 + logl2 + logl3

maximize(trace,parmset=meanparms+garchparms,pmethod=simplex,$
piters=10,method=bhhh,iters=400) LOGLL gstart+2 gend
*
* This does a BEKK model for the variance
*
dec rect VBB1(n,n) VBB2(n,n) VBB3(n,n) VCC1(n,n) VCC2(n,n) VCC3(n,n)
compute VBB1=.05*%identity(n),VBB2=.05*%identity(n),VBB3=.05*%identity(n),$
VCC1=.50*%identity(n),VCC2=.50*%identity(n),VCC3=.50*%identity(n)


nonlin(parmset=garchparms) VA1 VBB1 VCC1 VA2 VBB2 VCC2 VA3 VBB3 VCC3


compute VA1=%decomp(WWW1),VA2=%decomp(WWW2),VA3=%decomp(WWW3)
frml Gf1 = VA1*tr(VA1)+VCC1*G1{1}*tr(VCC1)+VBB1*uu1{1}*tr(VBB1)
frml Gf2 = VA2*tr(VA2)+VCC2*G2{1}*tr(VCC2)+VBB2*uu2{1}*tr(VBB2)
frml Gf3 = VA3*tr(VA3)+VCC3*G3{1}*tr(VCC3)+VBB3*uu3{1}*tr(VBB3)
gset LOGLL = logl1 + logl2 + logl3


maximize(trace,parmset=meanparms+garchparms,pmethod=simplex,piters=10,$
method=bhhh,iters=400) LOGLL gstart+2 gend
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群