forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch17q2c.m
154 lines (133 loc) · 4.03 KB
/
ch17q2c.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
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
%program which does empirical illustration for chapter 17 Exercise 2
%Estimates the homoskedastic threshold autoregressive model with unknown threshold
load realgdp.out;
rawdat=realgdp;
traw=size(rawdat,1);
yraw=rawdat(:,2);
%Now make growth rate of GDP variable
dyraw = 100*(log(yraw(2:traw,1)) - log(yraw(1:traw-1,1)));
traw=traw-1;
%Specify order of AR
p=2;
%Now lag raw data to set things up for TAR(p) model for GDP growth
%Chop off p initial conditions and make matrices of lagged variables
yun=dyraw(p+1:traw,1);
ylagun=zeros(traw-p,p);
for ii = 1:p
ylagun(:,ii)= dyraw(p+1-ii:traw-ii,1) ;
end
t=size(yun,1);
%sort the data by the variable used to define the thresholds
alldata=[yun ylagun];
allsort = sortrows(alldata,2);
y=allsort(:,1);
ylag=allsort(:,2:p+1);
%now do a big loop over every possible threshold
%make sure at least tprob% of observations lie in each regime
tprob=.15;
ibot=round(tprob*t);
itop=t-ibot;
%we are going to store results for every possible threshold, initialize them here
lmlikes=[];
threshs=[];
betas=[];
sig2s=[];
%Note you can't average variances across models, so average second moments
b2mos=[];
sig2mos=[];
for i= ibot+1:itop
tau = ylag(i,1);
%construct the dummy variable which = 1 if y(t-1)<=tau
d=zeros(t,1);
for i=1:t
if ylag(i,1)<=tau
d(i,1)=1;
end
end
%Now make up the X matrix
xraw = [ones(t,1) ylag];
k=size(xraw,2);
x1=zeros(t,k);
x2=zeros(t,k);
for j=1:k
x1(:,j) = xraw(:,j).*d;
x2(:,j) = xraw(:,j).*(ones(t,1)-d);
end
x = [x1 x2];
k=size(x,2);
%Now that we have constructed y and X, we just use results
%for the Normal linear regression model with conjugate prior
%Hyperparameters for natural conjugate prior (chosen fairly arbitrarily)
%We need an informative prior here since marginal likelihoods are being calculated
v0=5;
b0=0*ones(k,1);
s02=1;
capv0=.25*eye(k);
capv0inv=inv(capv0);
%The following, for a given data set and values for prior hyperparameters
%Calculates OLS and posterior quantities
%Ordinary least squares quantities
xsquare=x'*x;
bols = inv(xsquare)*x'*y;
s2 = (y-x*bols)'*(y-x*bols)/(t-k);
v=t-k;
%Posterior hyperparameters for Normal-Gamma
v1=v0+t;
capv1inv = capv0inv+ xsquare;
capv1=inv(capv1inv);
b1 = capv1*(capv0inv*b0 + xsquare*bols);
v1s12 = v0*s02 + v*s2 + (bols-b0)'*inv(capv0 + inv(xsquare))*(bols-b0);
s12 = v1s12/v1;
bcov = capv1*v1s12/(v1-2);
b2mo = zeros(k,1);
for j=1:k
b2mo(j,1) = bcov(j,j) + b1(j,1)^2;
end
%posterior mean and variance of error precision
hmean = 1/s12;
hvar=2/(v1s12);
hsd=sqrt(hvar);
%Using standard transformation from Gamma to inverted Gamma,
%get posterior mean and standard deviation for error variance
sigmean=v1s12/(v1-2);
sigvar = (2/(v1-4))*sigmean^2;
sig2mo = sigvar + sigmean^2;
%log of marginal likelihood
intcon=gammaln(.5*v1) + .5*v0*log(v0*s02)- gammaln(.5*v0) -.5*t*log(pi);
lmarglik=intcon + .5*log(det(capv1)/det(capv0)) - .5*v1*log(v1s12);
lmlikes=[lmlikes; lmarglik];
threshs=[threshs; tau];
betas=[betas; b1'];
b2mos=[b2mos; b2mo'];
sig2s=[sig2s; sigmean];
sig2mos=[sig2mos; sig2mo];
end
%Print out whatever you want
'Hyperparameters for informative natural conjugate prior'
b0
capv0
v0
s02
%what we have stored are log marginal likelihoods, transform them to produce probabilities
%next statement to avoid underflow/overflow problems
lmlikes = lmlikes - min(lmlikes);
probs = exp(lmlikes)./sum(exp(lmlikes));
%Now average parameters using probs as weights
bmean=zeros(k,1);
bsd=zeros(k,1);
for i=1:k
bmean(i,1) = sum(probs.*betas(:,i));
bsd(i,1) = sqrt(sum(probs.*b2mos(:,i)) - bmean(i,1)^2);
end
'Posterior mean and standard deviation for beta'
[bmean bsd]
sigmean = sum(probs.*sig2s);
sigsd = sqrt(sum(probs.*sig2mos) - sigmean^2);
'Posterior mean and standard deviation for error variance'
[sigmean sigsd]
%[threshs probs]
figure(1)
plot(threshs,probs)
title('Figure 17.1: Posterior of Threshold')
xlabel('GDP Last Quarter')
ylabel('Probability')