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 asparameter_names; every method takes or returns a 1-Dfloatarray of that length. Constant parameters are baked into the compiled function. The observed data are baked in too, unless the likelihood was compiled withdata_inputs=True, and so are the auxiliary data, unless it was compiled withauxdata_inputs=True: then each method takes an optionaldatamapping that replaces the binned observed counts, the auxiliary data, or both, for that call (seevalue()).value(),grad()andvalue_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 separatefunandjac, iminuit withgrad) 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:
fn (
Callable[...,list[ndarray[tuple[Any,...],dtype[float64]]]])dtype (
str)has_gradient (
bool)build_hessian (
Callable[[],Callable[...,list[ndarray[tuple[Any,...],dtype[float64]]]]])from_cache (
bool)data (
Mapping[str,Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]] |None)
- __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 ofdatain its order, and returning[nll], or[nll, grad]ifhas_gradient.parameter_names (
Sequence[str]) – Names of thePfree parameters, in vector order.bounds (
Sequence[tuple[float,float]]) –(lower, upper)per parameter;-inf/inffor an open side.dtype (
str) – Dtype of the parameter vector expected byfn.has_gradient (
bool) – Whetherfnalso returns the gradient.build_hessian (
Callable[[],Callable[...,list[ndarray[tuple[Any,...],dtype[float64]]]]]) – Compiles the Hessian, a function with the arguments offnreturning[hessian]; called at the first call tohessian().from_cache (
bool) – Whetherfnwas 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 offn, keyed by the name thedataargument ofvalue()uses; empty orNoneif the data are baked intofn.
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
xfrom one evaluation.Attributes
Free parameter names, in the order of every parameter vector.
Starting point from the model's parameter set, shape
(P,).(lower, upper)per parameter from the domain;-inf/inffor an open side or a parameter outside the domain.Whether the compiled function was loaded from the on-disk cache.
Parameters of interest that are free parameters, in the order given (for a model, the order the analysis lists them).
Index in
parameter_names(and in every parameter vector) of each ofparameters_of_interest.
- CompiledNLL.grad(x, data=None)[source]¶
Gradient of the negative log-likelihood at
x, shape(P,).datais as forvalue().- Parameters:
x (
Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str])data (
Mapping[str,Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]] |None)
- Return type:
- 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 parametersiandjofparameter_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.datais as forvalue().- Parameters:
x (
Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str])data (
Mapping[str,Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]] |None)
- Return type:
- CompiledNLL.to_vector(values)[source]¶
Parameter vector from a
{name: value}mapping of every free parameter.
- CompiledNLL.value(x, data=None)[source]¶
Negative log-likelihood at
x.- Parameters:
x (
Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]) – Parameter vector, shape(P,), ordered asparameter_names.data (
Mapping[str,Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]] |None) – Data to evaluate on instead of the model’s own, with one entry for every data input: withdata_inputs=True, the binned observed counts keyed likepyhs3.Model.observed_data, one(B,)array per HistFactory channel,Bthe channel’s total number of bins; withauxdata_inputs=True, the global observables keyed and shaped likepyhs3.Model.auxdata.Noneuses the model’s own data.
- Raises:
ValueError – If
xor an array ofdatahas the wrong shape, or ifdatais given to a likelihood compiled withoutdata_inputs=Trueorauxdata_inputs=True.KeyError – If
datanames something that is not a data input, or misses one.
- Return type:
- CompiledNLL.value_and_grad(x, data=None)[source]¶
Negative log-likelihood and its gradient at
xfrom one evaluation.datais as forvalue().- Parameters:
x (
Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str])data (
Mapping[str,Buffer|_SupportsArray[dtype[Any]] |_NestedSequence[_SupportsArray[dtype[Any]]] |complex|bytes|str|_NestedSequence[complex|bytes|str]] |None)
- Return type:
- 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/inffor 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
constis baked into the function and left out.
- CompiledNLL.poi_indices¶
Index in
parameter_names(and in every parameter vector) of each ofparameters_of_interest.