Parallel simulations

Simulation studies β€” parameter recovery, scenario sweeps, prior sensitivity β€” are loops of independent fits. Written as a plain for loop they run one at a time, so a single core works while the rest of the machine sits idle.

run_simulations spreads that loop across worker processes. Each worker is a separate Python process with its own JAX backend, so every simulation gets its own bf() instance, its own share of the cores and its own seed.

The same runner exists in all three languages: run_simulations(...) in Python, m$run_simulations(...) in R and m.run_simulations(...) in Julia, with grid() / m$grid() / m.grid() to build the scenarios. In R and Julia the workers are R or Julia processes, so they run your R or Julia function; the scenarios, the seeds and the split of the cores come from the same Python code, so a given grid gets the same seeds in every language.

Crucially, the scenario settings travel with the results: every row of the returned table carries the parameters that produced it, so comparing true against estimated values needs no bookkeeping of your own.


1. The pattern

Write a function that runs one simulation and returns the numbers you care about. run_simulations calls it once per scenario, in parallel, and stacks the results into a pandas DataFrame.

from BayesForge import bf, grid, run_simulations

def one_sim(<your scenario parameters>, seed, **_):
    m = bf(rand_seed=seed, print_devices_found=False)  # one bf per simulation

    data = <simulate a dataset using the scenario parameters>
    m.fit(<model>, obs=data, progress_bar=False)

    return {"estimate": ..., "truth": ...}   # -> one row of the result table

scenarios = grid(<param>=[...], <param>=[...], reps=<n>)   # or just an int
res = run_simulations(one_sim, scenarios)                  # -> DataFrame
library(BayesForge)
m <- importBF(print_devices_found = FALSE)

one_sim <- function(<your scenario parameters>, seed) {
    m <- importBF(rand_seed = seed, print_devices_found = FALSE)  # one model per simulation

    data <- <simulate a dataset using the scenario parameters>
    m$fit(<model>, obs = data, progress_bar = FALSE)

    list(estimate = ..., truth = ...)   # -> one row of the result table
}

scenarios <- m$grid(<param> = c(...), <param> = c(...), reps = <n>)  # or just a number
res <- m$run_simulations(one_sim, scenarios)                         # -> data.frame
using PyBayesForge
m = importBF(print_devices_found = false)

# @sim: the workers get this definition too (they are new Julia processes).
@sim function one_sim(; <your scenario parameters>, seed)
    m = importBF(rand_seed = seed, print_devices_found = false)  # one model per simulation

    data = <simulate a dataset using the scenario parameters>
    m.fit(<model>, obs = data, progress_bar = false)

    (estimate = ..., truth = ...)       # -> one row of the result table
end

scenarios = m.grid(<param> = [...], <param> = [...], reps = <n>)  # or just a number
res = m.run_simulations(one_sim, scenarios)  # -> vector of named tuples; DataFrame(res)

Three things the runner injects into every call, on top of your own parameters:

injected meaning
sim global index of the simulation
rep replicate index within its scenario cell
seed per-simulation seed, derived deterministically from the base seed

Pass seed into bf(rand_seed=seed). Without it every worker draws the same data, because bf() seeds the global NumPy and Python RNGs from that value.

In Python, always give one_sim a **_ catch-all so extra injected keys cannot break it. In R and Julia the runner drops the keys one_sim does not declare (unless it takes ... / kw...), so declare only what you use.

While it runs, R and Julia print one line per finished simulation, with what one_sim printed itself (cat, print, message in R; println in Julia, prefixed From worker k:); progress = FALSE silences it:

run_simulations: 24 simulations on 24 workers (1 cores each)
[ 1/24] worker 3 | sim 2 | N=100, b_true=-0.8 | 23.1s | ok
         fitting
In a .py script, use a main guard

Workers are spawned, which re-imports the calling script. Without the guard the module level runs again in every worker:

if __name__ == "__main__":
    res = run_simulations(one_sim, scenarios)
