-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path062_Pure_CPUs.R
359 lines (257 loc) · 9.41 KB
/
062_Pure_CPUs.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#===========
# 5 May 2024
#===========
# Aim:
# Try only parallelization over 140 CPUs
# Method:
# 055 non GPU + 061 parallel
#============================
# multi-CPU core pre-settings
#============================
#install.packages("optimParallel")
.libPaths("/bask/projects/v/vjgo8416-xchen")
library(optimParallel)
#==============
# Pre-settings
#==============
#-----
# df
#-----
df_2D_TW_CAMS <- readRDS("df_2D_TW_CAMS.rds")
#head(df_2D_TW_CAMS)
#str(df_2D_TW_CAMS)
# data.frame': 200 obs. of 12 variables
#---------
# data str
#---------
hierarchy_data_CAMS <- data.frame(
node_id = c(1, 2, 3, 4, 5, 5),
par_id = c(NA, 1, 2, 3, c(4, 1))
)
p = 5
data_str <- hierarchy_data_CAMS
#-----
# pars
#-----
source("Fn_para_mat_construct.R")
all_pars_lst_CAR_2D_CMS <- All_paras_CAR_2D(p = 5, data = hierarchy_data_CAMS)
all_pars_lst <- all_pars_lst_CAR_2D_CMS
#----------
# 2D coords
#----------
ds <- 0.1
s <- seq(-10 + ds/2, 10 - ds/2, by = ds)
crds <- cbind(s, s)
#----------------------------------------
# Construct displacement matrix (DSP_mat)
#----------------------------------------
# Aim:
# for TST12 function to construct shft dist matrix
source("Fn_make_DSP_mat.R")
DSP <- make_DSP_mat(crds = crds)
#str(DSP[, , 1]) # num [1:200, 1:200]
#str(DSP[, , 2])
#---------------------------
# Construct distance matrix
#---------------------------
# Aim:
# for H_adj and phi for UniCAR
DIST <- as.matrix(dist(crds, diag = T, upper = T))
Nb_radius <- 0.6 # lag-4
H_adj <- matrix(as.numeric(abs(DIST) < Nb_radius), nrow(DIST), nrow(DIST))
diag(H_adj) <- 0
spec <- eigen(H_adj, symmetric = T, only.values = T)$val
phi <- 1/max(abs(spec)) # 0.1251127
phi <- trunc(phi * 100)/100 # [1] 0.12
#=========
# neg_logL function
#=========
neg_logL_CAR_2D <- function(theta, p, data_str, all_pars_lst,
dsp_lon_mat, dsp_lat_mat, b, phi, H_adj, df){
source("Fn_TST12_SG_SGInv_CAR_2D.R")
source("Fn_I_sparse.R")
# connect each component of theta to all_pars_lst
# to incoporate each theta component into the neg log L function
theta_indx <- 1
for (lst in 1:length(all_pars_lst)){
for (i in 1:nrow(all_pars_lst[[lst]])){
for (j in 1:ncol(all_pars_lst[[lst]])){
if (is.na(all_pars_lst[[lst]][i, j])){
all_pars_lst[[lst]][i, j] <- theta[theta_indx]
theta_indx <- theta_indx + 1
}
}
}
}
# construct SIGMA_Y, SIGMA_Y_inv for process Y
SG_SG_inv_Y <- TST12_SG_SGInv_CAR_2D(p = p, data = data_str,
A_mat = all_pars_lst[[1]],
dsp_lon_mat = dsp_lon_mat,
dsp_lat_mat = dsp_lat_mat,
dlt_lon_mat = all_pars_lst[[2]],
dlt_lat_mat = all_pars_lst[[3]],
b = b, phi = phi,
H_adj = H_adj,
sig2_mat = all_pars_lst[[4]],
reg_ini = 1e-9, thres_ini = 1e-3)
SG_Y <- SG_SG_inv_Y$SIGMA
SG_Y_inv <- SG_SG_inv_Y$SIGMA_inv
#str(SG_Y) # num [1:1200, 1:1200]
# calculate SG_Ng
## 1st calcuate the # of parameters accumulated so far,
# so can connect theta components on top of current index
# with measurement error tau2
source("Fn_para_mat_construct.R")
all_pars_lst <- All_paras_CAR_2D(p = p, data = data_str)
# for assign NA
SUM <- 0
for (i in 1:length(all_pars_lst)){
s <- sum(is.na(all_pars_lst[[i]]))
SUM <- SUM + s
}
# SUM 33
# tau2 diag matrix
#tau2_mat <- diag(theta[SUM+1], theta[SUM+2], ..., theta[SUM+p] )
THETA <- c()
for(i in 1:p){
THETA <- c(THETA, theta[SUM + i])
}
tau2_mat <- diag(THETA)
# total # of locations of univariate process
n1 <- nrow(df) # 200
I_sp_mat <- I_sparse(size = n1, value = 1)
SG_Ng <- kronecker(tau2_mat, I_sp_mat)
SG_Ng_inv <- solve(SG_Ng)
#str(SG_Ng_ft) # num [1:450]
# SIGMA, SIGMA_inv for observation Z
SG_Z = SG_Y + SG_Ng
# SG_Z_inv = SG_Ng_inv - SG_Ng_inv(SG_Y_inv +SG_Ng_inv)^{-1}SG_Ng_inv
SG_Y_Ng <- SG_Y_inv + SG_Ng_inv
SG_Y_Ng_inv <- chol2inv(chol(SG_Y_Ng))
SG_Z_inv <- SG_Ng_inv - SG_Ng_inv %*% SG_Y_Ng_inv %*% SG_Ng_inv
# log_det(SG_Z)
source("Fn_log_det.R")
chol_SG_Z <- chol(SG_Z)
log_SG_Z_det <- log_det(chol_SG_Z)
#str(log_SG_Z_det) # num 987
# construct joint Z, stack each Zi in df
Z <- c()
for (i in 1:p) {
Z <- c(Z, df[[paste0("Z", i)]])
}
# str(Z)# num [1:1200] or use Fn_Stack_Z
# neg_logL
L <- length(Z) # different from n1 = length(Z1)
neg_logL <- - (- (L/2) * log(2*pi) - 1/2 * log_SG_Z_det -
1/2 * t(Z) %*% SG_Z_inv %*% Z) # a 1 by 1 matrix
neg_logL <- as.numeric(neg_logL) # a scalar
# return scalar
return(neg_logL)
}
#========
# Optim
#========
source("Fn_para_mat_construct.R")
all_pars_lst_CAR_2D_CMS <- All_paras_CAR_2D(p = 5, data = hierarchy_data_CAMS)
all_pars_lst <- all_pars_lst_CAR_2D_CMS
#-----------
# ini values
#-----------
ini <- c(0.2, 0.1, 0.1, 0.5) # A, dlt_lon, dlt_lat, sig2
Vals <- c()
for (i in 1:length(all_pars_lst)){
value <- rep(ini[i], sum(is.na(all_pars_lst[[i]])))
Vals <- c(Vals, value)
}
all_ini_Vals <- c(Vals, rep(0.1, p)) # with tau2s
#---------------------------------
## lower bound for each parameters
#---------------------------------
# NA: no lower bound
lower_bound <- c(rep(NA, sum(is.na(all_pars_lst[[1]]))), # A
rep(0.05, sum(is.na(all_pars_lst[[2]]))), # dlt_lon
rep(0.05, sum(is.na(all_pars_lst[[3]]))), # dlt_lat
rep(0.001, sum(is.na(all_pars_lst[[4]]))), # sig2
rep(0.001, p)) # tau2
#=============================
# multi-core parallel settings
#=============================
# Create a cluster
cl <- makeCluster(51)
# Set the created cluster as the default for parallel computation
setDefaultCluster(cl = cl)
# Specify the library path on each worker
clusterEvalQ(cl, {
.libPaths("/bask/projects/v/vjgo8416-xchen")
})
# Load the packages on each worker
clusterEvalQ(cl, {
#library(torch)
#library(GPUmatrix)
library(optimParallel)
})
# Load the core R package (no need to specify the path)
clusterEvalQ(cl, {
library(Matrix)
})
# Source functions here to the current env
source("Fn_Check_par_node.R")
source("Fn_I_sparse.R")
source("Fn_chol_inv_gpu.R") # input a gpumatrix, return chol inv
source("Fn_shft_dist_mat.R") # construct shifted distance matrix using shft displacement for b function
source("Fn_Waves.R")
source("Fn_Wendland_32.R") # R = 0.5
source("Fn_check_set_SpNorm_Reg.R") # SpN + tune regularize number
source("Fn_forceSym_GPU.R") # forceSym on GPU
source("Fn_Tst_sym_pd_GPU.R") # GPU version
source("Fn_Thres_tune_cov_GPU.R") # thresholding SIGMA_inv and return SIGMA and SIGMA_inv on GPU
source("Fn_para_mat_construct.R")
source("Fn_TST12_SG_SGInv_CAR_2D_GPU.R")
# Save each function as a variable in the current Env
Check_par_node <- get("Check_par_node")
I_sparse <- get("I_sparse")
chol_inv_gpu <- get("chol_inv_gpu")
Shft_dst_mat <- get("Shft_dst_mat")
TriWave_2D <- get("TriWave_2D")
WendLd32_2D <- get("WendLd32_2D")
check_set_SpNorm_Reg <- get("check_set_SpNorm_Reg")
forceSym_gpu <- get("forceSym_gpu")
Tst_sym_pd_gpu <- get("Tst_sym_pd_gpu")
spress_cov <- get("spress_cov")
check_pd_gpu <- get("check_pd_gpu")
Thres_tune_cov_gpu <- get("Thres_tune_cov_gpu")
TST12_SG_SGInv_CAR_2D_GPU <- get("TST12_SG_SGInv_CAR_2D_GPU")
Para_A_mat <- get("Para_A_mat")
Para_Dlt_lon_mat <- get("Para_Dlt_lon_mat")
Para_Dlt_lat_mat <- get("Para_Dlt_lat_mat")
Para_sig2_mat <- get("Para_sig2_mat")
All_paras_CAR_2D <- get("All_paras_CAR_2D")
# Export the variables to the cluster workers
clusterExport(cl, c("Check_par_node", "I_sparse", "chol_inv_gpu",
"TriWave_2D", "WendLd32_2D", "Tst_sym_pd_gpu",
"check_set_SpNorm_Reg", "spress_cov", "check_pd_gpu",
"Thres_tune_cov_gpu", "Shft_dst_mat", "forceSym_gpu",
"TST12_SG_SGInv_CAR_2D_GPU", "All_paras_CAR_2D", "Para_A_mat",
"Para_Dlt_lon_mat", "Para_Dlt_lat_mat", "Para_sig2_mat"))
# export each variable name to each worker
clusterExport(cl, c("all_ini_Vals", "p",
"hierarchy_data_CAMS", "all_pars_lst_CAR_2D_CMS",
"DSP", "phi", "H_adj", "df_2D_TW_CAMS", "lower_bound"))
#---------
# Tri-Wave
#---------
optm_pars_CAR_2D_TW_GPU_CPU <- optimParallel(par = all_ini_Vals, # ini guess
fn = neg_logL_CAR_2D,
p = p, data_str = hierarchy_data_CAMS,
all_pars_lst = all_pars_lst_CAR_2D_CMS,
dsp_lon_mat = DSP[, , 1],
dsp_lat_mat = DSP[, , 2],
b = "Tri-Wave",
phi = phi, H_adj = H_adj,
df = df_2D_TW_CAMS,
method = "L-BFGS-B",
lower = lower_bound,
control = list(maxit = 1,
factr=.05/.Machine$double.eps))
stopCluster(cl)
optm_pars_CAR_2D_TW_GPU_CPU