Go back to the About page.
Go back to the Approximation page
Let us set some global options for all code chunks in this document.
# Set seed for reproducibility
set.seed(1982)
# 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)
}
# function 1
gets_graph_interval <- function(n){
edge <- rbind(c(0,0),c(1,0))
edges = list(edge)
graph <- metric_graph$new(edges = edges)
graph$build_mesh(n = n)
return(graph)
}
# true eigenfunctions
interval.eig <- function(k,graph){
x <- c(0,graph$get_edge_lengths()[1]*graph$mesh$PtE[,2],1)
if(k==0){
f.0 <- rep(1,length(x))
f1 = c(f.0[1],f.0[length(f.0)], f.0[2:(length(f.0)-1)])
f = list(phi=f1)
}else{
f.c <- sqrt(2)*cos(pi*k*x)
f2 = c(f.c[1],f.c[length(f.c)], f.c[2:(length(f.c)-1)])
f <- list(psi=f2)
}
return(f)
}
# parameters
n = 333
rho = 1
m = 4
nu = 0.6
sigma = 1
n.overkill = 1000
graph = gets_graph_interval(n = n)
kappa = sqrt(8*nu)/rho
tau = sqrt(gamma(nu) / (sigma^2 * kappa^(2*nu) * (4*pi)^(1/2) * gamma(nu + 1/2))) #sigma = 1, d = 1
alpha = nu + 1/2
# getting true covariance matrix
true_cov_mat <- matrix(0,nrow = dim(graph$mesh$V)[1],ncol = dim(graph$mesh$V)[1])
for(i in 0:n.overkill){
if(i==0){
phi <- interval.eig(i,graph)$phi
true_cov_mat <- true_cov_mat + (1/(kappa^2 + (i*pi)^2)^(alpha))*phi%*%t(phi)
}else{
psi <- interval.eig(i,graph)$psi
true_cov_mat <- true_cov_mat + (1/(kappa^2 + (i*pi)^2)^(alpha))*psi%*%t(psi)
}
}
true_cov_mat <- true_cov_mat/tau^2
true_cov_mat_reordered <- true_cov_mat[c(1,3:(n+2),2), c(1,3:(n+2),2)] # needs reorder
# getting approximate matrix
op <- matern.operators(alpha = alpha, kappa = kappa, tau = tau,
m = m, graph = graph)
appr_cov_mat = op$covariance_mesh()
appr_cov_mat_reordered <- appr_cov_mat[c(1,3:(n+2),2), c(1,3:(n+2),2)] # needs reorder
# computing the errors
L_inf_error = max(abs(true_cov_mat - appr_cov_mat))
L_2_error = sqrt(as.double(t(graph$mesh$weights)%*%(true_cov_mat - appr_cov_mat)^2%*%graph$mesh$weights))
print(L_inf_error)
## [1] 0.0009502132
print(L_2_error)
## [1] 0.0001074629
# plot just for confirmation
plot(true_cov_mat_reordered[round(n/2)+1,], type="l", col = "blue", lty = 2) #true
lines(appr_cov_mat_reordered[round(n/2)+1,], type = "l", col = "red", lty = 3) #approx
legend("topright", legend = c("true", "approx"), col = c("blue", "red"), lty = 1)
Figure 1: True and approximate covariance matrix for the interval graph.
point <- c(1, 0.5)
c_cov <- op$cov_function_mesh(matrix(point,1,2))
loc <- graph$coordinates(PtE = point)
m1 <- which.min((graph$mesh$V[,1]-loc[1])^2 + (graph$mesh$V[,2]-loc[2])^2)
p <- graph$plot_function(true_cov_mat[m1,], plotly = TRUE, line_width = 3)
graph$plot_function(c_cov,p=p, line_color = "red", plotly = TRUE,line_width = 3)
Figure 2: True and approximate covariance matrix for the interval graph.
r = 1/(pi)
theta <- seq(from=-pi,to=pi,length.out = 100)
edge <- cbind(1+r+r*cos(theta),r*sin(theta))
edges = list(edge)
n = 666
graph <- metric_graph$new(edges = edges)
graph$build_mesh(n = n)
#true eigenfunctions
circle.eig <- function(k,graph,L){
x <- c(0,graph$get_edge_lengths()[1]*graph$mesh$PtE[,2],2)
if(k==0){
f.0 <- rep(1,length(x))
f.0 = f.0[-length(f.0)]
f = list(phi=f.0/sqrt(L))
}else{
f.c <- sqrt(2/L)*cos(pi*k*x/L)
f.c = f.c[-length(f.c)]
f.s <- sqrt(2/L)*sin(pi*k*x/L)
f.s = f.s[-length(f.s)]
f <- list(phi=f.c,psi=f.s)
}
return(f)
}
rho = 1
m = 4
nu = 0.6
sigma = 1
kappa = sqrt(8*nu)/rho
tau = sqrt(gamma(nu) / (sigma^2 * kappa^(2*nu) * (4*pi)^(1/2) * gamma(nu + 1/2))) #sigma = 1, d = 1
alpha = nu + 1/2
L = 1
#check KL expansion
true_cov_mat <- matrix(0,nrow = dim(graph$mesh$V)[1],ncol = dim(graph$mesh$V)[1])
for(i in 0:1000){
if(i==0){
phi <- circle.eig(i,graph,L)$phi
true_cov_mat <- true_cov_mat + (1/(kappa^2 + (i*pi/L)^2)^(alpha))*phi%*%t(phi)
}else{
eigen <- circle.eig(i,graph,L)
psi <- eigen$psi
phi <- eigen$phi
true_cov_mat <- true_cov_mat + (1/(kappa^2 + (i*pi/L)^2)^(alpha))*psi%*%t(psi) +
(1/(kappa^2 + (i*pi/L)^2)^(alpha))*phi%*%t(phi)
}
}
true_cov_mat <- true_cov_mat/tau^2/2
# getting approximate matrix
op <- matern.operators(alpha = alpha, kappa = kappa, tau = tau,
m = m, graph = graph)
appr_cov_mat = op$covariance_mesh()
# computing the errors
L_inf_error = max(abs(true_cov_mat - appr_cov_mat))
L_2_error = sqrt(as.double(t(graph$mesh$weights)%*%(true_cov_mat - appr_cov_mat)^2%*%graph$mesh$weights))
print(L_inf_error)
## [1] 0.0008314198
print(L_2_error)
## [1] 0.0002983357
#plot just for confirmation
plot(true_cov_mat[round(n/2),], type="l", col = "blue", lty = 2) #true
lines(appr_cov_mat[round(n/2),], type = "l", col = "red", lty = 3) #approx
legend("topright", legend = c("true", "approx"), col = c("blue", "red"), lty = 1)
Figure 3: True and approximate covariance matrix for the circle graph.
point <- c(1,0)
c_cov <- op$cov_function_mesh(matrix(point,1,2))
loc <- graph$coordinates(PtE = point)
m1 <- which.min((graph$mesh$V[,1]-loc[1])^2 + (graph$mesh$V[,2]-loc[2])^2)
p <- graph$plot_function(true_cov_mat[m1,], plotly = TRUE, line_width = 3)
graph$plot_function(c_cov,p=p, line_color = "red", plotly = TRUE,line_width = 3)
Figure 4: True and approximate covariance matrix for the circle graph.
We used R version 4.4.1 (R Core Team 2024a) and the following R packages: cowplot v. 1.1.3 (Wilke 2024), ggmap v. 4.0.0.900 (Kahle and Wickham 2013), ggpubr v. 0.6.0 (Kassambara 2023), ggtext v. 0.1.2 (Wilke and Wiernik 2022), grid v. 4.4.1 (R Core Team 2024b), here v. 1.0.1 (Müller 2020), htmltools v. 0.5.8.1 (Cheng et al. 2024), INLA v. 24.12.11 (Rue, Martino, and Chopin 2009; Lindgren, Rue, and Lindström 2011; Martins et al. 2013; Lindgren and Rue 2015; De Coninck et al. 2016; Rue et al. 2017; Verbosio et al. 2017; Bakka et al. 2018; Kourounis, Fuchs, and Schenk 2018), inlabru v. 2.12.0.9002 (Yuan et al. 2017; Bachl et al. 2019), knitr v. 1.48 (Xie 2014, 2015, 2024), latex2exp v. 0.9.6 (Meschiari 2022), Matrix v. 1.6.5 (Bates, Maechler, and Jagan 2024), MetricGraph v. 1.4.0.9000 (Bolin, Simas, and Wallin 2023b, 2023a, 2023c, 2024; Bolin et al. 2024), OpenStreetMap v. 0.4.0 (Fellows and JMapViewer library by Jan Peter Stotz 2023), osmdata v. 0.2.5 (Mark Padgham et al. 2017), patchwork v. 1.2.0 (Pedersen 2024), plotly v. 4.10.4 (Sievert 2020), plotrix v. 3.8.4 (J 2006), reshape2 v. 1.4.4 (Wickham 2007), rmarkdown v. 2.28 (Xie, Allaire, and Grolemund 2018; Xie, Dervieux, and Riederer 2020; Allaire et al. 2024), rSPDE v. 2.4.0.9000 (Bolin and Kirchner 2020; Bolin and Simas 2023; Bolin, Simas, and Xiong 2024), scales v. 1.3.0 (Wickham, Pedersen, and Seidel 2023), sf v. 1.0.19 (E. Pebesma 2018; E. Pebesma and Bivand 2023), sp v. 2.1.4 (E. J. Pebesma and Bivand 2005; Bivand, Pebesma, and Gomez-Rubio 2013), tidyverse v. 2.0.0 (Wickham et al. 2019), viridis v. 0.6.4 (Garnier et al. 2023), xaringanExtra v. 0.8.0 (Aden-Buie and Warkentin 2024).