# No guard: the workers are new R sessions that only run one_sim, with
# BayesForge already loaded. Objects one_sim takes from your session must be
# sent explicitly:
helper <- function(x) x + 1
res <- m$run_simulations(one_sim, scenarios, export = c("helper"))
# No guard: the workers are new Julia processes with PyBayesForge loaded.
# Functions defined in your session do not reach them: mark one_sim, every
# helper it calls and every extra package it uses with @sim, or load a file
# with `setup` (run in Main on every worker):
@sim using Statistics
@sim helper(x) = x + 1
res = m.run_simulations(one_sim, scenarios; setup = :(include("helpers.jl")))

Notebooks and the REPL need no guard.

Return numbers, not the model

A fitted bf object cannot cross a process boundary β€” m.diag holds closures over m, and m.dist is an unpicklable proxy. Return m.summary(), m.sampler_stats(), or a plain dict. Returning m raises an explicit error.


2. A concrete example

Recovering the slope of a linear regression across two sample sizes and three true slopes, four replicates each β€” 24 simulations.

import numpy as np
from BayesForge import bf, grid, run_simulations

def one_sim(N, b_true, seed, **_):
    m = bf(rand_seed=seed, print_devices_found=False)

    # --- simulate one dataset --------------------------------------
    x = np.asarray(m.dist.normal(0.0, 1.0, sample=True, shape=(N,)))
    y = np.asarray(m.dist.normal(1.5 + b_true * x, 1.0, sample=True))

    # --- the model -------------------------------------------------
    def linreg(x=None, y=None):
        a = m.dist.normal(0.0, 5.0, name="a")
        b = m.dist.normal(0.0, 5.0, name="b")
        s = m.dist.exponential(1.0, name="s")
        m.dist.normal(a + b * x, s, name="y", obs=y)

    # --- fit, then report truth against estimate -------------------
    m.fit(linreg, obs=dict(x=x, y=y),
          num_warmup=500, num_samples=500, num_chains=1,
          progress_bar=False)

    est = m.summary()
    return {"b_hat":  float(est.loc["b", "mean"]),
            "b_true": b_true,
            "r_hat":  float(est.loc["b", "r_hat"])}

if __name__ == "__main__":
    scenarios = grid(N=[100, 500], b_true=[-0.8, 0.0, 0.8], reps=4)   # 24 sims
    res = run_simulations(one_sim, scenarios)

    print(res[["sim", "N", "b_true", "b_hat", "elapsed_s"]].head())

    # Mean absolute error of the slope per scenario
    print((res.b_hat - res.b_true).abs().groupby([res.N, res.b_true]).mean())
library(BayesForge)
m <- importBF(print_devices_found = FALSE)

one_sim <- function(N, b_true, seed) {
    m <- importBF(rand_seed = seed, print_devices_found = FALSE)

    # --- simulate one dataset --------------------------------------
    x <- bf.dist.normal(0.0, 1.0, sample = TRUE, shape = N)
    y <- bf.dist.normal(1.5 + b_true * x, 1.0, sample = TRUE)

    # --- the model -------------------------------------------------
    linreg <- function(x, y) {
        a <- bf.dist.normal(0.0, 5.0, name = "a")
        b <- bf.dist.normal(0.0, 5.0, name = "b")
        s <- bf.dist.exponential(1.0, name = "s")
        bf.dist.normal(a + b * x, s, name = "y", obs = y)
    }

    # --- fit, then report truth against estimate -------------------
    m$fit(linreg, obs = list(x = x, y = y),
          num_warmup = 500L, num_samples = 500L, num_chains = 1L,
          progress_bar = FALSE)

    est <- m$summary()
    list(b_hat = est["b", "mean"],
         r_hat = est["b", "r_hat"])
}

scenarios <- m$grid(N = c(100, 500), b_true = c(-0.8, 0.0, 0.8), reps = 4)  # 24 sims
res <- m$run_simulations(one_sim, scenarios)

print(head(res[, c("sim", "N", "b_true", "b_hat", "elapsed_s")]))

# Mean absolute error of the slope per scenario
print(aggregate(abs(b_hat - b_true) ~ N + b_true, data = res, FUN = mean))
using PyBayesForge, PythonCall
m = importBF(print_devices_found = false)

