prior

selfisys.prior.get_summary(x, bins, normalisation=None, kmax=1.4)[source]

Compute a power-spectrum summary for given cosmological parameters.

Parameters:
  • x (array-like) – Cosmological parameters [h, Omega_b, Omega_m, n_s, sigma_8].

  • bins (array-like) – Wavenumber bins.

  • normalisation (float or None, optional) – Normalisation constant to scale the resulting spectrum.

  • kmax (float, optional) – Maximum wavenumber for get_Pk.

Returns:

theta – The computed power-spectrum values, optionally normalised.

Return type:

ndarray

Raises:

RuntimeError – If the power-spectrum computation fails unexpectedly.

selfisys.prior.logposterior_hyperparameters_parallel(selfi, theta_fiducial, Nbin_min, Nbin_max, theta_norm, k_corr, alpha_cv)[source]

Compute the log-posterior for the hyperparameters of the prior from [leclercq2019primordial], for use within the SelfiSys pipeline.

Parameters:
  • selfi (object) – The selfi object.

  • theta_fiducial (ndarray) – Fiducial spectrum.

  • Nbin_min (int) – Minimum bin index for the wavenumber range.

  • Nbin_max (int) – Maximum bin index for the wavenumber range.

  • theta_norm (float) – Hyperparameter controlling the overall uncertainty.

  • k_corr (float) – Hyperparameter controlling correlation scale.

  • alpha_cv (float) – Cosmic variance strength.

Returns:

The log-posterior value for the given hyperparameters.

Return type:

float

Raises:

RuntimeError – If the log-posterior computation fails unexpectedly.

selfisys.prior.perform_prior_optimisation_and_plot(selfi, theta_fiducial, theta_norm_mean=0.1, theta_norm_std=0.3, k_corr_mean=0.02, k_corr_std=0.015, k_opt_min=0.0, k_opt_max=1.4, theta_norm_min=0.04, theta_norm_max=0.12, k_corr_min=0.012, k_corr_max=0.02, meshsize=30, Nbin_min=0, Nbin_max=100, theta_norm=0.05, k_corr=0.015, alpha_cv=0.00065, plot=True, savepath=None)[source]

Optimise the hyperparameters for the selfi2019 prior (from [leclercq2019primordial]).

Parameters:
  • selfi (object) – The selfi object.

  • theta_fiducial (ndarray) – Fiducial spectrum.

  • theta_norm_mean (float, optional) – Mean of the Gaussian hyperprior on theta_norm. Default 0.1.

  • theta_norm_std (float, optional) – Standard deviation of the hyperprior on theta_norm. Default 0.3.

  • k_corr_mean (float, optional) – Mean of the Gaussian hyperprior on k_corr. Default 0.020.

  • k_corr_std (float, optional) – Standard deviation of the hyperprior on k_corr. Default 0.015.

  • k_opt_min (float, optional) – Minimum wavenumber for the prior optimisation. Default 0.0.

  • k_opt_max (float, optional) – Maximum wavenumber for the prior optimisation. Default 1.4.

  • theta_norm_min (float, optional) – Lower bound for theta_norm in the mesh. Default 0.04.

  • theta_norm_max (float, optional) – Upper bound for theta_norm in the mesh. Default 0.12.

  • k_corr_min (float, optional) – Lower bound for k_corr in the mesh. Default 0.012.

  • k_corr_max (float, optional) – Upper bound for k_corr in the mesh. Default 0.02.

  • meshsize (int, optional) – Number of points in each dimension of the plot mesh. Default 30.

  • Nbin_min (int, optional) – Minimum bin index for restricting the prior. Default 0.

  • Nbin_max (int, optional) – Maximum bin index for restricting the prior. Default 100.

  • theta_norm (float, optional) – Initial or default guess of theta_norm. Default 0.05.

  • k_corr (float, optional) – Initial or default guess of k_corr. Default 0.015.

  • alpha_cv (float, optional) – Cosmic variance term or similar. Default 0.00065.

  • plot (bool, optional) – If True, generate and show/save a 2D contour plot. Default True.

  • savepath (str, optional) – File path to save the plot. If None, the plot is displayed.

