-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathuncentered_parm_gibbs.m
82 lines (68 loc) · 2 KB
/
uncentered_parm_gibbs.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
%This m-file generates data and fits the model using
%the Gibbs sampler and the uncentered parameterization
clear;clc;
randn('seed',sum(100*clock));
nobs = 10;
T=2;
sigeps = 1;
sigeta = 10;
mu = 2;
B = (sigeps*sigeta)/(T*sigeta + sigeps);
ntotal = nobs*T;
%----------------
%GENERATE THE DATA
%-----------------
y = [];
for i = 1:nobs;
eta_i = mu + sqrt(sigeta)*randn(1,1);
y_temp = ones(2,1)*eta_i + sqrt(sigeps)*randn(2,1);
y = [y; y_temp];
end;
alpha_draws = randn(nobs,1);
%RUN THE POSTERIOR SIMULATOR
iter = 1000;
burn = 200;
for i = 1:iter;
%----------------------------
%POSTERIOR CONDITIONAL FOR MU
%-----------------------------
ystar = [];
for j = 1:nobs
ystar_tempp = y( ((2*j)-1):2*j ) - alpha_draws(j,1)*ones(T,1);
ystar = [ystar; ystar_tempp];
end;
D_mu = sigeps/ntotal;
d_mu = sum(ystar)/sigeps;
mu_draw = D_mu*d_mu + sqrt(D_mu)*randn(1,1);
%-----------------------------
%POSTERIOR CONDITIONAL FOR alpha's
%-----------------------------
ytilde = y - mu_draw;
for j = 1:nobs;
y_keep = ytilde(((2*j)-1):2*j);
DD = inv(T/sigeps + inv(sigeta));
Dd = sum(y_keep)/sigeps;
alpha_draws(j,1) = DD*Dd + sqrt(DD)*randn(1,1);
end;
if i > burn;
alpha_represent1(i-burn,1) = alpha_draws(3,1);
alpha_represent2(i-burn,1) = alpha_draws(5,1);
alpha_represent3(i-burn,1) = alpha_draws(9,1);
mu_final(i-burn,1) = mu_draw;
end;
end;
clc;
disp('Representative Correlations Between alpha_i and mu');
corrcoef(alpha_represent1,mu_final)
corrcoef(alpha_represent2,mu_final)
corrcoef(alpha_represent3,mu_final)
disp('Lag 1 Correlations - selected alpha_i and mu');
L = length(alpha_represent1);
a1 = alpha_represent1(2:L); aL1 = alpha_represent1(1:L-1);
a2 = alpha_represent2(2:L); aL2 = alpha_represent2(1:L-1);
a3 = alpha_represent3(2:L); aL3 = alpha_represent3(1:L-1);
mu1 = mu_final(2:L); muL1 = mu_final(1:L-1);
corrcoef(a1,aL1)
corrcoef(a2,aL2)
corrcoef(a3,aL3)
corrcoef(mu1,muL1)