@sim function one_sim(; N, b_true, seed)
    m = importBF(rand_seed = seed, print_devices_found = false)

    # --- simulate one dataset --------------------------------------
    x = m.dist.normal(0.0, 1.0, sample = true, shape = (N,))
    y = m.dist.normal(1.5 + b_true * x, 1.0, sample = true)

    # --- the model -------------------------------------------------
    @BF function linreg(x, y)
        a = m.dist.normal(0.0, 5.0, name = "a")
        b = m.dist.normal(0.0, 5.0, name = "b")
        s = m.dist.exponential(1.0, name = "s")
        m.dist.normal(a + b * x, s, name = "y", obs = y)
    end

    # --- fit, then report truth against estimate -------------------
    m.fit(linreg, obs = pydict(x = x, y = y),
          num_warmup = 500, num_samples = 500, num_chains = 1,
          progress_bar = false)

    est = m.summary()
    (b_hat = pyconvert(Float64, est.loc["b", "mean"]),
     r_hat = pyconvert(Float64, est.loc["b", "r_hat"]))
end

scenarios = m.grid(N = [100, 500], b_true = [-0.8, 0.0, 0.8], reps = 4)  # 24 sims
res = m.run_simulations(one_sim, scenarios)

using DataFrames
res = DataFrame(res)
println(first(res[:, [:sim, :N, :b_true, :b_hat, :elapsed_s]], 5))

# Mean absolute error of the slope per scenario
println(combine(groupby(res, [:N, :b_true]), [:b_hat, :b_true] => ((h, t) -> sum(abs.(h .- t)) / length(h)) => :mae))

res is a tidy DataFrame β€” the grid columns N and b_true are joined onto every row automatically:

   sim    N  b_true  b_hat  elapsed_s
0    0  100    -0.8  -0.64      8.952
1    1  100    -0.8  -0.80      9.099
2    2  100    -0.8  -0.93      8.598
3    3  100    -0.8  -0.62      8.620
4    4  100     0.0  -0.13      8.184

So the analysis is a groupby, with nothing to join by hand (the last line of the script above):

(res.b_hat - res.b_true).abs().groupby([res.N, res.b_true]).mean()
aggregate(abs(b_hat - b_true) ~ N + b_true, data = res, FUN = mean)
combine(groupby(res, [:N, :b_true]), [:b_hat, :b_true] => ((h, t) -> sum(abs.(h .- t)) / length(h)) => :mae)
N    b_true
100  -0.8      0.118
      0.0      0.073
      0.8      0.055
500  -0.8      0.015
      0.0      0.025
      0.8      0.018

Alongside your own columns, every row carries sim, rep, seed, worker, elapsed_s and error.


3. Chains, workers and cores

Chains and simulations compete for the same cores, so num_chains, workers and the fit’s chain_method must be chosen together. There are three sensible setups. All produce the same number of genuine chains and equivalent ESS β€” they differ in speed and in how many simulations are in flight at once.

fit’s chain_method pass num_chains= workers cores/worker sims at once
(single chain) omit n_cpu 1 n_cpu
'parallel' N n_cpu // N N n_cpu // N
'vectorized' omit n_cpu 1 n_cpu

Single-chain fits are the default and the quickest way through a large batch when you do not need a per-fit R-hat.

chain_method='parallel' puts one chain on one JAX device, so it needs a device per chain. Pass num_chains to the runner and take it back in your function, so the core budget and the fit can never disagree:

def one_sim(N, seed, num_chains, **_):
    ...
    m.fit(model, obs=data, num_chains=num_chains,
          chain_method="parallel", progress_bar=False)

res = run_simulations(one_sim, scenarios, num_chains=4)
one_sim <- function(N, seed, num_chains) {
    m <- importBF(rand_seed = seed, print_devices_found = FALSE)
    ...
    m$fit(model, obs = data, num_chains = as.integer(num_chains),
          chain_method = "parallel", progress_bar = FALSE)
}

res <- m$run_simulations(one_sim, scenarios, num_chains = 4)
@sim function one_sim(; N, seed, num_chains)
    m = importBF(rand_seed = seed, print_devices_found = false)
    ...
    m.fit(model, obs = data, num_chains = num_chains,
          chain_method = "parallel", progress_bar = false)
end

res = m.run_simulations(one_sim, scenarios; num_chains = 4)

On a 24-core machine num_chains=4 gives each worker 4 cores and 4 JAX devices, pins it to its own block of CPUs, and runs 6 simulations at a time instead of 24 β€” simulation concurrency traded for chain concurrency.