Returns:

(theta_norm, k_corr) after optimisation.

Return type:

tuple

Raises:
  • OSError – If file operations fail during saving the prior or posterior.

  • RuntimeError – If the optimisation fails unexpectedly.

class selfisys.prior.planck_prior(Omega_mean, Omega_cov, bins, normalisation, kmax, nsamples=10000, nthreads=-1, EPS_K=1e-07, EPS_residual=0.001, filename=None)[source]

Bases: object

Custom prior for the SelfiSys pipeline. This is the prior used in [hoellinger2024diagnosing], based on the Planck 2018 cosmological parameters.

This class provides methods to compute a power-spectrum prior from a prior distribution of cosmological parameters, using a Gaussian fit. See equation (7) in [hoellinger2024diagnosing].

Parameters:
  • Omega_mean (array-like) – Mean of the prior distribution on cosmological parameters.

  • Omega_cov (array-like) – Covariance matrix of the prior distribution on cosmological parameters.

  • bins (array-like) – Wavenumbers where the power spectrum is evaluated.

  • normalisation (float or None) – If not None, divide the power spectra by the normalisation.

  • kmax (float) – Maximum wavenumber for computations.

  • nsamples (int, optional) – Number of samples drawn from the prior on the cosmological parameters. Default is 10,000.

  • nthreads (int, optional) – Number of CPU threads for parallel tasks. Default is -1, that is, auto-detect the number of available threads.

  • EPS_K (float, optional) – Regularisation parameter for covariance inversion. Default 1e-7.

  • EPS_residual (float, optional) – Additional cutoff for matrix inversion. Default 1e-3.

  • filename (str or None, optional) – Path to a .npy file to store or load precomputed power spectra.

mean

Mean of the computed power spectra.

Type:

ndarray

covariance

Covariance matrix of the computed power spectra.

Type:

ndarray

inv_covariance

Inverse of the covariance matrix.

Type:

ndarray

Raises:
  • OSError – If file reading or writing fails.

  • RuntimeError – For unexpected HPC or multi-processing errors.

property Nbin_max

Index of the maximal wavenumber given self.kmax.

property Nbin_min

Index of the minimal wavenumber given k_min.

compute()[source]

Compute the prior (mean, covariance, and inverse covariance).

If self.filename exists, tries to load the prior. Otherwise, samples from the prior distribution on cosmological parameters and evaluates the power spectra in parallel.

Raises:
  • OSError – If self.filename is not writable/accessible.

  • RuntimeError – If multi-processing or power-spectra computations fail.

classmethod load(fname)[source]

Load the prior from input file.

Parameters:

fname (str) – Input HDF5 filename.

Returns:

The prior object.

Return type:

prior

Raises:

OSError – If the file cannot be read or is invalid.

logpdf(theta, theta_mean, theta_covariance, theta_icov)[source]

Return the log prior probability at a given point in parameter space.

Parameters:
  • theta (ndarray) – Evaluation point in parameter space.

  • theta_mean (ndarray) – Prior mean vector.

  • theta_covariance (ndarray) – Prior covariance matrix.

  • theta_icov (ndarray) – Inverse of the prior covariance matrix.

Returns:

Log prior probability value.

Return type:

float

sample(seedsample=None)[source]

Draw a random sample from the prior distribution.

Parameters:

seedsample (int, optional) – Seed for the random number generator.

Returns:

A single sample from the prior distribution.

Return type:

ndarray

save(fname)[source]

Save the prior to an output file.

Parameters:

fname (str) – Output HDF5 filename to store the prior data.

Raises:

OSError – If the file cannot be accessed or written.

selfisys.prior.worker_class(params)[source]

Worker function to compute power spectra with CLASS, compatible with Python multiprocessing.

Parameters:

params (tuple) – (x, bins, normalisation, kmax) where x is an array-like of cosmological parameters, bins is the wavenumber array, normalisation is a float or None, and kmax is a float.

Returns:

theta – Power-spectrum summary from get_summary.

Return type:

ndarray