Essentials of Time Series Modeling

Total Page:16

File Type:pdf, Size:1020Kb

Essentials of Time Series Modeling

Essentials of Time Series Modeling

Chapter 8 in The Essentials of Time Series Modeling: An Applied Treatment with Emphasis on Topics Relevant to Financial Analysis © by Houston H. Stokes 20 January 2008. All rights reserved. Preliminary Draft

Chapter 8

BGARCH Models – VARMA Modeling of the Second Moment...... 1 8.0 Basic Concepts...... 1 8.1 Specification of the BGARCH Model...... 1 8.2 The setup for Estimating a BGARCH Model...... 2 8.3 Estimating a BARCH Model using a Fortran setup...... 9 8.4 Using RATS and other software to estimate a BGARCH Model...... 10

BGARCH Models – VARMA Modeling of the Second Moment

8.0 Basic Concepts

While VARMA modeling as discussed in Stokes (Chapter 7, 1997) models the first moment of n series jointly, BGARCH (Bivariate GARCH modeling) is in essence a VARMA model of both the first and second moment of two series series. It is an extension of Chapter 7 to model multiple series, The B34S implementation of the BGARCH model allows multiple ways to specify the recursive model. The bgarch command allows specification of the model and produces the first and second moment residual vectors and the likelihood function. If this command is called repeatedly from a nonlinear maximizer such as cmax2, a bivariate garch model can be estimated. If a non standard or custom model is desired, it is possible to code the specification in Fortran, compile it and execute the resulting program to calculate the values built by bgarch in the usual case. Unlike a DLL implementation which is not portable, using this approach Fortran I/O and matrix command I/O are used to communicate between the matrix command program and the Fortran program. The Fortran implementation is slightly slower than the prior approach but gives unlimited flexibility. As a test case a number of problems suggested by Tsay (2002) will be discussed and the setups analyzed.

8.1 Specification of the BGARCH Model

The general BGARCH model for the constant correlation case is

轾f11(B ) f 12 ( B )轾xt 轾 a 1 轾 q 11 ( B ) q 12 ( B ) 轾 e1 t 轾 m 11 ( B ) m 12 ( B ) 轾s 11 t 犏犏= 犏 + 犏 犏 + 犏 犏 臌f21(B ) f 22 ( B )臌 yt 臌 a 2 臌 q 21 ( B ) q 22 ( B ) 臌 e 2 t 臌 m 21 ( B ) m 22 ( B ) 臌 s 22 t (8.1-1) s 2 轾b11(B) b 12 ( B) 轾11 t 轾a10 轾a11(B)a12 (B ) 轾e1t 犏 犏 = + 犏 s犏 a 犏a(B ) a ( B ) 2 臌b21(B ) b 22 ( B ) 臌犏22t 臌 20 臌21 22 臌犏e2t which is estimated by maximization of a complex likelihood function F that involves estimation of the correlation between the second moments r . The form used was

1 Essentials of Time Series Modeling

N 2 F = -.5 (ln(s11i ) + ln( s 22 i ) + ln(1 - r )) i= m N (8.1-2) 2 2 2 -.5/(1 -r )( ((e1i / s 11 i ) + ( e 2 i / s 11 i ) - 2 r e 1 i e 2 i / s 11 i s 22 i )) i= m

where N is the sample size and m equals the maximum lag plus 1. Tsay (2002) page 367 estimates a model between the daily log returns for Hong Kong ( xt ) and Japan ( yt ) in the period January 1 1996 to October 16, 1997. His reported model was

xt= -.118 x t-6 + e 1 t yt= e2 t 轾0.275 轾 0.711 0 轾 0.112 0 犏 犏 犏 2 (8.1-3) 轾s11t(.079) (0.068) 轾 s 11 t- 1 (0.032) 轾e 1 t - 1 犏=犏 + 犏 犏 + 犏 犏 s犏0.051 犏 0 .869 s 犏 0 0.091 2 臌犏22t 臌犏 22 t- 1 犏e2t- 1 犏 犏 犏 臌 臌(0.014) 臌 (0.028) 臌 (0.026)

Terms in ( ) are SE of the coefficients. The correlation between the second moment terms, r , was 0.226. The below listed code estimates a constant correlation bivariate garch model using the bgarch command, Fortran and RATS. The command syntax is simular to the garchest command and will not be discussed here. All coefficients estimated with B34S are substantially the same as found by Tsay. The final functional value found was –535.1266 in all cases. It is to be noted that Tsay (2002) used RATS in his calculations. The standard errors differ because of the algorithm used. In the B34S implementations the calls to outdouble allow monitoring the solution process in real time. Such monitoring allows one to see potential trouble spots that other wise might be obscured. The model estimated can be characterized as a bivariate diagonal constant correlation model since only one r is estimated and in (8.1-1) we assume bij (B )= 0 for i j . This restriction is relaxed later.

8.2 The setup for Estimating a BGARCH Model

The setup (see member bgarch_a in matrix.mac) runs without change on both unix and Windows 2000/XP. The Fortran implementation requires a compiler. On Linux g77 or a more specialized compiler like Lahey LF95 ca be used. As setup the Fortran code is not complete since it calls a BLAS routine dcopy. If this is not available, the appropriate include line can be uncovered to use the supplied blas routines.

/$ See Tsay (2002) page 367 Example 9.20 /$ /$ dorange setup so all three programs get same answer /$

2 Essentials of Time Series Modeling

%b34slet ibatch =%b34sget(ib34sg,ibatch,1) $ b34sexec scaio readsca %b34sif(&ibatch.ne.4)%then; file('/usr/local/lib/b34slm/findat01.mad') %b34sendif; %b34sif(&ibatch.ne.6)%then; file('c:\b34slm\findat01.mad') %b34sendif; dataset(d_HKJA); b34srun;

/$ Option 1 uses bgarch /$ Option 2 uses fortran

%b34slet dob34s1 = 1; %b34slet dob34s2 = 1; %b34slet dorats = 1;

%b34sif(&dob34s1.eq.1)%then; b34sexec matrix; call loaddata; count=0.0; call echooff; program test; zero=0.0d+00; call bgarch(res1,res2,arch1,arch2,data1,data2,func,7,nbad :ar11 array(:p6) index(6) :constant array(:zero,zero,a0,b0) :gma11 array(:a1) index(1) :gar11 array(:a2) index(1) :gma22 array(:b1) index(1) :gar22 array(:b2) index(1) :dorange index(8,469) :rho array(:rho)); count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, p6); call outdouble(10,4, a0); call outdouble(10,5, b0); call outdouble(10,6, a1); call outdouble(10,7, a2); call outdouble(10,8, b1); call outdouble(10,9, b2); call outdouble(10,10, rho); return; end; call print(test);

3 Essentials of Time Series Modeling j=integers(1,469); data1 = ln_hk(j); data2 = ln_ja(j); arch1 = data1*0.0 ; arch2 = data1*0.0 ; res1 = data1 ; res2 = data2 ;

/$ a0 = .1, a1 = .1, a2 = .4, b0 = .1, b1 = .2, b2 = .6 /$ p6 = .1, rho = 0.1 call cmaxf2(func :name test :parms p6 a0 b0 a1 a2 b1 b2 rho :ivalue array(:.1 .1 .1 .1 .4 .1 .6 .1) :maxit 300 :gradtol .1e-4 :lower array(:-.5 ,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12) :upper array(:.1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30) :print); /$ call tabulate(data1,data2,res1,res2,arch1,arch2); b34srun;

%b34sendif;

%b34sif(&dob34s2.eq.1)%then;

/$ Place Fortran on unit 4 /$ Here Fortran cannot be written on the fly with the /$ Matrix Command BUT Fortran is easier to see!! b34sexec matrix; call loaddata; /$ /$ Load Fortran /$ pgmcards; /$3456789012345678901234567890 c c BLAS service routines. W2K and Unix hooks supplied c include 'c:\b34slm\blashold.f' c include '/usr/local/lib/b34slm/blashold.f' c implicit real*8(a-h,o-z) parameter(nn=10000) dimension data1(nn) dimension data2(nn) dimension gdet(nn) dimension res1(nn) dimension res2(nn) dimension arch1(nn) dimension arch2(nn) dimension parm(100) call dcopy(nn,0.0d+00,0,gdet,1) call dcopy(nn,0.0d+00,0,data1,1) call dcopy(nn,0.0d+00,0,data2,1) call dcopy(nn,0.0d+00,0,res1 ,1)

