hessian_ci#
- class braintools.fit.hessian_ci(loss, best_params, *, confidence=0.95, kind='sse', n_data=None)#
Compute confidence intervals from the Hessian of a loss at its minimum.
The loss is expanded to second order around the optimum \(\hat\theta\) (in the constrained, physical parameter space), with Hessian \(H = \nabla^2 L(\hat\theta)\) taken over all
pscalar entries ofbest_params:kind='nll':lossis a negative log-likelihood and the Laplace approximation gives \(\mathrm{Cov} = H^{-1}\); intervals use the normal quantile.kind='sse':lossis a sum of squared errors overn_dataresiduals. With \(s^2 = L(\hat\theta) / (n - p)\), \(\mathrm{Cov} = 2 s^2 H^{-1}\), and intervals use the Student-t quantile with \(n - p\) degrees of freedom. For a linear model this is exactly the ordinary-least-squares covariance \(s^2 (X^\top X)^{-1}\).
The interval of parameter \(i\) is \(\hat\theta_i \pm q\,\sqrt{\mathrm{Cov}_{ii}}\).
- Parameters:
loss (
Callable[[Any],Any]) –loss(params) -> scalar, twice differentiable, taking a pytree with the structure ofbest_params.best_params (
Any) – The optimum (e.g.FitResult.best_params). Leaves may bebrainunitQuantities; the loss then receives Quantities and the results carry the same units.confidence (
float) – Two-sided confidence level in(0, 1).kind (
str) – Meaning of the loss, see above.n_data (
int|None) – Number of residuals in the sum of squares; required forkind='sse'and must exceed the number of parameters.
- Returns:
(estimate, stderr, low, high), each with the structure ofbest_params.- Return type:
- Raises:
ValueError – If
kindis unknown,confidenceis outside(0, 1),n_datais missing or not larger than the number of parameters forkind='sse', orbest_paramshas no leaves.TypeError – If
confidenceorn_datais traced, or the loss carries a physical unit.
See also
FitterProduces
best_params.
Notes
The intervals are asymptotic and assume the optimum is an interior minimum where the loss is locally quadratic. A singular or indefinite Hessian (non-identifiable parameters, a saddle) gives non-finite or meaningless standard errors.
confidenceandn_datafix the quantile, so they must be concrete; with them static, the function works underjax.jitandjax.vmapoverbest_params. It is not meant to be differentiated.Examples
>>> import jax.numpy as jnp >>> from braintools.fit import hessian_ci >>> x = jnp.arange(6.) >>> y = jnp.array([0.1, 1.9, 4.2, 5.8, 8.1, 9.9]) >>> sse = lambda p: jnp.sum((y - p['a'] - p['b'] * x) ** 2) >>> best = {'a': jnp.asarray(0.05714286), 'b': jnp.asarray(1.9771429)} >>> ci = hessian_ci(sse, best, n_data=6) >>> print(round(float(ci.stderr['b']), 4), round(float(ci.low['b']), 3), round(float(ci.high['b']), 3)) 0.0398 1.867 2.088