BayesianRegressionModels.jl
BayesianRegressionModels.jl describes hierarchical regression models with brms-style formulas and executes them in Julia, Stan or Python/JAX.
git clone -b ns/devibe https://github.com/nsiccha/BayesianRegressionModels.jl
cd BayesianRegressionModels.jl
julia --project=docs/start docs/start/setup.jl
julia --project=docs/startGaussian regression
panes: brm|stanblocks|rkppl|stan|jax:3using BayesianRegressionModels
function intro_model()
(@brm begin
y ~ Normal(loc, err)
loc ~ 1 + x + (1 | subj)
sd(loc, subj) ~ LogNormal(-1, 0.35)
err ~ LogNormal(-1, 0.35)
end)((;
x=[-1.0, 1.0, -1.0, 1.0, -1.0, 1.0],
subj=[1, 1, 2, 2, 3, 3],
y=[-0.6, 0.9, -0.2, 1.4, -0.8, 0.5],
))
end
model = intro_model()SBBRMI with data keys = [:n_subj, :n_terms_loc__subj_subj, :subj_idx, :x, :y]
configured submodels:
ranef_correlated_draws_single_margin_1 = Base.merge(BayesianRegressionModels._sb_single_margin_ranef(BayesianRegressionModels.ranef_correlated_draws), quote
tau ~ (StanBlocks.builtin.restricted)(lognormal, -1, 0.35; n = n_terms, lower = 0.0)
end)
emitted @slic body:
begin
b_loc__subj_subj ~ ranef_correlated_draws_single_margin_1(; group_idx = subj_idx, n_groups = n_subj, n_terms = n_terms_loc__subj_subj)
err ~ lognormal(-1, 0.35)
X_loc = hcat(rep_vector(1.0, num_elements(x)), x)
pop_loc ~ popefs(; X = X_loc)
r_loc_loc__subj_subj = b_loc__subj_subj[subj_idx, 1]
loc = pop_loc + r_loc_loc__subj_subj
y ~ normal(loc, err)
end"""
Group-level (varying) effects of grouping `g`.
Non-centered, from the scale `tau[1]` (drawn from its prior) and standardized effects
`z[1:length(levels(g)), 1:1] ~ Normal(0, 1)`. Returns the
level-by-margin matrix `z .* tau[1]`, one row per level of `g` in order.
"""
brm_group_effects(g) = begin
tau[1:1] .~ LogNormal.(-1, 0.35)
z[1:length(levels(g)), 1:1] .~ Normal.(0, 1)
return z .* tau[1]
end
"""
Population-level effects: one coefficient per column of the design matrix `X`.
Draws `beta_pop[1:ncoef] ~ Normal(loc, scale)` and returns the linear predictor
`X * beta_pop`. `ncoef` is the number of columns of `X`. Each prior argument (`loc`, `scale`) is one value for all coefficients or a vector with one per coefficient.
"""
brm_population_effects(X, ncoef, loc, scale) = begin
beta_pop[1:ncoef] .~ Normal.(loc, scale)
return X * beta_pop
end
begin
subj_fit_levels = brm_fit_levels(subj_fit)
b_loc__subj_subj ~ brm_group_effects(subj_fit_levels)
subj_level = brm_level_indices(subj, subj_fit_levels)
ranef_loc_loc__subj_subj = b_loc__subj_subj[subj_level, 1]
X_loc = hcat(ones(length(subj)), x)
pop_loc ~ brm_population_effects(X_loc, 2, 0.0, 1.0)
loc = pop_loc .+ ranef_loc_loc__subj_subj
err ~ LogNormal(-1.0, 0.35)
y .~ Normal.(loc, err)
endfunctions {
matrix hcat(
vector x,
vector y
) {
int n = dims(x)[1];
if (dims(y)[1] != n) reject("hcat: dim mismatch — `y` dim 1 (= ", dims(y)[1], ") does not match `n` (= ", n, "), inferred from `x` dim 1. `n` sizes: `x` dim 1 (= ", dims(x)[1], "), `y` dim 1 (= ", dims(y)[1], ").");
return append_col(x, y);
}
vector normal_lpdfs(
vector obs,
vector loc,
real scale
) {
return jbroadcasted_normal_lpdfs(obs, loc, scale);
}
vector jbroadcasted_normal_lpdfs(
vector x1,
vector x2,
real x3
) {
int n = dims(x1)[1];
vector[n] rv;
for(i in 1:n) {
rv[i] = normal_lpdfs(broadcasted_getindex(x1, i), broadcasted_getindex(x2, i), x3);
}
return rv;
}
real normal_lpdfs(
real args1,
real args2,
real args3
) {
return normal_lpdf(args1 | args2, args3);
}
real broadcasted_getindex(vector x, int i) {
return x[i];
}
vector normal_vector_rng(
int anontok__1,
vector a,
real b
) {
int n = anontok__1;
if((n == 0)) {
vector[n] rv;
return rv;
} else {
return to_vector(normal_rng(a, b));
}
}
}
data {
int n_terms_loc__subj_subj;
int n_subj;
int x_n;
vector[x_n] x;
int subj_idx_n;
array[subj_idx_n] int subj_idx;
int y_n;
vector[y_n] y;
}
transformed data {
matrix[x_n, 2] X_loc = hcat(rep_vector(1.0, num_elements(x)), x);
int pop_loc_n_covariates = 2;
}
parameters {
vector<lower=0.0>[n_terms_loc__subj_subj] b_loc__subj_subj_tau;
vector[(n_terms_loc__subj_subj * n_subj)] b_loc__subj_subj_z_flat;
real<lower=0.0> err;
vector[pop_loc_n_covariates] pop_loc_beta_pop;
}
transformed parameters {
matrix[n_terms_loc__subj_subj, n_subj] b_loc__subj_subj_z = to_matrix(b_loc__subj_subj_z_flat, n_terms_loc__subj_subj, n_subj);
matrix[n_subj, n_terms_loc__subj_subj] b_loc__subj_subj = ((diag_matrix(b_loc__subj_subj_tau) * b_loc__subj_subj_z)');
vector[x_n] pop_loc = (X_loc * pop_loc_beta_pop);
vector[subj_idx_n] r_loc_loc__subj_subj = b_loc__subj_subj[subj_idx, 1];
vector[x_n] loc = (pop_loc + r_loc_loc__subj_subj);
}
model {
b_loc__subj_subj_tau ~ lognormal(-1, 0.35);
b_loc__subj_subj_z_flat ~ std_normal();
err ~ lognormal(-1, 0.35);
pop_loc_beta_pop ~ std_normal();
y ~ normal(loc, err);
}
generated quantities {
vector[y_n] y_likelihood = normal_lpdfs(y, loc, err);
vector[y_n] y_gen = normal_vector_rng(y_n, loc, err);
}using ReactiveKernels, ReactiveKernelsPPL
rk = RKBRMI(model)
source = jax_source(rk.model, rk_bound_plan(rk))
write("model.py", source)
inputs = rk_bound_data(rk, source) # exactly the inputs prepare_data readsimport jax
import model as m
prepared = m.prepare_data(**inputs)
lp = m.log_density(u, **prepared) # u: unconstrained, length m.dimension(prepared)
lp = jax.jit(m.log_density)(u, **prepared) # one compiled function for every dataset of these shapes"""Generated by ReactiveKernelsPPL.jax_source from a built @rkppl program; do not edit.
Data: subj, subj_fit, x, y.
Model documentation:
- `b_loc__subj_subj`: Group-level (varying) effects of grouping `g`.
Non-centered, from the scale `tau[1]` (drawn from its prior) and standardized effects
`z[1:length(levels(g)), 1:1] ~ Normal(0, 1)`. Returns the
level-by-margin matrix `z .* tau[1]`, one row per level of `g` in order.
- `pop_loc`: Population-level effects: one coefficient per column of the design matrix `X`.
Draws `beta_pop[1:ncoef] ~ Normal(loc, scale)` and returns the linear predictor
`X * beta_pop`. `ncoef` is the number of columns of `X`. Each prior argument (`loc`, `scale`) is one value for all coefficients or a vector with one per coefficient.
"""
import numpy as np
import scipy.special as ssp
from typing import Any
import jax
import jax.numpy as jnp
import jax.scipy.linalg
import jax.scipy.special as jsp
from jax.typing import ArrayLike
jax.config.update("jax_enable_x64", True)
DATA = {"subj": {"dtype": "int64", "ndim": 1, "ragged": False}, "subj_fit": {"dtype": "int64", "ndim": 1, "ragged": False}, "x": {"dtype": "float64", "ndim": 1, "ragged": False}, "y": {"dtype": "float64", "ndim": 1, "ragged": False}}
INPUTS = ("unconstrained", "ppl_last_b_loc_subj_subj_z", "ppl_axis1_b_loc_subj_subj_z", "ppl_first_pop_loc_beta_pop", "ppl_last_pop_loc_beta_pop", "X_loc", "subj_level", "y")
PREPARED = {"ppl_last_b_loc_subj_subj_z": {"dtype": "int64", "ndim": 0, "ragged": False}, "ppl_axis1_b_loc_subj_subj_z": {"dtype": "int64", "ndim": 0, "ragged": False}, "ppl_first_pop_loc_beta_pop": {"dtype": "int64", "ndim": 0, "ragged": False}, "ppl_last_pop_loc_beta_pop": {"dtype": "int64", "ndim": 0, "ragged": False}, "X_loc": {"dtype": "float64", "ndim": 2, "ragged": False}, "subj_level": {"dtype": "int64", "ndim": 1, "ragged": False}, "y": {"dtype": "float64", "ndim": 1, "ragged": False}}
UNTRANSLATED = {}
def _rk_order_key_np(x):
# Julia's isless order of an array as sortable keys: a float is an integer
# key, -0.0 before 0.0 and every NaN last, equal for values isequal treats
# as equal. Integer and string arrays are their own keys.
x = np.asarray(x)
if x.dtype.kind != "f":
return x
bits = np.where(np.isnan(x), np.nan, x).astype(np.float64).view(np.int64)
return np.where(bits < 0, bits ^ np.int64(0x7FFFFFFFFFFFFFFF), bits)
def _rk_unique_np(x):
# Julia's unique of a vector: the first value of each isequal class, in
# order.
x = np.asarray(x)
_, first = np.unique(_rk_order_key_np(x), return_index=True)
return x[np.sort(first)]
def _rk_sort_np(x):
# Julia's sort of a vector: ascending in isless order, equal values in
# their order.
x = np.asarray(x)
return x[np.argsort(_rk_order_key_np(x), kind="stable")]
def _brm_sorted_labels_np(source):
labels = _rk_unique_np(source)
sorted_ = _rk_sort_np(labels)
return sorted_
def _brm_fit_levels_np(source):
value = _brm_sorted_labels_np(source)
levels = value
return levels
def _rk_dict_key(x):
if isinstance(x, tuple):
return (1, tuple(map(_rk_dict_key, x)))
if isinstance(x, np.ndarray):
return (2, x.shape, tuple(map(_rk_dict_key, x.ravel(order="F"))))
if isinstance(x, (complex, np.complexfloating)):
if x.imag == 0 and not np.signbit(x.imag):
return _rk_dict_key(x.real)
return (3, _rk_dict_key(x.real), _rk_dict_key(x.imag))
if isinstance(x, (float, np.floating)):
if np.isnan(x):
return (4,)
if x == 0 and np.signbit(x):
return (5,)
return (0, x)
class _RKDict:
def __init__(self, pairs=()):
self.data = {}
for key, value in pairs:
self[key] = value
def __len__(self):
return len(self.data)
def __contains__(self, key):
return _rk_dict_key(key) in self.data
def __getitem__(self, key):
return self.data[_rk_dict_key(key)][1]
def __setitem__(self, key, value):
self.data[_rk_dict_key(key)] = (key, value)
def get(self, key, default):
item = self.data.get(_rk_dict_key(key))
return default if item is None else item[1]
def setdefault(self, key, default):
return self.data.setdefault(_rk_dict_key(key), (key, default))[1]
def pop(self, key, default):
item = self.data.pop(_rk_dict_key(key), None)
return default if item is None else item[1]
def items(self):
return self.data.values()
def keys(self):
return (item[0] for item in self.items())
def values(self):
return (item[1] for item in self.items())
def copy(self):
return _RKDict(self.items())
def _brm_level_indices_np(labels, source):
value = _brm_fit_levels_np(source)
levels = value
lookup = _RKDict()
for k in range(1, len(levels) + 1):
lookup[levels[k - 1]] = k
indices = np.zeros((len(labels),), dtype=np.int64)
for r in range(1, len(labels) + 1):
position = lookup.get(labels[r - 1], 0)
if position == 0:
raise ValueError("".join([str("RK backend: group label `"), str(labels[r - 1]), str("` is not one of the "), str(len(levels)), str(" fitted levels. A replay keeps the fitted levels of every grouping it does not resample; name the grouping in `resample_groups` to take the new data's own levels.")]))
indices[r - 1] = position
return indices
def _rk_levels_np(x):
arr = np.asarray(x)
values = sorted(set(v for v in arr.tolist() if v is not None))
return np.array(values, dtype=object if arr.dtype == object else arr.dtype)
class StaticInt(int):
"""An integer of the prepared data that sets a shape.
It is an `int` everywhere, and `jax.jit` treats it as static, so the
functions jit with the prepared data as arguments; a new value traces
them again.
"""
def __repr__(self):
return "StaticInt(%d)" % int(self)
jax.tree_util.register_pytree_node(StaticInt, lambda s: ((), int(s)), lambda v, _: StaticInt(v))
def brm_population_effects(X: ArrayLike, beta_pop: ArrayLike, *, outputs: tuple[str, ...]) -> dict:
"""Population-level effects: one coefficient per column of the design matrix `X`.
Draws `beta_pop[1:ncoef] ~ Normal(loc, scale)` and returns the linear predictor
`X * beta_pop`. `ncoef` is the number of columns of `X`. Each prior argument (`loc`, `scale`) is one value for all coefficients or a vector with one per coefficient.
"""
results = {}
if any(name in outputs for name in ("value",)):
value = X @ beta_pop
if any(name in outputs for name in ("ppl_prior_beta_pop", "ppl_pw_prior_beta_pop")):
def ppl_pw_prior_beta_pop_cell(beta_pop_2):
log_scale = np.log(1.0)
standardized = (beta_pop_2 - 0.0) / jnp.where((beta_pop_2 == 0.0 if 1.0 == 0 else False), 1.0, 1.0)
standard_logpdf = -0.5 * np.log(2 * np.pi) - 0.5 * standardized ** 2
logpdf = standard_logpdf - jnp.where((beta_pop_2 != 0.0 if 1.0 == 0 else False), 0.0, log_scale)
return logpdf
ppl_pw_prior_beta_pop = (jnp.zeros(beta_pop.shape[:1], dtype=jnp.float64) if 0 in beta_pop.shape[:1] else jax.vmap(ppl_pw_prior_beta_pop_cell, in_axes=(0,))(beta_pop))
if any(name in outputs for name in ("ppl_prior_beta_pop",)):
ppl_prior_beta_pop = jnp.sum(ppl_pw_prior_beta_pop)
if "ppl_prior_beta_pop" in outputs:
results["ppl_prior_beta_pop"] = ppl_prior_beta_pop
if "ppl_pw_prior_beta_pop" in outputs:
results["ppl_pw_prior_beta_pop"] = ppl_pw_prior_beta_pop
if "value" in outputs:
results["value"] = value
return results
def _log_of(x: ArrayLike, active: ArrayLike) -> jax.Array:
"""Compute `_rk_log_of` from its authored source."""
return jnp.asarray(x, dtype=jnp.float64)
def brm_group_effects_density(
ppl_last_z: ArrayLike,
tau: ArrayLike,
unconstrained: ArrayLike,
*,
outputs: tuple[str, ...],
) -> dict:
"""Group-level (varying) effects of grouping `g`.
Non-centered, from the scale `tau[1]` (drawn from its prior) and standardized effects
`z[1:length(levels(g)), 1:1] ~ Normal(0, 1)`. Returns the
level-by-margin matrix `z .* tau[1]`, one row per level of `g` in order.
"""
results = {}
if any(name in outputs for name in ("ppl_arrflat_z", "ppl_prior_z", "ppl_pw_prior_z")):
ppl_arrflat_z = unconstrained[2:ppl_last_z]
if any(name in outputs for name in ("ppl_prior_tau", "ppl_pw_prior_tau")):
def ppl_pw_prior_tau_cell(tau_2):
log_scale = np.log(0.35)
logpdf_log_x = jax.lax.cond(tau_2 <= 0, lambda: -jnp.inf, lambda: jnp.log(tau_2))
logpdf_jacobian = jnp.where(tau_2 <= 0, 0.0, logpdf_log_x)
logpdf_logpdf_standardized = (logpdf_log_x - -1.0) / jnp.where((logpdf_log_x == -1.0 if 0.35 == 0 else False), 1.0, 0.35)
logpdf_logpdf_standard_logpdf = -0.5 * np.log(2 * np.pi) - 0.5 * logpdf_logpdf_standardized ** 2
logpdf = logpdf_logpdf_standard_logpdf - jnp.where((logpdf_log_x != -1.0 if 0.35 == 0 else False), 0.0, log_scale)
logpdf_2 = logpdf - logpdf_jacobian
return logpdf_2
ppl_pw_prior_tau = (jnp.zeros(tau.shape[:1], dtype=jnp.float64) if 0 in tau.shape[:1] else jax.vmap(ppl_pw_prior_tau_cell, in_axes=(0,))(tau))
if any(name in outputs for name in ("ppl_prior_tau",)):
ppl_prior_tau = jnp.sum(ppl_pw_prior_tau)
if any(name in outputs for name in ("ppl_prior_z", "ppl_pw_prior_z")):
def ppl_pw_prior_z_cell(z):
log_scale = np.log(1.0)
standardized = (z - 0.0) / jnp.where((z == 0.0 if 1.0 == 0 else False), 1.0, 1.0)
standard_logpdf = -0.5 * np.log(2 * np.pi) - 0.5 * standardized ** 2
logpdf = standard_logpdf - jnp.where((z != 0.0 if 1.0 == 0 else False), 0.0, log_scale)
return logpdf
ppl_pw_prior_z = (jnp.zeros(ppl_arrflat_z.shape[:1], dtype=jnp.float64) if 0 in ppl_arrflat_z.shape[:1] else jax.vmap(ppl_pw_prior_z_cell, in_axes=(0,))(ppl_arrflat_z))
if any(name in outputs for name in ("ppl_prior_z",)):
ppl_prior_z = jnp.sum(ppl_pw_prior_z)
if "ppl_arrflat_z" in outputs:
results["ppl_arrflat_z"] = ppl_arrflat_z
if "ppl_prior_tau" in outputs:
results["ppl_prior_tau"] = ppl_prior_tau
if "ppl_prior_z" in outputs:
results["ppl_prior_z"] = ppl_prior_z
if "ppl_pw_prior_tau" in outputs:
results["ppl_pw_prior_tau"] = ppl_pw_prior_tau
if "ppl_pw_prior_z" in outputs:
results["ppl_pw_prior_z"] = ppl_pw_prior_z
return results
def brm_group_effects(tau: ArrayLike, z: ArrayLike, *, outputs: tuple[str, ...]) -> dict:
"""Group-level (varying) effects of grouping `g`.
Non-centered, from the scale `tau[1]` (drawn from its prior) and standardized effects
`z[1:length(levels(g)), 1:1] ~ Normal(0, 1)`. Returns the
level-by-margin matrix `z .* tau[1]`, one row per level of `g` in order.
"""
results = {}
if any(name in outputs for name in ("value",)):
value = z * tau[0]
if "value" in outputs:
results["value"] = value
return results
def prepare_data(subj: ArrayLike, subj_fit: ArrayLike, x: ArrayLike, y: ArrayLike) -> dict:
"""The functions' data inputs, computed from the raw data once per dataset.
Pass the data by the names the model reads them; a list of arrays is a ragged
column. Call the other functions with the result: `f(u, **prepared)`.
"""
subj = np.asarray(subj, dtype=np.int64)
subj_fit = np.asarray(subj_fit, dtype=np.int64)
x = np.asarray(x, dtype=np.float64)
y = np.asarray(y, dtype=np.float64)
X_loc = np.asarray(np.concatenate([np.ones((len(subj),), dtype=np.float64)[:, None], x[:, None]], axis=1), dtype=np.float64)
value = _brm_fit_levels_np(subj_fit)
subj_fit_levels = np.asarray(value, dtype=np.int64)
value_2 = _brm_level_indices_np(subj, subj_fit_levels)
subj_level = np.asarray(value_2, dtype=np.int64)
ppl_axis1_b_loc_subj_subj_z = len(_rk_levels_np(subj_fit_levels))
ppl_last_b_loc_subj_subj_z = 2 + ppl_axis1_b_loc_subj_subj_z
ppl_first_pop_loc_beta_pop = ppl_last_b_loc_subj_subj_z + 1
ppl_last_pop_loc_beta_pop = ppl_first_pop_loc_beta_pop + 1
return {"ppl_last_b_loc_subj_subj_z": StaticInt(ppl_last_b_loc_subj_subj_z), "ppl_axis1_b_loc_subj_subj_z": StaticInt(ppl_axis1_b_loc_subj_subj_z), "ppl_first_pop_loc_beta_pop": StaticInt(ppl_first_pop_loc_beta_pop), "ppl_last_pop_loc_beta_pop": StaticInt(ppl_last_pop_loc_beta_pop), "X_loc": np.asarray(X_loc, dtype=np.float64), "subj_level": np.asarray(subj_level, dtype=np.int64), "y": np.asarray(y, dtype=np.float64)}
def model(_entry: str, unconstrained: ArrayLike, **prepared: ArrayLike) -> Any:
"""Shared model computation. The generated entry wrappers select a static output."""
ppl_last_b_loc_subj_subj_z = prepared["ppl_last_b_loc_subj_subj_z"]
ppl_axis1_b_loc_subj_subj_z = prepared["ppl_axis1_b_loc_subj_subj_z"]
ppl_first_pop_loc_beta_pop = prepared["ppl_first_pop_loc_beta_pop"]
ppl_last_pop_loc_beta_pop = prepared["ppl_last_pop_loc_beta_pop"]
X_loc = prepared["X_loc"]
subj_level = prepared["subj_level"]
y = prepared["y"]
unconstrained = jnp.asarray(unconstrained)
X_loc = jnp.asarray(X_loc)
subj_level = jnp.asarray(subj_level)
y = jnp.asarray(y)
if _entry in ("log_density", "log_prior", "log_likelihood", "constrain"):
u_argument = unconstrained[0]
constrain_2 = jnp.exp(u_argument)
plate_argument = unconstrained[1:2]
def b_loc_subj_subj_tau_cell(ppl_cell_b_loc_subj_subj_tau):
constrain_3 = jnp.exp(ppl_cell_b_loc_subj_subj_tau)
return constrain_3
b_loc_subj_subj_tau = (jnp.zeros(plate_argument.shape[:1], dtype=jnp.float64) if 0 in plate_argument.shape[:1] else jax.vmap(b_loc_subj_subj_tau_cell, in_axes=(0,))(plate_argument))
if _entry in ("log_density", "log_jacobian"):
plate_argument_2 = unconstrained[1:2]
def ppl_ljcells_b_loc_subj_subj_tau_cell(ppl_cell_b_loc_subj_subj_tau):
return ppl_cell_b_loc_subj_subj_tau
ppl_ljcells_b_loc_subj_subj_tau = (jnp.zeros(plate_argument_2.shape[:1], dtype=jnp.float64) if 0 in plate_argument_2.shape[:1] else jax.vmap(ppl_ljcells_b_loc_subj_subj_tau_cell, in_axes=(0,))(plate_argument_2))
if _entry in ("log_density", "log_prior", "log_likelihood", "constrain"):
pop_loc_beta_pop = unconstrained[ppl_first_pop_loc_beta_pop - 1:ppl_last_pop_loc_beta_pop]
if _entry in ("log_density", "log_prior", "log_likelihood"):
pop_loc = brm_population_effects(
X=(X_loc if _entry in ("log_density", "log_likelihood") else None),
beta_pop=(pop_loc_beta_pop if _entry in ("log_density", "log_prior", "log_likelihood") else None),
outputs=tuple(
name for name, needed in (
("ppl_prior_beta_pop", ("log_density", "log_prior")),
("ppl_pw_prior_beta_pop", ("log_density", "log_prior")),
("value", ("log_density", "log_likelihood")),
)
if _entry in needed
),
)
if _entry in ("log_density", "log_prior"):
ppl_prior_err_log_scale = np.log(0.35)
def else_arm():
log_of = _log_of(u_argument, jnp.logical_not(constrain_2 <= 0))
return log_of
ppl_prior_err_logpdf_log_x = jax.lax.cond(constrain_2 <= 0, lambda: -jnp.inf, else_arm)
ppl_prior_err_logpdf_jacobian = jnp.where(constrain_2 <= 0, 0.0, ppl_prior_err_logpdf_log_x)
ppl_prior_err_logpdf_logpdf_standardized = (ppl_prior_err_logpdf_log_x - -1.0) / jnp.where((ppl_prior_err_logpdf_log_x == -1.0 if 0.35 == 0 else False), 1.0, 0.35)
ppl_prior_err_logpdf_logpdf_standard_logpdf = -0.5 * np.log(2 * np.pi) - 0.5 * ppl_prior_err_logpdf_logpdf_standardized ** 2
ppl_prior_err_logpdf = ppl_prior_err_logpdf_logpdf_standard_logpdf - jnp.where((ppl_prior_err_logpdf_log_x != -1.0 if 0.35 == 0 else False), 0.0, ppl_prior_err_log_scale)
logpdf = ppl_prior_err_logpdf - ppl_prior_err_logpdf_jacobian
if _entry in ("log_density", "log_jacobian"):
u_argument_2 = unconstrained[0]
plate_fold = jnp.sum(ppl_ljcells_b_loc_subj_subj_tau)
log_jacobian_2 = u_argument_2 + plate_fold
if _entry in ("log_density", "log_prior", "log_likelihood", "constrain"):
b_loc_subj_subj = brm_group_effects_density(
ppl_last_z=(ppl_last_b_loc_subj_subj_z if _entry in ("log_density", "log_prior", "log_likelihood", "constrain") else None),
tau=(b_loc_subj_subj_tau if _entry in ("log_density", "log_prior") else None),
unconstrained=(unconstrained if _entry in ("log_density", "log_prior", "log_likelihood", "constrain") else None),
outputs=tuple(
name for name, needed in (
("ppl_arrflat_z", ("log_density", "log_prior", "log_likelihood", "constrain")),
("ppl_prior_tau", ("log_density", "log_prior")),
("ppl_prior_z", ("log_density", "log_prior")),
("ppl_pw_prior_tau", ("log_density", "log_prior")),
("ppl_pw_prior_z", ("log_density", "log_prior")),
)
if _entry in needed
),
)
if _entry in ("log_density", "log_likelihood", "constrain"):
b_loc_subj_subj_z = jnp.reshape(b_loc_subj_subj["ppl_arrflat_z"], (ppl_axis1_b_loc_subj_subj_z, 1), order="F")
if _entry in ("log_density", "log_prior"):
prior = logpdf + b_loc_subj_subj["ppl_prior_tau"] + b_loc_subj_subj["ppl_prior_z"] + pop_loc["ppl_prior_beta_pop"]
if _entry in ("log_density", "log_likelihood"):
b_loc_subj_subj_2 = brm_group_effects(
tau=(b_loc_subj_subj_tau if _entry in ("log_density", "log_likelihood") else None),
z=(b_loc_subj_subj_z if _entry in ("log_density", "log_likelihood") else None),
outputs=tuple(
name for name, needed in (
("value", ("log_density", "log_likelihood")),
)
if _entry in needed
),
)
ppl_column_b_loc_subj_subj_1 = b_loc_subj_subj_2["value"][:, 0]
plate_argument_3 = ppl_column_b_loc_subj_subj_1[subj_level - 1]
def ppl_offset_loc_cell(pop_loc_2, value):
plate_value = pop_loc_2 + value
return plate_value
ppl_offset_loc = (jnp.zeros(pop_loc["value"].shape[:1], dtype=jnp.float64) if 0 in pop_loc["value"].shape[:1] and plate_argument_3.shape[:1] == pop_loc["value"].shape[:1] else jax.vmap(ppl_offset_loc_cell, in_axes=(0, 0))(pop_loc["value"], plate_argument_3))
def ppl_pw_y_resp_cell(y_2, loc, err):
def then_arm():
log_scale = jnp.log(err)
standardized = (y_2 - loc) / jnp.where(jnp.logical_and(err == 0, y_2 == loc), 1.0, err)
standard_logpdf = -0.5 * np.log(2 * np.pi) - 0.5 * standardized ** 2
return standard_logpdf - jnp.where(jnp.logical_and(err == 0, y_2 != loc), 0.0, log_scale)
ppl_density = jax.lax.cond(err > 0, then_arm, lambda: -jnp.inf)
return ppl_density
ppl_pw_y_resp = (jnp.zeros(y.shape[:1], dtype=jnp.float64) if 0 in y.shape[:1] and ppl_offset_loc.shape[:1] == y.shape[:1] else jax.vmap(ppl_pw_y_resp_cell, in_axes=(0, 0, None))(y, ppl_offset_loc, constrain_2))
ppl_lik_y_resp = jnp.sum(ppl_pw_y_resp)
if _entry in ("log_density",):
posterior = prior + ppl_lik_y_resp + log_jacobian_2
if not (unconstrained.size == ppl_last_pop_loc_beta_pop):
raise ValueError("[layout] packed length ≠ layout total: a packed point holds the coordinates of the binding whose layout packed it")
if _entry == "log_density":
return jnp.asarray(posterior, dtype=jnp.float64)
if _entry == "log_prior":
return jnp.asarray(prior, dtype=jnp.float64)
if _entry == "log_likelihood":
return jnp.asarray(ppl_lik_y_resp, dtype=jnp.float64)
if _entry == "log_jacobian":
return jnp.asarray(log_jacobian_2, dtype=jnp.float64)
if _entry == "constrain":
return {"err": jnp.asarray(constrain_2, dtype=jnp.float64), "b_loc__subj_subj": {"tau": jnp.asarray(b_loc_subj_subj_tau), "z": jnp.asarray(b_loc_subj_subj_z)}, "pop_loc": {"beta_pop": jnp.asarray(pop_loc_beta_pop)}}
def log_density(unconstrained: ArrayLike, **prepared: ArrayLike) -> jax.Array:
"""The log density a sampler targets at `unconstrained`, the packed unconstrained parameters (length `dimension(prepared)`), for the data `prepare_data` returned: `f(u, **prepared)`.
The sum of `log_prior`, `log_likelihood` and `log_jacobian`.
"""
return model("log_density", unconstrained, **prepared)
def log_prior(unconstrained: ArrayLike, **prepared: ArrayLike) -> jax.Array:
"""The log prior density of the constrained parameters at `unconstrained`, the packed unconstrained parameters (length `dimension(prepared)`), for the data `prepare_data` returned: `f(u, **prepared)`."""
return model("log_prior", unconstrained, **prepared)
def log_likelihood(unconstrained: ArrayLike, **prepared: ArrayLike) -> jax.Array:
"""The log likelihood of the observations at `unconstrained`, the packed unconstrained parameters (length `dimension(prepared)`), for the data `prepare_data` returned: `f(u, **prepared)`."""
return model("log_likelihood", unconstrained, **prepared)
def log_jacobian(unconstrained: ArrayLike, **prepared: ArrayLike) -> jax.Array:
"""The log Jacobian of the constraining transforms at `unconstrained`, the packed unconstrained parameters (length `dimension(prepared)`), for the data `prepare_data` returned: `f(u, **prepared)`."""
return model("log_jacobian", unconstrained, **prepared)
def constrain(unconstrained: ArrayLike, **prepared: ArrayLike) -> dict:
"""The constrained value of each parameter, and of each requested value, at `unconstrained`, the packed unconstrained parameters (length `dimension(prepared)`), for the data `prepare_data` returned: `f(u, **prepared)`. Returns a dict, a submodel's values nested under its name:
- `err`
- `b_loc__subj_subj.tau`
- `b_loc__subj_subj.z`
- `pop_loc.beta_pop`
"""
return model("constrain", unconstrained, **prepared)
def dimension(prepared: dict) -> int:
"""The length of `unconstrained` for the data `prepared` (what `prepare_data` returned)."""
return int(prepared["ppl_last_pop_loc_beta_pop"])
def parameters(prepared: dict) -> dict:
"""Each parameter's 0-based `start`/`stop` in `unconstrained`, its transform and dimensions,
for the data `prepared` (what `prepare_data` returned)."""
return {"err": {"start": 0, "stop": 1, "transform": "exp", "dims": ()}, "b_loc__subj_subj.tau": {"start": 1, "stop": 2, "transform": "exp", "dims": (1,)}, "b_loc__subj_subj.z": {"start": 2, "stop": int(prepared["ppl_last_b_loc_subj_subj_z"]), "transform": "identity", "dims": (int(prepared["ppl_axis1_b_loc_subj_subj_z"]), 1)}, "pop_loc.beta_pop": {"start": int(prepared["ppl_first_pop_loc_beta_pop"]) - 1, "stop": int(prepared["ppl_last_pop_loc_beta_pop"]), "transform": "identity", "dims": (2,)}}6 observations in 3 subjects; response: yUse Julia 1.10 for this example. The six synthetic observations have a population slope for x and subject-specific intercepts. The positive scale priors are explicit; the population intercept and slope default to standard normals. The tabs show the same declaration as a StanBlocks model, an RKPPL model, Stan source and a JAX module. Compare opens with BRM, StanBlocks and RKPPL side by side. Python snippets need the separate environment described in the JAX example; the page build executes the Julia model and export steps.
The fit uses four WarmupHMC chains, retaining results when three complete. These estimates come from the cached run; the build renders the model and its results without sampling again.
| Quantity | Median | 2.5% | 97.5% | R̂ | Bulk ESS |
|---|---|---|---|---|---|
| Intercept | 0.1925 | -0.3124 | 0.7111 | 1.0 | 3097 |
| x | 0.7277 | 0.4506 | 0.9822 | 1.001 | 2660 |
| err | 0.2946 | 0.1558 | 0.5578 | 1.0 | 1666 |
{"spec":"{\"$schema\":\"https://vega.github.io/schema/vega-lite/v5.json\",\"_aovSummary\":{\"bands\":[[\"q025\",\"q975\",\"q025/q975\"],[\"q25\",\"q75\",\"q25/q75\"]],\"point_col\":\"q50\",\"value_label\":\"q50\"},\"config\":{\"axis\":{\"labelLimit\":0},\"header\":{\"labelLimit\":0},\"legend\":{\"labelLimit\":0}},\"height\":210,\"layer\":[{\"data\":{\"values\":[{\"observation\":\"1\",\"observed\":-0.6,\"q025\":-1.445,\"q25\":-0.7967,\"q50\":-0.5383,\"q75\":-0.2773,\"q975\":0.3312,\"response\":\"y\",\"source_subject\":1.0,\"time\":-1.0},{\"observation\":\"2\",\"observed\":0.9,\"q025\":0.05553,\"q25\":0.6029,\"q50\":0.8806,\"q75\":1.148,\"q975\":1.774,\"response\":\"y\",\"source_subject\":1.0,\"time\":1.0},{\"observation\":\"3\",\"observed\":-0.2,\"q025\":-1.197,\"q25\":-0.4693,\"q50\":-0.2073,\"q75\":0.04217,\"q975\":0.5931,\"response\":\"y\",\"source_subject\":2.0,\"time\":-1.0},{\"observation\":\"4\",\"observed\":1.4,\"q025\":0.3187,\"q25\":0.9611,\"q50\":1.197,\"q75\":1.405,\"q975\":1.93,\"response\":\"y\",\"source_subject\":2.0,\"time\":1.0},{\"observation\":\"5\",\"observed\":-0.8,\"q025\":-1.503,\"q25\":-1.046,\"q50\":-0.7995,\"q75\":-0.5492,\"q975\":0.1083,\"response\":\"y\",\"source_subject\":3.0,\"time\":-1.0},{\"observation\":\"6\",\"observed\":0.5,\"q025\":-0.1073,\"q25\":0.4311,\"q50\":0.6733,\"q75\":0.9255,\"q975\":1.495,\"response\":\"y\",\"source_subject\":3.0,\"time\":1.0}]},\"encoding\":{\"tooltip\":[{\"field\":\"q50\",\"title\":\"Response (estimate)\",\"type\":\"quantitative\"},{\"field\":\"observation\",\"type\":\"nominal\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"q025\",\"scale\":{\"zero\":false},\"title\":\"Response\",\"type\":\"quantitative\"},\"x2\":{\"field\":\"q975\"},\"y\":{\"axis\":{\"title\":null},\"field\":\"observation\",\"title\":\"Observation\",\"type\":\"nominal\"}},\"mark\":{\"color\":\"#2a78d6\",\"strokeWidth\":1.5,\"type\":\"rule\"},\"params\":[{\"bind\":\"scales\",\"name\":\"grid\",\"select\":{\"encodings\":[\"x\"],\"type\":\"interval\"}}]},{\"data\":{\"values\":[{\"observation\":\"1\",\"observed\":-0.6,\"q025\":-1.445,\"q25\":-0.7967,\"q50\":-0.5383,\"q75\":-0.2773,\"q975\":0.3312,\"response\":\"y\",\"source_subject\":1.0,\"time\":-1.0},{\"observation\":\"2\",\"observed\":0.9,\"q025\":0.05553,\"q25\":0.6029,\"q50\":0.8806,\"q75\":1.148,\"q975\":1.774,\"response\":\"y\",\"source_subject\":1.0,\"time\":1.0},{\"observation\":\"3\",\"observed\":-0.2,\"q025\":-1.197,\"q25\":-0.4693,\"q50\":-0.2073,\"q75\":0.04217,\"q975\":0.5931,\"response\":\"y\",\"source_subject\":2.0,\"time\":-1.0},{\"observation\":\"4\",\"observed\":1.4,\"q025\":0.3187,\"q25\":0.9611,\"q50\":1.197,\"q75\":1.405,\"q975\":1.93,\"response\":\"y\",\"source_subject\":2.0,\"time\":1.0},{\"observation\":\"5\",\"observed\":-0.8,\"q025\":-1.503,\"q25\":-1.046,\"q50\":-0.7995,\"q75\":-0.5492,\"q975\":0.1083,\"response\":\"y\",\"source_subject\":3.0,\"time\":-1.0},{\"observation\":\"6\",\"observed\":0.5,\"q025\":-0.1073,\"q25\":0.4311,\"q50\":0.6733,\"q75\":0.9255,\"q975\":1.495,\"response\":\"y\",\"source_subject\":3.0,\"time\":1.0}]},\"encoding\":{\"tooltip\":[{\"field\":\"q50\",\"title\":\"Response (estimate)\",\"type\":\"quantitative\"},{\"field\":\"observation\",\"type\":\"nominal\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"q25\",\"scale\":{\"zero\":false},\"title\":\"Response\",\"type\":\"quantitative\"},\"x2\":{\"field\":\"q75\"},\"y\":{\"axis\":{\"title\":null},\"field\":\"observation\",\"title\":\"Observation\",\"type\":\"nominal\"}},\"mark\":{\"color\":\"#2a78d6\",\"strokeWidth\":8.0,\"type\":\"rule\"}},{\"data\":{\"values\":[{\"observation\":\"1\",\"observed\":-0.6,\"q025\":-1.445,\"q25\":-0.7967,\"q50\":-0.5383,\"q75\":-0.2773,\"q975\":0.3312,\"response\":\"y\",\"source_subject\":1.0,\"time\":-1.0},{\"observation\":\"2\",\"observed\":0.9,\"q025\":0.05553,\"q25\":0.6029,\"q50\":0.8806,\"q75\":1.148,\"q975\":1.774,\"response\":\"y\",\"source_subject\":1.0,\"time\":1.0},{\"observation\":\"3\",\"observed\":-0.2,\"q025\":-1.197,\"q25\":-0.4693,\"q50\":-0.2073,\"q75\":0.04217,\"q975\":0.5931,\"response\":\"y\",\"source_subject\":2.0,\"time\":-1.0},{\"observation\":\"4\",\"observed\":1.4,\"q025\":0.3187,\"q25\":0.9611,\"q50\":1.197,\"q75\":1.405,\"q975\":1.93,\"response\":\"y\",\"source_subject\":2.0,\"time\":1.0},{\"observation\":\"5\",\"observed\":-0.8,\"q025\":-1.503,\"q25\":-1.046,\"q50\":-0.7995,\"q75\":-0.5492,\"q975\":0.1083,\"response\":\"y\",\"source_subject\":3.0,\"time\":-1.0},{\"observation\":\"6\",\"observed\":0.5,\"q025\":-0.1073,\"q25\":0.4311,\"q50\":0.6733,\"q75\":0.9255,\"q975\":1.495,\"response\":\"y\",\"source_subject\":3.0,\"time\":1.0}]},\"encoding\":{\"tooltip\":[{\"field\":\"q50\",\"title\":\"Response (estimate)\",\"type\":\"quantitative\"},{\"field\":\"observation\",\"type\":\"nominal\"},{\"field\":\"q025\",\"title\":\"q025\",\"type\":\"quantitative\"},{\"field\":\"q975\",\"title\":\"q975\",\"type\":\"quantitative\"}],\"x\":{\"field\":\"q50\",\"scale\":{\"zero\":false},\"type\":\"quantitative\"},\"y\":{\"field\":\"observation\",\"type\":\"nominal\"}},\"mark\":{\"color\":\"white\",\"filled\":true,\"size\":80,\"stroke\":\"#333\",\"strokeWidth\":1.5,\"type\":\"point\"}},{\"data\":{\"values\":[{\"observation\":\"1\",\"observed\":-0.6,\"q025\":-1.445,\"q25\":-0.7967,\"q50\":-0.5383,\"q75\":-0.2773,\"q975\":0.3312,\"response\":\"y\",\"source_subject\":1.0,\"time\":-1.0},{\"observation\":\"2\",\"observed\":0.9,\"q025\":0.05553,\"q25\":0.6029,\"q50\":0.8806,\"q75\":1.148,\"q975\":1.774,\"response\":\"y\",\"source_subject\":1.0,\"time\":1.0},{\"observation\":\"3\",\"observed\":-0.2,\"q025\":-1.197,\"q25\":-0.4693,\"q50\":-0.2073,\"q75\":0.04217,\"q975\":0.5931,\"response\":\"y\",\"source_subject\":2.0,\"time\":-1.0},{\"observation\":\"4\",\"observed\":1.4,\"q025\":0.3187,\"q25\":0.9611,\"q50\":1.197,\"q75\":1.405,\"q975\":1.93,\"response\":\"y\",\"source_subject\":2.0,\"time\":1.0},{\"observation\":\"5\",\"observed\":-0.8,\"q025\":-1.503,\"q25\":-1.046,\"q50\":-0.7995,\"q75\":-0.5492,\"q975\":0.1083,\"response\":\"y\",\"source_subject\":3.0,\"time\":-1.0},{\"observation\":\"6\",\"observed\":0.5,\"q025\":-0.1073,\"q25\":0.4311,\"q50\":0.6733,\"q75\":0.9255,\"q975\":1.495,\"response\":\"y\",\"source_subject\":3.0,\"time\":1.0}]},\"encoding\":{\"tooltip\":[{\"field\":\"observed\",\"type\":\"quantitative\"},{\"field\":\"observation\",\"type\":\"nominal\"}],\"x\":{\"field\":\"observed\",\"scale\":{\"zero\":false},\"title\":\"Response\",\"type\":\"quantitative\"},\"y\":{\"field\":\"observation\",\"title\":\"Observation\",\"type\":\"nominal\"}},\"mark\":{\"color\":\"#eb6834\",\"filled\":true,\"size\":40.5,\"type\":\"point\"}}],\"width\":500,\"description\":\"Observed responses (orange) and posterior predictive medians with central 50% and 95% intervals (blue). Six observations leave substantial predictive uncertainty.\"}","caption":"Observed responses (orange) and posterior predictive medians with central 50% and 95% intervals (blue). Six observations leave substantial predictive uncertainty.","alt":"Observed responses (orange) and posterior predictive medians with central 50% and 95% intervals (blue). Six observations leave substantial predictive uncertainty."}| Completed chains | Omitted chains | Retained draws | Divergences |
|---|---|---|---|
| 3 | 4 | 3000 | 0 |
Documentation
| Part | Purpose |
|---|---|
| Start | Install the source environment and inspect a fitted model. |
| Guides | Model syntax, priors, response families, fields, backends and centering. |
| Examples | Worked models with fits, diagnostics and figures. |
| Reference | Public functions and the term catalogue. |
Reproduce
The reproduction notes explain the fit key, priors and summary files. Run sampling deliberately from the checkout; selecting chains by completion time can affect inference.
julia --startup-file=no --threads=1 --project=test research/start/fit.jl| Field | Value |
|---|---|
| commit | 2148fd8b76f2ef10001081303720721423bc2f1f |
| uncommitted_changes | yes |
| started | 2026-11-11 01:59 local time |
| host | a Linux fleet host, 1 Julia threads |
| julia | 1.10.12 |
| BayesianRegressionModels | 0.2.273 |
| sampler | WarmupHMC completion_warmup_mcmc, adaptive_centering_problem over the BridgeStan problem, AutoEnzyme |
| chains | 4 per fit, quorum 3, 1000 draws each |
| key | 16a6a41e619d9eebcd7495465ec688c37e7ac391fe521d59d854edd71fc0d018 |
| harness_version | 27a8d666a2db2bc3569e19981e3ffa01f628d7704f70cd65e4a597f3fb20d0fb |
| settings | |
| seed | 20261111 |
| StanBlocks | 0.2.31 |
| WarmupHMC | 0.2.11 |
| Enzyme | 0.13.218 |
| seconds | 65.576799819 |
| divergent | 0 |
| omitted_chains | [4] |
| dimension | 7 |
| completed | 2026-11-11T02:01:22.374 |
| status | completed |