4 Essentials of Time Series Modeling

call dcopy(nn,0.0d+00,0,res2 ,1) call dcopy(nn,0.0d+00,0,arch1,1) call dcopy(nn,0.0d+00,0,arch2,1) open(unit=8,file='data.dat') open(unit=9,file='tdata.dat') read(8,*)nob read(8,*)(data1(ii),ii=1,nob) read(8,*)(data2(ii),ii=1,nob) read(9,*)npar read(9,*)(parm(ii),ii=1,npar) read(9,*) arch1(1) read(9,*) arch2(1) close(unit=9) c :parms p6 a0 a1 a2 b0 b1 b2 rho func=0.0d+00 call dcopy(469,data1,1,res1,1) call dcopy(469,data2,1,res2,1) c c top replicates RATS which sets initial values to c 0.0 .... c do i=8 ,469 c do i=1 ,469

if(i.gt.6)res1(i)= data1(i)-parm(1)*data1(i-6) res2(i)= data2(i) if(i.gt.1)arch1(i) = * parm(2)+parm(3)*res1(i-1)**2+parm(4)*arch1(i-1) if(i.gt.1)arch2(i) = * parm(5)+parm(6)*res2(i-1)**2+parm(7)*arch2(i-1) c if(i.ge.8)then part0=(dlog(arch1(i))+dlog(arch2(i))+dlog(1.0-parm(8)**2)) part1=((res1(i)**2/arch1(i)) + (res2(i)**2/arch2(i))) part2=(2.0d+00*parm(8)*res1(i)*res2(i))/dsqrt(arch1(i)*arch2(i)) func=func -(.5d+00*part0) * -(.5d+00*((part1-part2)/(1.0d+00 - parm(8)**2))) endif c enddo c close(unit=8) open(unit=8,file='testout') write(8,fmt='(e25.16)')func close(unit=8) stop end b34sreturn;

/$ This section compiles Fortran and gets ready to execute

/$ Setup fortran parm=array(8:); call open(70,'_test.f');

5 Essentials of Time Series Modeling call rewind(70); call rewind(4); call copyf(4,70); call close(70); maxlag=0 ; j=integers(469) ; data1=ln_hk(j) ; data2=ln_ja(j) ; arch1=array(norows(data1):); arch2=array(norows(data2):);

* compile fortran and save data;

/$ lf95 is Lahey Compiler /$ g77 is Linux Compiler /$ fortcl is script to run Lahey LF95 on Unix to link libs call dodos('lf95 _test.f'); * call dounix('g77 _test.f -o_test'); * call dounix('lf95 _test.f -o_test'); call dounix('fortcl _test.f -o_test'); call copyout('_test.lst'); call open(72,'data.dat'); call rewind(72); call write(norows(data1),72); call write(data1,72,'(3e25.16)'); call write(data2,72,'(3e25.16)'); call close(72); count=0.0; call echooff; program test; call open(72,'tdata.dat'); call rewind(72); npar=8; call write(npar,72); call write(parm,72,'(e25.16)'); arch1(1)=0.0d+00 ; call write(arch1(1),72,'(e25.16)'); arch2(1)=0.0d+00 ; call write(arch2(1),72,'(e25.16)'); call close(72); call dodos('_test'); call dounix('./_test '); call open(71,'testout'); func=0.0; call read(func,71); call close(71);

/$ These optional statements slow things down /$ but help us understand the model

6 Essentials of Time Series Modeling

count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, parm(1)); call outdouble(10,4, parm(2)); call outdouble(10,5, parm(3)); call outdouble(10,6, parm(4)); call outdouble(10,7, parm(5)); call outdouble(10,8, parm(6)); call outdouble(10,9, parm(7)); call outdouble(10,10, parm(8)); return; end; call print(test); call cmaxf2(func :name test :parms parm :ivalue array(:.1 .1 .1 .4 .1 .1 .6 .1) :maxit 300 /$ :gradtol .1e-4 :lower array(: -.5 ,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12,.1d-12) :upper array(: .1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30,.1d+30) :print); b34srun; %b34sendif;

%b34sif(&dorats.eq.1)%then; b34sexec options open('rats.dat') unit(28) disp=unknown$ b34srun$ b34sexec options open('rats.in') unit(29) disp=unknown$ b34srun$ b34sexec options clean(28)$ b34srun$ b34sexec options clean(29)$ b34srun$ b34sexec pgmcall$ rats passasts PCOMMENTS('* ', '* Data passed from B34S(r) system to RATS', '* ') $ PGMCARDS$ ** Example 9.1 (** Only the first 469 data points are used **) * all 0 491:1 * open data hkja.dat * data(org=obs) / r1 r2

* Book example 9.20 page 367 set r1 = ln_hk set r2 = ln_ja set h1 = 0.0 set h2 = 0.0 nonlin p6 a0 a1 a2 b0 b1 b2 rho frml a1t = r1(t)-p6*r1(t-6) frml a2t = r2(t) frml gvar1 = a0+a1*a1t(t-1)**2+a2*h1(t-1)

7 Essentials of Time Series Modeling frml gvar2 = b0+b1*a2t(t-1)**2+b2*h2(t-1) frml gdet = -0.5*(log(h1(t)=gvar1(t))+log(h2(t)=gvar2(t)) $ + log(1.0-rho**2)) frml garchln = gdet-0.5*((a1t(t)**2/h1(t))+(a2t(t)**2/h2(t)) $ -2*rho*a1t(t)*a2t(t)/sqrt(h1(t)*h2(t)))/(1.0-rho**2) smpl 8 469 compute a0 = .1, a1 = .1, a2 = .4, b0 = .1, b1 = .2, b2 = .6 compute p6 = .1, rho = 0.1 *nlpar(criterion=value,cvcrit=0.000001) maximize(method=bhhh,recursive,iterations=150) garchln b34sreturn$ b34srun $ b34sexec options close(28)$ b34srun$ b34sexec options close(29)$ b34srun$ b34sexec options /$ dodos(' rats386 rats.in rats.out ') dodos('start /w /r rats32s rats.in /run') dounix('rats rats.in rats.out')$ B34SRUN$ b34sexec options npageout WRITEOUT('Output from RATS',' ',' ') COPYFOUT('rats.out') dodos('ERASE rats.in','ERASE rats.out','ERASE rats.dat') dounix('rm rats.in','rm rats.out','rm rats.dat') $ B34SRUN$ %b34sendif;

Edited output from this bgarch job follows:

TEST = Program

PROGRAM TEST$ ZERO=0.0D+00$ CALL BGARCH(RES1,RES2,ARCH1,ARCH2,DATA1,DATA2,FUNC,7,NBAD :AR11 ARRAY(:P6) INDEX(6) :CONSTANT ARRAY(:ZERO,ZERO,A0,B0) :GMA11 ARRAY(:A1) INDEX(1) :GAR11 ARRAY(:A2) INDEX(1) :GMA22 ARRAY(:B1) INDEX(1) :GAR22 ARRAY(:B2) INDEX(1) :DORANGE INDEX(8,469) :RHO ARRAY(:RHO))$ COUNT=COUNT+1.0$ CALL OUTDOUBLE(10,1 , FUNC)$ CALL OUTDOUBLE(10,2 , COUNT)$ CALL OUTDOUBLE(10,3, P6)$ CALL OUTDOUBLE(10,4, A0)$ CALL OUTDOUBLE(10,5, B0)$ CALL OUTDOUBLE(10,6, A1)$ CALL OUTDOUBLE(10,7, A2)$ CALL OUTDOUBLE(10,8, B1)$ CALL OUTDOUBLE(10,9, B2)$ CALL OUTDOUBLE(10,10, RHO)$ RETURN$ END$

8 Essentials of Time Series Modeling

Constrained Maximum Likelihood Estimation using CMAXF2 Command Final Functional Value -535.1265751799311 # of parameters 8 # of good digits in function 15 # of iterations 48 # of function evaluations 74 # of gradiant evaluations 50 Scaled Gradient Tolerance 1.000000000000000E-05 Scaled Step Tolerance 3.666852862501036E-11 Relative Function Tolerance 3.666852862501036E-11 False Convergence Tolerance 2.220446049250313E-14 Maximum allowable step size 2828.427124746190 Size of Initial Trust region -1.000000000000000

