Evaluate a Model’s PDF and Log-PDF¶
You have a Model built from an HS3 workspace and need probability
density (or log-density) values at one or more parameter points.
>>> import pyhs3
>>> import numpy as np
>>> workspace_json = {
... "metadata": {"hs3_version": "0.2"},
... "distributions": [
... {
... "name": "gauss",
... "type": "gaussian_dist",
... "x": "x",
... "mean": "mu",
... "sigma": "sigma",
... }
... ],
... }
>>> ws = pyhs3.Workspace(**workspace_json)
>>> model = ws.model(0, progress=False)
Choose the type-safe or convenience API¶
pyhs3 gives you two pairs of evaluation methods:
pdf/logpdfrequire every parameter value to be a numpy array. Use these in production code and performance-critical loops — no conversion happens on each call.pdf_unsafe/logpdf_unsafeaccept plain Python floats and lists, converting them to numpy arrays for you. Use these in a notebook, a test, or anywhere the conversion overhead doesn’t matter.
Evaluate with pre-converted arrays:
>>> pdf_value = model.pdf("gauss", x=np.array(0.0), mu=np.array(0.0), sigma=np.array(1.0))
>>> pdf_value
array(0.398942...)
Or with the convenience API, passing floats directly:
>>> model.pdf_unsafe("gauss", x=0.0, mu=0.0, sigma=1.0)
array(0.398942...)
Get the log-density instead¶
logpdf/logpdf_unsafe mirror pdf/pdf_unsafe exactly, but
return the natural logarithm of the density, evaluated directly in log
space rather than by taking log(pdf(...)) yourself:
>>> model.logpdf(
... "gauss", x=np.array(0.0), mu=np.array(0.0), sigma=np.array(1.0)
... )
array(-0.918938...)
See How pyhs3 Builds a Model for why logpdf stays finite in cases where
np.log(model.pdf(...)) would underflow to -inf.
Evaluate at many points at once¶
By default, a parameter compiles as a scalar tensor and only accepts a single value per call. To evaluate at many points in one call, mark the parameter as a vector before building the model:
>>> import pytensor.tensor as pt
>>> workspace_json_with_params = dict(
... workspace_json,
... parameter_points=[
... {
... "name": "defaults",
... "parameters": [
... {"name": "x", "value": 0.0},
... {"name": "mu", "value": 0.0},
... {"name": "sigma", "value": 1.0},
... ],
... }
... ],
... )
>>> ws_vec = pyhs3.Workspace(**workspace_json_with_params)
>>> parameterset = ws_vec.parameter_points[0]
>>> parameterset["x"].kind = pt.vector
>>> import warnings
>>> with warnings.catch_warnings(record=True) as w:
... warnings.simplefilter("always")
... vec_model = ws_vec.model(0, parameter_set=parameterset)
... print(w[0].message)
...
Parameter 'x' has kind override vector (default would be scalar)
>>> x_values = np.linspace(-3.0, 3.0, 5)
>>> result = vec_model.pdf("gauss", x=x_values, mu=np.array(0.0), sigma=np.array(1.0))
>>> result.shape
(1, 5)
x_values has shape (5,), one value per evaluation point. The result
carries a leading singleton axis, (1, 5), because overriding kind
this way marks x as a plain vector parameter rather than an observable —
pyhs3 broadcasts a non-observable vector override differently from an
observable’s data axis. Observables carried by a likelihood’s data are
vectorized for you automatically, without needing this override at all; see
Broadcasting with Vector Parameters for the full rules on which parameters can be vectors,
which shape convention applies to each, and how their shapes combine.
Evaluate a joint likelihood for a fit¶
pdf/logpdf evaluate one distribution at a time. Fitting needs the
joint log-likelihood across every channel and constraint term at once — for
that, build the model from an Analysis or Likelihood instead of a
plain domain index, which exposes model.log_prob, model.data, and
model.free_params. Jaxifying the result requires the jax optional
extra (pip install pyhs3[jax]):
>>> analysis_workspace = {
... "metadata": {"hs3_version": "0.2"},
... "distributions": [
... {
... "name": "gauss",
... "type": "gaussian_dist",
... "x": "x",
... "mean": "mu",
... "sigma": "sigma",
... }
... ],
... "parameter_points": [
... {
... "name": "nominal",
... "parameters": [
... {"name": "mu", "value": 0.0},
... {"name": "sigma", "value": 1.0},
... ],
... }
... ],
... "domains": [
... {
... "name": "fit_region",
... "type": "product_domain",
... "axes": [
... {"name": "mu", "min": -5.0, "max": 5.0},
... {"name": "sigma", "min": 0.01, "max": 10.0},
... ],
... }
... ],
... "data": [
... {
... "name": "observed_x",
... "type": "unbinned",
... "entries": [[0.4], [-0.2], [0.1]],
... "axes": [{"name": "x", "min": -5.0, "max": 5.0}],
... }
... ],
... "likelihoods": [
... {"name": "gauss_likelihood", "distributions": ["gauss"], "data": ["observed_x"]}
... ],
... "analyses": [
... {
... "name": "gauss_fit",
... "likelihood": "gauss_likelihood",
... "domains": ["fit_region"],
... "parameters_of_interest": ["mu"],
... "init": "nominal",
... }
... ],
... }
>>> analysis_ws = pyhs3.Workspace(**analysis_workspace)
>>> fit_model = analysis_ws.model(analysis_ws.analyses["gauss_fit"])
>>> nll_graph = pyhs3.jaxify(-2 * fit_model.log_prob)
>>> nll_value = nll_graph(**fit_model.data, **fit_model.free_params)[0]
>>> nll_value.shape
()
>>> print(f"-2 log L = {float(nll_value):.4f}")
-2 log L = ...
Warning
The object pyhs3.jaxify returns is not jitted. Calling it directly
runs JAX eagerly, one operation at a time, which on a large likelihood is
orders of magnitude slower than the same call wrapped in jax.jit.
Wrap it for any repeated evaluation, such as a fit or a
scan, for example nll = jax.jit(lambda params: nll_graph(**fit_model.data, **params)[0]).
The single call above is fine without jax.jit, since jitting would add
compile time and save nothing. compile_nll() compiles
and bakes in the data for you; see Minimize the NLL with SciPy or iminuit.
fit_model.free_params gives every non-constant parameter’s starting value
from the workspace, and fit_model.data gives the observed arrays each
distribution needs, so the two dicts together are exactly what a jaxified
log_prob expects as keyword arguments. See log_prob,
data, free_params for what each
requires, and How pyhs3 Builds a Model for why the joint
likelihood is exposed in log space rather than probability space. pyhs3
doesn’t ship a minimizer itself; this section stops at producing the
callable one needs. See Fit a Model with Optimistix for minimizing
nll_graph over free_params with optimistix. See
Comparing Absolute NLL Values with RooFit if the absolute value differs
from RooFit’s.
Call a compiled likelihood with low overhead¶
To evaluate the full likelihood repeatedly, for example in a fit, use
compile_nll() (see Minimize the NLL with SciPy or iminuit). It compiles
the negative log-likelihood and its gradient as a function of one parameter
vector, with the data and constant parameters baked in, and handles the input
order and dtypes described below for you. The rest of this section is for
compiling individual expressions yourself.
model.pdf and model.logpdf take keyword arguments, so each call
reorders them into the compiled function’s positional order before
evaluating. In a loop that evaluates a small model many times, you avoid
that cost by compiling the expression once and calling it with positional
arrays yourself. On a small model, where the evaluation itself is cheap, the
per-call overhead of keyword dicts and Python floats can dominate; calling the
compiled function with positional numpy arrays and trust_input=True
removes most of it (see issue #115). Time your own model
before relying on it.
pyhs3 compiles its functions with trust_input=True. PyTensor then skips
input validation, so every input must already be a numpy array whose dtype
equals the input variable’s .dtype and whose shape matches the parameter: a
0-d array for a scalar parameter, a 1-d array for a vector one. pyhs3 creates
its inputs with pt.scalar and pt.vector, so the dtype is PyTensor’s
config.floatX, which is float64 by default. Build each argument with
np.asarray(value, dtype=var.dtype). A Python float, a mismatched dtype,
or a wrong shape is not rejected with a clear error. It fails inside the
compiled code or returns wrong values.
Inputs are positional, and the order is not the order of the workspace. For a
single distribution, pars() returns the names in the order
the compiled function expects:
>>> model.pars("gauss")
['sigma', 'mu', 'x']
Compile the log-density with the same inputs and call it with one array per
name, in that order. Each array here has shape () and the dtype of its
input variable:
>>> from pytensor import function
>>> from pytensor.graph.traversal import explicit_graph_inputs
>>> log_density = model.log_distributions["gauss"]
>>> inputs = [v for v in explicit_graph_inputs([log_density]) if v.name is not None]
>>> [v.name for v in inputs] == model.pars("gauss")
True
>>> fast_logpdf = function(inputs, log_density, trust_input=True)
>>> values = {"x": 0.0, "mu": 0.0, "sigma": 1.0}
>>> args = [np.asarray(values[v.name], dtype=v.dtype) for v in inputs]
>>> fast_logpdf(*args)
array(-0.918938...)
Build the ordered args list once, outside the loop, so the loop body is a single positional call.
For the joint likelihood, the input order is the order
explicit_graph_inputs yields for model.log_prob. Compile log_prob
with exactly that list. fit_model.data and fit_model.free_params supply
the values. fit_model.data already holds arrays of shape (n_entries,);
free_params holds Python floats, so each becomes a 0-d array. Casting every
value to its input variable’s dtype covers both:
>>> from pytensor import function
>>> from pytensor.graph.traversal import explicit_graph_inputs
>>> nll_inputs = [
... v for v in explicit_graph_inputs([fit_model.log_prob]) if v.name is not None
... ]
>>> sorted(v.name for v in nll_inputs)
['mu', 'sigma', 'x']
>>> fast_log_prob = function(nll_inputs, fit_model.log_prob, trust_input=True)
>>> point = {**fit_model.data, **fit_model.free_params}
>>> nll_args = [np.asarray(point[v.name], dtype=v.dtype) for v in nll_inputs]
>>> fast_log_prob(*nll_args).shape
()
See Fit a Model with Optimistix for passing a jaxified log_prob to a
minimizer, and pars() and log_prob for
the ordering guarantees.