-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUPDATE.m
170 lines (114 loc) · 5.33 KB
/
UPDATE.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
function [lambda_snr_vec, lambda_coup_vec, VECTORS] = UPDATE(DATA, DAG, nue_var, lambda_snr_vec, lambda_coup_vec, VECTORS)
global alpha_snr;
global beta_snr;
global alpha_coup;
global beta_coup;
n_nodes = length(DATA);
for i_node=1:n_nodes
vector_i = VECTORS{i_node};
n_comps = length(DATA{i_node});
parents = find(DAG(:,i_node));
ind1 = find(vector_i==1);
ind0 = find(vector_i==0);
lambda_coup = lambda_coup_vec(i_node,1);
lambda_snr = lambda_snr_vec(i_node,1);
LAMBDA_VEC = vector_i;
LAMBDA_VEC(ind0) = lambda_snr;
LAMBDA_VEC(ind1) = lambda_coup;
LAMBDA_MAT = diag(LAMBDA_VEC);
LAMBDA_MAT = LAMBDA_MAT([1;parents+1],[1;parents+1]);
%%% FOR THE FIRST SEGMENT:
LAMBDA_VEC_first = vector_i;
LAMBDA_VEC_first(ind0) = lambda_snr; %%%
LAMBDA_VEC_first(ind1) = lambda_snr; %%%
LAMBDA_MAT_first = diag(LAMBDA_VEC_first);
LAMBDA_MAT_first = LAMBDA_MAT_first([1;parents+1],[1;parents+1]);
alpha_sigma = nue_var/2;
beta_sigma = nue_var/2;
for component=1:n_comps
data = DATA{i_node}{component};
[n_plus, n_obs] = size(data);
X = [ones(1,n_obs);data(parents,:)]; % pred x obs
y = data(end,:)'; % obs x 1
if (component==1)
mue_prior = zeros(length(parents)+1,1); % pred x 1
LAMBDA = LAMBDA_MAT_first;
else
if (length(parents)>0)
mue_prior = vector_i([1;parents+1],1) .* mue_apost;
else
mue_prior = vector_i(1,1) .* mue_apost;
end
LAMBDA = LAMBDA_MAT;
end
m_tilde = X'*mue_prior; % obs x 1
% obs x obs
inv_Sigma_tilde = eye(n_obs) - X'*inv(inv(LAMBDA)+X*X')*X;
Delta2 = (y-m_tilde)'*inv_Sigma_tilde*(y-m_tilde);
alpha_sigma = alpha_sigma + n_obs/2;
beta_sigma = beta_sigma + Delta2/2;
% pred x pred
Sigma_inv = inv(LAMBDA) + X*X';
mue_apost = inv(Sigma_inv)*(inv(LAMBDA)*mue_prior+X*y);
end
inv_var_all = gamrnd(alpha_sigma,(1/beta_sigma));
var_all = 1/inv_var_all;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha_snr_i = alpha_snr;
beta_snr_i = beta_snr;
alpha_coup_i = alpha_coup;
beta_coup_i = beta_coup;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha_snr_i = alpha_snr_i + (length(ind1) * 1 + length(ind0) * n_comps)/2;
alpha_coup_i = alpha_coup_i + (length(ind1) * (n_comps-1))/2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for component=1:n_comps
data = DATA{i_node}{component};
[n_plus, n_obs] = size(data);
X = [ones(1,n_obs);data(parents,:)]; % pred x obs
y = data(end,:)'; % obs x 1
if (component==1)
mue_prior = zeros(length(parents)+1,1); % pred x 1
LAMBDA = LAMBDA_MAT_first;
else
if (length(parents)>0)
mue_prior = vector_i([1;parents+1],1) .* mue_apost;
else
mue_prior = vector_i(1,1) .* mue_apost;
end
LAMBDA = LAMBDA_MAT;
end
% pred x pred
Sigma_inv = inv(LAMBDA) + X*X';
mue_apost = inv(Sigma_inv)*(inv(LAMBDA)*mue_prior+X*y);
W_i = mvnrnd(mue_apost,var_all*inv(Sigma_inv));
W_i = W_i';
if (component==1)
beta_snr_i = beta_snr_i + 0.5 * inv_var_all*(W_i-mue_prior)'*inv(eye(length(parents)+1))*(W_i-mue_prior);
else
indi = find(vector_i~=(-1));
vec_i = vector_i(indi);
ind0_new = find(vec_i==0);
ind1_new = find(vec_i==1);
W_i_snr = W_i(ind0_new);
W_i_coup = W_i(ind1_new);
mue_prior_snr = mue_prior(ind0_new);
mue_prior_coup = mue_prior(ind1_new);
n0 = length(ind0);
n1 = length(ind1);
if(n0>0)
beta_snr_i = beta_snr_i + 0.5 * inv_var_all*(W_i_snr -mue_prior_snr)' *inv(eye(n0)) *(W_i_snr-mue_prior_snr);
end
if(n1>0)
beta_coup_i = beta_coup_i + 0.5 * inv_var_all*(W_i_coup-mue_prior_coup)' *inv(eye(n1))*(W_i_coup-mue_prior_coup);
end
end
end
inv_lambda_coup = gamrnd(alpha_coup_i,(1/beta_coup_i));
lambda_coup_vec(i_node,1) = (1/inv_lambda_coup);
inv_lambda_snr = gamrnd(alpha_snr_i,(1/beta_snr_i));
lambda_snr_vec(i_node,1) = (1/inv_lambda_snr);
end
return