-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path032b_1D_simu_SpN_6_filds_Threshold.R
467 lines (320 loc) · 13 KB
/
032b_1D_simu_SpN_6_filds_Threshold.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#============
# 16 Feb.2024
#============
# Aim:
# produce the 1D-simulation plots for part I of the paper
# in particular, thresholding and set those value in the precision matrix
# < threshold to exact zero.
# Method:
# source("032_1D_simu_plt_SpN_6_fileds.R")
# TST9_SpNormPert_SG_SGInv
# modify to thresholding the final run of SG_inv to exact zero for
# values below given threshold
# threshold ini is set to 1e-3 (quite aggressive)
# the function sourced "Fn_Thres_tune_cov.R" will automatically check if
# the resulting SG_inv after thresholding is p.d. or not
# if not p.d., it will automatically tune the thresholding value
# to the one that is 0.1 magnitude smaller
# and will check the resulting SG_inv after new threshold is p.d. or not
# until find a largest possible thresholding value that could set to values
# below this threshold to exact zero while maitain the p.d. of SG_inv
# Values:
# the function TST9b automatically return the constructed SIGMA and SIGMA_inv
# that has values below the largest possible threshold being exact zero
# both SIGMA and SIGMA_inv are p.d.
TST9b_SpNormPert_SG_SGInv <- function(p, data, A_mat, dlt_mat, sig2_mat, kappa_mat,
d_vec, h, thres_ini = 1e-3) {
source("Fn_Matern_32.R")
source("Fn_Check_par_node.R")
source("Fn_Waves.R")
source("Fn_Wendland_32.R") # R = 0.5
source("Fn_Pert_Mat.R")
source("Fn_Tst_sym_pd.R")
source("Fn_check_set_SpN_Pert.R")# lower kappa of B
source("Fn_Thres_tune_cov.R") # thresholding SIGMA_inv and return SIGMA and SIGMA_inv
C11 <- Matern_32(Var = sig2_mat[1, 1], Kappa = kappa_mat[1, 1], d_vec = d_vec)
n <- nrow(C11)
SIGMA <- C11
for(r in seq(2, p, by = 1)){
PN <- Check_par_node(Node = r, data = data)
R <- C <- NULL
cat("r:", r, "\n")
for(c in seq(1, (r-1), by = 1)){
BT <- NULL
C_rc <- 0
for(t in c(PN)){
#B_rt <- wave_v9(h = h, delta = dlt_mat[r, t], A = A_mat[r, t])
#B_rt <- wave_v7(h = h, delta = dlt_mat[r, t], A = A_mat[r, t])
#B_rt <- wave_v6(h = h, delta = dlt_mat[r, t], A = A_mat[r, t])
#B_rt <- wave_v5(h = h, delta = dlt_mat[r, t], A = A_mat[r, t])
#B_rt <- wave_v4(h = h, delta = dlt_mat[r, t], A = A_mat[r, t])
B_rt <- WendLd_32(r = h, R = 0.5, dlt = dlt_mat[r, t], A = A_mat[r, t])
## spectral normalization of B_rt
B_rt <- check_set_SpNorm_pert(B_rt)
cat("B cond numb:", kappa(B_rt), "\n")
BT <- rbind(BT, t(B_rt))
C_rc <- C_rc + B_rt %*% SIGMA[((t-1)*n+1) : (t*n), ((c-1)*n+1): (c*n)]
}
R <- cbind(R, C_rc)
C_cr <- t(C_rc)
C <- rbind(C, C_cr)
}
D_rr <- Matern_32(Var = sig2_mat[r, r], Kappa = kappa_mat[r, r], d_vec = d_vec)
t <- c(PN)
Subset_cols <- function(t) {
start_col <- (t - 1) * n + 1
end_col <- t * n
result <- R[, start_col:end_col]
}
result_lst <- lapply(t, FUN = Subset_cols)
R_subset <- do.call(cbind, result_lst)
C_rr <- R_subset %*% BT + D_rr
SG <- SIGMA # p-1 for inverse
SG <- forceSymmetric(SG)
Col <- rbind(C, C_rr)
Row <- rbind(SG, R)
SIGMA <- cbind(Row, Col)
SIGMA <- forceSymmetric(SIGMA)
## SIGMA_Inv
Drr_inv <- chol2inv(chol(D_rr)) # Schur complement inverse
if (r == 2){
SG_inv <- chol2inv(chol(SG))
}
CDrr_in <- C %*% Drr_inv
CDR_sym <- forceSymmetric(CDrr_in %*% R)
#CDR_sym <- forceSymmetric(C %*% Drr_inv %*% R)
cat("condition number of C", kappa(C), "\n")
cat("condition number of CDinv", kappa(CDrr_in), "\n")
cat("condition number of CDinvR", kappa(CDR_sym), "\n")
BK1 <- SG_inv %*% (SG + CDR_sym) %*% SG_inv
BK1 <- forceSymmetric(BK1)
BK2 <- - SG_inv %*% C %*% Drr_inv
BK3 <- - Drr_inv %*% R %*% SG_inv
BK4 <- forceSymmetric(Drr_inv)
#cat("BK4", "\n")
#Tst_sym_pd(BK4)
SIGMA_inv <- rbind(cbind(BK1, BK2), cbind(BK3, BK4))
#SG_inv <- SIGMA_inv
SG_inv <- forceSymmetric(SIGMA_inv)
# early perturb SG_inv if not p.d.
cat("r", r, "\n")
#SG_inv <- Pert_Mat(SG_inv)
SG_inv <- Pert_Mat_2(SG_inv) # larger pertub range
cat("SG_inv", "\n")
Tst_sym_pd(SG_inv)
if (r == p) {
# threshold SG_inv with thres_ini
SG_inv_ini <- SG_inv * (abs(SG_inv) > thres_ini)
# 1. tune threshold if SG_inv_ini is NOT p.d.,
# 2. cov_mat construct with new thres
# 3. check p.d. until cov_mat is p.d. with the updated largest possible thres
# 4. return the thresholded and p.d. SIMGA_inv and SIGMA
SG_SG_inv_thres <- Thres_tune_cov(thres_ini = thres_ini, cov_mat_thres = SG_inv_ini,
cov_mat = SG_inv)
return(list(SIGMA = as.matrix(SIGMA),
SIGMA_inv = SG_SG_inv_thres$SIGMA_inv))
}
}
}
#=========
# Settings
#=========
#------------------------------------
# Location, displacements, distance
#------------------------------------
ds <- 0.1 # for with SpN plts
ds <- 0.05 # for w/o SpN plts, esp. SIGMA; also try for plts with SpN
# ds = 0.05 has better visualization effect for both with, w/o SpN
s <- seq(-1 + ds/2, 1 - ds/2, by = ds)
str(s) # num [1:20]; num [1:40]
# displacements between pairs of points
# a vector quantity has magnitude and direction
H <- outer(s, s, FUN = "-")
H <- t(H)
str(H) # num [1:20, 1:20]; num [1:40, 1:40]
# distance
# a scalar quantity
D_vec <- as.double(c(abs(H)))
str(D_vec) # num [1:400]; num [1:1600]
#----------------
# data structure
#----------------
p = 6
hierarchy_data6 <- data.frame(
node_id = c(1, 2, 3, 3, 4, 4, 5, 6, 6, 6),
par_id = c(NA, 1, c(2, 1), c(2, 3), 4, c(1, 3, 5))
)
#-----------
# Parameters
#-----------
source("Fn_para_mat_construct.R")
all_pars_lst_6 <- All_paras(p = 6, data = hierarchy_data6)
A_mat_0.1 <- Fn_set_ini_vals(pars_mat = all_pars_lst_6[[1]], ini_vals = 0.1)
dlt_mat_0.5 <- Fn_set_ini_vals(pars_mat = all_pars_lst_6[[2]], ini_vals = 0.5)
sig2_mat_1 <- Fn_set_ini_vals(pars_mat = all_pars_lst_6[[3]], ini_vals = 1)
kappa_mat_2 <- Fn_set_ini_vals(pars_mat = all_pars_lst_6[[4]], ini_vals = 2)
#========================
# Test the TST9b function
#========================
# Aim:
# to test the TST9b function, to see if it automatically
# tune the thresholding value for SG_inv to ensure both
# its sparsity and p.d.
#----------
# Tri-Wave
#----------
# With SpN
SG_SG_inv_6_a01d05_TriWave_Thres <- TST9b_SpNormPert_SG_SGInv(p = 6, data = hierarchy_data6,
A_mat = A_mat_0.1, dlt_mat = dlt_mat_0.5,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H, thres_ini = 1e-3)
# r 6
#No need to perturb.
#SG_inv
#[1] "Symmetric: Yes"
#[1] "p.d.: Yes"
#ini thres: 0.001
#length(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA) # [1] 14400
## Without SpN
SG_SG_inv_6_a01d05_TriWave_Thres_NoSpN <- TST9b_SpNormPert_SG_SGInv(p = 6, data = hierarchy_data6,
A_mat = A_mat_0.1, dlt_mat = dlt_mat_0.5,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H, thres_ini = 1e-3)
# r 6
#No need to perturb.
#SG_inv
#[1] "Symmetric: Yes"
#[1] "p.d.: Yes"
#ini thres: 0.001
#---------
# Wendland
#---------
# with SpN
SG_SG_inv_6_a01d05_Wend_Thres <- TST9b_SpNormPert_SG_SGInv(p = 6, data = hierarchy_data6,
A_mat = A_mat_0.1, dlt_mat = dlt_mat_0.5,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H, thres_ini = 1e-3)
# r 6
# No need to perturb.
#SG_inv
#[1] "Symmetric: Yes"
#[1] "p.d.: Yes"
#ini thres: 0.001
#new thres: 1e-04
#[1] "Symmetric: Yes"
#[1] "p.d.: Yes"
#length(SG_SG_inv_6_a01d05_Wend_Thres$SIGMA) # [1] 14400
# without SpN
SG_SG_inv_6_a01d05_Wend_Thres_NOSpN <- TST9b_SpNormPert_SG_SGInv(p = 6, data = hierarchy_data6,
A_mat = A_mat_0.1, dlt_mat = dlt_mat_0.5,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H, thres_ini = 1e-3)
#r 6
#No need to perturb.
#SG_inv
#[1] "Symmetric: Yes"
#[1] "p.d.: Yes"
#ini thres: 0.001
#========
# Plot
#========
#---------
# Tri-Wave
#---------
# With SpN
plt_Sig(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA_inv, p = 6)
plt_Sig(log(abs(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA_inv)), p = 6)
# Without SpN
plt_Sig(SG_SG_inv_6_a01d05_TriWave_Thres_NoSpN$SIGMA, p = 6)
plt_Sig(log(abs(SG_SG_inv_6_a01d05_TriWave_Thres_NoSpN$SIGMA_inv)), p = 6)
#----------
# Wendland
#----------
# With SpN
plt_Sig(Sigma = SG_SG_inv_6_a01d05_Wend_Thres$SIGMA, p = 6)
plt_Sig(Sigma = log(SG_SG_inv_6_a01d05_Wend_Thres$SIGMA), p = 6)
plt_Sig(Sigma = SG_SG_inv_6_a01d05_Wend_Thres$SIGMA_inv, p = 6)
plt_Sig(log(abs(SG_SG_inv_6_a01d05_Wend_Thres$SIGMA_inv)), p = 6)
# Without SpN
plt_Sig(SG_SG_inv_6_a01d05_Wend_Thres_NOSpN$SIGMA, p = 6)
plt_Sig(Sigma = SG_SG_inv_6_a01d05_Wend_Thres_NOSpN$SIGMA_inv, p = 6)
plt_Sig(log(abs(SG_SG_inv_6_a01d05_Wend_Thres_NOSpN$SIGMA_inv)), p = 6)
#------------------------------------------------------
# Functions for SIGMA, SIGMA_Inv side by side with main
#------------------------------------------------------
Plot_SG_Main <- function(Sigma, p) {
# to reverse order of cols in Sigma
rev_Sigma <- Sigma[, ncol(Sigma):1]
# Plot the matrix with reversed y-axis scale
par(mar = c(3, 3, 4.5, 1), cex.main = 2)
image(1:nrow(Sigma), 1:ncol(Sigma), rev_Sigma,
main = expression(atop(Sigma~ (log), atop("Tri-wave; p = 6")))
#main = expression(atop(Sigma^{-1}~ (log), atop("Wendland; p = 6")))
)
}
Plot_SG_Inv_Main <- function(Sigma, p) {
# to reverse order of cols in Sigma
rev_Sigma <- Sigma[, ncol(Sigma):1]
# Plot the matrix with reversed y-axis scale
par(mar = c(3, 3, 4.5, 1), cex.main = 2)
image(1:nrow(Sigma), 1:ncol(Sigma), rev_Sigma,
main = expression(atop(Sigma^{-1}~ (log), atop("Tri-wave; p = 6; Threshold = 1e-3")))
#main = expression(atop(Sigma^{-1}~ (log), atop("Wendland; p = 6")))
)
}
Plot_SG_Main_Wend <- function(Sigma, p) {
# to reverse order of cols in Sigma
rev_Sigma <- Sigma[, ncol(Sigma):1]
# Plot the matrix with reversed y-axis scale
par(mar = c(3, 3, 4.5, 1), cex.main = 2)
image(1:nrow(Sigma), 1:ncol(Sigma), rev_Sigma,
#main = expression(atop(Sigma~ (log), atop("Tri-wave; p = 6")))
main = expression(atop(Sigma~ (log), atop("Wendland; p = 6")))
)
}
Plot_SG_Inv_Main_Wend <- function(Sigma, p) {
# to reverse order of cols in Sigma
rev_Sigma <- Sigma[, ncol(Sigma):1]
# Plot the matrix with reversed y-axis scale
par(mar = c(3, 3, 4.5, 1), cex.main = 2)
image(1:nrow(Sigma), 1:ncol(Sigma), rev_Sigma,
#main = expression(atop(Sigma^{-1}~ (log), atop("Tri-wave; p = 6")))
main = expression(atop(Sigma^{-1}~ (log), atop("Wendland; p = 6; Threshold = 1e-4")))
)
}
## Side by side with main
jpeg(paste0(image.path, "SG_SGinv_Wave_WL_SpN_SBS_Thres.jpeg"),
width = 10, height = 9, units = "in", res = 300)
par(mfrow = c(2, 2))
#Plot_SG_Main(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA, p = 6)
Plot_SG_Main(log(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA), p = 6)
Plot_SG_Inv_Main(log(abs(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA_inv)), p = 6)
Plot_SG_Main_Wend(Sigma = log(SG_SG_inv_6_a01d05_Wend$SIGMA), p = 6)
Plot_SG_Inv_Main_Wend(Sigma = log(abs(SG_SG_inv_6_a01d05_Wend$SIGMA_inv)), p = 6)
dev.off()
#============================================
# Experiement largest possible ini threshold
#============================================
SG_SG_inv_6_a01d05_Wend <- TST9_SpNormPert_SG_SGInv(p = 6, data = hierarchy_data6,
A_mat = A_mat_0.1, dlt_mat = dlt_mat_0.5,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H)
sg_inv <- SG_SG_inv_6_a01d05_Wend$SIGMA_inv
min(sg_inv) # [1] -1349.841
min(abs(sg_inv)) # [1] 9.580329e-09
sg_inv_ini <- sg_inv * (abs(sg_inv) > 1e-4)
length(which(sg_inv_ini == 0)) # 822
Tst_sym_pd(sg_inv_ini) # p.d. YES
length(sg_inv_ini) # 120*120 = 14400
length(which(sg_inv_ini == 0)) / length(sg_inv_ini) *100
# 5.708% sparse
#====================
# Sparsity percentage
#====================
length(which(SG_SG_inv_6_a01d05_Wend_Thres$SIGMA_inv == 0))
# [1] 822
length(which(SG_SG_inv_6_a01d05_TriWave_Thres$SIGMA_inv == 0))
# [1] 418
120*120 # 14400
418/14400 * 100 # 2.902778%
822 / 14400 * 100 # 5.708333%