# Name Coefficient Standard Error T Value 1 P6 -0.11770539 0.52504819E-01 -2.2418017 2 A0 0.27468080 0.11586946 2.3706057 3 B0 0.50737013E-01 0.16636081E-01 3.0498176 4 A1 0.11212703 0.31121400E-01 3.6028918 5 A2 0.71118253 0.86043145E-01 8.2654176 6 B1 0.91184222E-01 0.25655515E-01 3.5541763 7 B2 0.86865093 0.31714232E-01 27.389941 8 RHO 0.22604462 0.42504783E-01 5.3180985

SE calculated as sqrt |diagonal(inv(%hessian))|

8.3 Estimating a BARCH Model using a Fortran setup

The next example uses Fortran in place of the bgarch command. The below listed program loads data into the user's Fortran program that has been compiled and will be repeatedly executed during running of the job. This program will run without change on unix or W2K. The commands dodos or dounix execute the Fortran program and the results are then read back into the B34S and displayed.

TEST = Program

PROGRAM TEST$ CALL OPEN(72,'tdata.dat')$ CALL REWIND(72)$ NPAR=8$ CALL WRITE(NPAR,72)$ CALL WRITE(PARM,72,'(e25.16)')$ ARCH1(1)=0.0D+00 $ CALL WRITE(ARCH1(1),72,'(e25.16)')$ ARCH2(1)=0.0D+00 $ CALL WRITE(ARCH2(1),72,'(e25.16)')$ CALL CLOSE(72)$ CALL DODOS('_test')$ CALL DOUNIX('./_test ')$ CALL OPEN(71,'testout')$ FUNC=0.0$ CALL READ(FUNC,71)$ CALL CLOSE(71)$ COUNT=COUNT+1.0$ CALL OUTDOUBLE(10,1 , FUNC)$ CALL OUTDOUBLE(10,2 , COUNT)$

9 Essentials of Time Series Modeling

CALL OUTDOUBLE(10,3, PARM(1))$ CALL OUTDOUBLE(10,4, PARM(2))$ CALL OUTDOUBLE(10,5, PARM(3))$ CALL OUTDOUBLE(10,6, PARM(4))$ CALL OUTDOUBLE(10,7, PARM(5))$ CALL OUTDOUBLE(10,8, PARM(6))$ CALL OUTDOUBLE(10,9, PARM(7))$ CALL OUTDOUBLE(10,10, PARM(8))$ RETURN$ END$ Answers from the Fortran implementation of the problem.

Constrained Maximum Likelihood Estimation using CMAXF2 Command Final Functional Value -535.1265751744659 # of parameters 8 # of good digits in function 15 # of iterations 48 # of function evaluations 75 # of gradiant evaluations 50 Scaled Gradient Tolerance 6.055454452393343E-06 Scaled Step Tolerance 3.666852862501036E-11 Relative Function Tolerance 3.666852862501036E-11 False Convergence Tolerance 2.220446049250313E-14 Maximum allowable step size 2828.427124746190 Size of Initial Trust region -1.000000000000000

# Name Coefficient Standard Error T Value 1 BETA___1 -0.11770582 0.52388346E-01 -2.2467940 2 BETA___2 0.27468941 0.10402664 2.6405680 3 BETA___3 0.11212582 0.30331159E-01 3.6967204 4 BETA___4 0.71117722 0.75938736E-01 9.3651443 5 BETA___5 0.50737048E-01 0.52223099E-01 0.97154417 6 BETA___6 0.91184324E-01 0.13322618 0.68443247 7 BETA___7 0.86865079 0.17826320 4.8728554 8 BETA___8 0.22604479 0.43001036E-01 5.2567290

SE calculated as sqrt |diagonal(inv(%hessian))|

8.4 Using RATS and other software to estimate a BGARCH Model The third and final example displays output from the Rats job that replicates Tsay (2002).

Series Obs Mean Std Error Minimum Maximum LN_HK 491 -0.01412424 1.82878368 -13.77200000 16.00500000 LN_JA 491 -0.07763340 1.17170044 -4.03500000 7.84600000 TREND 491 246.00000000 141.88375524 1.00000000 491.00000000

** Example 9.1 (** Only the first 469 data points are used **) * all 0 491:1 * open data hkja.dat * data(org=obs) / r1 r2 * Book example 9.20 page 367 set r1 = ln_hk set r2 = ln_ja set h1 = 0.0 set h2 = 0.0 nonlin p6 a0 a1 a2 b0 b1 b2 rho frml a1t = r1(t)-p6*r1(t-6) frml a2t = r2(t) frml gvar1 = a0+a1*a1t(t-1)**2+a2*h1(t-1) frml gvar2 = b0+b1*a2t(t-1)**2+b2*h2(t-1) frml gdet = -0.5*(log(h1(t)=gvar1(t))+log(h2(t)=gvar2(t)) $

10 Essentials of Time Series Modeling

+ log(1.0-rho**2)) frml garchln = gdet-0.5*((a1t(t)**2/h1(t))+(a2t(t)**2/h2(t)) $ -2*rho*a1t(t)*a2t(t)/sqrt(h1(t)*h2(t)))/(1.0-rho**2) smpl 8 469 compute a0 = .1, a1 = .1, a2 = .4, b0 = .1, b1 = .2, b2 = .6 compute p6 = .1, rho = 0.1 *nlpar(criterion=value,cvcrit=0.000001) maximize(method=bhhh,recursive,iterations=150) garchln

MAXIMIZE - Estimation by BHHH Convergence in 37 Iterations. Final criterion was 0.0000099 < 0.0000100 Usable Observations 462 Function Value -535.12665928

Variable Coeff Std Error T-Stat Signif ******************************************************************************* 1. P6 -0.117708456 0.044215311 -2.66217 0.00776398 2. A0 0.274679627 0.079180556 3.46903 0.00052234 3. A1 0.112114472 0.032437543 3.45632 0.00054761 4. A2 0.711156301 0.067750355 10.49672 0.00000000 5. B0 0.050734549 0.013616277 3.72602 0.00019453 6. B1 0.091184164 0.026212498 3.47865 0.00050394 7. B2 0.868653124 0.027697605 31.36203 0.00000000 8. RHO 0.226049840 0.046806571 4.82945 0.00000137

Tsay (2002) example 9.2 relates monthly log returns of IBM stock ( xt ) and monthly log returns of the S & P 500 ( yt ) in the period January 1926 to December 1999. Unlike the prior example, we now allow lag relationships between the second moments. In terms of equation (8.1) b12(B )= 0, b 21 ( B )构 0, a 12 ( B ) 0 and a 21 ( B ) 0 . Tsay's results were:

xt=1.351 + 0.072 x t-1 + 0.055 x t - 2 + 0.119 y t - 2 + e 1 t yt=0.703 + e2 t 轾2.98 轾 0.079 0轾 0.873- 0.031 2 (8.4-1) s 犏 犏 轾e 犏 s 轾11t犏(0.59) 犏 (0.013) 1 t- 1 (0.020) (0.009) 轾11t- 1 犏 = +犏 + 犏 犏 s 犏2.09 犏 0.042 0.045 2 犏-.066 .913 s 臌犏 22t 犏e2t- 1 臌犏 22t- 1 犏 犏 臌 犏 臌(0.47) 臌 (0.009) (0.010) 臌(0.015) (0.014) where r = 0.614 with SE .020 . The setup (see member bgarch_b in matrix.mac) listed below shows how this constant correlation bgarch model is estimated using three methods. Only the B34S bgarch and RATS output will be shown to save space.

%b34slet ibatch =%b34sget(ib34sg,ibatch,1) $ b34sexec scaio readsca %b34sif(&ibatch.ne.4)%then; file('/usr/local/lib/b34slm/findat01.mad') %b34sendif; %b34sif(&ibatch.ne.6)%then; file('c:\b34slm\findat01.mad') %b34sendif; dataset(M_IBMLN2); b34srun;

11 Essentials of Time Series Modeling

/$ See Tsay(2002) page 368 Example 9.2 Equation 9.22 /$ Note that B34S slightly "beats" rats

/$ # 1 => use bgarch 131 sec /$ # 2 => use fortran 231 sec

