Last updated: 2025-04-22

Checks: 7 0

Knit directory: BOSS_website/

This reproducible R Markdown analysis was created with workflowr (version 1.7.1). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.


Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.

Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.

The command set.seed(20250415) was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.

Great job! Recording the operating system, R version, and package versions is critical for reproducibility.

Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.

Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.

Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.

The results in this page were generated with repository version 6af839c. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.

Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish or wflow_git_commit). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:


Ignored files:
    Ignored:    .DS_Store
    Ignored:    .Rhistory
    Ignored:    .Rproj.user/
    Ignored:    analysis/.DS_Store
    Ignored:    analysis/.Rhistory
    Ignored:    code/.DS_Store
    Ignored:    data/.DS_Store
    Ignored:    data/sim1/
    Ignored:    output/.DS_Store
    Ignored:    output/sim2/.DS_Store

Untracked files:
    Untracked:  code/mortality_BG_grid.R
    Untracked:  code/mortality_NL_grid.R
    Untracked:  data/co2/
    Untracked:  data/mortality/
    Untracked:  data/simA1/
    Untracked:  output/co2/
    Untracked:  output/mortality/
    Untracked:  output/sim2/quad_sparse_list.rda
    Untracked:  output/simA1/

Unstaged changes:
    Modified:   output/sim2/BO_data_to_smooth.rda
    Modified:   output/sim2/BO_result_list.rda
    Modified:   output/sim2/rel_runtime.rda

Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.


These are the previous versions of the repository in which changes were made to the R Markdown (analysis/diagnostic.rmd) and HTML (docs/diagnostic.html) files. If you’ve configured a remote Git repository (see ?wflow_git_remote), click on the hyperlinks in the table below to view the files as they were in that past version.

File Version Author Date Message
Rmd 6af839c Ziang Zhang 2025-04-22 workflowr::wflow_publish("analysis/diagnostic.rmd")
html b02f19c david.li 2025-04-22 Build site.
Rmd 8b99f87 david.li 2025-04-22 wflow_publish("analysis/diagnostic.Rmd")
html b5d1ce2 Ziang Zhang 2025-04-22 Build site.
Rmd 074cea3 Ziang Zhang 2025-04-22 workflowr::wflow_publish("analysis/diagnostic.rmd")
html 6bb1cbf Ziang Zhang 2025-04-22 Build site.
Rmd 8550bd7 Ziang Zhang 2025-04-22 workflowr::wflow_publish("analysis/diagnostic.rmd")

Introduction

In this tutorial, we examine several diagnostic tools that can be used to assess the convergence of BOSS.

For now, let’s assume the following true (log) posterior:

library(npreg)
Package 'npreg' version 1.1.0
Type 'citation("npreg")' to cite this package.
library(ggplot2)
library(aghq)

set.seed(123)
noise_var = 1e-6
function_path <- "./code"
output_path <- "./output/simA1"
data_path <- "./data/simA1"
source(paste0(function_path, "/00_BOSS.R"))

lower = 0; upper = 10

log_prior <- function(x){
  1
}
log_likelihood <- function(x){
  log(x + 1) * (sin(x * 4) + cos(x * 2))
}
eval_once <- function(x){
  log_prior(x) + log_likelihood(x)
}
eval_once_mapped <- function(y){
  eval_once(pnorm(y) * (upper - lower) + lower) + dnorm(y, log = T) + log(upper - lower)
}
x <- seq(0.01,9.99, by = 0.01)
y <- qnorm((x - lower)/(upper - lower))
true_log_norm_constant <- log(integrate(f = function(y) exp(eval_once_mapped(y)), lower = -Inf, upper = Inf)$value)
true_log_post_mapped <- function(y) {eval_once_mapped(y) - true_log_norm_constant}
plot((true_log_post_mapped(y)) ~ y, type = "l", cex.lab = 1.5, cex.axis = 1.5, 
     xlab = "y", ylab = "Log density", lwd = 2, col = "blue")

Version Author Date
b5d1ce2 Ziang Zhang 2025-04-22
6bb1cbf Ziang Zhang 2025-04-22
true_log_post <- function(x) {true_log_post_mapped(qnorm((x - lower)/(upper - lower))) - dnorm(qnorm((x - lower)/(upper - lower)), log = T) - log(upper - lower)}
integrate(function(x) exp(true_log_post(x)), lower = 0, upper = 10)
1 with absolute error < 9.1e-05
plot(exp(true_log_post(x)) ~ x, type = "l", cex.lab = 1.5, cex.axis = 1.5, 
     xlab = "x", ylab = "Posterior density", lwd = 2, col = "blue")

Version Author Date
b5d1ce2 Ziang Zhang 2025-04-22

KL Divergence

Let \(f_t\) and \(f_{t-j}\) be the corresponding surrogate density at time \(t\) and \(t-j\), respectively. We can compute the KL divergence between \(f_t\) and \(f_{t-j}\) as follows: \[Q_t = KL(f_t,f_{t-j}) = \int \log \frac{f_t(x)}{f_{t-j}(x)}f_{t}(x)dx.\]

For one-dimensional problems, this can be done efficiently through numerical integration. For higher-dimensional problems, sampling-based methods can be used to approximate the KL divergence.

result_ad <- BOSS(
  func = eval_once, initial_design = 5,
  update_step = 5, max_iter = 30,
  opt.lengthscale.grid = 100, opt.grid = 1000,
  delta = 0.01, noise_var = noise_var,
  lower = lower, upper = upper,
  verbose = 0,
  KL_iter_check = 1, KL_check_warmup = 5, KL_eps = 0, criterion = "KL"
)

plot(result_ad$KL_result$KL ~ result_ad$KL_result$i,
     xlab = "Iteration", ylab = "KL statistic",
     main = "KL statistic over iterations", 
     log = "y",
     pch = 19, col = "blue")

Version Author Date
6bb1cbf Ziang Zhang 2025-04-22

Based on the KL divergence, it seems like the algorithm has converged around 30 iterations.

KS Statistics

The Kolmogorov-Smirnov (KS) statistic measures the maximum difference between the cumulative distribution functions (CDFs) \(F_t\) and \(F_{t-j}\) of the surrogate densities \(f_t\) and \(f_{t-j}\), respectively. Specifically, for one dimensional problems, the KS statistic is defined as: \[Q_t = \max_x |F_t(x) - F_{t-j}(x)|.\]

result_ad <- BOSS(
  func = eval_once, initial_design = 5,
  update_step = 5, max_iter = 30,
  opt.lengthscale.grid = 100, opt.grid = 1000,
  delta = 0.01, noise_var = noise_var,
  lower = lower, upper = upper,
  verbose = 0,
  KS_iter_check = 1, KS_check_warmup = 5, KS_eps = 0, criterion = "KS"
)

plot(result_ad$KS_result$KS ~ result_ad$KS_result$i,
     xlab = "Iteration", ylab = "KS statistic",
     main = "KS statistic over iterations", 
     log = "y",
     pch = 19, col = "blue")

Version Author Date
6bb1cbf Ziang Zhang 2025-04-22

Based on the KS statistic, the conclusion is similar to that of the KL divergence. The KS statistics is very close to 0 after 30 iterations, indicating that the algorithm has likely converged.