-
Notifications
You must be signed in to change notification settings - Fork 0
/
INITIALIZE.R
197 lines (139 loc) · 6.12 KB
/
INITIALIZE.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
INITIALISE <- function(DATA_ALL, DAG, steps, nue_var, lambda_snr_vec, lambda_coup_vec, MATRIX, VECTORS){
n_plus <- dim(DATA_ALL[[1]])[1]
m <- dim(DATA_ALL[[1]])[2]
n_nodes <- length(DATA_ALL)
DATA <- list()
for (node_i in 1:n_nodes){
data <- DATA_ALL[[node_i]]
nested_DATA <- list()
for (component in 1:max(MATRIX[node_i, ])){
nested_DATA[[component]] <- data[ , which(MATRIX[node_i, ] == component)]
}
DATA[[node_i]] <- nested_DATA
}
log_score <- COMPUTE_LOG_SCORE(DATA, DAG, MATRIX, nue_var, lambda_snr_vec, lambda_coup_vec, VECTORS)
Run <- list()
for (i in 1:(steps+1)){
Run$dag[[i]] <- 0
Run$matrix[[i]] <- 0 ## Check the data structure for each again!
Run$Log_Scores[[i]] <- 0
Run$lambda_snr_vec[[i]] <- 0
Run$lambda_coup_vec[[i]] <- 0
Run$VECTORS[[i]] <- 0
}
# Initialisation:
Run$dag[[1]] <- DAG
Run$matrix[[1]] <- MATRIX
Run$Log_Scores[[1]] <- log_score
Run$steps <- 1
Run$lambda_snr_vec[[1]] <- lambda_snr_vec
Run$lambda_coup_vec[[1]] <- lambda_coup_vec
Run$VECTORS[[1]] <- VECTORS
return(Run)
}
##########################################################
##########################################################
COMPUTE_LOG_SCORE <- function(DATA, DAG, MATRIX, nue_var, lambda_snr_vec, lambda_coup_vec, VECTORS) {
#global Prior; Prior needs to be a global variable!
log_prob_breaks <- 0
n_nodes <- dim(MATRIX)[1]
m <- dim(MATRIX)[2]
for (i_node in 1:n_nodes){
k <- length(DATA[[i_node]])
log_prob_k <- log(dpois(k,1))
k_cps <- k-1
breakpoints <- which((MATRIX[i_node, 2:ncol(MATRIX)] - MATRIX[i_node, 1:(ncol(MATRIX)-1)])!=0) # does this work?
if (length(breakpoints) == 0){
log_prob_break <- 0
} else {
breakpoints <- matrix( c(0, breakpoints, m), nrow=1)
log_prob_break <- log(prod(1:(2*k_cps+1))) - log(prod(((m-1)-(2*k_cps+1)+1):(m-1)))
for (i in 2:length(breakpoints)){
log_prob_break <- log_prob_break + log(breakpoints[i]-breakpoints[i-1] - 1)
}
}
log_prob_breaks <- log_prob_breaks + log_prob_break + log_prob_k
}
#################################################################################
log_prob_graph = 0
for (node in 1:n_nodes){
log_prob_graph = log_prob_graph + Prior[length(which(DAG[,node] != 0))+1]
}
#################################################################################
log_prob_data = 0
for (i_node in 1:n_nodes){
k_i = length(DATA[[i_node]])
parents = which(DAG[ ,i_node] != 0)
lambda_coup = lambda_coup_vec[i_node, 1]
lambda_snr = lambda_snr_vec[i_node, 1]
sum_log_det_Sigma_tilde = 0
sum_Delta2 = 0
vector_i = VECTORS[[i_node]]
ind1 = which(vector_i==1)
ind0 = which(vector_i==0)
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)]
for (component in 1:k_i){
data = DATA[[i_node]][component][[1]] # to make data as a matrix [[1]]
n_plus <- dim(data)[1]
n_obs <- dim(data)[2]
if (n_obs == 0){
# do nothing
} else {
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)
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
Sigma_tilde = diag(n_obs) + (t(X) * LAMBDA) %*% X ### not sure if here %*% or * is correct.
# pred * obs
inv_Sigma_tilde = diag(n_obs) - t(X) %*% solve(solve(LAMBDA) + X %*% t(X)) %*% X
# (1 x obs) * (obs x obs) * (obs x 1)
sum_Delta2 = sum_Delta2 + (t(y - m_tilde) %*% inv_Sigma_tilde %*% (y - m_tilde))
sum_log_det_Sigma_tilde = sum_log_det_Sigma_tilde + log(det(Sigma_tilde))
Sigma_inv = solve(LAMBDA) + X %*% t(X) # pred * pred
mue_apost = solve(Sigma_inv) %*% (solve(LAMBDA) %*% mue_prior + X %*% y) # pred x 1
}
}
sum_1 = log(gamma((m + nue_var)/2)) - log(gamma(nue_var/2))
sum_2 = (nue_var/2) * log(nue_var) - (m/2)*log(pi) - 0.5 * sum_log_det_Sigma_tilde
sum_3 = -(m + nue_var)/2 * log(nue_var + sum_Delta2)
log_score_i = sum_1 + sum_2 + sum_3
log_prob_data = log_prob_data + log_score_i
}
#################################################################################
# global alpha_snr; # They all need to be global variables!
# global beta_snr;
# global alpha_coup;
# global beta_coup;
log_prob_lambda = 0
for (i_node in 1:n_nodes){
log_prob_lambda_snr_i = log(dgamma(1/lambda_snr_vec[i_node, 1], alpha_snr, scale = (1/beta_snr)))
log_prob_lambda_coup_i = log(dgamma(1/lambda_coup_vec[i_node, 1], alpha_coup, scale = (1/beta_coup)))
log_prob_lambda = log_prob_lambda + log_prob_lambda_snr_i + log_prob_lambda_coup_i
}
#################################################################################
log_prob_VECTOR = (sum(sum(DAG)) + n_nodes) * log(0.5)
#################################################################################
log_score = log_prob_breaks + log_prob_graph + log_prob_data + log_prob_lambda + log_prob_VECTOR
return (log_score)
}
## need to check whether the matrix multiplication works. this is just vague draft. compare with matlab by stopping each step !!!