%b34slet dob34s1 = 1; %b34slet dob34s2 = 1;

%b34slet dorats = 1;

/$ Uses BGARCH

%b34sif(&dob34s1.eq.1)%then; b34sexec matrix; call loaddata; program test; call bgarch(res1,res2,arch1,arch2,data1,data2,func,3,nbad :ar11 array(:p11 p22) index(1 2) :ar12 array(:p12) index(2) :gma11 array(:a11) index(1) :gar11 array(:b11) index(1) :gar12 array(:b12) index(1) :gma22 array(:a22) index(1) :gma21 array(:a21) index(1) :gar21 array(:b21) index(1) :gar22 array(:b22) index(1) :rho array(:rho) :dorange index(3,888) :constant array(:c1 c2 a0 b0));

count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, c1); call outdouble(10,4, p11); call outdouble(10,5, p22); call outdouble(10,6, p12); call outdouble(10,7, c2); call outdouble(10,8, a0); call outdouble(10,9, a11); call outdouble(40,1, b11); call outdouble(40,2, b12); call outdouble(40,3, b0); call outdouble(40,4, a21); call outdouble(40,5, a22); call outdouble(40,6, b21); call outdouble(40,7, b22); call outdouble(40,8, rho); return; end; call print(test);

/$ c1 = 1.4, c2 = 0.7, p11 = 0.1, p22 = 0.1, p12 = -0.1 /$ a0 = 3.0, a11=0.1, a21=0.02, a22=0.05 /$ b0=2.0, b11=.8, b12=.01, b21=.01, b22=.8, rho = 0.1 count=0.0; j=integers(1,888); data1=ibmln(j);

12 Essentials of Time Series Modeling data2=spln(j); call echooff; call cmaxf2(func :name test :parms c1 p11 p22 p12 c2 a0 a11 b11 b12 b0 a21 a22 b21 b22 rho

:ivalue array(:1.4, .1, .1, -.1, .7 3.0, .1, .8, .01, 2.0, .02, .05,.01, .8, .1) :maxit 30000 :maxfun 30000 /$ /$ Rats Names /$ c1 p11 p22 p12 c2 /$ a0 a11 b11 b12 b0 /$ a21 a22 b21 b22 rho /$ :lower array(:.1d-12,.1d-12,.1d-12,-.2, .1d-12, .1d-12,.1d-12,.1d-12,-.06, .1d-12, .1d-12,.1d-12,-.1, .1d-12,.1d-12)

:upper array(:.1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3) :print); b34srun; %b34sendif;

%b34sif(&dob34s2.eq.1)%then;

/$ Place Fortran on unit 4 /$ Here Fortran cannot be written on the fly with the /$ Matrix Command BUT Fortran is easier to see!! b34sexec matrix; call loaddata; /$ /$ Load Fortran /$ pgmcards; /$3456789012345678901234567890 c c BLAS service routines. W2K and Unix hooks supplied c include 'c:\b34slm\blashold.f' c include '\usr\local\lib\b34slm\blashold.f' c implicit real*8(a-h,o-z) parameter(nn=10000) dimension data1(nn) dimension data2(nn) dimension res1(nn) dimension res2(nn) dimension arch1(nn) dimension arch2(nn) dimension parm(100) call dcopy(nn,0.0d+00,0,data1,1) call dcopy(nn,0.0d+00,0,data2,1) call dcopy(nn,0.0d+00,0,res1 ,1)

13 Essentials of Time Series Modeling

call dcopy(nn,0.0d+00,0,res2 ,1) call dcopy(nn,0.0d+00,0,arch1,1) call dcopy(nn,0.0d+00,0,arch2,1) open(unit=8,file='data.dat') open(unit=9,file='tdata.dat') read(8,*)nob read(8,*)(data1(ii),ii=1,nob) read(8,*)(data2(ii),ii=1,nob) read(8,*)(res1(ii),ii=1,nob) read(8,*)(res2(ii),ii=1,nob) read(8,*)(arch1(ii),ii=1,nob) read(8,*)(arch2(ii),ii=1,nob) read(9,*)npar read(9,*)(parm(ii),ii=1,npar) close(unit=9) c func=0.0d+00 c do i=3 ,nob

