-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path014_Investigate_p.d._condition_SIGMA_inv_Haville.R
221 lines (148 loc) · 5.34 KB
/
014_Investigate_p.d._condition_SIGMA_inv_Haville.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
#==============
# 27 Nov. 2023
#==============
# Aim:
# from SIGMA_inv formula, use Haville P243 Corollary 14.8.6
# obtain BK4 is p.d. and SIGMA_inv / BK4 is p.d.
# so, Drr_inv is p.d.
# SG_inv from last run is p.d.
# so now want to check the p.d. of SG_inv each run
# Method:
# try wave_v5, wave_v6 using TST4_SG_SG_INV
# TST4: Test the p.d. of BK4 and SG_inv of last run
# Discover:
# complete consistency between p.d. of SG_inv of last run
# and p.d. of final SIGMA_inv
# Strategies:
# 1. test p.d. of SG_inv each run, if not, add perturb
# 2. preconditioning the SG_inv, to improve its
# numerical stability; ref: Matrix computation P650
TST4_build_SG_SGInv <- function(p, data, A_mat, dlt_mat, sig2_mat, kappa_mat, d_vec, h) {
source("Fn_Matern_32.R")
source("Fn_Check_par_node.R")
source("Fn_Waves.R")
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_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])
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_Inv
Drr_inv <- chol2inv(chol(D_rr)) # Schur complement inverse
if (r == 2){
SG_inv <- chol2inv(chol(SG))
}
CDR_sym <- forceSymmetric(C %*% Drr_inv %*% R)
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)
cat("SG_inv", "\n")
Tst_sym_pd(SG_inv)
# can add perturb if not p.d. due to numerical issue
if (r == p) return(
list(SIGMA = as.matrix(forceSymmetric(SIGMA)),
SIGMA_inv = as.matrix(forceSymmetric(SIGMA_inv)))
)
}
}
#------
# data
#------
p = 5
hierarchy_data <- data.frame(
node_id = c(1, 2, 3, 3, 4, 4, 5),
par_id = c(NA, 1, c(2, 1), c(2, 3), 4)
)
#-----------
# Parameters
#-----------
source("Fn_para_mat_construct.R")
all_pars_lst_5 <- All_paras(p = 5, data = hierarchy_data)
source("Fn_set_ini_vals.R")
A_mat_0.5 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[1]], ini_vals = 0.5)
dlt_mat_0.5 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[2]], ini_vals = 0.5)
sig2_mat_1 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[3]], ini_vals = 1)
kappa_mat_2 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[4]], ini_vals = 2)
#=========================================================
# Test under all dlt and A combinations, p.d. of SIGMA_inv
#=========================================================
# Method:
# use TST4_SG_SGInv with
# wave_v5 (slow decay phi = 2; same region supprt)
# wave_v6 (slow decay phi = 1/2; same region supprt)
sig2_mat_1 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[3]], ini_vals = 1)
kappa_mat_2 <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[4]], ini_vals = 2)
for (dlt in seq(0.1, 1, by = 0.2)){
cat("dlt:", dlt, "\n")
dlt_mat_d <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[2]], ini_vals = dlt)
for (a in seq(0.5, 1, by = 0.1)){
cat("A:", a, "\n")
A_mat_a <- Fn_set_ini_vals(pars_mat = all_pars_lst_5[[1]], ini_vals = a)
SG_SG_inv_5 <- TST4_build_SG_SGInv(p = 5, data = hierarchy_data,
A_mat = A_mat_a, dlt_mat = dlt_mat_d,
sig2_mat = sig2_mat_1, kappa_mat = kappa_mat_2,
d_vec = D_vec, h = H)
cat("SIGMA_inv", "\n")
Tst_sym_pd(SG_SG_inv_5$SIGMA_inv)
}
}
#=============
# Conclusion
#=============
#----------
## wave_v5:
#----------
# this time all combinations of dlt and A
# for wave_v5, the final SIGMA_inv are all p.d.
# campare with 012_TST2_wave_v5
# encouter 3 non-p.d. cases using tiny perturb
# so the non-p.d. cases are indeed due to numerical issue
#----------
## wave_v6
#----------
# this time final SIGMA_inv is directly consistant with
# the SG_inv each run
# most of SG_inv becomes not p.d. at the r = 5 or 4, 5
#---------
# wave_v4
#---------
# this time final SIGMA_inv is directly consistant with
# the SG_inv each run
# most of SG_inv becomes not p.d. at the r = 5 or 4, 5