pyhs3.compiled.CompiledNLL

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

Negative log-likelihood compiled as a function of one parameter vector.

Build it with pyhs3.Model.compile_nll(). The free parameters are ordered as parameter_names; every method takes or returns a 1-D float array of that length. Constant parameters are baked into the compiled function. The observed data are baked in too, unless the likelihood was compiled with data_inputs=True, and so are the auxiliary data, unless it was compiled with auxdata_inputs=True: then each method takes an optional data mapping that replaces the binned observed counts, the auxiliary data, or both, for that call (see value()).

value(), grad() and value_and_grad() share one cached evaluation per parameter vector and data, so a minimizer that asks for the value and then the gradient at the same point (SciPy with separate fun and jac, iminuit with grad) pays for one evaluation. hessian() is a separate function, compiled at its first call and cached the same way.

Examples

With SciPy:

nll = model.compile_nll()
result = scipy.optimize.minimize(
    nll.value_and_grad, nll.initial, jac=True, bounds=nll.bounds
)
best_fit = nll.to_dict(result.x)

With iminuit:

minuit = iminuit.Minuit(nll.value, nll.initial, grad=nll.grad, name=nll.parameter_names)
minuit.limits = nll.bounds
minuit.migrad()
Parameters:
__init__(fn, parameter_names, initial, bounds, dtype, *, has_gradient, build_hessian, from_cache=False, parameters_of_interest=(), data=None)[source]

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

__init__(fn, parameter_names, initial, ...)

Wrap a compiled function of the parameter vector.

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 at x.

value_and_grad(x[, data])

Negative log-likelihood and its gradient at x from one evaluation.

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.

CompiledNLL.grad(x, data=None)[source]

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

data is as for value().

Parameters:
Return type:

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

CompiledNLL.hessian(x, data=None)[source]

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]]

CompiledNLL.to_dict(x)[source]

{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]

CompiledNLL.to_vector(values)[source]

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

Parameters:

values (Mapping[str, float])

Return type:

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

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

Negative log-likelihood at x.

Parameters:
Raises:
  • ValueError – If x or an array of data has the wrong shape, or if data is given to a likelihood compiled without data_inputs=True or auxdata_inputs=True.

  • KeyError – If data names something that is not a data input, or misses one.

Return type:

float

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

Negative log-likelihood and its gradient at x from one evaluation.

data is as for value().

Parameters:
Return type:

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

CompiledNLL.parameter_names

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

CompiledNLL.initial

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

CompiledNLL.bounds

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

CompiledNLL.from_cache

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

CompiledNLL.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.

CompiledNLL.poi_indices

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