forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch11q3e.m
89 lines (76 loc) · 1.98 KB
/
ch11q3e.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
%Matlab code for part e) Question 3 of Chapter 11
%This program calculates posterior means and standard deviations
%for regressions coefficients using both analytical methods and
%Monte Carlo integration
%Note that I am drawing from the t-distribution for beta
%the function tdis_rnd.m does this, which I got
%from James LeSage's Econometrics Toolbox (www.spatial-econometrics.com/)
%Load in the data you generated in part a)
load ch2q2.out;
n=size(ch2q2,1);
y=ch2q2(:,1);
x=ch2q2(:,2:3);
k=size(x,2);
%Hyperparameters for natural conjugate prior
%I am using the suffix "0" to denote prior hyperparameters
v0=1;
s2inv0=1;
s02=1/s2inv0;
b0=zeros(k,1);
b0(k,1)=1;
c=1;
capv0=c*eye(k);
capv0inv=inv(capv0);
%Ordinary least squares quantities
bols = inv(x'*x)*x'*y;
s2 = (y-x*bols)'*(y-x*bols)/(n-k);
v=n-k;
%Posterior hyperparameters for Normal-Gamma
xsquare=x'*x;
v1=v0+n;
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-k);
bsd=zeros(k,1);
for i = 1:k
bsd(i,1)=sqrt(bcov(i,i));
end
vscale = s12*capv1;
vchol=chol(vscale);
vchol=vchol';
%Now start Monte Carlo loop
%beta is t(b1,vscale,v1)
b2mean=zeros(k,1);
b2square=zeros(k,1);
%Specify the number of replications
r=10000;
%
%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(b1,bscale,v1)
for i = 1:r
%draw a t(0,1,v1) then transform to yield draw of beta
bdraw=b1 + vchol*tdis_rnd(k,v1);
b2mean=b2mean+bdraw;
b2square=b2square+bdraw.^2;
end
b2mean=b2mean./r;
b2square=b2square./r;
b2var=b2square - b2mean.^2;
b2sd=sqrt(b2var);
nse=b2sd./sqrt(r);
%Print out whatever you want
'Hyperparameters for informative natural conjugate prior'
b0
capv0
v0
s02
'Posterior results based on Informative Prior'
b1
bsd
b2mean
b2sd
nse