Covariance and parameter utilities#
These modules support direct construction of covariance models. Most config-driven analyses do not need to call them.
Track free kernel parameters, priors, and sampler vectors.
Kernel parameters start fixed. Parameterized.set_free() assigns a prior;
log_scale=True makes samplers store and explore log10(value).
- class ps_eor.ml_gpr.params.ParamEntry(name, get, set, prior, log_scale, kern, attr)[source]#
Bases:
objectAccess and sampling metadata for one free parameter.
- site_to_sampling_space(site_val)[source]#
Convert a gradient-sampler site value to stored sampling space.
- sampling_space_to_site(sampling_val)[source]#
Inverse of
site_to_sampling_space(), for seeding a sampler at a known point such as the MAP.
- class ps_eor.ml_gpr.params.Parameterized[source]#
Bases:
objectFree/fixed and prior bookkeeping for one kernel’s hyperparameters.
Mixed into each kernel class. Every candidate starts fixed at its constructor value and becomes sampleable only through
set_free().- set_free(name, prior, log_scale=False)[source]#
Sample this parameter under prior.
With
log_scale=True, samplers operate onlog10(value). Returnsselfso calls can be chained.
- class ps_eor.ml_gpr.params.Components[source]#
Bases:
dictNamed covariance components, including a
'noise'kernel.This behaves like a dictionary and adds a readable parameter summary. Names beginning with
fgandeorsupport the result helpers:components = Components({'fg': k_fg, 'eor': k_eor, 'noise': k_noise})
- class ps_eor.ml_gpr.params.FlatParams(entries)[source]#
Bases:
objectEvery free hyperparameter of a whole model as one flat vector.
Vectors use sampling space: parameters marked
log_scale=Trueare stored aslog10(value).- classmethod from_components(components: dict)[source]#
Gather the free parameters of every kernel in a Components dict.
- from_array(arr)[source]#
Write an exact, finite sampling-space vector onto the kernels.
The entire vector is validated before any kernel state is mutated.
Convert 21-cm power spectra to frequency covariance and draw realizations.
CovarianceGenerator performs the power-spectrum conversion for each
UV bin. Its differentiable path preserves gradients from a VAE-generated
spectrum. get_samples() draws real or complex Gaussian realizations.
- exception ps_eor.ml_gpr.covariance.InvalidPowerSpectrumError[source]#
Bases:
RuntimeErrorRaised when a power spectrum cannot produce a valid covariance.
- ps_eor.ml_gpr.covariance.get_unscaled_dist(X, X2=None)[source]#
Return pairwise Euclidean distances without a lengthscale.
- class ps_eor.ml_gpr.covariance.CovarianceGenerator(freqs, uv_bins, log_scale_interpolatation=True, padding=2, interp_kind='quadratic')[source]#
Bases:
objectConvert an isotropic 3D power spectrum to frequency covariance.
The frequency and UV geometry is precomputed at construction. Prefer
get_cached_cov_gen()when evaluating several spectra on one grid.- c_nu_from_ps21_differentiable(k_mean, ps3d, normalize=True)[source]#
Return covariance while preserving gradients to
ps3d.ps3dmust be a tensor sampled atk_mean.
- ps_eor.ml_gpr.covariance.get_cached_cov_gen(freqs, uv_bins)[source]#
Return the cached covariance generator for hashable grid tuples.
- ps_eor.ml_gpr.covariance.c_nu_to_K(freqs, c_nu_nu)[source]#
Expand lag covariance into full frequency-frequency matrices.
- ps_eor.ml_gpr.covariance.is_positive_definite(B)[source]#
Return whether
Badmits a Cholesky decomposition.
- ps_eor.ml_gpr.covariance.nearest_positive_definite(A, maxtries=10)[source]#
Return a nearby positive-definite matrix.
Raises
numpy.linalg.LinAlgErroraftermaxtriescorrections. This is a NumPy port of John D’Errico’snearestSPDimplementation, based on Higham (1988).[1] https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd [2] N.J. Higham, “Computing a nearest symmetric positive semidefinite matrix” (1988): https://doi.org/10.1016/0024-3795(88)90223-6
- ps_eor.ml_gpr.covariance.nearest_postive_definite(A, maxtries=10)#
Return a nearby positive-definite matrix.
Raises
numpy.linalg.LinAlgErroraftermaxtriescorrections. This is a NumPy port of John D’Errico’snearestSPDimplementation, based on Higham (1988).[1] https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd [2] N.J. Higham, “Computing a nearest symmetric positive semidefinite matrix” (1988): https://doi.org/10.1016/0024-3795(88)90223-6
- ps_eor.ml_gpr.covariance.get_samples(X, n, K, complex_type=True, nearest_pd=True, method='eigh')[source]#
Draw
nGaussian realizations with covarianceK.Xsupplies the mean-vector length. The result has shape(len(X), n)and is complex unlesscomplex_type=False. Raisesnumpy.linalg.LinAlgErrorif K is not finite; withnearest_pd=True, a non-positive-definite matrix is repaired first.