derivkit.forecasting.sampling_utils module#
Gaussian sampling-kernel helpers derived from a Fisher matrix.
This module implements a Fisher-based Gaussian sampling distribution
q centered at theta0 with covariance:
(kernel_scale**2) * pinv(F)
where F is the Fisher information matrix evaluated at theta0.
In this approximation, F is the Hessian of -log L at theta0 and
acts as the local inverse covariance.
This kernel is used to generate candidate points and, for importance sampling,
to evaluate log(q) when sampling posteriors approximated with Fisher or DALI.
For importance sampling, log(q) is subtracted from the target log-posterior to form log-weights.
It provides:
construction of Fisher-based sampling covariances,
stabilized Cholesky factorization for near-singular kernels,
sampling and log-density evaluation of the kernel,
fast bounds rejection masks,
MCMC walker initialization under sampler bounds.
These utilities do not depend on a specific sampler implementation. They provide kernel draws, log-densities, and bounds filtering that are called by the GetDist importance-sampling wrappers and by the emcee walker initialization routine.
- derivkit.forecasting.sampling_utils.apply_parameter_bounds(samples: ndarray[tuple[Any, ...], dtype[floating]], parameter_bounds: Sequence[tuple[float | None, float | None]] | None) ndarray[tuple[Any, ...], dtype[float64]]#
Applies per-parameter bounds to a set of samples.
This function performs a fast, axis-aligned rejection step: any sample with at least one parameter value outside the specified bounds is discarded. It does not evaluate priors or compute weights.
- Parameters:
samples – Sample array with shape
(n_samples, p)withpparameters.parameter_bounds – Optional sequence of
(lower, upper)bounds, one per parameter. UseNoneto indicate an unbounded side (e.g.(0.0, None)). IfNone, no filtering is applied.
- Returns:
Samples satisfying all bounds, with shape
(n_kept, p).n_keptmay be zero.- Raises:
ValueError – If
parameter_boundsis provided but does not have lengthp.
- derivkit.forecasting.sampling_utils.fisher_to_cov(fisher: ndarray[tuple[Any, ...], dtype[floating]], *, rcond: float | None = None) ndarray[tuple[Any, ...], dtype[float64]]#
Converts a Fisher matrix to a covariance matrix using pseudoinverse.
- Parameters:
fisher – Fisher information matrix with shape
(p, p).rcond – Cutoff ratio for small singular values in pseudoinverse. If
None, the default fromnp.linalg.pinvis used.
- Returns:
Covariance matrix with shape
(p, p)given bypinv(fisher).- Raises:
ValueError – If
fisheris not a square 2D array.
- derivkit.forecasting.sampling_utils.init_walkers_from_fisher(theta0: ndarray[tuple[Any, ...], dtype[floating]], fisher: ndarray[tuple[Any, ...], dtype[floating]], *, n_walkers: int, init_scale: float, seed: int | None, sampler_bounds: Sequence[tuple[float | None, float | None]] | None) ndarray[tuple[Any, ...], dtype[float64]]#
Returns initial MCMC walker positions from a Fisher-based Gaussian sampling kernel.
Returns an array of walker positions centered at
theta0with scatter set by a Fisher-derived covariance(init_scale^2) * pinv(F). Ifsampler_boundsare provided, positions outside the bounds are rejected and additional candidates are generated untiln_walkerspositions are collected or a retry limit is reached.- Parameters:
theta0 – Kernel mean with shape
(p,)withpparameters.fisher – Fisher information matrix with shape
(p, p).n_walkers – Number of walker positions to return.
init_scale – Multiplicative scale applied to the kernel covariance.
seed – Optional random seed for reproducible initialization.
sampler_bounds – Optional per-parameter
(lower, upper)bounds. UseNonefor an unbounded side.
- Returns:
Array of initial positions with shape
(n_walkers, p).- Raises:
ValueError – If
theta0andfisherhave incompatible shapes.ValueError – If
sampler_boundsis provided and does not have lengthp.RuntimeError – If sufficient in-bounds positions cannot be generated within the retry limit.
- derivkit.forecasting.sampling_utils.kernel_cov_from_fisher(fisher: ndarray[tuple[Any, ...], dtype[floating]], *, kernel_scale: float) ndarray[tuple[Any, ...], dtype[float64]]#
Returns the covariance of the Fisher-based Gaussian sampling kernel.
This is a thin wrapper around
derivkit.forecasting.integrations.sampling_utils.fisher_to_cov()that convertsfisherto a covariance via pseudoinverse and applies the scalingkernel_scale^2.The Fisher matrix is treated as the Hessian of
-log Lattheta0. In this local approximation it acts as an inverse covariance, andkernel_scalecontrols the overall kernel width. A pseudoinverse is used so the covariance is defined even iffisheris singular.- Parameters:
fisher – Fisher information matrix with shape
(p, p).kernel_scale – Multiplicative scale factor applied to the covariance. Increasing values widen the kernel; decreasing values narrow it.
- Returns:
Kernel covariance matrix with shape
(p, p), equal to(kernel_scale^2) * fisher_to_cov(fisher).- Raises:
ValueError – If
fisheris not a square 2D array.
- derivkit.forecasting.sampling_utils.kernel_samples_from_fisher(theta0: ndarray[tuple[Any, ...], dtype[floating]], fisher: ndarray[tuple[Any, ...], dtype[floating]], *, n_samples: int, kernel_scale: float, seed: int | None) ndarray[tuple[Any, ...], dtype[float64]]#
Draws samples from a Fisher-based Gaussian sampling kernel.
Samples are drawn from the Gaussian kernel density
q(theta)with meantheta0and covariance(kernel_scale^2) * pinv(F):q(theta) = Normal(mean=theta0, cov=(kernel_scale^2) * pinv(F))
Fis the Fisher information matrix evaluated attheta0. In this approximation,Facts as an inverse covariance and Normal represents the multivariate normal distribution. A pseudoinverse is used so the covariance is defined even ifFis singular or ill-conditioned.- Parameters:
theta0 – Kernel mean with shape
(p,)withpparameters.fisher – Fisher information matrix with shape
(p, p).n_samples – Number of samples to draw.
kernel_scale – Multiplicative scale applied to the covariance. Increasing values widen the kernel; decreasing values narrow it.
seed – Optional random seed for reproducible draws.
- Returns:
Array of samples with shape
(n_samples, p).
- derivkit.forecasting.sampling_utils.log_gaussian_kernel(samples: ndarray[tuple[Any, ...], dtype[floating]], theta0: ndarray[tuple[Any, ...], dtype[floating]], fisher: ndarray[tuple[Any, ...], dtype[floating]], *, kernel_scale: float) ndarray[tuple[Any, ...], dtype[float64]]#
Log-density of a Fisher-based Gaussian sampling kernel.
Defines a Gaussian kernel
qwith meantheta0and covariance(kernel_scale^2) * pinv(F)where
Fis the Fisher information matrix. The covariance is formed with a pseudoinverse (allowing singular or ill-conditionedF). A small diagonal jitter is added before evaluating the log-density so the calculation remains well-defined for near-singular covariances.- Parameters:
samples – Sample locations with shape
(n_samples, p).theta0 – Kernel mean with shape
(p,).fisher – Fisher information matrix with shape
(p, p).kernel_scale – Multiplicative scale applied to the covariance.
- Returns:
Log of the kernel probability density
log(q(theta)), evaluated at each sample point, with shape(n_samples,).- Raises:
ValueError – If input shapes are incompatible.
RuntimeError – If the jittered covariance is not positive-definite.
- derivkit.forecasting.sampling_utils.stabilized_cholesky(cov: ndarray[tuple[Any, ...], dtype[floating]]) ndarray[tuple[Any, ...], dtype[float64]]#
Returns a Cholesky factor of a covariance matrix.
This function computes a lower-triangular matrix
Lsuch thatcovis approximatelyL @ L.T, even whencovis nearly singular or only positive semi-definite.- Parameters:
cov – Covariance matrix with shape
(p, p)withpparameters.- Returns:
A lower-triangular Cholesky factor
Lof the regularized covariance.- Raises:
ValueError – If
covis not a square 2D array.