-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathbimod_mix.m
145 lines (118 loc) · 4 KB
/
bimod_mix.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
clear;
clc;
randn('seed',sum(100*clock));
rand('seed',sum(100*clock));
%------------------------------------------------------------------
%generate normal mixture data
%------------------------------------------------------------------
m1 = 0;
m2 = 2;
s1 = 1;
s2 = .5;
Puse = .4; %first component
nobs = 3000;
for j = 1:nobs;
unif = rand(1,1);
if unif <= Puse
y(j,1) = m1 + sqrt(s1)*randn(1,1);
else
y(j,1) = m2 + sqrt(s2)*randn(1,1);
end;
end;
%ygrid = linspace(-2,5,500)';
%true_density = Puse*normpdf(ygrid,m1,sqrt(s1)) + (1-Puse)*normpdf(ygrid,m2,sqrt(s2));
%plot(ygrid,true_density);
%pause;
xmat = [ones(nobs,1)];
%-------------------------------------------------
%CHOOSE THE PRIOR SPECIFICATIONS
%-------------------------------------------------
n_comp = 2;
alphas = 2*ones(n_comp,1);
mu_beta1 = [0]'; mu_beta2 = 3;
V_beta1 = 4*eye(size(xmat,2)); V_beta2 = 4*eye(size(xmat,2));
beta0 = [mu_beta1 mu_beta2];
inv_parms = [V_beta1 V_beta2];
a = [3 3]';
b = [(1/2) (1/2)]';
%-------------------------------------------------
%SET VECTOR, MATRIX LENGTHS AND INITIAL CONDITIONS.
%-------------------------------------------------
iter = 2000;
burn = 500;
beta1_final = zeros(iter-burn,n_comp);
sig_final = zeros(iter-burn,n_comp);
P_final = zeros(iter-burn,1);
%start out with tau = the true division of the components.
P = [.5 .5];
betas = [6 6];
sig_temp = [2 2];
tempp = .5*sign(rand(nobs,1)-.5) + .5;
tau = [tempp (1-tempp)];
clear tempp;
%-----------------------
%BEGIN THE GIBBS SAMPLER
%-----------------------
for j = 2:iter;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw the Coefficient Vectors%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for l = 1:n_comp;
points = find(tau(:,l)==1);
x_use = xmat(points,:);
y_use = y(points);
D_beta = inv(x_use'*x_use/sig_temp(:,l) + inv_parms(:,l));
d_beta = x_use'*y_use/sig_temp(:,l) + inv_parms(:,l)*beta0(:,l);
H = chol(D_beta);
betas(:,l) = D_beta*d_beta + H'*randn(size(xmat,2),1);
clear points x_use y_use D_beta d_beta H;
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Draw the VARIANCE PARAMETERS%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for l = 1:n_comp;
points = find(tau(:,l)==1);
x_use = xmat(points,:);
y_use = y(points);
resids = .5*(y_use - betas(:,l))'*(y_use - betas(:,l));
sig_temp(:,l) = invgamrnd( (length(y_use)/2 + a(l,1)), inv(resids + inv(b(l,1))),1,1);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%
%SAMPLE THE COMPONENT LABEL VECTOR%
%%%%%%%%%%%%%%%%%%%%%%%%%%
tempp1 = normpdf(y,betas(:,1),sqrt(sig_temp(:,1)));
tempp2 = normpdf(y,betas(:,2),sqrt(sig_temp(:,2)));
probs_1 = P(1)*tempp1;
probs_2 = P(2)*tempp2;
normalize = probs_1 + probs_2;
tempp = rand(nobs,1);
tau = [((.5*sign(probs_1./normalize - tempp)+.5)) (1 - (.5*sign(probs_1./normalize - tempp)+.5))];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%SAMPLE THE COMPONENT PROBABILTIES%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
nn = sum(tau);
P = dirchlet_rnd([(nn(1) + alphas(1)) (nn(2) + alphas(2))]);
if j >burn
beta1_final(j-burn,:) = [betas(:,1) betas(:,2)];
P_final(j-burn) = P(1);
sig_final(j-burn,:) = [sig_temp(:,1) sig_temp(:,2)];
end;
end;
ygrid = linspace(-2,5,500)';
for j = 1:length(ygrid);
density(j,1) = mean ((P_final(:,1)).*normpdf(ygrid(j,1),(beta1_final(:,1)),(sqrt(sig_final(:,1)))) + ...
(1- (P_final(:,1))).*normpdf(ygrid(j,1),(beta1_final(:,2)),(sqrt(sig_final(:,2)))) );
end;
plot(ygrid,density);
true_density = Puse*normpdf(ygrid,m1,sqrt(s1)) + (1-Puse)*normpdf(ygrid,m2,sqrt(s2));
hold on;
plot(ygrid,true_density,'--');
hold off;
clc;
disp('Mean of intercepts');
mean(beta1_final)
disp('Mean of Variance Parameters');
mean(sig_final)
disp('Mean of Probability of first component');
mean(P_final)
save bimod_mix_res ygrid density true_density;