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()andvalue_and_grad()take a(K, P)array, one parameter vector per row with columns ordered asparameter_names, for any batch sizeK, and return one result per row:value()gives shape(K,)andgrad()(K, P).initial,bounds,to_vector()andto_dict()describe one parameter vector, as onCompiledNLL; 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 sameKreuse it.With
data_inputs=Trueorauxdata_inputs=True, thedataargument holds one array per data input, as forCompiledNLL, 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:
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)¶
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
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 ofx.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.
- BatchedCompiledNLL.grad(x, data=None)¶
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:
- 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 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:
- BatchedCompiledNLL.to_dict(x)¶
{name: value}mapping from a parameter vector.
- BatchedCompiledNLL.to_vector(values)¶
Parameter vector from a
{name: value}mapping of every free parameter.
- BatchedCompiledNLL.value(x, data=None)[source]¶
Negative log-likelihood per row of
x, shape(K,).- 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:
- BatchedCompiledNLL.value_and_grad(x, data=None)[source]¶
Negative log-likelihood
(K,)and gradient(K, P)per row ofx.- 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:
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/inffor 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
constis baked into the function and left out.
- BatchedCompiledNLL.poi_indices¶
Index in
parameter_names(and in every parameter vector) of each ofparameters_of_interest.