candl.likelihood#
Main module containing the likelihood and prior classes.
Overview#
Primary CMB likelihood class:
Lensing likelihood class:
Gaussian prior:
Misc:
- class candl.likelihood.GaussianPrior(prior_covariance, par_names, central_value=None, multiplicative_par=False)#
Base class for Gaussian priors.
- Attributes:
- central_valuearray, float (optional)
The central value of the prior.
- par_nameslist, str
The names of the parameters this prior acts on.
- prior_covariancearray, float
The covariance matrix.
- prior_covariance_cholarray, float
Cholesky decomposition of the covariance matrix.
- multiplicative_parbool
Whether the parameters this prior is placed on act as multiplicative factors - uses log of the parameters rather than calculating offset with the central values
Methods
__init__(prior_covariance, par_names[, ...])Initialise a new instance of the GaussianPrior class.
log_like(sampled_pars)Calculates the positive log likelihood of the prior.
- log_like(sampled_pars)#
Calculates the positive log likelihood of the prior.
- Parameters:
- sampled_parsdict
Dictionary that holds the values of all parameters in par_names.
- Returns:
- float
The positive log likelihood of the prior evaluated for the sampled_params.
- class candl.likelihood.LensLike(data_set_file, **kwargs)#
Lensing likelihood class used to move from theory Dls and parameter values to the log likelihood value.
- Attributes:
- N_binsarray, int
The number of band power bins of each spectrum.
- N_bins_totalint
The total number of band power bins.
- N_ell_bins_theoryint
The number of ell bins (same for each spectrum).
- N_spectra_totalint
The number of spectra.
- bins_start_ixarray, int
The start indices for each spectrum in the long data vector.
- bins_stop_ixarray, int
The stop indices for each spectrum in the long data vector.
- covariancearray, float
The band power covariance matrix.
- covariance_chol_decarray, float
The cholesky decomposition of the band power covariance matrix.
- crop_maskarray, bool
A mask to be applies to the original data vector for any subselection.
- data_bandpowersarray, float
The data band powers, blinded if requested.
- _data_bandpowersarray, float
The unblinded data band powers - careful!
- _blinding_functionarray, float
ratio of blinded to unblinded band powers - careful!
- data_modellist of candl.transformations.abstract_base.Transformation
List of transformations to be applied to the theory spectra.
- data_set_dictdict
Dictionary read in from data set .yaml file containing all the information needed to initialise the likelihood.
- data_set_filestr
The file path of the data set .yaml file.
- effective_ellsarray, float
Bin centres for each spectrum.
- ellsarray, float
Angular multipole moments for a single spectrum.
- lensing_fiducial_correctionarray, float
M * Cfid, loaded from files.
- long_ellsarray, float
Angular multipole moments for all spectra.
- namestr
Name of the likelihood.
- priorslist, GaussianPrior
List of priors to be applied.
- required_nuisance_parameterslist, str
List of nuisance parameters required for the likelihood.
- spec_orderlist
List of strings specifying the order of spectra in the long data and model vectors.
- spec_typeslist
List of strings specifying the spectrum type (pp).
- unique_spec_typeslist
List of strings specifying the spectrum type (TT, TE, EE) without duplicate entries in no particular order.
- tiled_ellsarray, float
2d array of ell values for unbinned model spectra.
- window_functionslist of arrays (float)
Band power window functions. Each entry in the N_specs long list is and array with size (N_theory_bins, N_bins).
Methods
__init__(data_set_file, **kwargs)Initialise a new instance of the LensLike class.
log_like(params)Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
log_like_for_bdp(params, bdp)Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
prior_logl(params)Returns the positive log likelihood of the priors.
gaussian_logl(delta_bdp)Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed.
chi_square(params)Returns the chi squared for a given set of theory Dls and nuisance parameter values.
get_model_specs(params)Returns the theory spectra adjusted for the foreground/nuisance model.
bin_model_specs(model_specs)Bin model spectra.
Initialise the data model, i.e. the series of transformations to be applied to the theory spectra.
Initialise the priors.
Adjust all relevant attributes of the likelihood for an arbitrary subselection of the data.
Generate a crop mask.
interpret_crop_hint(crop_hint[, verbose])Interpret a string hint that indicates a crop mask.
Calculate useful variables concerning the ell range and binning.
blind_bandpowers([seed])Applies blinding function to bandpowers by multiplying by a random sinusoid function and a slope.
- bin_model_specs(model_specs)#
Bin model spectra.
- Parameters:
- model_specsarray, float
The spectra to be binned.
- Returns:
- array (float)
The binned spectra.
- blind_bandpowers(seed=None)#
Applies blinding function to bandpowers by multiplying by a random sinusoid function and a slope. It’s important that the same spectrum types get hit by the same blinding function, i.e. e.g. the same for all TT spectra regardless of frequency.
- Parameters:
- seedint, optional
random seed to use
- Returns:
- array
blinded band powers
- array
blinding function
- chi_square(params)#
Returns the chi squared for a given set of theory Dls and nuisance parameter values. Intentionally not reusing the gaussian_logl function as we might add a beam covariance contribution at a later date.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Chi square value
- crop_for_data_selection()#
Adjust all relevant attributes of the likelihood for an arbitrary subselection of the data. This function manipulates not only all the information the likelihood stores on the number of bins, spectrum types etc., but also crops the band powers, covariance, and window functions.
- gaussian_logl(delta_bdp)#
Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed.
- Parameters:
- delta_bdparray, float
Difference between data and model band powers
- Returns:
- float
Positive log likelihood.
- generate_crop_mask()#
Generate a crop mask. A crop mask is understood to be a boolean mask to be applied to the uncropped data vector with True entries for bins that should be used and False entries for bins that should be ignored. The method accessed the information on data selection passed by the user interprets it.
If multiple string hints are passed then only bins selected by the most hints are used.
See also: interpret_crop_hint
- Returns:
- arraybool
The mask to be applied to the uncropped long data vector.
- get_ell_helpers()#
Calculate useful variables concerning the ell range and binning.
- Returns:
- arrayfloat
array running over the angular multipoles for one spectrum (2->N_ell_bins_theory+2)
- arrayfloat
2d array giving the angular multipole range for each spectrum in rows
- arrayfloat
long array giving the angular multipole range for each spectrum (concatenated)
- arrayfloat
long array effective containing the effective bin centres (concatenated according to spectra_order).
- get_model_specs(params)#
Returns the theory spectra adjusted for the foreground/nuisance model.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- array (float)
Theory Dls adjusted for the nuisance and foreground model. Covering ell=2-(2+N_ell_bins_theory).
- init_data_model()#
Initialise the data model, i.e. the series of transformations to be applied to the theory spectra. Crucially, this function initialises all the transformation instances and passes them the keywords they need. It steps through the attributes needed by the initialisation of each class and supplies these from attributes of the likelihood as arguments or info from the relevant section of the .yaml file where the transformation. Additionally, it creates the freq_info list of effective frequencies and completes the path of the template_file.
- Returns:
- list of candl.transformations.abstract_base.Transformation
The initialised transformation instances in the intended order ready to be applied to the theory spectra.
- init_priors()#
Initialise the priors. Similar to init_data_model, this function steps through the priors requested by the user and initialises GaussianPrior instances using the passed information.
- Returns:
- list of GaussianPrior
The initialised priors ready to be evaluated.
- interpret_crop_hint(crop_hint, verbose=False)#
Interpret a string hint that indicates a crop mask. The intended format is: “(data) (action)”, where “(data)” specifies which part of the data is selected and “(action)” declares what to do with this selection. Understood options for “(data)” are: * spectrum types, e.g. “pp” * ell range, e.g. “ell<650” or “ell>1500” Understood options for “(action)” are: * “remove” remove this part * “only” only keep this part, removing all the rest
- Returns:
- arraybool
The mask to be applied to the uncropped long data vector corresponding to this data hint.
- log_like(params)#
Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Negative log likelihood.
- log_like_for_bdp(params, bdp)#
Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values. Shortcut to input band powers is useful to jit up the likelihood function with more flexibility, e.g. when calculating the derivative for different mock data sets.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Negative log likelihood.
- prior_logl(params)#
Returns the positive log likelihood of the priors.
- Parameters:
- paramsdict
Dictionary containing nuisance parameter values.
- Returns:
- float
Positive log likelihood.
- class candl.likelihood.Like(data_set_file, **kwargs)#
Likelihood class for primary CMB power spectra (TT/TE/EE/BB) used to move from theory Dls and parameter values to log likelihood value.
- Attributes:
- N_binsarray, int
The number of band power bins of each spectrum.
- N_bins_totalint
The total number of band power bins.
- N_ell_bins_theoryint
The number of ell bins (same for each spectrum).
- N_spectra_totalint
The number of spectra.
- bins_start_ixarray, int
The start indices for each spectrum in the long data vector.
- bins_stop_ixarray, int
The stop indices for each spectrum in the long data vector.
- covariancearray, float
The band power covariance matrix.
- covariance_chol_decarray, float
The cholesky decomposition of the band power covariance matrix.
- crop_maskarray, bool
A mask to be applies to the original data vector for any subselection.
- data_bandpowersarray, float
The data band powers, blinded if requested.
- _data_bandpowersarray, float
The unblinded data band powers - careful!
- _blinding_functionarray, float
ratio of blinded to unblinded band powers - careful!
- data_modellist of candl.transformations.abstract_base.Transformation
List of transformations to be applied to the theory spectra.
- data_set_dictdict
Dictionary read in from data set .yaml file containing all the information needed to initialise the likelihood.
- data_set_filestr
The file path of the data set .yaml file.
- effective_ellsarray, float
Bin centres for each spectrum.
- effective_frequenciesdict
Dictionary containing effective frequency information from the corresponding .yaml file.
- ellsarray, float
Angular multipole moments for a single spectrum.
- long_ellsarray, float
Angular multipole moments for all spectra.
- namestr
Name of the likelihood.
- priorslist, GaussianPrior
List of priors to be applied.
- required_nuisance_parameterslist, str
List of nuisance parameters required for the likelihood.
- spec_freqslist
List of lists giving the two frequencies of each spectrum.
- spec_orderlist
List of strings specifying the order of spectra in the long data and model vectors.
- spec_typeslist
List of strings specifying the spectrum type (TT, TE, EE).
- unique_spec_typeslist
List of strings specifying the spectrum type (TT, TE, EE) without duplicate entries in no particular order.
- tiled_ellsarray, float
2d array of ell values for unbinned model spectra.
- window_functionslist of arrays (float)
Band power window functions in order of spectra. One list entry for each spectrum with size (N_theory_bins, N_bins). Window functions start at ell=2.
Methods
__init__(data_set_file, **kwargs)Initialise a new instance of the Like class.
log_like(params)Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
log_like_for_bdp(params, bdp)Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
prior_logl(params)Returns the positive log likelihood of the priors.
gaussian_logl(data_bandpowers, binned_theory_Dls)Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed.
Calculate the positive log likelihood, including determinant of the covariance i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) + log | cov | Adds beam covariance and re-computes the Cholesky decomposition.
HL_bin_loop(i, in_bundle)Basic loop of the Hamimeche-Lewis likelihood approximation operating on a single bin.
HL_logl(data_bandpowers, binned_theory_Dls)Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed.
offset_lognorm_logl(data_bandpowers, ...)Calculate the positive log likelihood for an offset lognormal distribution i.e.: logl = 0.5 * (x-µ) @ C^-1 @ (x-µ) + ∑ln(x) where x is ln(model-data_offset).
chi_square(params)Returns the chi squared for a given set of theory Dls and nuisance parameter values.
get_model_specs(params)Returns the theory spectra adjusted for the foreground/nuisance model.
bin_model_specs(model_specs)Bin model spectra.
Initialise the data model, i.e. the series of transformations to be applied to the theory spectra.
Initialise the priors.
Adjust all relevant attributes of the likelihood for an arbitrary subselection of the data.
Generate a crop mask.
interpret_crop_hint(crop_hint[, verbose])Interpret a string hint that indicates a crop mask.
Calculate useful variables concerning the ell range and binning.
blind_bandpowers([seed])Applies blinding function to bandpowers by multiplying by a random sinusoid function and a slope.
- HL_bin_loop(i, in_bundle)#
Basic loop of the Hamimeche-Lewis likelihood approximation operating on a single bin. Usees the Hamimeche-Lewis approximation for low ell spectra (https://arxiv.org/pdf/0801.0554).
- Parameters:
- iint
The index of the band power bin to operate on
- in_bundletuple
Tuple of the input model spectrum (as a map-based 3d array) and the X vector that gets populated
- Returns:
- float
Positive log likelihood.
- HL_logl(data_bandpowers, binned_theory_Dls)#
Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed. Usees the Hamimeche-Lewis approximation for low ell spectra (https://arxiv.org/pdf/0801.0554). data_bandpowers argument is not needed, but requested here for a consistent call signature with other likelihoods.
- Parameters:
- data_bandpowersarray, float
Data band powers
- binned_theory_Dlsarray, float
Model spectra
- Returns:
- float
Positive log likelihood.
- bin_model_specs(model_specs)#
Bin model spectra.
- Parameters:
- model_specsarray, float
The spectra to be binned.
- Returns:
- array (float)
The binned spectra.
- blind_bandpowers(seed=None)#
Applies blinding function to bandpowers by multiplying by a random sinusoid function and a slope. It’s important that the same spectrum types get hit by the same blinding function, i.e. e.g. the same for all TT spectra regardless of frequency.
- Parameters:
- seedint, optional
random seed to use
- Returns:
- array
blinded band powers
- array
blinding function
- chi_square(params)#
Returns the chi squared for a given set of theory Dls and nuisance parameter values. Adds beam covariance contribution to covariance if present.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Chi square value
- crop_for_data_selection()#
Adjust all relevant attributes of the likelihood for an arbitrary subselection of the data. This function manipulates not only all the information the likelihood stores on the number of bins, spectrum types etc., but also crops the band powers, covariance, and window functions.
- gaussian_logl(data_bandpowers, binned_theory_Dls)#
Calculate the positive log likelihood, i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) Uses the cholesky decomposition of the covariance for speed.
- Parameters:
- data_bandpowersarray, float
Data band powers
- binned_theory_Dlsarray, float
Model spectra
- Returns:
- float
Positive log likelihood.
- gaussian_logl_beam_and_detcov(data_bandpowers, binned_theory_Dls)#
Calculate the positive log likelihood, including determinant of the covariance i.e.: logl = 0.5 * (x-m) @ C^-1 @ (x-m) + log | cov | Adds beam covariance and re-computes the Cholesky decomposition.
- Parameters:
- data_bandpowersarray, float
Data band powers
- binned_theory_Dlsarray, float
Model spectra
- Returns:
- float
Positive log likelihood.
- generate_crop_mask()#
Generate a crop mask. A crop mask is understood to be a boolean mask to be applied to the uncropped data vector with True entries for bins that should be used and False entries for bins that should be ignored. The method accessed the information on data selection passed by the user interprets it.
If multiple string hints are passed then only bins selected by the most hints are used.
See also: interpret_crop_hint
- Returns:
- arraybool
The mask to be applied to the uncropped long data vector.
- get_ell_helpers()#
Calculate useful variables concerning the ell range and binning.
- Returns:
- arrayfloat
array running over the angular multipoles for one spectrum (2->N_ell_bins_theory+2)
- arrayfloat
2d array giving the angular multipole range for each spectrum in rows
- arrayfloat
long array giving the angular multipole range for each spectrum (concatenated)
- arrayfloat
long array effective containing the effective bin centres (concatenated according to spectra_order).
- get_model_specs(params)#
Returns the theory spectra adjusted for the foreground/nuisance model.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- array (float)
Theory Dls adjusted for the nuisance and foreground model. Covering ell=2-(2+N_ell_bins_theory).
- init_data_model()#
Initialise the data model, i.e. the series of transformations to be applied to the theory spectra. Crucially, this function initialises all the transformation instances and passes them the keywords they need. It steps through the attributes needed by the initialisation of each class and supplies these from attributes of the likelihood as arguments or info from the relevant section of the .yaml file where the transformation. Additionally, it creates the freq_info list of effective frequencies and completes the path of the template_file.
- Returns:
- list of candl.transformations.abstract_base.Transformation
The initialised transformation instances in the intended order ready to be applied to the theory spectra.
- init_priors()#
Initialise the priors. Similar to init_data_model, this function steps through the priors requested by the user and initialises GaussianPrior instances using the passed information.
- Returns:
- list of GaussianPrior
The initialised priors ready to be evaluated.
- interpret_crop_hint(crop_hint, verbose=False)#
Interpret a string hint that indicates a crop mask. The intended format is: “(data) (action)”, where “(data)” specifies which part of the data is selected and “(action)” declares what to do with this selection. Understood options for “(data)” are: * a specific spectrum as specified in the input .yaml file, e.g. “EE 90x90” * spectrum types, e.g. “TT” * frequencies and frequency combinations, e.g. “90” or “150x220” * ell range, e.g. “ell<650” or “ell>1500” Understood options for “(action)” are: * “remove” remove this part * “only” only keep this part, removing all the rest
- Returns:
- arraybool
The mask to be applied to the uncropped long data vector corresponding to this data hint.
- log_like(params)#
Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Negative log likelihood.
- log_like_for_bdp(params, bdp)#
Returns the negative log likelihood for a given set of theory Dls and nuisance parameter values. Shortcut to input band powers is useful to jit up the likelihood function with more flexibility, e.g. when calculating the derivative for different mock data sets.
- Parameters:
- paramsdict
Dictionary containing theory Dls and nuisance parameter values. Required entries: “Dl”, the theory Dls starting from ell=2 and going to ell=2+N_ell_bins_theory. Nuisance parameters for any requested models.
- Returns:
- float
Negative log likelihood.
- offset_lognorm_logl(data_bandpowers, binned_theory_Dls)#
Calculate the positive log likelihood for an offset lognormal distribution i.e.: logl = 0.5 * (x-µ) @ C^-1 @ (x-µ) + ∑ln(x) where x is ln(model-data_offset). See eq. 7 in Prince, Calabrese, Dunkley for details (https://arxiv.org/pdf/2403.00085) Uses the cholesky decomposition of the covariance for speed. Data band powers not needed but maintained for consistent call signature.
- Parameters:
- data_bandpowersarray, float
Data band powers
- binned_theory_Dlsarray, float
Model spectra
- Returns:
- float
Positive log likelihood.
- prior_logl(params)#
Returns the positive log likelihood of the priors.
- Parameters:
- paramsdict
Dictionary containing nuisance parameter values.
- Returns:
- float
Positive log likelihood.
- candl.likelihood.cholesky_decomposition(covariance, N_sims=None, N_bins=None)#
Performs the Ccholesky decomposition of the covariance matrix. Stops the program if unsuccessful.
- Parameters:
- covariancearray (float)
The matrix to be decomposed
- N_simsint, optional
Number of simulations (used for the Hartlap correction).
- N_binsint, optional
Number of bins in the data vector (used for the Hartlap correction).
- Returns:
- array (float)
Cholesky decomposition of the input matrix.
- candl.likelihood.get_matrix_sqrt(in_matrix, inverse=False)#
Matrix square-root function that forces eigenvalues to be positive
- Parameters:
- in_matrix2d array (float)
The matrix to be be operated on
- inversebool, optional
Whether to calculate the inverse, or not
- Returns
- ————–
- array (float)
(Inverse) square root of the original matrix
- candl.likelihood.get_start_stop_ix(N_bins)#
Generates a list of start anad stop indices given the number of bins in each spectrum.
- Parameters:
- N_binsarray (int)
List with an int entry for each spectrum giving the number of bins.
- Returns:
- array (int)
The start indices of each spectrum in a long vector.
- array (int)
The stop indices of each spectrum in a long vector.