forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrats_gibbs.m
130 lines (109 loc) · 3.87 KB
/
rats_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
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
%This function runs the Gibbs sampler
%using the Rat growth data.
clear; clc;
randn('seed',sum(100*clock));
load rats.raw.txt; %MAY NEED TO CHANGE if the .txt extension is not on your saved data set !!!
[nn,kk] = size(rats);
xuse = [ones(5,1) [8 15 22 29 36]'];
%-----------------------------
%Declare the prior values
%-----------------------------
a = 3;
b = 1/(2*20);
%this chooses the prior mean for
%sigma^2 equal to 20 with std
%also equal to 20
eta = [100 15]'
C = [40^2 0; 0 10^2];
%this chooses the prior to center
%the weight at birth at 100, and
%the growth rate at 15. C makes
%these choices reasonably diffuse
rho =5;
R = [10^2 0; 0 .5^2];
%this prior specifies some degree
%of variation across rats, and does
%not restrict them to have equal
%birth weights and growth rates.
% for i = 1:1000;
% tempp = wish_rnd(inv(rho*R),rho);
% tempp2 = inv(tempp);
% std1(i,1) = sqrt(tempp2(1,1));
% std2(i,1) = sqrt(tempp2(2,2));
% end;
% hist(std1,50);
% hist(std2,50);
iter = 10000;
%declare the number of iterations
burn = 500;
%declare the length of the burn-in
%---------------------------------
%Set lengths of parmaeter vectors
%---------------------------------
sigma2 = zeros(iter,1);
theta0 = zeros(2,iter);
theta_int = zeros(30,iter);
theta_rate = zeros(30,iter);
invSigma = zeros(2,2,iter);
var_int = zeros(iter,1);
var_rate = zeros(iter,1);
correl = zeros(iter,1);
%----------------------
%Set initial conditions
%----------------------
sigma2(1) = 20;
invSigma(:,:,1) = inv([100 0; 0 1]);
theta0(:,1) = [100 10]';
%Begin the Gibbs Sampler
for i = 2:iter;
total_resid = 0;
%Do conditional for all theta_i
for j=1:30;
Dtemp = inv(xuse'*xuse/sigma2(i-1) + invSigma(:,:,i-1) );
dtemp = xuse'*rats(j,2:kk)'/sigma2(i-1) + invSigma(:,:,i-1)*theta0(:,i-1);
H = chol(Dtemp);
theta_temp = Dtemp*dtemp + H'*randn(2,1);
theta_int(j,i) = theta_temp(1,1);
theta_rate(j,i) = theta_temp(2,1);
%use this later for sigma^2 conditional
resids =rats(j,2:kk)' - xuse*[theta_int(j,i) theta_rate(j,i)]';
tempp = sum(resids.^2);
total_resid = total_resid+tempp;
end;
thetabar = [mean(theta_int(:,i)) mean(theta_rate(:,i))]';
%Do conditional for theta0
Dtemp = inv(30*invSigma(:,:,i-1) + inv(C));
dtemp = 30*invSigma(:,:,i-1)*thetabar + inv(C)*eta;
H = chol(Dtemp);
theta0(:,i) = Dtemp*dtemp + H'*randn(2,1);
%Do Conditional for sigma2
sigma2(i,1) = invgamrnd((150/2)+a, inv( .5*total_resid + inv(b)),1,1);
%Do Conditional for Sigma^-1.
tempp3 = 0;
for jj = 1:30;
tempp1 = [theta_int(jj,i) theta_rate(jj,i)]' - theta0(:,i);
tempp2 = tempp1*tempp1';
tempp3 = tempp3 + tempp2;
end;
invSigma(:,:,i) = wish_rnd(inv(tempp3 + rho*R), 30+rho);
Sigma = inv(invSigma(:,:,i));
var_int(i,1) = Sigma(1,1);
var_rate(i,1) = Sigma(2,2);
correl(i,1) = Sigma(1,2)/(sqrt(Sigma(1,1))*sqrt(Sigma(2,2)));
end;
int_use = theta_int(:,burn+1:iter);
rate_use = theta_rate(:,burn+1:iter);
mu_ints = mean(int_use')';
mu_rates = mean(rate_use')';
save ratgibbs mu_ints mu_rates;
clc;
disp('Posterior means of parameters');
disp('Order: common intercept, common rate, variance_intercept, variance_rate, correlation');
disp('intercept(10), intercept(25), rate(10), rate(25)');
parms = [theta0(1,burn+1:iter)' theta0(2,burn+1:iter)' var_int(burn+1:iter) var_rate(burn+1:iter) correl(burn+1:iter)...
theta_int(10,burn+1:iter)' theta_int(25,burn+1:iter)' theta_rate(10,burn+1:iter)' theta_rate(25,burn+1:iter)'];
mean(parms)
std(parms)
disp('10th and 90th percentiles');
prctile(parms,10)
prctile(parms,90)