res1(i) =data1(i)-parm(1)-parm(2)*data1(i-1)-parm(3)*data1(i-2) * -parm(4)*data2(i-2) res2(i) =data2(i)-parm(5) arch1(i)=parm(6)+ parm(7)*res1(i-1)**2 * + parm(8)*arch1(i-1) + parm(9)*arch2(i-1) arch2(i)=parm(10)+parm(11)*res1(i-1)**2 + parm(12)*res2(i-1)**2 * +parm(13)*arch1(i-1) + parm(14)*arch2(i-1) c if(i.ge.4)then part0=(dlog(arch1(i))+dlog(arch2(i))+dlog(1.0-parm(15)**2)) part1=((res1(i)**2/arch1(i)) + (res2(i)**2/arch2(i))) part2=(2.0d+00*parm(15)*res1(i)*res2(i))/dsqrt(arch1(i)*arch2(i)) func=func -(.5d+00*part0) * -(.5d+00*((part1-part2)/(1.0d+00 - parm(15)**2))) endif enddo c rewind(unit=8) write(8,*)nob write(8,fmt='(3e25.16)')(data1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(data2(ii),ii=1,nob) write(8,fmt='(3e25.16)')(res1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(res2(ii),ii=1,nob) write(8,fmt='(3e25.16)')(arch1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(arch2(ii),ii=1,nob) close(unit=8) open(unit=8,file='testout') write(8,fmt='(e25.16)')func close(unit=8) stop end b34sreturn;

/$ This section compiles Fortran and gets ready to go

/$ Setup fortran parm=array(15:); call open(70,'_test.f'); call rewind(70); call rewind(4); call copyf(4,70);

14 Essentials of Time Series Modeling call close(70); maxlag=0 ; j=integers(888) ; data1=ibmln(j) ; data2=spln(j) ; arch1=array(norows(data1):); arch2=array(norows(data2):);

* compile fortran and save data;

/$ lf95 is Lahey Compiler /$ g77 is Linux Compiler /$ fortcl is script to run Lahey LF95 on Unix to link libs call dodos('lf95 _test.f'); * call dounix('g77 _test.f -o_test'); * call dounix('lf95 _test.f -o_test'); call dounix('fortcl _test.f -o_test'); call copyout('_test.lst'); call open(72,'data.dat'); call rewind(72); call write(norows(data1),72); call write(data1,72,'(3e25.16)'); call write(data2,72,'(3e25.16)'); call write(arch1,72,'(3e25.16)'); call write(arch2,72,'(3e25.16)'); call write(arch1,72,'(3e25.16)'); call write(arch2,72,'(3e25.16)'); call close(72); count=0.0; call echooff; program test; call open(72,'tdata.dat'); call rewind(72); npar=15; call write(npar,72); call write(parm,72,'(3e25.16)'); call close(72); call dodos('_test'); call dounix('./_test '); call open(71,'testout'); func=0.0; call read(func,71); call close(71);

/$ These optional statements slow things down /$ but help us understand the model

count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, parm(1)); call outdouble(10,4, parm(2)); call outdouble(10,5, parm(3)); call outdouble(10,6, parm(4)); call outdouble(10,7, parm(5)); call outdouble(10,8, parm(6));

15 Essentials of Time Series Modeling

call outdouble(10,9, parm(7)); call outdouble(40,1, parm(8)); call outdouble(40,2, parm(9)); call outdouble(40,3, parm(10)); call outdouble(40,4, parm(11)); call outdouble(40,5, parm(12)); call outdouble(40,6, parm(13)); call outdouble(40,7, parm(14)); call outdouble(40,8, parm(15)); return; end; call print(test);

/$ c1 = 1.4, c2 = 0.7, p11 = 0.1, p22 = 0.1, p12 = -0.1 /$ a0 = 3.0, a11=0.1, a21=0.02, a22=0.05 /$ b0=2.0, b11=.8, b12=.01, b21=.01, b22=.8, rho = 0.1 call cmaxf2(func :name test :parms parm :ivalue array(:1.4, .1, .1, -.1, .7 3.0, .1, .8, .01, 2.0, .02, .05,.01, .8, .1) :maxit 30000 :maxfun 30000 /$ /$ Rats Names /$ c1 p11 p22 p12 c2 /$ a0 a11 b11 b12 b0 /$ a21 a22 b21 b22 rho /$ :lower array(:.1d-12,.1d-12,.1d-12,-.2, .1d-12, .1d-12,.1d-12,.1d-12,-.06, .1d-12, .1d-12,.1d-12,-.1, .1d-12,.1d-12)

:upper array(:.1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3) :print); b34srun; %b34sendif;

%b34sif(&dorats.eq.1)%then;

%b34slet dob34s = 0; %b34slet dorats = 1;

%b34sif(&dorats.eq.1)%then; b34sexec options open('rats.dat') unit(28) disp=unknown$ b34srun$ b34sexec options open('rats.in') unit(29) disp=unknown$ b34srun$ b34sexec options clean(28)$ b34srun$ b34sexec options clean(29)$ b34srun$ b34sexec pgmcall$ rats passasts PCOMMENTS('* ', '* Data passed from B34S(r) system to RATS', '* ') $ PGMCARDS$ ** Example 9.2: constant correlation coefficient **

16 Essentials of Time Series Modeling

* all 0 888:1 * open data m-ibmspln.dat * data(org=obs) / r1 r2

* Book 9.22 Page 369 set r1 = ibmln set r2 = spln set h1 = 0.0 set h2 = 0.0 nonlin c1 p11 p22 p12 c2 a0 a11 b11 b12 b0 a21 a22 b21 b22 rho frml a1t = r1(t)-c1-p11*r1{1}-p22*r1{2}-p12*r2{2} frml a2t = r2(t)-c2 frml gvar1 = a0+a11*a1t(t-1)**2+b11*h1(t-1)+b12*h2(t-1) frml gvar2 = b0+a21*a1t(t-1)**2+a22*a2t(t-1)**2+b21*h1(t-1)+b22*h2(t-1) frml gdet = -0.5*(log(h1(t)=gvar1(t))+log(h2(t)=gvar2(t))+log(1.0-rho**2)) frml garchln = gdet-0.5/(1.0-rho**2)*((a1t(t)**2/h1(t))+(a2t(t)**2/h2(t)) $ -2*rho*a1t(t)*a2t(t)/sqrt(h1(t)*h2(t))) smpl 4 888 compute c1 = 1.4, c2 = 0.7, p11 = 0.1, p22 = 0.1, p12 = -0.1 compute a0 = 3.0, a11=0.1, a21=0.02, a22=0.05 compute b0=2.0, b11=.8, b12=.01, b21=.01, b22=.8, rho = 0.1 * maximize(method=simplex,iterations=10) garchln maximize(method=bhhh,recursive,iterations=150) garchln b34sreturn$ b34srun $ b34sexec options close(28)$ b34srun$ b34sexec options close(29)$ b34srun$ b34sexec options /$ dodos(' rats386 rats.in rats.out ') dodos('start /w /r rats32s rats.in /run') dounix('rats rats.in rats.out')$ B34SRUN$ b34sexec options npageout WRITEOUT('Output from RATS',' ',' ') COPYFOUT('rats.out') dodos('ERASE rats.in','ERASE rats.out','ERASE rats.dat') dounix('rm rats.in','rm rats.out','rm rats.dat') $ B34SRUN$ %b34sendif;

Edited results for bgarch and RATS follow. RATS replicates Tsay (2002) but has a slightly bigger likelihood function (-3689.8294) than found by bgarch (-3689.8012).

Note that Tsay reported the sign incorrectly for -0.119yt-2 in his book but has mentioned this in his errata page.

17 Essentials of Time Series Modeling

Constrained Maximum Likelihood Estimation using CMAXF2 Command Final Functional Value -3689.801156624453 # of parameters 15 # of good digits in function 15 # of iterations 68 # of function evaluations 90 # of gradiant evaluations 70 Scaled Gradient Tolerance 6.055454452393343E-06 Scaled Step Tolerance 3.666852862501036E-11 Relative Function Tolerance 3.666852862501036E-11 False Convergence Tolerance 2.220446049250313E-14 Maximum allowable step size 4096.718198753729 Size of Initial Trust region -1.000000000000000

# Name Coefficient Standard Error T Value 1 C1 1.3436731 0.19914078 6.7473528 2 P11 0.72890375E-01 0.28080180E-01 2.5957945 3 P22 0.55302948E-01 0.34655857E-01 1.5957750 4 P12 -0.11901636 0.41117622E-01 -2.8945340 5 C2 0.70165515 0.13753707 5.1015710 6 A0 2.9199444 0.52236831 5.5898191 7 A11 0.77658174E-01 0.81376967E-03 95.430164 8 B11 0.87525012 0.19449415E-01 45.001360 9 B12 -0.30945280E-01 0.10403645E-01 -2.9744652 10 B0 2.0745947 0.28030664 7.4011614 11 A21 0.41281895E-01 0.54934788E-02 7.5147091 12 A22 0.45807403E-01 0.13320599E-01 3.4388397 13 B21 -0.64651592E-01 0.61561760E-02 -10.501908 14 B22 0.91252166 0.17755942E-01 51.392466 15 RHO 0.61413865 0.21362661E-01 28.748229

SE calculated as sqrt |diagonal(inv(%hessian))|

Rats output for the above problem.

MAXIMIZE - Estimation by BHHH Convergence in 30 Iterations. Final criterion was 0.0000091 < 0.0000100 Usable Observations 885 Function Value -3689.82894719

Variable Coeff Std Error T-Stat Signif ******************************************************************************* 1. C1 1.350993713 0.224600739 6.01509 0.00000000 2. P11 0.072135720 0.028846844 2.50065 0.01239673 3. P22 0.055137272 0.034352092 1.60506 0.10847994 4. P12 -0.119231475 0.044040861 -2.70729 0.00678345 5. C2 0.703340778 0.154515184 4.55192 0.00000532 6. A0 2.976867784 0.591164242 5.03560 0.00000048 7. A11 0.079142552 0.012694020 6.23463 0.00000000 8. B11 0.872841408 0.020402845 42.78038 0.00000000 9. B12 -0.031306242 0.009296977 -3.36736 0.00075892 10. B0 2.094428326 0.465721590 4.49717 0.00000689 11. A21 0.041930388 0.009244965 4.53548 0.00000575 12. A22 0.045423782 0.009798448 4.63581 0.00000356 13. B21 -0.065627943 0.015433276 -4.25237 0.00002115 14. B22 0.912855323 0.014181747 64.36833 0.00000000 15. RHO 0.614103105 0.019799555 31.01601 0.00000000

18 Essentials of Time Series Modeling

The time varying BGARCH model assumes 8.1 as the first and second moment but instead of the likelihood function 8.2,

gt= q0 + q 1r t- 1 + q 2 e 1 t - 1 e 2 t - 1/ ( s 11 t - 1 s 22 t - 1 )

gt g t rt=e/(1 + e ) for r t > 0

gt g t rt=(e - 1) /(1 + e ) for r t < 0 N (8.4-2) 2 F = -.5 (ln(s11t ) + ln( s 22 t ) + ln(1 - r t )) i= m N 2 2 2 -.5/ (1 -rt )((e1 i / s 11 i ) + ( e 2 i / s 11 i ) - 2 r t e 1 t e 2 t / s 11 i s 22 i ) i= m is used. In contrast to the constant correlation formulation, here we get a vector of second moment correlations. Using this model on the prior dataset, Tsay (2001) obtains:

xt=1.318 + 0.076 x t-1 - .068 y t - 2 + e 1 t yt=.673 + e2 t 轾2.80 轾 0.084 0轾 0.864- .020 轾s 犏 犏 轾e 犏 s 11t犏(0.58) 犏 (.013) 1 t (0.021) (0.009) 轾11 t 犏= = 犏 + 犏 犏 s 犏1.71 犏 0.037 0.054e 犏- .058 .914 s 臌犏22t 臌犏 2 t 臌犏 22 t (8.4-3) 犏 犏 犏 臌(0.40) 臌 (0.009) (0.010)臌 (0.014) (0.013)

gt= -2.024 + 3.983r t-1 + 0.088( e 1 t - 1 e 2 t - 1 ) / s 11 t - 1 s 22 t - 1 (0.050) (0.090) (0.019)

where F=-3679.63. Note that Tsay allowed b12<0 ( - .020) and b 21 < 0 ( - .058) where for notational simplicity we drop the general form bij (B ) . The setup to estimate such a model using three methods, bgarch command, Fortran and RATS, (see bgarch_c in matrix.mac) is given next. This model is quite complex and detailed. Most users should run bgarch unless a custom formulation is desired. Detailed study of the RATS and Fortran setups will show all important aspects of the problem and the potential for problems. The outdouble command is especially useful to detect convergence problems as they unfold. The RATS program has the potential to drop large numbers of observations if an error in taking the log is obtained due to negative values. Relaying on the results of such models may result in misleading findings. b34sexec scaio readsca /$ file('/usr/local/lib/b34slm/findat01.mad') file('c:\b34slm\findat01.mad') dataset(m_ibmln2); b34srun;

19 Essentials of Time Series Modeling

/$ # 1 => bgarch /$ # 2 => fortran

%b34slet dob34s1= 1; %b34slet dob34s2= 0; /$ /$ stablemod = 1 /$ Forces GARCH parameters GE 0.0 /$ Tsay RATS code allows "unstable" models due to /$ unconstrained estimator /$ stablemod = 1 can cause problems in convergence since /$ parameters are at theirt zero point /$ /$ => stablemod=0 => -3678.3455 /$ => stablemod=1 => -3685.0870

%b34slet stablemod=0;

%b34slet dorats = 0;

%b34sif(&dob34s1.eq.1)%then; b34sexec matrix; call loaddata; program test; call bgarch(res1,res2,arch1,arch2,ibmln,spln,func,3,nbad :ar11 array(:p1) index(1) :ar12 array(:p3) index(2) :gar11 array(:b1) index(1) :gma11 array(:a1) index(1) :gar12 array(:f1) index(1) :gma22 array(:a11) index(1) :gar22 array(:b11) index(1) :gma21 array(:d11) index(1) :gar21 array(:f11) index(1) :rho array(:q0,q1,q2) :tvrho rho :dorange index(3,888) :constant array(:c1 c2 a0 a00));

count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, c1); call outdouble(10,4, p1); call outdouble(10,5, p3); call outdouble(10,6, c2); call outdouble(10,7, a0); call outdouble(10,8, a1); call outdouble(10,9, b1); call outdouble(40,1, f1); call outdouble(40,2, a00); call outdouble(40,3, a11); call outdouble(40,4, b11); call outdouble(40,5, f11); call outdouble(40,6, d11); call outdouble(40,7, q0); call outdouble(40,8, q1); call outdouble(40,9, q2); count=count+1.; /$ /$ Trap > 0 value and reset

20 Essentials of Time Series Modeling

/$ if(func.gt.0.0)func=-10.d+9; return; end; call print(test); count=0.0;

/$ c1 = 1.4, p1 = 0.1, p3 =-.1 , c2 = .07, a0 = 2.95 /$ a1 = .08 b1 = .87 f1 =-.03 a00= 2.05 a11=.05 /$ b11= .92 f11=-.06 d11=.04 q0 = -2.0 q1 = 3.0 /$ q2 = .1 j=integers(888) ; data1=ibmln(j) ; data2=spln(j) ; res1 =array(norows(data1):); res2 =array(norows(data1):); arch1=array(norows(data1):) + 45.; arch2=array(norows(data2):) + 31.; rho =array(norows(data2):) + .8 ; call echooff; call cmaxf2(func :name test :parms c1 p1 p3 c2 a0 a1 b1 f1 a00 a11 b11 f11 d11 q0 q1 q2 /$

%b34sif(&stablemod.eq.1)%then;

:ivalue array(: 1.4, .08, -.07, .7, 2.95, .08, .87, .01, 2.05, .05, .92, .01, .04, -2.0, 3.0, .1) :lower array(:.1d-12, .1d-12, -.4, .1d-12, .1d-12, .1d-12, .1d-12, .1d-12 .1d-12, .1d-12, .1d-12, .1d-12, .1d-12, -6., .1d-12 .1d-12) %b34sendif;

%b34sif(&stablemod.eq.0)%then; /$ /$ These values "beat Tsay" but model is not stable!!! /$ Stokes feels that GARCH mdoels should be estimarted with /$ Constraints suggested by theory!! This is not possible with /$ older versioons of RATS /$ :ivalue array(: 1.3, .08, -.07, .7, 2.8 , .08, .87, -.01, 1.7 , .05, .91, -.01, .04, -2.0, 4.1, .08) /$ These values suggested by Tsay cause problems /$ :ivalue array(: 1.4, 0.1, -.1 , .07, 2.95, /$ .08, .87, -.03, 2.05, .05, /$ .92, -.06, .04, -2.0, 3.0, /$ .1)

21 Essentials of Time Series Modeling

:lower array(:.1d-12, .1d-12, -.4, .1d-12, .1d-12, .1d-12, .1d-12, -.02 .1d-12, .1d-12, .1d-12, -.06 .1d-12, -4., .1d-12 .1d-12)

%b34sendif;

:upper array(:.1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+1, .1d+3 .1d+3)

:maxit 30000 :maxfun 30000 :maxg 10000 :print); b34srun; %b34sendif;

%b34sif(&dob34s2.eq.1)%then;

/$ Place Fortran on unit 4 /$ Here Fortran cannot be written on the fly with the /$ Matrix Command BUT Fortran is easier to see!! b34sexec matrix; call loaddata; /$ /$ Load Fortran /$ pgmcards; /$3456789012345678901234567890 implicit real*8(a-h,o-z) parameter(nn=10000) dimension data1(nn) dimension data2(nn) dimension res1(nn) dimension res2(nn) dimension arch1(nn) dimension arch2(nn) dimension rho(nn) dimension parm(100) call dcopy(nn,0.0d+00,0,data1,1) call dcopy(nn,0.0d+00,0,data2,1) call dcopy(nn,0.0d+00,0,res1 ,1) call dcopy(nn,0.0d+00,0,res2 ,1) call dcopy(nn,0.0d+00,0,arch1,1) call dcopy(nn,0.0d+00,0,arch2,1) call dcopy(nn,0.0d+00,0,rho,1) open(unit=8,file='data.dat') open(unit=9,file='tdata.dat') read(8,*)nob read(8,*)(data1(ii),ii=1,nob) read(8,*)(data2(ii),ii=1,nob) read(8,*)(res1(ii),ii=1,nob) read(8,*)(res2(ii),ii=1,nob) read(8,*)(arch1(ii),ii=1,nob) read(8,*)(arch2(ii),ii=1,nob) read(8,*)(rho(ii),ii=1,nob) read(9,*)npar read(9,*)(parm(ii),ii=1,npar)

22 Essentials of Time Series Modeling

close(unit=9) c c c Rats Info to motivate the Fortran code listed below c c parms c1 p1 p3 c2 a0 c a1 b1 f1 a00 a11 c b11 f11 d11 q0 q1 q2 c a1t = r1(t)-c1-p1*r1(t-1)-p3*r2(t-2) c a2t = r2(t)-c2 c gvar1 = a0+a1*a1t(t-1)**2+b1*h1(t-1)+f1*h2(t-1) c gvar2 = a00+a11*a2t(t-1)**2+b11*h2(t-1)+f11*h1(t-1)+d11*a1t(t-1)**2 c rh1 = q0 + q1*rho(t-1) + q2*a1t(t-1)*a2t(t-1)/sqrt(h1(t-1)*h2(t-1)) c rh = exp(rh1(t))/(1+exp(rh1(t))) c gdet = -0.5*(log(h1(t)=gvar1(t))+log(h2(t)=gvar2(t)) $ c +log(1.0-(rho(t)=rh(t))**2)) c garchln = gdet-0.5/(1.0-rho(t)**2)*((a1t(t)**2/h1(t))+ $ c (a2t(t)**2/h2(t)) $ c -2*rho(t)*a1t(t)*a2t(t)/sqrt(h1(t)*h2(t))) c func=0.0d+00 c do i=3 ,nob c res1(i) =data1(i)-parm(1)-parm(2)*data1(i-1)-parm(3)*data2(i-2) res2(i) =data2(i)-parm(4) arch1(i)=parm(5)+ parm(6)*res1(i-1)**2 * + parm(7)*arch1(i-1) + parm(8)*arch2(i-1) arch2(i)=parm(9)+ parm(10)*res2(i-1)**2 * + parm(11)*arch2(i-1) + parm(12)*arch1(i-1) * + parm(13)*res1(i-1)**2 test=1.d+30 if( (dabs(arch1(i-1)*arch2(i-1))).gt.0.0d+00) * test=dsqrt(dabs(arch1(i-1)*arch2(i-1))) rh1 =parm(14)+parm(15)*rho(i-1) if(test.ne..1d+30)rh1=rh1+(parm(16)*((res1(i-1)*res2(i-1)))/test) c c Tsay calls rho rh c if(rh1.gt. 709.782d+00)then rho(i)=1.0d+00 elseif(rh1.lt.-709.782d+00)then rho(i)=0.0d+00 else rho(i) = dexp(rh1)/(1.0d+00+dexp(rh1)) endif c c rho(i) = (dexp(rh1)-1.0)/(1+dexp(rh1)) c c gets what Tsay calls gdet c if(i.ge.4)then part0=0.0d+00 if(arch1(i).gt.0.0d+00) part0=part0+dlog(arch1(i)) if(arch2(i).gt.0.0d+00) part0=part0+dlog(arch2(i)) if(rho(i)**2.ne.1.0d+00 )part0=part0+dlog(1.0-(rho(i)**2))

part1=0.0d+00 part2=0.0d+00 if(arch1(i).ne.0.0d+00.and.arch2(i).ne.0.0d+00) * part1=((res1(i)**2/arch1(i)) + (res2(i)**2/arch2(i))) test=.1D-10 if(dabs((arch1(i)*arch2(i))).gt.0.0d+00)

23 Essentials of Time Series Modeling

*test=dsqrt(dabs(arch1(i)*arch2(i))) part2=(2.0d+00*rho(i)*res1(i)*res2(i))/test c func=func -(.5d+00*part0) c if(rho(i)**2.ne.1.0d+00)then func=func-(.5d+00*((part1-part2)/(1.0d+00- (rho(i)**2)))) else func=dmin1(func,-1.d+10) endif c endif c enddo c rewind(unit=8) write(8,*)nob write(8,fmt='(3e25.16)')(data1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(data2(ii),ii=1,nob) write(8,fmt='(3e25.16)')(res1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(res2(ii),ii=1,nob) write(8,fmt='(3e25.16)')(arch1(ii),ii=1,nob) write(8,fmt='(3e25.16)')(arch2(ii),ii=1,nob) write(8,fmt='(3e25.16)')(rho(ii),ii=1,nob) close(unit=8) open(unit=8,file='testout') rewind(unit=8) write(8,fmt='(e25.16)')func write(8,fmt='(5e16.8)')(parm(ii),ii=1,16) close(unit=8) stop end b34sreturn;

/$ This section compiles Fortran and gets ready to go

/$ Setup fortran parm=array(16:); call open(70,'_test.f'); call rewind(70); call rewind(4); call copyf(4,70); call close(70); j=integers(888) ; data1=ibmln(j) ; data2=spln(j) ; res1 =array(norows(data1):); res2 =array(norows(data1):); arch1=array(norows(data1):) + 45.; arch2=array(norows(data2):) + 31.; rho =array(norows(data2):) + .8 ;

* compile fortran and save data;

/$ lf95 is Lahey Compiler /$ g77 is Linux Compiler /$ fortcl is script to run Lahey LF95 on Unix to link libs call dodos('lf95 _test.f');

24 Essentials of Time Series Modeling

* call dounix('g77 _test.f -o_test'); * call dounix('lf95 _test.f -o_test'); call dounix('fortcl _test.f -o_test'); call copyout('_test.lst'); /$ call copyout('_test.map'); call open(72,'data.dat'); call rewind(72); call write(norows(data1),72); call write(data1,72,'(3e25.16)'); call write(data2,72,'(3e25.16)'); call write(res1 ,72,'(3e25.16)'); call write(res2 ,72,'(3e25.16)'); call write(arch1,72,'(3e25.16)'); call write(arch2,72,'(3e25.16)'); call write(rho, 72,'(3e25.16)'); call close(72); count=0.0; call echooff; program test; call open(72,'tdata.dat'); call rewind(72); npar=16; call write(npar,72); call write(parm,72,'(3e25.16)'); call close(72); call dodos('_test'); /$ call copyout('j') call dounix('./_test '); call open(71,'testout'); func=0.0; call read(func,71);

if(func.gt.0.0)then; call print(func,parm); func=-10.d+9 ; endif; call close(71);

/$ These optional statements slow things down /$ but help us understand the model

count=count+1.0; call outdouble(10,1 , func); call outdouble(10,2 , count); call outdouble(10,3, parm(1)); call outdouble(10,4, parm(2)); call outdouble(10,5, parm(3)); call outdouble(10,6, parm(4)); call outdouble(10,7, parm(5)); call outdouble(10,8, parm(6)); call outdouble(10,9, parm(7)); call outdouble(40,1, parm(8)); call outdouble(40,2, parm(9)); call outdouble(40,3, parm(10)); call outdouble(40,4, parm(11)); call outdouble(40,5, parm(12)); call outdouble(40,6, parm(13));

25 Essentials of Time Series Modeling

call outdouble(40,7, parm(14)); call outdouble(40,8, parm(15)); call outdouble(40,9, parm(16)); count=count+1. ; return; end; call print(test); call cmaxf2(func :name test :parms parm

%b34sif(&stablemod.eq.1)%then; :ivalue array(: 1.4, .08, -.07, .7, 2.95, .08, .87, .01, 2.05, .05, .92, .01, .04, -2.0, 3.0, .1) :lower array(:.1d-12, .1d-12, -.4, .1d-12, .1d-12, .1d-12, .1d-12, .1d-12 .1d-12, .1d-12, .1d-12, .1d-12, .1d-12, -6., .1d-12 .1d-12) %b34sendif;

/$ This is a very unstable model - "Beats "Tsay result

%b34sif(&stablemod.eq.0)%then; /$ /$ These values "beat Tsay" but model is not stable!!! /$ Stokes feels that GARCH mdoels should be estimarted with /$ Constraints suggested by theory!! This is not possible with /$ older versioons of RATS /$ :ivalue array(: 1.3, .08, -.07, .7, 2.8 , .08, .87, -.01, 1.7 , .05, .91, -.01, .04, -2.0, 4.1, .08) /$ These values suggested by Tsay cause problems /$ :ivalue array(: 1.4, 0.1, -.1 , .07, 2.95, /$ .08, .87, -.03, 2.05, .05, /$ .92, -.06, .04, -2.0, 3.0, /$ .1)

:lower array(:.1d-12, .1d-12, -.4, .1d-12, .1d-12, .1d-12, .1d-12, -.02 .1d-12, .1d-12, .1d-12, -.06 .1d-12, -4., .1d-12 .1d-12) :maxsteps 10000. /$:steptol 10. %b34sendif;

:upper array(:.1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+3, .1d+1, .1d+3 .1d+3) :maxit 300000 :maxfun 300000 :maxg 1000 :print); b34srun; %b34sendif;

26 Essentials of Time Series Modeling

%b34sif(&dorats.eq.1)%then; b34sexec options open('rats.dat') unit(28) disp=unknown$ b34srun$ b34sexec options open('rats.in') unit(29) disp=unknown$ b34srun$ b34sexec options clean(28)$ b34srun$ b34sexec options clean(29)$ b34srun$ b34sexec pgmcall$ rats passasts PCOMMENTS('* ', '* Data passed from B34S(r) system to RATS', '* ') $ PGMCARDS$ ** Use in Chapter 9: Example 9.2, page 372. ** * all 0 888:1 * open data m-ibmspln.dat * data(org=obs) / r1 r2

* Book 9.24 set r1 = ibmln set r2 = spln set h1 = 45.0 set h2 = 31.0 set rho = 0.8 nonlin c1 p1 p3 c2 a0 a1 b1 f1 a00 a11 b11 f11 d11 q0 q1 q2 frml a1t = r1(t)-c1-p1*r1(t-1)-p3*r2(t-2) frml a2t = r2(t)-c2 frml gvar1 = a0+a1*a1t(t-1)**2+b1*h1(t-1)+f1*h2(t-1) frml gvar2 = a00+a11*a2t(t-1)**2+b11*h2(t-1)+f11*h1(t-1)+d11*a1t(t-1)**2 frml rh1 = q0 + q1*rho(t-1) + q2*a1t(t-1)*a2t(t-1)/sqrt(h1(t-1)*h2(t-1)) * See Tsay page 372 ********* setup for + rho !!!!! frml rh = exp(rh1(t))/(1+exp(rh1(t))) * frml rh = (exp(rh1(t))-1.)/(1+exp(rh1(t))) frml gdet = -0.5*(log(h1(t)=gvar1(t))+log(h2(t)=gvar2(t)) $ +log(1.0-(rho(t)=rh(t))**2)) frml garchln = gdet-0.5/(1.0-rho(t)**2)*((a1t(t)**2/h1(t))+ $ (a2t(t)**2/h2(t)) $ -2*rho(t)*a1t(t)*a2t(t)/sqrt(h1(t)*h2(t))) smpl 4 888 compute c1 = 1.4, c2 = 0.7, p1 = 0.1, f1 = -.03, d11=.04, f11=-.06 compute p2 = 0.1, p3 = -0.1, p0 = 0.1, q0 = -2.0, q1 = 3.0, q2 = 0.1 compute a0 = 2.95, a1 = 0.08, b1 = 0.87, a00 = 2.05 compute a11 = 0.05, b11 = 0.92 *maximize(method=simplex,iterations=10) garchln nlpar(criterion=value,cvcrit=0.00001) maximize(method=bhhh,recursive,iterations=150) garchln b34sreturn$ b34srun $ b34sexec options close(28)$ b34srun$ b34sexec options close(29)$ b34srun$ b34sexec options /$ dodos(' rats386 rats.in rats.out ') dodos('start /w /r rats32s rats.in /run') dounix('rats rats.in rats.out')$ B34SRUN$ b34sexec options npageout WRITEOUT('Output from RATS',' ',' ') COPYFOUT('rats.out') dodos('ERASE rats.in','ERASE rats.out','ERASE rats.dat') dounix('rm rats.in','rm rats.out','rm rats.dat')

27 Essentials of Time Series Modeling

$ B34SRUN$ %b34sendif;

The B34S "unstable" model beats the Tsay "unstable" result since the F = -3678.3455 is marginally smaller than the value of -3679.63620302 obtained for the RATS result.

Constrained Maximum Likelihood Estimation using CMAXF2 Command Final Functional Value -3678.345535129444 # of parameters 16 # of good digits in function 15 # of iterations 45 # of function evaluations 65 # of gradiant evaluations 47 Scaled Gradient Tolerance 6.055454452393343E-06 Scaled Step Tolerance 3.666852862501036E-11 Relative Function Tolerance 3.666852862501036E-11 False Convergence Tolerance 2.220446049250313E-14 Maximum allowable step size 5944.190441094565 Size of Initial Trust region -1.000000000000000

# Name Coefficient Standard Error T Value 1 C1 1.3472474 0.20380367 6.6105157 2 P1 0.72382927E-01 0.26613825E-01 2.7197492 3 P3 -0.69406290E-01 0.30571978E-01 -2.2702584 4 C2 0.68647800 0.14265574 4.8121302 5 A0 2.9204556 0.24008417 12.164299 6 A1 0.80722367E-01 0.28737354E-02 28.089700 7 B1 0.86584238 0.43772522E-02 197.80500 8 F1 -0.20000000E-01 0.46572307E-02 -4.2943975 9 A00 1.8764795 0.16388337 11.450091 10 A11 0.54890298E-01 0.70178612E-02 7.8215138 11 B11 0.91304582 0.93495290E-02 97.656879 12 F11 -0.60000000E-01 0.22919061E-02 -26.179083 13 D11 0.36389155E-01 0.24107361E-02 15.094624 14 Q0 -2.1158937 0.11004978E-01 -192.26696 15 Q1 4.1581942 0.14193920E-01 292.95601 16 Q2 0.64027046E-01 0.10612451E-01 6.0332006

SE calculated as sqrt |diagonal(inv(%hessian))| Tsay RATS setup as listed in Tsay (2002)

MAXIMIZE - Estimation by BHHH Convergence in 10 Iterations. Final criterion was 0.0000086 < 0.0000100 Usable Observations 885 Function Value -3679.63620302

Variable Coeff Std Error T-Stat Signif ********************************************************************* 1. C1 1.317852631 0.214784518 6.13570 0.00000000 2. P1 0.076103508 0.025996944 2.92740 0.00341807 3. P3 -0.068348759 0.033526878 -2.03863 0.04148739 4. C2 0.673403174 0.150878804 4.46321 0.00000807 5. A0 2.798653715 0.578651502 4.83651 0.00000132 6. A1 0.083639381 0.012840261 6.51384 0.00000000 7. B1 0.864244881 0.021486265 40.22313 0.00000000 8. F1 -0.019952391 0.008764116 -2.27660 0.02281010 9. A00 1.710072144 0.395688583 4.32176 0.00001548 10. A11 0.054012015 0.010054202 5.37208 0.00000008 11. B11 0.913870237 0.013326620 68.57480 0.00000000 12. F11 -0.058113856 0.014433037 -4.02645 0.00005663 13. D11 0.037118203 0.009038689 4.10659 0.00004015 14. Q0 -2.023943194 0.049564072 -40.83489 0.00000000 15. Q1 3.983172216 0.090162814 44.17755 0.00000000 16. Q2 0.087557384 0.018504587 4.73166 0.00000223

28 Essentials of Time Series Modeling

We next illustrate the stable model case (stablemod = 1). Here b12>0 and b 21 > 0 by assumption. As a result of this binding constraint, the F value is more negative than in the unconstrained case and the "significance" of these parameters found in the unconstrained case goes away. These results, as in the unstable model case, support the rejection of the constant correlation assumption since q0, q 1 , and q 2 were highly significant, coefficients -2.126, 4.1716 and .0694, with t statistics of –1315.4, 1518.2 and 16.28 respectively which is consistent with ri构 r j for i j .

Constrained Maximum Likelihood Estimation using CMAXF2 Command Final Functional Value -3685.087608283086 # of parameters 16 # of good digits in function 15 # of iterations 162 # of function evaluations 208 # of gradiant evaluations 164 Scaled Gradient Tolerance 6.055454452393343E-06 Scaled Step Tolerance 3.666852862501036E-11 Relative Function Tolerance 3.666852862501036E-11 False Convergence Tolerance 2.220446049250313E-14 Maximum allowable step size 5476.340018662099 Size of Initial Trust region -1.000000000000000

# Name Coefficient Standard Error T Value 1 C1 1.2932238 0.13953357 9.2681914 2 P1 0.68825809E-01 0.25746331E-01 2.6732278 3 P3 -0.69997944E-01 0.33215789E-01 -2.1073696 4 C2 0.66149942 0.13046163 5.0704520 5 A0 3.2214844 0.48580896 6.6311753 6 A1 0.85384676E-01 0.11894944E-01 7.1782325 7 B1 0.84072152 0.17846166E-01 47.109363 8 F1 0.10000334E-12 0.12010791E-01 0.83261244E-11 9 A00 0.65862965 0.22337285 2.9485663 10 A11 0.81625178E-01 0.11601773E-01 7.0355778 11 B11 0.89462357 0.11991360E-01 74.605678 12 F11 0.99999869E-13 0.10100599E-01 0.99003896E-11 13 D11 0.10000334E-12 0.99689656E-02 0.10031466E-10 14 Q0 -2.1263629 0.16164713E-02 -1315.4350 15 Q1 4.1715517 0.27476807E-02 1518.2083 16 Q2 0.69422979E-01 0.42635195E-02 16.283021

SE calculated as sqrt |diagonal(inv(%hessian))|

The BGARCH model, while complex to estimate, was shown to be in essence a jointly estimated VARMA model on both the first and second moment. Constant correlation and time varying variants were estimated. The implications of imposing stability assumptions on the model were illustrated. While the bgarch command can handle routine setups of a general nature, the ability to code a custom model in Fortran and repeatedly branch to this compiled routine was shown to be a viable way to estimate a nonstandard model. All BGARCH setups listed showed the above methods of estimation and in addition showed RATS setups.

29

Recommended publications