LaTeX#

Render a blayers / NumPyro model as the LaTeX of its generative form.

A blayers model is a plain NumPyro function that adds layers into a linear predictor and caps it with a link. model_to_latex() traces the model once and prints the priors and likelihood as a block of sampling statements — the “methods section” version of the model — so you never hand-transcribe it.

Example

from blayers import AdaptiveLayer, InterceptLayer, gaussian_link
from blayers.latex import model_to_latex

def model(x, y=None):
    mu = InterceptLayer()("intercept") + AdaptiveLayer()("mu", x)
    return gaussian_link(mu, y)

print(model_to_latex(model, x=x_train))   # raw LaTeX
model_to_latex(model, x=x_train)           # renders inline in Jupyter

What is and isn’t recovered#

A trace exposes every sample site, its distribution, and shape, so the priors and likelihood are exact. How the layers combine into the linear predictor (a + b·x) lives in plain Python, not the trace, so the output is the standard generative model (a list of \sim statements), not a reconstructed regression equation. The linear predictor is denoted \eta_i.

class blayers.latex.LatexStr[source]#

Bases: str

A str of LaTeX that also renders inline in Jupyter.

print(x) / str(x) give the raw LaTeX; in a notebook the object displays as typeset math via _repr_latex_.

blayers.latex.model_to_latex(model_fn, *, env='align', seed_value=0, **data)[source]#

Render a blayers / NumPyro model as the LaTeX of its generative form.

Traces model_fn once (with no observed y) and emits a block of sampling statements: every latent’s prior, then the likelihood. Hierarchy is recovered automatically — a coefficient whose scale is a sampled site prints \sim \mathrm{Normal}(0, \lambda_{\mathrm{name}}) rather than a number.

Parameters:
  • model_fn (Callable) – A blayers / NumPyro model. Pass inputs the same way you would to blayers.fit(), but without y (supplying it would condition the obs site; the likelihood is rendered from its distribution family regardless).

  • env ("align", "aligned", or "none") – LaTeX environment to wrap the lines in. "none" returns the bare \\-separated body.

  • seed_value (int) – PRNG seed for the trace (only affects sampled shapes, not the output).

  • **data – Model inputs (e.g. x) and constants, used to fix site shapes.

Returns:

The LaTeX source (a str subclass that also renders inline in Jupyter).

Return type:

LatexStr

Examples

>>> print(model_to_latex(model, x=x_train))
\begin{align}
...
\end{align}