pyhs3.compiled.BatchedCompiledNLL

class pyhs3.compiled.BatchedCompiledNLL(fn, parameter_names, initial, bounds, dtype, *, has_gradient, build_hessian, from_cache=False, parameters_of_interest=(), data=None)[source]

Negative log-likelihood evaluated at a batch of parameter vectors.

Build it with Model.compile_nll(backend="jax", batched=True). value(), grad() and value_and_grad() take a (K, P) array, one parameter vector per row with columns ordered as parameter_names, for any batch size K, and return one result per row: value() gives shape (K,) and grad() (K, P). initial, bounds, to_vector() and to_dict() describe one parameter vector, as on CompiledNLL; stack such vectors as rows to build a batch.

XLA compiles the function once per batch size, at the first call with that K; later calls with the same K reuse it.

With data_inputs=True or auxdata_inputs=True, the data argument holds one array per data input, as for CompiledNLL, shared by every row of the batch.

Example

A grid of 9 values of one parameter, the others at their initial values:

nll = model.compile_nll(backend="jax", batched=True)
X = np.tile(nll.initial, (9, 1))  # (K, P)
X[:, nll.parameter_names.index("mu")] = np.linspace(0.0, 4.0, 9)
values, grads = nll.value_and_grad(X)  # (9,), (9, P)
Parameters:
__init__(fn, parameter_names, initial, bounds, dtype, *, has_gradient, build_hessian, from_cache=False, parameters_of_interest=(), data=None)

Wrap a compiled function of the parameter vector.

Parameters:
  • fn (Callable[..., list[ndarray[tuple[Any, ...], dtype[float64]]]]) – Compiled function taking the (P,) parameter vector, then one array per entry of data in its order, and returning [nll], or [nll, grad] if has_gradient.

  • parameter_names (Sequence[str]) – Names of the P free parameters, in vector order.

  • initial (Sequence[float]) – Starting value per parameter.

  • bounds (Sequence[tuple[float, float]]) – (lower, upper) per parameter; -inf/inf for an open side.

  • dtype (str) – Dtype of the parameter vector expected by fn.

  • has_gradient (bool) – Whether fn also returns the gradient.

  • build_hessian (Callable[[], Callable[..., list[ndarray[tuple[Any, ...], dtype[float64]]]]]) – Compiles the Hessian, a function with the arguments of fn returning [hessian]; called at the first call to hessian().

  • from_cache (bool) – Whether fn was loaded from the on-disk cache.

  • parameters_of_interest (Sequence[str]) – Names of the parameters of interest; those that are not free parameters are left out.

  • data (Mapping[str, Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]] | None) – Default value of each data input of fn, keyed by the name the data argument of value() uses; empty or None if the data are baked into fn.

Methods

grad(x[, data])

Gradient of the negative log-likelihood at x, shape (P,).

hessian(x[, data])

Hessian of the negative log-likelihood at x, shape (P, P).

to_dict(x)

{name: value} mapping from a parameter vector.

to_vector(values)

Parameter vector from a {name: value} mapping of every free parameter.

value(x[, data])

Negative log-likelihood per row of x, shape (K,).

value_and_grad(x[, data])

Negative log-likelihood (K,) and gradient (K, P) per row of x.

Attributes

parameter_names

Free parameter names, in the order of every parameter vector.

initial

Starting point from the model's parameter set, shape (P,).

bounds

(lower, upper) per parameter from the domain; -inf/inf for an open side or a parameter outside the domain.

from_cache

Whether the compiled function was loaded from the on-disk cache.

parameters_of_interest

Parameters of interest that are free parameters, in the order given (for a model, the order the analysis lists them).

poi_indices

Index in parameter_names (and in every parameter vector) of each of parameters_of_interest.

BatchedCompiledNLL.grad(x, data=None)

Gradient of the negative log-likelihood at x, shape (P,).

data is as for value().

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[float64]]

BatchedCompiledNLL.hessian(x, data=None)

Hessian of the negative log-likelihood at x, shape (P, P).

Entry [i, j] is the second derivative with respect to parameters i and j of parameter_names. The matrix is float64 and exactly symmetric. At a minimum, its inverse is the covariance estimate of the fitted parameters. The Hessian is compiled at the first call, so a likelihood that never needs it pays nothing. data is as for value().

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[float64]]

BatchedCompiledNLL.to_dict(x)

{name: value} mapping from a parameter vector.

Parameters:

x (Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str])

Return type:

dict[str, float]

BatchedCompiledNLL.to_vector(values)

Parameter vector from a {name: value} mapping of every free parameter.

Parameters:

values (Mapping[str, float])

Return type:

ndarray[tuple[Any, ...], dtype[float64]]

BatchedCompiledNLL.value(x, data=None)[source]

Negative log-likelihood per row of x, shape (K,).

Parameters:
Return type:

ndarray[tuple[Any, ...], dtype[float64]]

BatchedCompiledNLL.value_and_grad(x, data=None)[source]

Negative log-likelihood (K,) and gradient (K, P) per row of x.

Parameters:
Return type:

tuple[ndarray[tuple[Any, ...], dtype[float64]], ndarray[tuple[Any, ...], dtype[float64]]]

BatchedCompiledNLL.parameter_names

Free parameter names, in the order of every parameter vector.

BatchedCompiledNLL.initial

Starting point from the model’s parameter set, shape (P,).

BatchedCompiledNLL.bounds

(lower, upper) per parameter from the domain; -inf/inf for an open side or a parameter outside the domain.

BatchedCompiledNLL.from_cache

Whether the compiled function was loaded from the on-disk cache.

BatchedCompiledNLL.parameters_of_interest

Parameters of interest that are free parameters, in the order given (for a model, the order the analysis lists them). A parameter of interest held const is baked into the function and left out.

BatchedCompiledNLL.poi_indices

Index in parameter_names (and in every parameter vector) of each of parameters_of_interest.