-
Notifications
You must be signed in to change notification settings - Fork 0
/
BRCODE.SRC
2560 lines (2150 loc) · 70.8 KB
/
BRCODE.SRC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@October 11, 2004 version@
@Copyrigth, Pierre Perron (1998, 1999, 2004).@
proc(0)=pbreak(bigt,y,z,q,m,h,eps1,robust,prewhit,hetomega,
hetq,doglobal,dotest,dospflp1,doorder,dosequa,dorepart,estimbic,estimlwz,
estimseq,estimrep,estimfix,x,eps,maxi,fixb,betaini,printd,hetdat,hetvar,fixn);
local datevec,bigvec,global,i,supfl,ndat,maic,mbic,mlwz,ssrzero,nbreak;
local dateseq,ftest,wftest,cv,repartda,zz,siglev,j,nbr,datese,cvm,reparv,ii;
print "The options chosen are:";
print "h = " h;
print "eps1 = " eps1;
print "hetdat = " hetdat;
print "hetvar = " hetvar;
print "hetomega = " hetomega;
print "hetq = " hetq;
print "robust = " robust "(prewhit = " prewhit ")";
print "The maximum number of breaks is: " m;
print "********************************************************";
siglev={10, 5, 2.5, 1};
if doglobal == 1;
/* The following calls the procedure to obtain the break dates and the
associated sum of squared residuals for all numbers of breaks between
1 and m.*/
Print "Output from the global optimization";
Print "********************************************************";
if p == 0;
{global,datevec,bigvec}=dating(y,z,h,m,q,bigt);
else;
print "This is a partial structural change model and the following";
print "specifictaions were used:";
print "The number of regressors with fixed coefficients, p: " p;
print "Whether (1) or not (0) the initial values of beta are fixed: "
fixb;
print "If so, at the following values: " betaini;
print "The convergence criterion is: " eps;
print "Whether (1) or not (0) the iterations are printed: " printd;
print "--------------------------";
{global,datevec,bigvec}=nldat(y,z,x,h,m,p,q,bigt,fixb,
eps,maxi,betaini,printd);
endif;
i=1;
do while i <= m;
print "The model with" i "breaks has SSR : " global[i,1];
print "The dates of the breaks are: " datevec[1:i,i];
i=i+1;
endo;
endif;
if dotest == 1;
/* the following calls the procedure to estimate and print various
tests for the number of breaks. It also returns the UDmax and WDmax
tests.*/
print "********************************************************";
Print "Output from the testing procedures";
print "********************************************************";
print "a) supF tests against a fixed number of breaks";
print "--------------------------------------------------------------";
ftest=zeros(m,1);
wftest=zeros(m,1);
i=1;
do while i <= m;
ftest[i,1]=pftest(y,z,i,q,bigt,datevec,prewhit,robust,x,p,hetdat,hetvar);
print "The supF test for 0 versus" i "breaks (scaled by q) is:" ftest[i,1];
i=i+1;
endo;
print "-------------------------";
j=1;
do while j <= 4;
cv=getcv1(j,eps1);
print "The critical values at the " siglev[j,1] "% level are (for k=1 to " m "):";
print cv[q,1:m];
j=j+1;
endo;
print "--------------------------------------------------------------";
print "b) Dmax tests against an unknown number of breaks";
print "--------------------------------------------------------------";
print "The UDmax test is: " maxc(ftest);
j=1;
do while j<= 4;
cvm=getdmax(j,eps1);
print "(the critical value at the " siglev[j,1] "% level is: " cvm[q,1] ")";
j=j+1;
endo;
print "********************************************************";
j=1;
do while j <= 4;
cv=getcv1(j,eps1);
i=1;
do while i <= m;
wftest[i,1]=cv[q,1]*ftest[i,1]/cv[q,i];
i=i+1;
endo;
print "---------------------";
print "The WDmax test at the " siglev[j,1] "% level is: " maxc(wftest);
cvm=getdmax(j,eps1);
print "(The critical value is: " cvm[q,2] ")";
j=j+1;
endo;
endif;
if dospflp1 == 1;
/* The following calls the procedure that estimates the supF(l+1|l)
tests where the first l breaks are taken from the global minimization*/
print "********************************************************";
print "supF(l+1|l) tests using global otimizers under the null";
print "--------------------------------------------------------------";
i=1;
do while i <= m-1;
{supfl,ndat}=spflp1(bigvec,datevec[1:i,i],i+1,y,z,h,prewhit,robust,
x,p,hetdat,hetvar);
print "The supF(" i+1 "|" i ") test is : " supfl;
print "It corresponds to a new break at: " ndat;
i=i+1;
endo;
print "********************************************************";
j=1;
do while j <=4;
cv=getcv2(j,eps1);
print "The critical values of supF(i+1|i) at the " siglev[j,1] "% level are (for i=1 to " m ") are: ";
print cv[q,1:m];
j=j+1;
endo;
endif;
if doorder == 1;
/* The following calls the procedure to estimate the number of
breaks using the information criteria BIC and LWZ (Liu, Wu and
Zidek). It return the two orders selected. */
print "********************************************************";
Print "Output from the application of Information criteria";
print "--------------------------------------------------------------";
if p == 0;
zz=z;
else;
zz=z~x;
endif;
ssrzero=ssrnul(y,zz);
{mbic,mlwz}=order(ssrzero,global,bigt,m,q);
endif;
if dosequa == 1;
/*The following section calls the sequential procedure that estimate
each break one at a time. It stops when the supF(l+1|l) test is not
significant. It returns the number of breaks found and the break dates.
Note that it can be used independently of the other procedures, i.e.
global minimizers need not be obtained since it used a method to
compute the breaks in O(T) operations.*/
nbreak=zeros(4,1);
dateseq=zeros(4,m);
j =1;
do while j <= 4;
print "********************************************************";
Print "Output from the sequential procedure at significance level " siglev[j,1] "%";
print "--------------------------------------------------------------";
{nbr,datese}=sequa(m,j,q,h,bigt,robust,prewhit,z,y,
x,p,hetdat,hetvar,eps1);
print "----------------------------------------------------";
print "The sequential procedure estimated the number of breaks at:" nbr;
if nbreak >= 1;
print "The break dates are: " datese;
else;
endif;
nbreak[j,1]=nbr;
if nbr /= 0;
dateseq[j,1:nbreak[j,1]]=datese';
endif;
j=j+1;
endo;
endif;
if dorepart == 1;
@The following procedure construct the so-called repartition estimates
of the breaks obtained by the sequential method (see Bai (1995), Estimating
Breaks one at a time, Econometric Theory, 13, 315-352. It allows
estimates that have the same asymptotic distribution as those obtained
by global minimization. Otherwise, the output from the procedure "estim"
below do not delivers asymptotically correct confidence intervals for the
break dates.@
reparv=zeros(4,m);
j=1;
do while j <= 4;
print "********************************************************";
print "Output from the repartition procedure for the " siglev[j,1] "% significance level";
if nbreak[j,1] == 0;
print "********************************************************";
print "The sequential procedure found no break and ";
print "the repartition procedure is skipped.";
print "********************************************************";
else;
{repartda}=preparti(y,z,nbreak[j,1],dateseq[j,1:nbreak[j,1]]',h,x,p);
print "----------------------------------------";
print "The updated break dates are :" repartda;
reparv[j,1:nbreak[j,1]]=repartda';
endif;
j=j+1;
endo;
endif;
/* the following calls a procedure which estimates the model using
as the number of breaks the value specified in the first argument.
It also calls a procedure to calculate standard errors
in a way that depends on the specification for robust
and returns asymptotic confidence intervals for the break dates.*/
if estimbic == 1;
print "********************************************************";
Print "Output from the estimation of the model selected by BIC";
print "--------------------------------------------------------------";
call estim(mbic,q,z,y,datevec[.,mbic],robust,prewhit,
hetomega,hetq,x,p);
endif;
if estimlwz == 1;
print "********************************************************";
Print "Output from the estimation of the model selected by LWZ";
print "--------------------------------------------------------------";
call estim(mlwz,q,z,y,datevec[.,mlwz],robust,prewhit,
hetomega,hetq,x,p);
endif;
if estimseq == 1;
print "********************************************************";
ii=0;
j=1;
do while j <= 4;
if ii == 0;
if nbreak[j,1] /= 0;
Print "Output from the estimation of the model selected by the
sequential method at significance level " siglev[j,1] "%";
print "--------------------------------------------------------------";
call estim(nbreak[j,1],q,z,y,dateseq[j,1:nbreak[j,1]]',robust,prewhit,
hetomega,hetq,x,p);
endif;
endif;
j=j+1;
if j <= 4;
if nbreak[j,1] == nbreak[j-1,1];
if nbreak[j,1] == 0;
print "For the " siglev[j,1] "% level, the model is the same as for the " siglev[j-1,1] "% level.";
print "The estimation is not repeated.";
print "----------------------------------------------------------------";
ii=1;
else;
if dateseq[j,1:nbreak[j,1]] == dateseq[j-1,1:nbreak[j-1,1]];
print "For the " siglev[j,1] "% level, the model is the same as for the " siglev[j-1,1] "% level.";
print "The estimation is not repeated.";
print "----------------------------------------------------------------";
ii=1;
endif;
endif;
else;
ii=0;
endif;
endif;
endo;
endif;
if estimrep == 1;
ii=0;
print "********************************************************";
j=1;
do while j <= 4;
if ii == 0;
if nbreak[j,1] == 0;
print "********************************";
print "The sequential procedure at the significance level " siglev[j,1] "% found no break and ";
print "the repartition procedure was skipped.";
print "********************************************************";
else;
print "Output from the estimation of the model selected by the";
print "repartition method from the sequential procedure at the significance level " siglev[j,1] "%";
print "--------------------------------------------------------------";
call estim(nbreak[j,1],q,z,y,reparv[j,1:nbreak[j,1]]',robust,prewhit,
hetomega,hetq,x,p);
endif;
endif;
j=j+1;
if j <= 4;
if nbreak[j,1] == nbreak[j-1,1];
if nbreak[j,1] == 0;
print "For the " siglev[j,1] "% level, the model is the same as for the " siglev[j-1,1] "% level.";
print "The estimation is not repeated.";
print "----------------------------------------------------------------";
ii=1;
else;
if dateseq[j,1:nbreak[j,1]] == dateseq[j-1,1:nbreak[j-1,1]];
print "For the " siglev[j,1] "% level, the model is the same as for the " siglev[j-1,1] "% level.";
print "The estimation is not repeated.";
print "----------------------------------------------------------------";
ii=1;
endif;
endif;
else;
ii=0;
endif;
endif;
endo;
endif;
if estimfix == 1;
print "********************************************************";
Print "Output from the estimation of the model with" fixn "breaks";
print "--------------------------------------------------------------";
call estim(fixn,q,z,y,datevec[.,fixn],robust,prewhit,
hetomega,hetq,x,p);
endif;
endp;
@*******************************************************************@
proc(3)=dating(y,z,h,m,q,bigt);
@This is the main procedure which calculates the break points that
globally minimizes the SSR. It returns optimal dates and associated SSR
for all numbers of breaks less than or equal to m.@
local datevec,optdat,optssr,dvec,i,ssrmin,datx,j1,ib,jlast;
local global,vecssr,jb,xx,bigvec;
datevec=zeros(m,m);
optdat=zeros(bigt,m);
optssr=zeros(bigt,m);
dvec=zeros(bigt,1);
global=zeros(m,1);
bigvec=zeros(bigt*(bigt+1)/2,1);
@
segment to construct the vector of SSR of dimension T*(T+1)/2
@
i=1;
do while i <= bigt-h+1;
{vecssr}=ssr(i,y,z,h,bigt);
bigvec[(i-1)*bigt+i-(i-1)*i/2:i*bigt-(i-1)*i/2,1]=vecssr[i:bigt,1];
i=i+1;
endo;
@
Section that applies the dynamic programming algorithm to look for the
optimal breaks
The first case is when m = 1. Here the dynamic programming algorithm is not
needed and one call to the parti(.) procedure is enough.
@
if m == 1;
{ssrmin,datx}=parti(1,h,bigt-h,bigt,bigvec,bigt);
datevec[1,1]=datx;
global[1,1]=ssrmin;
else;
@ when m > 1, a dynamic programming algorithm is used.
The first step is to obtain the optimal one break partitions for all
possible ending dates from 2h to T-mh+1.
The optimal dates are stored in a vector optdat.
The associated ssr are stored in a vector optssr.
@
j1=2*h; @First loop. Looking for the@
do while j1 <= bigt; @optimal one break partitions@
{ssrmin,datx}=parti(1,h,j1-h,j1,bigvec,bigt);@for break dates between h and@
optssr[j1,1]=ssrmin; @T-h. j1 is the last date of the@
optdat[j1,1]=datx; @segments.@
j1=j1+1;
endo;
global[1,1]=optssr[bigt,1];
datevec[1,1]=optdat[bigt,1];
@
When this is done the algorithm looks for optimal 2,3,... breaks
partitions. The index used is ib.
@
ib=2;
do while ib <= m;
if ib == m; @If we have reached the highest number of breaks
considered, only one segment is considered, that
which ends at the last date of the sample.@
jlast=bigt;
jb=ib*h; /* date of the break */
do while jb <=jlast-h;
dvec[jb,1] = optssr[jb,ib-1]+bigvec[(jb+1)*bigt-jb*(jb+1)/2,1];
jb=jb+1;
endo;
optssr[jlast,ib]=minc(dvec[ib*h:jlast-h,1]);
optdat[jlast,ib]=(ib*h-1)+minindc(dvec[ib*h:jlast-h,1]);
else;
@If we have not reached the highest number of breaks
considered, we need to loop over the last date of
the segment, between (ib+1)*h and T.@
jlast=(ib+1)*h;
do while jlast <= bigt;
jb=ib*h; /* date of the break */
do while jb <=jlast-h;
dvec[jb,1] = optssr[jb,ib-1]+bigvec[jb*bigt-jb*(jb-1)/2+jlast-jb,1];
jb=jb+1;
endo;
optssr[jlast,ib]=minc(dvec[ib*h:jlast-h,1]);
optdat[jlast,ib]=(ib*h-1)+minindc(dvec[ib*h:jlast-h,1]);
jlast=jlast+1;
endo;
endif;
@At each time we loop the results with ib breaks are retrieved
and printed@
datevec[ib,ib]=optdat[bigt,ib];
i=1;
do while i <= ib-1;
xx=ib-i;
datevec[xx,ib]=optdat[datevec[xx+1,ib],xx];
i=i+1;
endo;
global[ib,1]=optssr[bigt,ib];
ib=ib+1;
endo;
endif; /*closing the if for the case m >1*/
retp(global,datevec,bigvec);
endp;
@********************************************************************@
proc(1)=ssr(start,y,z,h,last);
local vecssr,delta1,delta2,inv1,inv2,invz,res,v,f,r;
@
This procedure computes recursive residuals from a data set that
starts at date "start" and ends at date "last". It returns a
vector of sum of squared residuals (SSR) of length last-start+1 (stored for
convenience in a vector of length T).
start: starting entry of the sample used.
last: ending date of the last segment considered
y: dependent variable
z: matrix of regressors of dimension q
h: minimal length of a segment
@
/* initialize the vectors */
vecssr=zeros(last,1);
/* initialize the recursion with the first h data points */
inv1=inv(z[start:start+h-1,.]'z[start:start+h-1,.]);
delta1=inv1*(z[start:start+h-1,.]'y[start:start+h-1,1]);
res=y[start:start+h-1,1]-z[start:start+h-1,.]*delta1;
vecssr[start+h-1,1]=res'res;
/* loop to construct the recursive residuals and update the SSR */
r=start+h;
do while r <= last;
v=y[r,1]-z[r,.]*delta1;
invz=inv1*z[r,.]';
f=1+z[r,.]*invz;
delta2=delta1+(invz*v)/f;
inv2=inv1-(invz*invz')/f;
inv1=inv2;
delta1=delta2;
vecssr[r,1]=vecssr[r-1,1]+v*v/f;
r=r+1;
endo;
retp(vecssr);
endp;
@********************************************************************@
proc(2)=parti(start,b1,b2,last,bigvec,bigt);
local k,dvec,j,ssrmin,dx,ini,jj;
@
procedure to obtain an optimal one break partitions for a segment that
starts at date start and ends at date last. It return the optimal break
and the associated SSR.
start: begining of the segment considered
b1: first possible break date
b2: last possible break date
last: end of segment considered
@
dvec=zeros(bigt,1);
ini=(start-1)*bigt-(start-2)*(start-1)/2+1;
j=b1;
do while j<=b2;
jj=j-start;
k=j*bigt-(j-1)*j/2+last-j;
dvec[j,1]=bigvec[ini+jj,1]+bigvec[k,1];
j=j+1;
endo;
ssrmin=minc(dvec[b1:b2,1]);
dx=(b1-1)+minindc(dvec[b1:b2,1]);
retp(ssrmin,dx);
endp;
@********************************************************************@
proc(0)=estim(m,q,z,y,b,robust,prewhit,hetomega,hetq,x,p);
local dum,nt,vdel,zbar,i,d,bound,reg;
@ This procedure estimate by OLS the model given the obtained break
dates. It also computes and report confidence intervals for the break
dates. The method used depends on the specification for robust
@
if m == 0;
print "There are no breaks in this model and estimation is skipped";
else;
nt=rows(z);
d=(m+1)*q+p; @total number of parameters@
vdel=zeros(d,d);
@Construct the Z_bar matrix. The diagonal partition of Z at the
estimated break dates.@
{zbar}=pzbar(z,m,b);
@Estimation and printing@
_olsres=1;
__con=0;
if p == 0;
reg=zbar;
else;
reg=x~zbar;
endif;
call ols(0,y,reg);
vdel=pvdel(y,z,m,q,bigt,b,prewhit,robust,x,p,1,hetdat,hetvar);
print "--------------------------------------------------------------";
print "Corrected standard errors for the coefficients";
print "--------------------------------------------------------------";
i=1;
do while i <= d;
print "The corrected standard error for coefficient" i "is:" sqrt(vdel[i,i]);
i=i+1;
endo;
if robust == 0 and hetdat == 1 and hetvar == 0;
print "In the case robust == 0, hetdat == 1 and hetvar == 0, the 'corrected' are the same";
print "as that of the printout except for a different small sample correction.";
endif;
@confidence interval for the break dates@
bound=interval(y,z,zbar,b,q,m,robust,prewhit,hetomega,hetq,x,p);
print "--------------------------------------------------------------";
print "Confidence intervals for the break dates";
print "--------------------------------------------------------------";
i=1;
do while i <= m;
print "The 95% C.I. for the" i "th break is: " bound[i,1] bound[i,2];
print "The 90% C.I. for the" i "th break is: " bound[i,3] bound[i,4];
i=i+1;
endo;
print "********************************************************";
endif;
endp;
@********************************************************************@
proc ssrnul(y,zz);
local delta,ssrn;
@Computes the SSR under the null of no break@
delta=olsqr(y,zz);
ssrn=(y-zz*delta)'(y-zz*delta);
retp(ssrn);
endp;
@********************************************************************@
proc correct(reg,res,prewhit);
local jh,imat,d,nt,i,b,bmat,vstar,hac,vmat;
@main procedures which activates the computation of robust standard
errors.@
d=cols(reg);
nt=rows(reg);
b=zeros(d,1);
bmat=zeros(d,d);
vstar=zeros(nt-1,d);
vmat=zeros(nt,d);
@First construct the matriz z_t*u_t.@
i=1;
do while i <= d;
vmat[.,i]=reg[.,i].*res;
i=i+1;
endo;
@Procedure that applies prewhitenning to the matrix vmat by filtering
with a VAR(1). If prewhit=0, it is skipped.@
if prewhit == 1;
/* estimating the VAR(1) */
i=1;
do while i <= d;
b=olsqr(vmat[2:nt,i],vmat[1:nt-1,.]);
bmat[.,i]=b;
vstar[.,i]=vmat[2:nt,i]-vmat[1:nt-1,.]*b;
i=i+1;
endo;
/* call the kernel on the residuals */
jh=jhatpr(vstar);
/* recolor */
hac=inv(eye(d)-bmat)*jh*(inv(eye(d)-bmat))';
else;
hac=jhatpr(vmat);
endif;
retp(hac);
endp;
@********************************************************************@
proc jhatpr(vmat);
local d,nt,jhat,j,st;
@Procedure to compute the long-run covariance matrix of vmat.@
nt=rows(vmat);
d=cols(vmat);
jhat=zeros(d,d);
/* calling the automatic bandwidth selection*/
{st}=bandw(vmat);
@lag 0 covariance@
jhat=vmat'vmat;
@forward sum@
j=1;
do while j <= nt-1;
jhat=jhat+kern(j/st)*vmat[j+1:nt,.]'vmat[1:nt-j,.];
j=j+1;
endo;
@bacward sum@
j=1;
do while j <= nt-1;
jhat=jhat+kern(j/st)*vmat[1:nt-j,.]'vmat[j+1:nt,.];
j=j+1;
endo;
@small sample correction@
jhat=jhat/(nt-d);
retp(jhat);
endp;
@********************************************************************@
proc bandw(vhat);
local nt,i,b,sig,a2,a2n,a2d,st,d;
@procedure that compute the automatic bandwidth based on AR(1)
approximation for each vector of the matrix vhat. Each are given
equal weigths of 1.@
nt=rows(vhat);
d=cols(vhat);
a2n=0;
a2d=0;
i=1;
do while i <= d;
b=olsqr(vhat[2:nt,i],vhat[1:nt-1,i]);
sig=(vhat[2:nt,i]-b*vhat[1:nt-1,i])'(vhat[2:nt,i]-b*vhat[1:nt-1,i]);
sig=sig/(nt-1);
a2n=a2n+4*b*b*sig*sig/(1-b)^8;
a2d=a2d+sig*sig/(1-b)^4;
i=i+1;
endo;
a2=a2n/a2d;
st=1.3221*(a2*nt)^.2;
retp(st);
endp;
@********************************************************************@
proc kern(x);
local del,del1,ker;
@procedure to evaluate the quadratic kernel at some value x.@
del=6*pi*x/5;
ker=3*(sin(del)/del-cos(del))/(del*del);
retp(ker);
endp;
@*******************************************************************@
proc pzbar(zz,m,bb);
local nt,q1,zb,i;
@procedure to construct the diagonal partition of z with m break at
dates b.@
nt=rows(zz);
q1=cols(zz);
zb=zeros(nt,(m+1)*q1);
zb[1:bb[1,1],1:q1]=zz[1:bb[1,1],.];
i=2;
do while i <= m;
zb[bb[i-1,1]+1:bb[i,1],(i-1)*q1+1:i*q1]=zz[bb[i-1,1]+1:bb[i,1],.];
i=i+1;
endo;
zb[bb[m,1]+1:nt,m*q1+1:(m+1)*q1]=zz[bb[m,1]+1:nt,.];
retp(zb);
endp;
@********************************************************************@
proc interval(y,z,zbar,b,q,m,robust,prewhit,hetomega,hetq,x,p);
local nt,inter,res,qmat,delta,omega,delv,a,bound,i;
local dbdel,dd,bf,qmat1,phi1s,phi2s,eta,cvf,omega1;
@Procedure that compute confidence intervals for the break dates
based on the "shrinking shifts asymptotic framework.@
cvf=zeros(4,1);
nt=rows(z);
if p == 0;
delta=olsqr(y,zbar);
res=y-zbar*delta;
else;
dbdel=olsqr(y,zbar~x);
res=y-(zbar~x)*dbdel;
delta=dbdel[1:(m+1)*q,1];
endif;
bound=zeros(m,4);
bf=zeros(m+2,1);
bf[1,1]=0;
bf[2:m+1,1]=b[1:m,1];
bf[m+2,1]=nt;
i=1;
do while i <= m;
delv=delta[i*q+1:(i+1)*q,1]-delta[(i-1)*q+1:i*q,1];
if robust == 0;
if hetq == 1;
qmat=z[bf[i,1]+1:bf[i+1,1],.]'z[bf[i,1]+1:bf[i+1,1],.]/(bf[i+1,1]-bf[i,1]);
qmat1=z[bf[i+1,1]+1:bf[i+2,1],.]'z[bf[i+1,1]+1:bf[i+2,1],.]
/(bf[i+2,1]-bf[i+1,1]);
else;
qmat=z'z/nt;
qmat1=qmat;
endif;
if hetomega == 1;
phi1s=res[bf[i,1]+1:bf[i+1,1],1]'res[bf[i,1]+1:bf[i+1,1],1]
/(bf[i+1,1]-bf[i,1]);
phi2s=res[bf[i+1,1]+1:bf[i+2,1],1]'res[bf[i+1,1]+1:bf[i+2,1],1]
/(bf[i+2,1]-bf[i+1,1]);
else;
phi1s=res'res/nt;
phi2s=phi1s;
endif;
eta=delv'qmat1*delv/(delv'qmat*delv);
cvf=cvg(eta,phi1s,phi2s);
/*
print "------------------------------------------------------------";
print "The critical values for the construction of the confidence";
print "intervals for the " i "th break are (2.5%,5%,95%,97.5%):";
print cvf';
*/
a=(delv'qmat*delv)/phi1s;
bound[i,1]=b[i,1]-cvf[4,1]/a;
bound[i,2]=b[i,1]-cvf[1,1]/a;
bound[i,3]=b[i,1]-cvf[3,1]/a;
bound[i,4]=b[i,1]-cvf[2,1]/a;
else;
if hetq == 1;
qmat=z[bf[i,1]+1:bf[i+1,1],.]'z[bf[i,1]+1:bf[i+1,1],.]/(bf[i+1,1]-bf[i,1]);
qmat1=z[bf[i+1,1]+1:bf[i+2,1],.]'z[bf[i+1,1]+1:bf[i+2,1],.]
/(bf[i+2,1]-bf[i+1,1]);
else;
qmat=z'z/nt;
qmat1=qmat;
endif;
if hetomega == 1;
omega=correct(z[bf[i,1]+1:bf[i+1,1],.],res[bf[i,1]+1:bf[i+1,1],1],prewhit);
omega1=correct(z[bf[i+1,1]+1:bf[i+2,1],.],res[bf[i+1,1]+1:bf[i+2,1],1],
prewhit);
else;
omega=correct(z,res,prewhit);
omega1=omega;
endif;
phi1s=delv'omega*delv/(delv'qmat*delv);
phi2s=delv'omega1*delv/(delv'qmat1*delv);
eta=delv'qmat1*delv/(delv'qmat*delv);
cvf=cvg(eta,phi1s,phi2s);
/*
print "------------------------------------------------------------";
print "The critical values for the construction of the confidence";
print "intervals for the " i "th break are (2.5%,5%,95%,97.5%):";
print cvf';
*/
a=(delv'qmat*delv)^2/(delv'omega*delv);
bound[i,1]=b[i,1]-cvf[4,1]/a;
bound[i,2]=b[i,1]-cvf[1,1]/a;
bound[i,3]=b[i,1]-cvf[3,1]/a;
bound[i,4]=b[i,1]-cvf[2,1]/a;
endif;
i=i+1;
endo;
bound[.,1]=int(bound[.,1]);
bound[.,2]=int(bound[.,2])+1;
bound[.,3]=int(bound[.,3]);
bound[.,4]=int(bound[.,4])+1;
retp(bound);
endp;
@********************************************************************@
proc psigmq(res,b,q,i,nt);
local sigmat,kk,sigmq;
@procedure that computes a diagonal matrix of dimension i+1 with ith
entry the estimate of the variance of the residuals for segment i.
@
sigmat=zeros(i+1,i+1);
sigmat[1,1]=res[1:b[1,1],1]'res[1:b[1,1],1]/b[1,1];
kk = 2;
do while kk <= i;
sigmat[kk,kk]=res[b[kk-1,1]+1:b[kk,1],1]'
res[b[kk-1,1]+1:b[kk,1],1]/(b[kk,1]-b[kk-1,1]);
kk=kk+1;
endo;
sigmat[i+1,i+1]=res[b[i,1]+1:nt,1]'res[b[i,1]+1:nt,1]/(nt-b[i,1]);
retp(sigmat);
endp;
@********************************************************************@
proc plambda(b,m,bigt);
local lambda,k;
@procedure that construct a diagonal matrix of dimension m+1 with ith
entry (T_i - T_i-1)/T.@
lambda=zeros(m+1,m+1);
lambda[1,1]=b[1,1]/bigt;
k=2;
do while k <= m;
lambda[k,k]=(b[k,1]-b[k-1,1])/bigt;
k=k+1;
endo;
lambda[m+1,m+1]=(bigt-b[m,1])/bigt;
retp(lambda);
endp;
@********************************************************************@
proc pvdel(y,z,i,q,bigt,b,prewhit,robust,x,p,withb,hetdat,hetvar);
local zbar,delv,vdel,hac,res,j,sig,sigmq,lambda,q22,reg;
local vv,ev,q21,q11,aaa,bbb,xbar,aa,bb,cc,dd,mat,ff,gam,ie,w,wbar,ww,gg;
@procedure that compute the covariance matrix of the estimates delta.@
ev=ones(i+1,1);
{zbar}=pzbar(z,i,b);
if p == 0;
delv=olsqr(y,zbar);
res=y-zbar*delv;
reg=zbar;
else;
delv=olsqr(y,zbar~x);
res=y-(zbar~x)*delv;
if withb == 0;
reg=zbar-x*inv(x'x)*x'zbar;
else;
reg=x~zbar;
endif;
endif;
if robust == 0;
@