kinextract.LOSVDErrorEstimator
- class kinextract.LOSVDErrorEstimator(fit: dict, cfg)[source]
Unified interface for estimating uncertainties on a recovered LOSVD.
Wraps the output of a completed
run_spectral_fit()– a fit that recovers a non-parametric line-of-sight velocity distributionbon the velocity gridxl, together with template weightsw– and provides three complementary, combinable error estimation strategies plus Gauss-Hermite (GH) moment error propagation.run_spectral_fit()’s default output is a genuine MAP/penalized-likelihood fit, solaplace_covariance()andbias_correction()work directly on it. If instead handed a fit produced by the optional, non-default full-posterior path (kinextract.bayesian.fit_state_bayesian(), whose reported point estimate is the NUTS posterior mean, not a MAP mode), those two methods raise a clearValueErrorrather than silently computing a meaningless Hessian at a non-stationary point (see_require_genuine_map_fit()) – useplot_losvd_posterior()for error bars on such a fit instead.residual_bootstrap()works either way (it always refits fresh via the MAP machinery internally, regardless of what kind of fit it was constructed from):Laplace / penalized-likelihood covariance (
laplace_covariance()) — fast (~seconds), computes a finite-difference Hessian of the MAP objective and inverts it to obtain the Bayesian posterior covariance under the Laplace approximation. Cheap and always worth running first, but likely underestimates true (frequentist) errors if the noise model or regularization strength is imperfect. Requires a genuine MAP fit (see above).Residual bootstrap (
residual_bootstrap()) — the most statistically honest but most expensive method (minutes, embarrassingly parallel vian_jobs). Draws many synthetic spectra by adding resampled residuals (optionally in contiguous blocks, to preserve correlated noise from sky subtraction/resampling) to the best-fit model, refits each synthetic spectrum through the full pipeline (including, for continuum-cofit fits, the joint continuum), and uses the resulting ensemble of recovered LOSVDs/GH moments to build frequentist error bars and percentile intervals.Bias-corrected LOSVD (
bias_correction()) — analytic and nearly free once the Hessian is available. The roughness/smoothing penalty used during the MAP fit shrinks the recovered LOSVD toward flatness (regularization bias); the hat (influence) matrix of the linearized penalized problem is used to estimate and subtract this bias, at the cost of amplified noise in the corrected LOSVD. Requires a genuine MAP fit (see above); also seeFitConfig’s “Known limitations” section – this correction was separately found to be actively harmful near the instrumental resolution limit, independent of the MAP-vs-Bayesian question here.Gauss-Hermite moment errors — propagates the LOSVD covariance (from either the Laplace or bootstrap methods) through the GH moment extraction (
kinextract.losvd.fit_losvd_gauss_hermite()) via the delta method, giving uncertainties directly on the physically interpretable moments (V, sigma, h3, h4) rather than just on the raw LOSVD bins.
summarize()combines whichever of these methods were run into a single dict with a recommended central estimate and error bar (preferring bootstrap over Laplace when both are available), andplot_losvd_with_errors()visualizes the result.- Parameters:
fit (dict) – Output of
run_spectral_fit()for the fit whose errors are to be estimated. Must containfit["state"](theFitState),fit["a_map"](the full MAP parameter vector), andfit["outputs"]with keys"b"(recovered LOSVD),"w"(template weights), and"gp"(best-fit model spectrum).cfg (FitConfig) – Configuration object used to produce the original fit. Reused as the starting point for bootstrap refits (with relaxed optimizer tolerances — see
_make_frozen_cfg/_make_frozen_cfg_joint) and to rebuild parameter bounds identical to those used in the original fit.
- a_map
Full MAP parameter vector (LOSVD bins, template weights, continuum offsets, etc.), as a flat array.
- Type:
ndarray
- b_map
MAP (best-fit) non-parametric LOSVD amplitudes on the velocity grid
xl.- Type:
ndarray, shape (nl,)
- bounds
Parameter bounds for the full parameter vector, rebuilt identically to those used in the original MAP fit; used to keep bootstrap refits within the same feasible region.
- __init__(fit: dict, cfg)[source]
Initialize the estimator from a completed fit and its configuration.
Extracts the MAP parameter vector, recovered LOSVD, template weights, and best-fit model spectrum from
fit, and rebuilds the parameter bounds used during the original fit (viabuild_initial_guess_nonparam()) so that subsequent bootstrap refits (residual_bootstrap()) are constrained identically to the original MAP optimization.
Methods
__init__(fit, cfg)Initialize the estimator from a completed fit and its configuration.
bias_correction()Estimate and subtract the regularization bias from the MAP LOSVD.
laplace_covariance([rel_step, ...])Compute the Laplace-approximation posterior covariance at a_map.
plot_bootstrap_distribution(bootstrap_result)Diagnostic plot of the full bootstrap ensemble (LOSVD bins and GH moments).
plot_losvd_with_errors(summary[, fig, ax, show])Plot the recovered LOSVD together with its estimated error band.
plot_spectrum_with_errors(summary[, fig, ...])Plot the best-fit spectrum with observational and model error bands.
residual_bootstrap([n_bootstrap, ...])Run a residual block bootstrap to estimate LOSVD error bars.
summarize([laplace_result, ...])Combine results from multiple methods into a unified summary.