Describe your model in natural language, with its likelihood and priors. AI4BayesCode parses the model description, selects built-in blocks for the model, and generates compiled code you can run from R, Python, or C++.
Before writing any code, AI4BayesCode presents the parsed model and asks you to confirm that it matches the intended model. After code generation, the system checks that the sampler compiles and screens the code for bugs that compile and run but give a wrong posterior. It then runs the sampler on simulated data to confirm that the right posterior is recovered, and it reports R-hat, effective sample size (ESS), and posterior predictive checks.
The stateful sampler paradigm is central to AI4BayesCode. Each block retains its internal state while its conditioning quantities change, so it continues sampling within a larger MCMC procedure without reinitialization. This paradigm enables modular sampling components, potentially developed by different contributors, to be composed coherently within larger MCMC procedures. The same paradigm also supports rapid model development and exploration.
AI4BayesCode is built to be extended. New kinds of models can be supported quickly by adding a block, so new methods can be incorporated into the system as they are developed. The block library is also open to contribution, so users can add their own blocks and grow the set of models the system can build.
Examples
From model description to runnable chain.
You describe your model in natural language, with priors and likelihood. AI4BayesCode builds the sampler, validates it, compiles it to C++, and provides R and Python bindings. Below are two short model descriptions and the samplers they produce.
Natural-language model description
Bayesian linear regression, y ~ N(Xbeta, sigma^2). Normal prior on beta,
half-Normal on sigma.
Block assignment
joint_nuts_block(beta, sigma)
Generated interface
library(AI4BayesCode)
ai4bayescode_generate(
"linear regression, y ~ N(Xbeta, sigma^2), normal prior on beta, half-normal on sigma",
classname = "BayesLinReg", output_path = "./generated")
# a sampler written by the coding agent is loaded the same wayai4bayescode_source("./generated/BayesLinReg.cpp")
ai4bayescode_doc(BayesLinReg) # the constructor arguments# run several chains and check convergence (the usual workflow)
run <- ai4bayescode_run_chains(
function(s) new(BayesLinReg, y, X,
rng_seed = s, keep_history = TRUE),
n_chains = 4, n_burn = 5000, n_keep = 5000)
ai4bayescode_rhat_summary(run) # cross-chain R-hat and ESSai4bayescode_diagnose(run$histories[[1]]) # trace, ACF, density# optional: stateful usage, run a single chain yourself
m <- new(BayesLinReg, y, X, rng_seed = 1L, keep_history = TRUE)
m$step(10000)
hist <- m$get_history() # named list of matrices
cur <- m$get_current() # current draw
importAI4BayesCodeAI4BayesCode.generate(
"linear regression, y ~ N(Xbeta, sigma^2), normal prior on beta, half-normal on sigma",
classname="BayesLinReg", output_path="./generated")
# a sampler written by the coding agent is loaded the same way
mod = AI4BayesCode.source("./generated/BayesLinReg.cpp")
AI4BayesCode.doc(mod.BayesLinReg) # the constructor arguments# run several chains and check convergence (the usual workflow)
runs = AI4BayesCode.run_chains(
lambda s: mod.BayesLinReg(y, X,
rng_seed=s, keep_history=True),
n_chains=4, n_burn=5000, n_keep=5000)
AI4BayesCode.rhat_summary(runs) # cross-chain R-hat and ESSAI4BayesCode.diagnose(runs[0]) # trace, ACF, density# optional: stateful usage, run a single chain yourself
m = mod.BayesLinReg(y, X, rng_seed=1, keep_history=True)
m.step(10000)
hist = m.get_history() # dict of arrays
cur = m.get_current() # current draw
Natural-language model description
Spike-and-slab regression, y ~ N(Xbeta, sigma^2). beta_j ~ Laplace(b*sigma) if
gamma_j = 1, else 0. gamma_j ~ Bernoulli(pi). pi ~ Beta(1, 1),
half-Normal priors on sigma and b.
Block assignment
rjmcmc_block(beta, gamma)
joint_nuts_block(sigma, b)
beta_gibbs_block(pi)
Generated interface
library(AI4BayesCode)
ai4bayescode_generate(
"spike and slab, y ~ N(Xbeta, sigma^2), beta_j ~ Laplace(b*sigma) if gamma_j=1 else 0, gamma_j ~ Bernoulli(pi), pi ~ Beta(1,1), sigma ~ HalfNormal(0,1), b ~ HalfNormal(0,1)",
classname = "SpikeLaplace", output_path = "./generated")
# a sampler written by the coding agent is loaded the same wayai4bayescode_source("./generated/SpikeLaplace.cpp")
ai4bayescode_doc(SpikeLaplace) # the constructor arguments# run several chains and check convergence (the usual workflow)
run <- ai4bayescode_run_chains(
function(s) new(SpikeLaplace, y, X,
rng_seed = s, keep_history = TRUE),
n_chains = 4, n_burn = 5000, n_keep = 5000)
ai4bayescode_rhat_summary(run)
# posterior inclusion probabilities
inc <- colMeans(run$histories[[1]]$gamma)
# optional: stateful usage, run one chain with full control
m <- new(SpikeLaplace, y, X, rng_seed = 1L, keep_history = TRUE)
m$set_current(list(sigma = 1.0)) # warm-start a parameter
m$step(2000) # run burn-in iterations
m$readapt_NUTS(500L, FALSE, -1L) # re-tune NUTS (n, reset, max_tree_depth)
m$step(10000)
ai4bayescode_diagnose(m$get_history(), n_burn = 2000) # drop the burn-in iterations
importnumpyas np
importAI4BayesCodeAI4BayesCode.generate(
"spike and slab, y ~ N(Xbeta, sigma^2), beta_j ~ Laplace(b*sigma) if gamma_j=1 else 0, gamma_j ~ Bernoulli(pi), pi ~ Beta(1,1), sigma ~ HalfNormal(0,1), b ~ HalfNormal(0,1)",
classname="SpikeLaplace", output_path="./generated")
# a sampler written by the coding agent is loaded the same way
mod = AI4BayesCode.source("./generated/SpikeLaplace.cpp")
AI4BayesCode.doc(mod.SpikeLaplace) # the constructor arguments# run several chains and check convergence (the usual workflow)
runs = AI4BayesCode.run_chains(
lambda s: mod.SpikeLaplace(y, X,
rng_seed=s, keep_history=True),
n_chains=4, n_burn=5000, n_keep=5000)
AI4BayesCode.rhat_summary(runs)
# posterior inclusion probabilities
inc = runs[0]["hist"]["gamma"].mean(axis=0)
# optional: stateful usage, run one chain with full control
m = mod.SpikeLaplace(y, X, rng_seed=1, keep_history=True)
m.set_current({"sigma": np.array([1.0])}) # warm-start a parameter
m.step(2000); m.readapt_NUTS(500) # run burn-in iterations, then re-tune NUTS
m.step(10000)
AI4BayesCode.diagnose(m.get_history(), n_burn=2000) # drop the burn-in iterations