NumPyro¶
A NumPyro model is a Python function, traced by JAX, whose calls numpyro.sample register named sites,
and whose handlers record those sites in an execution trace (Phan et al., 2019). To compare densities, the
test probe gives values for the latent sites and the observations and sums the log densities the trace
records (Transpilation correctness).
How NumPyro evaluates a log density¶
NumPyro evaluates a model by running it under effect handlers, which record each site's distribution and
value in a trace (Phan et al., 2019), following the lightweight implementation of probabilistic programs by
execution traces (Wingate et al., 2011). The probe is
numpyro.infer.util.log_density, which returns
the sum of the log densities of the trace's sites, the
observations included.
Change of variables¶
The renderer requests no reparameterization, so the emitted model is in constrained coordinates:
\(\Psi_{\mathsf{NumPyro}} = \mathrm{id}\) and the log Jacobian is \(0\). NumPyro's inference may transform
coordinates, through
TransformReparam or an automatic
guide such as AutoNormal, and a probe in those
coordinates would need their Jacobian. The comparison here uses the model's density at constrained values.
Family parameterizations¶
NumPyro's distributions follow the interface of
torch.distributions in JAX, and the map is the identity for every QVR family with a NumPyro class:
Normal(μ, σ)toNormal(loc=μ, scale=σ);Dirichlet(α)toDirichlet(concentration=α), withjnp.full((K,), α)for a scalar α;Categorical(probs=p)toCategorical(probs=p);Bernoulli(p)toBernoulli(probs=p), andBernoulli(logits=l)toBernoulli(logits=l);LogitNormaltoLogitNormal(loc=μ, scale=σ).
In each case \(\pi_{F, \mathsf{NumPyro}}\) is the identity and \(c_{F, \mathsf{NumPyro}} = 0\).
The emitted OneHotCategorical helper reuses NumPyro's categorical law for
probability/logit conversion and sampling. Its score sums the selected normalized
log probabilities over the event axis. Raw logits are normalized with
logsumexp; treating them as already normalized would change the measure by a
parameter-dependent factor. Zero entries contribute zero even when the
corresponding category has log probability negative infinity.
Emitted constructs¶
sample and observe. numpyro.sample("x", <dist>) for a latent variable and
numpyro.sample("y", <dist>, obs=y_data) for an observation. Each adds its site's log density to the
value of log_density.
Plates. Nested with numpyro.plate(name, B): contexts
(plate), which state that the
\(B\) draws are conditionally independent, so their density is the product
(plates).
marginalize. The renderer computes, for each value of the latent variable, the log prior of the value
and the log likelihood of the scope, stacks them, reduces them with logsumexp, and adds the result with
numpyro.factor. A grouped block sums the rows of each group, by via, before the reduction. No sample site
remains for the variable summed out.
score, let, return. numpyro.factor("name", expr) for a score, an assignment for a let, and
Python's return.
Evidence¶
- Level 1. Structural tests check the emitted
modelfunction and the pipelineLower,NumPyroRenderer, andEmitPretty(python). - Level 2. Python's
astparser accepts the modules of the syntax matrix. - Level 3. On selected fixtures,
numpyro.infer.util.log_densityis compared with the QVR density on the shared grid and tolerance (evidence).
Cited sources¶
Du Phan, Neeraj Pradhan, and Martin Jankowiak. Composable Effects for Flexible and Accelerated Probabilistic Programming in NumPyro. arXiv preprint arXiv:1912.11554, 2019. Presented at the NeurIPS 2019 workshop on Program Transformations for Machine Learning. URL: https://arxiv.org/abs/1912.11554, doi:10.48550/arXiv.1912.11554.
David Wingate, Andreas Stuhlmueller, and Noah Goodman. Lightweight Implementations of Probabilistic Programming Languages Via Transformational Compilation. In Proceedings of the Fourteenth International Conference on Artificial Intelligence and Statistics, volume 15 of Proceedings of Machine Learning Research, 770–778. PMLR, 2011. URL: https://proceedings.mlr.press/v15/wingate11a.html.