-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPDATE.R
181 lines (120 loc) · 5.73 KB
/
UPDATE.R
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
171
172
173
174
175
176
177
178
179
UPDATE <- function(DATA, DAG, nue_var, lambda_snr_vec, lambda_coup_vec, VECTORS){
################## MAKE THEM GLOBAL!
# global alpha_snr;
# global beta_snr;
#
# global alpha_coup;
# global beta_coup;
n_nodes = length(DATA)
for (i_node in 1:n_nodes){
vector_i = VECTORS[[i_node]]
n_comps = length(DATA[[i_node]])
parents = which(DAG[,i_node] != 0)
ind1 = which(vector_i==1)
ind0 = which(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(drop(LAMBDA_VEC))
LAMBDA_MAT = LAMBDA_MAT[c(1,parents+1),c(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(drop(LAMBDA_VEC_first))
LAMBDA_MAT_first = LAMBDA_MAT_first[c(1,parents+1),c(1,parents+1)]
alpha_sigma = nue_var/2
beta_sigma = nue_var/2
for (component in 1:n_comps){
data = DATA[[i_node]][component][[1]] # to make data as a matrix [[1]]
n_plus <- dim(data)[1]
n_obs <- dim(data)[2]
X = rbind(matrix(1, 1, n_obs), data[parents,])
y = as.matrix(data[nrow(data),]) # transpose no need?
if (component == 1){
mue_prior = matrix(0, length(parents)+1, 1) # pred x 1
LAMBDA = LAMBDA_MAT_first
} else {
if (length(parents) > 0) {
mue_prior = vector_i[c(1, parents+1), 1] * mue_apost
} else {
mue_prior = vector_i[1,1] * mue_apost
}
LAMBDA = LAMBDA_MAT
}
m_tilde = t(X) %*% mue_prior # obs x 1
# obs x obs
inv_Sigma_tilde = diag(n_obs) - t(X) %*% solve(solve(LAMBDA) + X %*% t(X)) %*% X
Delta2 = t(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 = solve(LAMBDA) + X %*% t(X)
mue_apost = solve(Sigma_inv) %*% (solve(LAMBDA) %*% mue_prior + X %*% y)
} # for loop component ends
inv_var_all = rgamma(n = 1, shape = alpha_sigma, scale = (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 in 1:n_comps){
data = DATA[[i_node]][component][[1]] # to make data as a matrix [[1]]
n_plus <- dim(data)[1]
n_obs <- dim(data)[2]
X = rbind(matrix(1, 1, n_obs), data[parents,])
y = as.matrix(data[nrow(data),]) # transpose no need?
if (component == 1){
mue_prior = matrix(0, length(parents)+1, 1) # pred x 1
LAMBDA = LAMBDA_MAT_first
} else {
if (length(parents) > 0) {
mue_prior = vector_i[c(1, parents+1), 1] * mue_apost
} else {
mue_prior = vector_i[1,1] * mue_apost
}
LAMBDA = LAMBDA_MAT
}
# pred x pred
Sigma_inv = solve(LAMBDA) + X %*% t(X) # pred * pred
mue_apost = solve(Sigma_inv) %*% (solve(LAMBDA) %*% mue_prior + X %*% y)
########## no need to transpose, as it is already a column vector now (no need to specify "n" either, mvrnorm works similar way as mvnrnd in matlab and gives the same dimension as mue_apost)
W_i = as.matrix(MASS::mvrnorm(mu = mue_apost, Sigma = var_all*solve(Sigma_inv)))
if (component==1){
beta_snr_i = beta_snr_i + 0.5 * inv_var_all * t(W_i - mue_prior) %*% solve(diag(length(parents)+1)) %*% (W_i - mue_prior)
} else {
indi = which(ifelse(vector_i %in% -1, 0, 1) !=0 )
vec_i = vector_i[indi]
ind0_new = which(vec_i==0)
ind1_new = which(vec_i==1)
W_i_snr = W_i[ind0_new]
W_i_coup = W_i[ind1_new] # HERE CHECK ALL IF THE INDEXING WORKED FINE!!!!! ALL VETORS if just [] works or do i need to specify [,] comma somwhere...
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 * t(W_i_snr - mue_prior_snr) %*% solve(diag(n0)) %*% (W_i_snr - mue_prior_snr)
}
if(n1>0){
beta_coup_i = beta_coup_i + 0.5 * inv_var_all * t(W_i_coup-mue_prior_coup) %*% solve(diag(n1)) %*% (W_i_coup - mue_prior_coup)
}
}
} # component for-loop ends
inv_lambda_coup = rgamma(n = 1, shape = alpha_coup_i, scale = (1/beta_coup_i))
lambda_coup_vec[i_node, 1] = (1/inv_lambda_coup)
inv_lambda_snr = rgamma(n = 1, shape = alpha_snr_i, scale = (1/beta_snr_i))
lambda_snr_vec[i_node, 1] = (1/inv_lambda_snr)
} # for loop i_node ends
output <- list(lambda_snr_vec, lambda_coup_vec, VECTORS)
return(output)
} # UPDATE function ends