Skip to content

Instantly share code, notes, and snippets.

@martinmodrak
Created February 27, 2024 12:45
Show Gist options
  • Save martinmodrak/b651d7dce7d17f937203cabf3cd0cb30 to your computer and use it in GitHub Desktop.
Save martinmodrak/b651d7dce7d17f937203cabf3cd0cb30 to your computer and use it in GitHub Desktop.
SBC shows failures when rejecting individual data points
library(SBC)
library(cmdstanr)
library(bayesplot)
library(posterior)
library(future)
plan(multisession)
options(SBC.min_chunk_size = 10)
m_code <- " data {
int<lower=0> N;
array[N] real y;
}
parameters {
real mu;
}
model {
mu ~ normal(0, 2);
y ~ normal(mu, 1);
}
"
backend <- SBC_backend_cmdstan_sample(cmdstan_model(write_stan_file(m_code)), iter_warmup = 800, iter_sampling = 800)
N <- 10
generator <- SBC_generator_function(function() {
mu <- rnorm(1, 0, 2)
# Reject observations one at a time
y <- rep(-Inf, N)
repeat {
bad_indices <- y < -1
if(!any(bad_indices)) {
break
}
y[bad_indices] <- rnorm(sum(bad_indices), mu, 1)
}
list(
variables = list(mu = mu),
generated = list(N = N, y = y)
)
})
set.seed(2323455)
datasets <- generate_datasets(generator, 100)
results <- compute_SBC(datasets, backend, keep_fits = FALSE)
plot_ecdf_diff(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment