-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarginal_sd.Rmd
534 lines (434 loc) · 15.1 KB
/
marginal_sd.Rmd
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
---
title: "Computing the marginal standard deviation explicitly and computationally efficiently"
date: "Created: 05-07-2024. Last modified: `r format(Sys.time(), '%d-%m-%Y.')`"
output:
html_document:
mathjax: "https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"
highlight: pygments
theme: flatly
code_folding: show # class.source = "fold-hide" to hide code and add a button to show it
# df_print: paged
toc: true
toc_float:
collapsed: true
smooth_scroll: true
number_sections: false
fig_caption: true
code_download: true
always_allow_html: true
bibliography:
- references.bib
- grateful-refs.bib
header-includes:
- \newcommand{\ar}{\mathbb{R}}
- \newcommand{\llav}[1]{\left\{#1\right\}}
- \newcommand{\pare}[1]{\left(#1\right)}
- \newcommand{\Ncal}{\mathcal{N}}
- \newcommand{\Vcal}{\mathcal{V}}
- \newcommand{\Ecal}{\mathcal{E}}
- \newcommand{\Wcal}{\mathcal{W}}
---
```{r xaringanExtra-clipboard, echo = FALSE}
htmltools::tagList(
xaringanExtra::use_clipboard(
button_text = "<i class=\"fa-solid fa-clipboard\" style=\"color: #00008B\"></i>",
success_text = "<i class=\"fa fa-check\" style=\"color: #90BE6D\"></i>",
error_text = "<i class=\"fa fa-times-circle\" style=\"color: #F94144\"></i>"
),
rmarkdown::html_dependency_font_awesome()
)
```
```{css, echo = FALSE}
body .main-container {
max-width: 100% !important;
width: 100% !important;
}
body {
max-width: 100% !important;
}
body, td {
font-size: 16px;
}
code.r{
font-size: 14px;
}
pre {
font-size: 14px
}
.custom-box {
background-color: #f5f7fa; /* Light grey-blue background */
border-color: #e1e8ed; /* Light border color */
color: #2c3e50; /* Dark text color */
padding: 15px; /* Padding inside the box */
border-radius: 5px; /* Rounded corners */
margin-bottom: 20px; /* Spacing below the box */
}
.caption {
margin: auto;
text-align: center;
margin-bottom: 20px; /* Spacing below the box */
}
```
Below we set some global options for all code chunks in this document.
```{r}
rm(list = ls())
# Set seed for reproducibility
set.seed(593)
# Set global options for all code chunks
knitr::opts_chunk$set(
# Disable messages printed by R code chunks
message = FALSE,
# Disable warnings printed by R code chunks
warning = FALSE,
# Show R code within code chunks in output
echo = TRUE,
# Include both R code and its results in output
include = TRUE,
# Evaluate R code chunks
eval = TRUE,
# Enable caching of R code chunks for faster rendering
cache = FALSE,
# Align figures in the center of the output
fig.align = "center",
# Enable retina display for high-resolution figures
retina = 2,
# Show errors in the output instead of stopping rendering
error = TRUE,
# Do not collapse code and output into a single block
collapse = FALSE
)
# Start the figure counter
fig_count <- 0
# Define the captioner function
captioner <- function(caption) {
fig_count <<- fig_count + 1
paste0("Figure ", fig_count, ": ", caption)
}
# Define the function to truncate a number to two decimal places
truncate_to_two <- function(x) {
floor(x * 100) / 100
}
```
Below we load the necessary libraries and define the auxiliary functions.
```{r}
library(rSPDE)
library(MetricGraph)
library(sp)
library(osmdata)
library(dplyr)
library(plotly)
library(scales)
library(patchwork)
library(tidyr)
library(here)
library(rmarkdown)
# Cite all loaded packages
library(grateful)
```
```{r}
rtilde_matern_interval <- function(kappa, tau, le, t1, t2) (1 / (2 * kappa * tau^2 * sinh(kappa * le))) * (cosh(kappa * (le - abs(t1 - t2))) + cosh(kappa * (t1 + t2 - le)))
sparse_A <- function(n) bandSparse(n-1, n, k = c(0, 1), diagonals = list(rep(1, n), rep(-1, n - 1)))
```
Let's build a small graph to illustrate how to compute the marginal standard deviation.
```
{r}
edge1 <- rbind(c(0,0), c(1,0))[2:1, ]
theta <- seq(from = -pi, to = pi,length.out = 100)
edge2 <- cbind(1+1/pi+cos(theta)/pi, sin(theta)/pi)
edge3 <- rbind(c(-1,-1), c(1,0))[2:1, ]
edges <- list(edge1, edge2, edge3)
graph <- metric_graph$new(edges = edges)
```
```
{r}
edge <- rbind(c(0,0),c(1,0))
edges <- list(edge)
graph <- metric_graph$new(edges = edges)
```
```
{r}
edges_roots_of_unity <- function(n, length = 10) {
x_coords <- cos(2 * pi * (0:(n-1)) / n)
y_coords <- sin(2 * pi * (0:(n-1)) / n)
return(lapply(1:n, function(i) {rbind(c(0, 0), c(length*x_coords[i], length*y_coords[i]))}))
}
n <- 10
my_length <- 2
graph <- metric_graph$new(edges = edges_roots_of_unity(n, length = my_length))
```
```{r}
a <- 3
b <- 12
edge2_x <- runif(1, 8, 12)
edge1 <- rbind(c(0,0),c(-runif(1, a, b),0))
edge2 <- rbind(c(0,0),c(edge2_x,0))
edge3 <- rbind(c(edge2_x,0),c(edge2_x + runif(1, a, b),0))
edge4 <- rbind(c(edge2_x,0),c(edge2_x + runif(1, a, b),-runif(1, a, b)))
edge5 <- rbind(c(edge2_x,0),c(edge2_x + runif(1, a, b),runif(1, a, b)))
edge6 <- rbind(c(0,0),c(-runif(1, a, b),-runif(1, a, b)))
edge7 <- rbind(c(0,0),c(-runif(1, a, b), runif(1, a, b)))
edges <- list(edge1, edge2, edge3, edge4, edge5, edge6, edge7)
graph <- metric_graph$new(edges = edges)
```
```
{r}
edges <- logo_lines()
graph <- metric_graph$new(edges = edges, perform_merges = TRUE)
```
```
{r}
call <- opq(bbox = c(39.0884, 22.33, 39.115, 22.3056))
call <- add_osm_feature(call, key = "highway",value=c("motorway",
"primary","secondary",
"tertiary",
"residential"))
data <- osmdata_sf(call)
graph <- graph_components$new(data)
graph <- graph$get_largest()
graph$prune_vertices()
```
```{r}
graph_copy <- graph$clone()
graph_pcr_1 <- graph$clone()
graph_pcr_2 <- graph$clone()
graph$build_mesh(h = 0.1)
```
```{r}
sigma <- 1
range <- 1
nu <- 1/2
kappa <- sqrt(8*nu)/range
tau <-sqrt(gamma(nu) / (sigma^2 * kappa^(2*nu) * (4*pi)^(1/2) * gamma(nu + 1/2)))
alpha <- nu + 1/2 #alpha = 1
rtilde_int <- function(t1, t2, le) rtilde_matern_interval(kappa = kappa, tau = tau, le, t1, t2)
```
```{r}
get_marginal_std <- function(kappa, tau, graph){
V <- graph$V
E <- graph$E
degrees <- graph$get_degrees()
edge_lengths <- as.vector(graph$get_edge_lengths())
VtE <- graph$mesh$VtE
if(nrow(V) == 2 & nrow(E) == 1){
aux_function <- function(VtErow) {
edge <- VtErow[1]
s <- VtErow[2]
le <- edge_lengths[edge]
return(rtilde_int(le*s, le*s, le))
}
} else {
# Build matrix U
U <- matrix(nrow = 0, ncol = 2)
for (vertex in seq_len(nrow(V))) {
if (degrees[vertex] > 1) {
edges_leaving <- cbind(which(E[,1] %in% vertex), 0)
edges_entering <- cbind(which(E[,2] %in% vertex), 1)
if (sum(dim(edges_leaving)) == 2 ) {edges_leaving <- NULL}
if (sum(dim(edges_entering)) == 2 ) {edges_entering <- NULL}
constraints <- rbind(edges_leaving, edges_entering)
ordered_constraints <- constraints[order(constraints[, 1], constraints[, 2]),]
U <- rbind(U, ordered_constraints)
}
}
# Build matrix A
A <- bdiag(lapply(degrees[degrees >1], sparse_A))
# Build matrix Sigma
edge_lengths_in_U <- edge_lengths[U[, 1]]
s_diag <- edge_lengths_in_U*U[, 2]
diag <- as.vector(mapply(rtilde_int, t1 = s_diag, t2 = s_diag, le = edge_lengths_in_U))
Sigma <- Diagonal(x = diag)
for (edge in seq_len(nrow(E))) {
pos <- which(U[, 1] == edge)
if (length(pos) > 1) {
Sigma[pos[1], pos[2]] <- Sigma[pos[2], pos[1]] <- rtilde_int(t1 = 0, t2 = edge_lengths[edge], le = edge_lengths[edge])
}
}
ASigmaAt <- A %*% Sigma %*% t(A)
aux_function <- function(VtErow) {
edge <- VtErow[1]
s <- VtErow[2]
le <- edge_lengths[edge]
pos <- which(U[, 1] == edge)
cc <- mapply(rtilde_int, t1 = le*s, t2 = U[pos,2]*le, le = le)
v <- if(length(pos) == 1) as.vector(A[, pos] * cc) else as.vector(A[, pos] %*% cc)
return(rtilde_int(le*s, le*s, le) - t(v) %*% solve(ASigmaAt, v))
}
}
return(sqrt(apply(X = VtE, MARGIN = 1, FUN = aux_function)))
}
```
```{r}
compute_pcr_2v <- function(cov_matrix, graph, cor_threshold = 0.1) {
# Compute the correlation matrix
cor_matrix <-cov2cor(cov_matrix)
# Compute the geodesic distance matrix
graph$compute_geodist_mesh()
# Extract the geodesic distance matrix
dist_matrix <- graph$mesh$geo_dist
# Initialize the vector to store the practical correlation range
pcr <- numeric(dim(cor_matrix)[1])
process_row <- function(row_index) {
# For each row_index, create an auxiliary matrix aux_mat with the correlation and geodesic distance
aux_mat <- cbind(cor_matrix[row_index, ], dist_matrix[row_index, ])
# Order the auxiliary matrix by the geodesic distance
ordered_aux_mat <- aux_mat[order(aux_mat[, 2]), ]
# Filter the auxiliary ordered matrix by the correlation threshold
filtered_aux_mat <- ordered_aux_mat[ordered_aux_mat[,1] < cor_threshold, ]
if (nrow(filtered_aux_mat) > 0) {
# Return the minimum geodesic distance such that the correlation is below the threshold
return(filtered_aux_mat[1, 2])
} else {
# If the condition is not satisfied, record the minimum correlation value and return an error message
min <- min(aux_mat[, 1])
stop(paste0("The condition cor_matrix[row_index, ] < cor_threshold is not satisfied for row_index = ",
row_index, ". Increase cor_threshold. The minimum value of cor_matrix[row_index, ] is ",
min))
}
}
pcr <- sapply(1:dim(cor_matrix)[1], process_row)
return(pcr)
}
```
```{r}
std_cheap <- get_marginal_std(kappa = kappa, tau = tau, graph = graph)
```
```{r}
mesh_loc <- graph$get_mesh_locations() %>%
as.data.frame() %>%
mutate(y = 1) %>%
rename(edge_number = V1, distance_on_edge = V2)
graph_copy$add_observations(mesh_loc,
edge_number = "edge_number",
distance_on_edge = "distance_on_edge",
data_coords = "PtE",
normalized = TRUE,
clear_obs = TRUE)
graph_copy$observation_to_vertex()
Q <- spde_precision(kappa = kappa,
tau = tau,
alpha = alpha,
graph = graph_copy,
BC = 0) # BC = 1
Sigma <- solve(Q)
std_expensive <- sqrt(diag(Sigma))
nonstat_cov <- Matrix::Diagonal(x = 1/std_cheap)%*% Sigma %*%Matrix::Diagonal(x = 1/std_cheap)
nonstat_est_sigma <- sqrt(Matrix::diag(nonstat_cov))
sum(abs(std_cheap - std_expensive))
plot(std_cheap - std_expensive)
```
```{r}
graph_pcr_1$build_mesh(h = 0.1)
graph_pcr_2$build_mesh(h = 0.1)
pcr_1 <- compute_pcr_2v(cov_matrix = Sigma, graph = graph_pcr_1, cor_threshold = 0.1)
pcr_2 <- compute_pcr_2v(cov_matrix = nonstat_cov, graph = graph_pcr_2, cor_threshold = 0.1)
sum(abs(pcr_1 - pcr_2))
plot(pcr_1 - pcr_2)
```
The following are the plots for the marginal standard deviation.
:::: {style="display: grid; grid-template-columns: 400px 400px 400px; grid-column-gap: 1px;"}
::: {}
```{r, out.width = "100%"}
a = 2.35
graph$plot_function(X = std_expensive, vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, edge_color = "black", edge_width = 3) %>% # true one
graph$plot_function(X = std_cheap, p = ., vertex_size = 1, type = "plotly", line_color = "blue", line_width = 3, edge_color = "black", edge_width = 3) %>% # approx one
config(mathjax = 'cdn') %>%
layout(title = TeX("\\sigma(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::: {}
```{r, out.width = "100%"}
graph$plot_function(X = nonstat_est_sigma, vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\sigma_u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::: {}
```{r, out.width = "100%"}
graph$plot_function(X = pcr_1, vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>% # true one
graph$plot_function(X = pcr_2, p = ., vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>% # approx one
config(mathjax = 'cdn') %>%
layout(title = TeX("\\rho(s), \\rho_u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::::
The following are the corresponding plots for the practical correlation range.
:::: {style="display: grid; grid-template-columns: 400px 400px 400px; grid-column-gap: 1px;"}
::: {}
```{r, out.width = "100%"}
a = 2.35
graph$plot_function(X = pcr_1, vertex_size = 1, type = "plotly", line_color = "blue", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\rho(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::: {}
```{r, out.width = "100%"}
graph$plot_function(X = pcr_2, vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\rho_u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::: {}
```{r, out.width = "100%"}
graph$plot_function(X = pcr_1 - pcr_2, vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\rho_u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
:::
::::
```{r}
Sigma <- cov2cor(Sigma)
```
```{r, out.width = "100%"}
graph$plot_function(X = Sigma[300,], vertex_size = 1, type = "plotly", line_color = "red", line_width = 3, edge_color = "black", edge_width = 3) %>%
graph$plot_function(X = Sigma[5,], p = ., vertex_size = 1, type = "plotly", line_color = "darkgreen", line_width = 3, edge_color = "black", edge_width = 3) %>%
graph$plot_function(X = Sigma[3,], p = ., vertex_size = 1, type = "plotly", line_color = "blue", line_width = 3, edge_color = "black", edge_width = 3) %>%
config(mathjax = 'cdn') %>%
layout(title = TeX("\\rho_u(s)"),
font = list(family = "Palatino"),
showlegend = FALSE,
scene = list(
aspectratio = list(x = 1, y = 1, z = 1),
camera = list(
eye = list(x = -0.7*a, y = 0.7*a, z = 0.7*a))))
```
# References
```{r}
cite_packages(output = "paragraph", out.dir = ".")
```