Probabilistic modeling with Equinox and NumPyro: Bayesian neural networks, Gaussian processes, and composable GP building blocks.
pyrox bridges Equinox modules and NumPyro traces so one module class can host deterministic parameters, random sample sites, priors, guides, and mode switching — without duplicating inference logic. NumPyro owns inference; pyrox just makes modules visible to it.
Package layout¶
pyrox._core— the Equinox-to-NumPyro bridge.PyroxModule,PyroxParam,PyroxSample,Parameterized,pyrox_method.pyrox_gp— Gaussian process building blocks: kernels, sparse/variational GPs, non-Gaussian inference, pathwise samplers, multi-output GPs.pyrox_nn— Bayesian and uncertainty-aware NN layers: dense variants, spectral layers, SNGP, ensembles, the Bayesian Neural Field stack.pyrox_lgm— latent Gaussian models in precision form: GMRF components, PC priors,inla().pyrox_ssm— state-space models as NumPyro models, whose inference recurses in time: Markov (Kalman) GPs over SDE kernels, sparse and non-Gaussian Markov GPs, the normalizing Kalman filter.pyrox_gauss— Gaussian NumPyro distributions on gaussx operators: multivariate normals, linear-Gaussian state-space models,MarkovGaussian, andGMRFDistributionfor gaussx’s GMRF fields. It imports gaussx and NumPyro, notpyrox.pyrox_flows— the NumPyro bridges for gauss_flows’ normalizing flows:NumpyroBase(a NumPyro distribution as a flowjax base),flow_to_numpyro(a flow as a NumPyro distribution) andFlowGuide(an SVI guide built on a flow). It imports gauss_flows, flowjax and NumPyro, notpyrox.pyrox_xtreme— extreme value modelling, formerly xtremax: GEV, GPD, Gumbel, Fréchet and Weibull distributions as NumPyro likelihoods, temporal, spatial and spatiotemporal point processes, and xarray block maxima, thresholds and declustering. It imports NumPyro, notpyrox; see Extremes.
Installation¶
pyrox is not on PyPI yet, and the name pyrox on PyPI belongs to an unrelated project, so pip install pyrox installs the wrong package.
Install from GitHub with uv:
uv add "pyrox @ git+https://github.com/jejjohnson/pyrox.git#subdirectory=packages/pyrox"
uv add "pyrox-gp @ git+https://github.com/jejjohnson/pyrox.git#subdirectory=packages/pyrox-gp"pyrox-gp, pyrox-nn and pyrox-lgm also need a geonnax v0.0.11 override until kernellib pins that version; the README has the details.
Three modeling patterns¶
Pattern A — pure Equinox module injected into a NumPyro model¶
import equinox as eqx
import numpyro
import numpyro.distributions as dist
def model(x, y=None):
net = MLP(key=key) # any eqx.Module
W = numpyro.sample("W", prior)
net = eqx.tree_at(lambda m: m.W, net, W)
f = numpyro.deterministic("f", net(x))
numpyro.sample("obs", dist.Normal(f, 0.1), obs=y)Pattern B — PyroxModule owns its probabilistic semantics¶
import jax.numpy as jnp
import numpyro.distributions as dist
from pyrox._core import PyroxModule, pyrox_method
class BayesianLinear(PyroxModule):
pyrox_name = "BayesianLinear"
in_features: int
out_features: int
@pyrox_method
def __call__(self, x):
W = self.pyrox_sample(
"weight",
dist.Normal(0, 1).expand([self.in_features, self.out_features]).to_event(2),
)
b = self.pyrox_param("bias", jnp.zeros(self.out_features))
return x @ W + bPattern C — Parameterized for constrained params, priors, and guides¶
import jax.numpy as jnp
import numpyro.distributions as dist
from pyrox._core import Parameterized, pyrox_method
class RBFKernel(Parameterized):
pyrox_name = "RBFKernel"
def setup(self):
self.register_param(
"variance",
jnp.array(1.0),
constraint=dist.constraints.positive,
)
self.register_param(
"lengthscale",
jnp.array(1.0),
constraint=dist.constraints.positive,
)
self.set_prior("variance", dist.LogNormal(0.0, 1.0))
self.autoguide("variance", "normal")
@pyrox_method
def __call__(self, X1, X2):
v = self.get_param("variance")
ls = self.get_param("lengthscale")
sq = jnp.sum((X1[:, None] - X2[None, :]) ** 2 / ls**2, axis=-1)
return v * jnp.exp(-0.5 * sq)Switch kernel.set_mode("guide") to draw variational params instead of sampling the prior. "normal" autoguides respect the registered constraint via TransformedDistribution.
Links¶
One trend in the extremes, four priors: the same model with its trend from pyrox-gp, pyrox-ssm, pyrox-lgm and pyrox-nn
Map methane from one overpass: one GP from its evidence to a map, and the three patterns on the same model