chain_method='vectorized' vmaps the chains inside a single device, so no cores are reserved for them and every core stays free for a different simulation. Do not pass num_chains for this one: it would reserve cores that vectorized chains never use.

Measured on 24 simulations over 24 cores, each drawing 4 chains at equal ESS:

setup wall
workers=24, cores_per_worker=1, vectorized 17.5 s
num_chains=4 (6 workers Γ— 4 cores), parallel 21.9 s
workers=6, cores_per_worker=4, vectorized 29.1 s
The silent trap

Asking a fit for num_chains=4 with chain_method='parallel' without telling the runner leaves the worker with one device. NumPyro then draws the chains sequentially β€” correct results, needlessly slow, with a warning per fit. Passing num_chains to run_simulations prevents it.


4. Useful options

argument what it does
workers How many simulations run at once. Defaults to one per core, divided by cores_per_worker.
fn_kwargs Constants passed to every call. In Python they are serialised once per worker rather than once per scenario β€” put large shared arrays here.
seed Base seed. Per-simulation seeds derive from it deterministically, so a rerun reproduces regardless of worker count or completion order β€” and the same grid gets the same seeds in Python, R and Julia.
on_error 'record' (default) puts the error in the row’s error column and keeps going; 'raise' aborts the run.
backend 'serial' runs in-process for debugging, so pdb / browser() and full tracebacks work.
progress FALSE / false silences the per-simulation lines.
export (R) Names of objects from your session that one_sim needs on the workers.
setup (Julia) An expression run in Main on every worker before the simulations, e.g. :(include("sim.jl")); @sim covers single definitions.

Inspecting failures without losing the rest of the batch:

res = run_simulations(one_sim, scenarios, on_error="record")
res[res.error.notna()][["sim", "N", "error"]]
res <- m$run_simulations(one_sim, scenarios, on_error = "record")
res[!is.na(res$error), c("sim", "N", "error")]
res = m.run_simulations(one_sim, scenarios; on_error = :record)
filter(r -> !ismissing(r.error), res)
JIT Caching: Define the model at module level for maximum speed

Each worker JIT-compiles the model on its first fit. When the model is built fresh inside one_sim (a closure, as in the example above), every simulation pays full compilation because the JIT cache key changes each time. Defining it at module level lets a worker reuse its compiled executable across all the simulations it handles β€” often the single largest speedup available.

To do this, ensure your global model function accepts m (the simulation’s specific bf instance) as an argument, and bind it with functools.partial when calling m.fit:

from functools import partial

# 1. Define the model globally, requiring 'm' as an argument
def linreg(m, x=None, y=None):
    a = m.dist.normal(0.0, 5.0, name="a")
    b = m.dist.normal(0.0, 5.0, name="b")
    s = m.dist.exponential(1.0, name="s")
    m.dist.normal(a + b * x, s, name="y", obs=y)

def one_sim(N, b_true, seed, **_):
    m = bf(rand_seed=seed, print_devices_found=False)
    
    # [ simulate x and y here ]
    
    # 2. Bind 'm' to the global function with functools.partial
    m.fit(partial(linreg, m),
          obs=dict(x=x, y=y),
          num_warmup=500, num_samples=500, num_chains=1,
          progress_bar=False)
    
    return {"b_hat": float(m.summary().loc["b", "mean"])}
# R: m$fit() converts the R model into a new Python function at every call,
# so the compiled model is not reused across fits; define the model once
# for readability only.
linreg <- function(x, y) {
    a <- bf.dist.normal(0.0, 5.0, name = "a")
    b <- bf.dist.normal(0.0, 5.0, name = "b")
    s <- bf.dist.exponential(1.0, name = "s")
    bf.dist.normal(a + b * x, s, name = "y", obs = y)
}
# Julia: m.fit() converts the @BF model into a new Python function at every
# call, so the compiled model is not reused across fits; define the model
# once for readability only.
@sim linreg_for(m) = begin
    @BF function linreg(x, y)
        a = m.dist.normal(0.0, 5.0, name = "a")
        b = m.dist.normal(0.0, 5.0, name = "b")
        s = m.dist.exponential(1.0, name = "s")
        m.dist.normal(a + b * x, s, name = "y", obs = y)
    end
    linreg
end