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: object

Access 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: object

Free/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 on log10(value). Returns self so calls can be chained.

set_fixed(name, value)[source]#

Pin this parameter at value and drop any prior on it. Returns self.

free_param_names()[source]#

Names of the parameters currently being sampled.

flat_param_entries(prefix)[source]#

ParamEntry per free parameter, named f'{prefix}.{name}'.

Raises if a parameter was left neither free nor fixed.

describe()[source]#

Describe every candidate parameter as free or fixed.

class ps_eor.ml_gpr.params.Components[source]#

Bases: dict

Named covariance components, including a 'noise' kernel.

This behaves like a dictionary and adds a readable parameter summary. Names beginning with fg and eor support the result helpers:

components = Components({'fg': k_fg, 'eor': k_eor, 'noise': k_noise})
class ps_eor.ml_gpr.params.FlatParams(entries)[source]#

Bases: object

Every free hyperparameter of a whole model as one flat vector.

Vectors use sampling space: parameters marked log_scale=True are stored as log10(value).

classmethod from_components(components: dict)[source]#

Gather the free parameters of every kernel in a Components dict.

to_array()[source]#

Current parameter values, in sampling space.

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.

log_prior(arr=None, sampling_space=False)[source]#

Total log prior over all free parameters, -inf outside the support.

Set sampling_space=True when a sampler steps in that space; the required Jacobian is then included for log-scaled parameters.

prior_transform(u)[source]#

Map a unit-cube point to a sampling-space parameter vector.

randomize(rng=None)[source]#

One prior draw per free parameter, in sampling space – used to initialize MCMC walkers.

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: RuntimeError

Raised 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: object

Convert 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.

ps3d must be a tensor sampled at k_mean.

c_nu_from_ps21_fct(ps3d_fct, normalize=True)[source]#

Return covariance for a callable dimensional power spectrum.

c_nu_from_ps21(k_mean, ps3d, normalize=True)[source]#

Return covariance from dimensionless Delta²(k) samples.

ps3d is sampled at k_mean and is assumed isotropic.

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 B admits a Cholesky decomposition.

ps_eor.ml_gpr.covariance.nearest_positive_definite(A, maxtries=10)[source]#

Return a nearby positive-definite matrix.

Raises numpy.linalg.LinAlgError after maxtries corrections. This is a NumPy port of John D’Errico’s nearestSPD implementation, 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.LinAlgError after maxtries corrections. This is a NumPy port of John D’Errico’s nearestSPD implementation, 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 n Gaussian realizations with covariance K.

X supplies the mean-vector length. The result has shape (len(X), n) and is complex unless complex_type=False. Raises numpy.linalg.LinAlgError if K is not finite; with nearest_pd=True, a non-positive-definite matrix is repaired first.