AI4BayesCode

From natural language descriptions to validated, modular, stateful Bayesian samplers.

Systematic validation, before and after.

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.

Stateful sampler paradigm for composing existing samplers within larger MCMC procedures.

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.

An extensible system, open to new models and contributors.

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 way
ai4bayescode_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 ESS
ai4bayescode_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

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 way
ai4bayescode_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

Paper

Read the paper.

AI4BayesCode received a Paper Award at STAI-X 2026, and the full paper is available on arXiv. The benchmark suite of 136 models in the paper can be downloaded.