forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch17q1.m
88 lines (81 loc) · 2.35 KB
/
ch17q1.m
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
%program which does empirical illustration for chapter 17 Exercise 1
%Univariate AR(p) model based on Geweke (1988) who uses an AR(3)
%The definitions of the features of interest relating to secular/cyclical behavior
%are defined for the AR(3) so always set p=3
load realgdp.txt;
rawdat=realgdp;
traw=size(rawdat,1);
yraw=rawdat(:,2);
%Specify order of AR
p=3;
%Now log and lag raw data to set things up for AR(p) model in logs
%Chop off p initial conditions and make matrices of lagged variables
y=log(yraw(p+1:traw,1));
ylag=zeros(traw-p,p);
for ii = 1:p
ylag(:,ii)= log(yraw(p+1-ii:traw-ii,1));
end
t=size(y,1);
x = [ones(t,1) ylag];
k=size(x,2);
%Posterior under noninformative prior is t(bmean,vscale,v)
v=t-k;
xtxinv = inv(x'*x);
bmean=xtxinv*x'*y;
s2 = (y-x*bmean)'*(y-x*bmean)/v;
vscale=s2*xtxinv;
vchol=chol(vscale);
vchol=vchol';
%Now start Monte Carlo loop
%Specify the number of replications
r=10000;
%initialize variables
expcount=0;
ccount=0;
minroot=0;
minroo2=0;
%tdis_rnd takes random draws from the multivariate t
%with mean zero and scale, V=I
%Hence we have to transform draws to get t(bmean,vscale,v)
for i = 1:r
%draw a t(0,1,v) then transform to yield draw of beta
bdraw=bmean + vchol*tdis_rnd(k,v);
%Matlabs root-finding subroutine orders the polynomial coefficients in the reverse order we have, hence make bstar
bstar = [-bdraw(4,1) -bdraw(3,1) -bdraw(2,1) 1];
rr=roots(bstar);
%calculate absolute value of roots
rabs=abs(rr);
minrabs=min(rabs);
minroot=minroot + minrabs;
minroo2=minroo2 + minrabs^2;
if minrabs<1
expcount=expcount+1;
end
%count the number of roots which are complex
icount=0;
for j=1:3
if abs(imag(rr(j,1)))>0
icount=icount+1;
end
end
if icount==2
ccount=ccount+1;
end
end
%Now create some results to print out
bcov = vscale*v/(v-2);
bsd=zeros(k,1);
for i = 1:k
bsd(i,1)=sqrt(bcov(i,i));
end
%now get posterior mean and standard deviation for the smalllest root
minroot = minroot/r;
mrootsd=sqrt(minroo2/r - minroot^2);
'Posterior mean and standard deviation for coefficients (intercept followed by AR coeffs)'
[bmean bsd]
'Posterior mean and standard deviation for minimum root (in absolute value'
[minroot mrootsd]
'Probability that the series is explosive'
expcount/r
'Probability that the series is oscillatory'
ccount/r