candl.tools#
Various tools that interact with the likelihood and its products.
Overview:#
Fisher matrix calculation:
Simple minimiser:
Mock data generation:
Multi-frequency analysis:
Bundling likelihood and theory code together:
Helpers to transform model spectra:
- candl.tools.add_uniform_scatter(start_params, box_width, par_errs, seed=None)#
Add uniform scatter to a point, helpful to offset starting points for minimisers.
- Parameters:
- start_paramsdict
Central parameter values.
- box_widthfloat
Width of the sampled region in units of par_errs.
- par_errsdict
Parameter errors.
- seedint
RNG seed.
- Returns:
- dict
Offset parameters.
- dict
Parameter values delimiting the sampled region.
- candl.tools.conditional_prediction(x, x_bar, sigma, mask=None)#
Linear algebra behind the conditional prediction. Code supplied by Karim Benabed.
- Parameters:
- xarray (float)
The data vector.
- x_bararray (float)
The best-fit model.
- sigmaarray (float)
the covariance matrix
- maskarray (bool)
Mask specifying which parts to excise in generating the conditional prediction.
- Returns:
- float:
Conditional prediction
- float:
Conditional covariance
Notes
Original doc-string: This computes the conditional. x is the data vector, x_bar is the theoretical prediction. Both are 1D vectors and the ordering is whatever you want, provided that it’s the same than the sigma covariance (a 2d array obviously). Mask is an optional 1D array of boolean, same size as x, to select a particular part of the data you want to ignore in the conditional implements eq. 353 of https://www.math.uwaterloo.ca/~hwolkowi/matrixcookbook.pdf. Returns, x_tilde and sigma_tilde.
- candl.tools.generate_mock_data(pars, pars_to_theory_specs, like, N_real, seed=None)#
Generate mock band powers based on the covariance matrix.
- Parameters:
- parsdict
Fiducial parameter values.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- likeLike
Likelihood to take care of transformation and binning.
- N_realint
Number of realisations requested
- seedint
RNG seed.
- Returns:
- array
Binned model Dls (CMB and all transformation of the likelihood)
- list
List of mock band power realisations.
- candl.tools.get_fisher_matrix(pars_to_theory_specs, like, pars, par_order=None, return_par_order=False, return_par_cov=True)#
Calculate the Parameter covariance matrix based on the Fisher matrix using a differentiable theory code and the covariance of the data set. Applies priors on nuisance parameters.
- Parameters:
- pars_to_theory_specsfunc
Differentiable function moving dictionary of parameters to CMB spectra.
- likecandl.Like
Likelihood
- parsdict
Dictionary of parameter values.
- par_orderlist (optional)
Order of parameters requested in returned matrix.
- return_par_orderbool (optional)
return a list of parameters to indicate the order in the matrix.
- return_par_covbool (optional)
Return the parameter covariance matrix, setting to False will return the Fisher matrix (i.e. not inverted).
- Returns:
- array (float)
Parameter covariance matrix.
- list (str)
List of parameter names.
- candl.tools.get_foreground_contributions(like, params, return_type='arr', remove_zeros=True, binning='unbinned')#
Return the foreground contributions. This method is intended to be accessed by the user for visualisation purposes to show the contributions of the different foreground components.
- Parameters:
- paramsdict
Nuisance parameter values.
- return_typestr
One of [“arr”, “dict”, “dict by spec”]. Determines the output format.
- remove_zerosbool
Remove foreground components that are zero for all ells.
- binningstr
One of [“unbinned”, “binned”, “both”]. Whether to return the foreground components for individual ells, binned, or both.
- Returns:
- Depending on return_type:
- “arr”array, float
A (N_ell_bins_theory*N_spectra_total or N_bins_total x N_foregrounds) array containing all the foreground contributions.
- “dict”dict
A dictionary with keys corresponding to the different foreground components and the values are (N_ell_bins_theory*N_spectra_total or N_bins_total) long vectors containing each contribution.
- “dict by spec”dict
A dictionary with keys corresponding to different spectra, each holding another dictionary with foreground names as keys and contributions as values.
- if binning is “both” then the output is wrapped in a dictionary containing “binned” and “unbinned” contributions
- at the lowest level.
- candl.tools.get_params_to_chi_square_func(like, pars_to_theory_specs)#
Thin wrapper bundling together likelihood and theory code to move straight from parameters to chi square.
- Parameters:
- like: candl.Like
Likelihood to be used.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- Returns:
- func:
Function that takes a dictionary of parameter values as input and returns the chi square value (bundling theory code and likelihood together).
- candl.tools.get_params_to_logl_func(like, pars_to_theory_specs)#
Thin wrapper bundling together likelihood and theory code to move straight from parameters to logl.
- Parameters:
- like: candl.Like
Likelihood to be used.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- Returns:
- func:
Function that takes a dictionary of parameter values as input and returns the log likelihood (bundling theory code and likelihood together).
- candl.tools.make_MV_combination(like, data_CMB_only, design_matrix, blinded=False)#
Combine multifrequency band powers into MV estimate. See Appendix C4 here https://arxiv.org/pdf/1507.02704.pdf for details.
- Parameters:
- likecandl.Like
The likelihood.
- data_CMB_onlyarray (float)
The data bandpowers with all transformations undone.
- design_matrixarray (int)
The design matrix.
- blindedbool
Whether operations are carried out in blinded or unblinded space (applies additional factor of the blinding function to the covariance).
- Returns:
- dict
“MV spec” : MV spectrum (array (float)) “MV cov” : MV covariance (array (float)) “MV ell” : effetive multipoles (array (float)) “mix mat” : mixing matrix (array (float))
- candl.tools.make_difference_spectra(spec_str_1, spec_str_2, data_CMB_only, like, blinded=False)#
Generate frequency conditional prediction. See Section 3.6.3 here https://arxiv.org/pdf/1907.12875.pdf for details.
- Parameters:
- spec_str_1str
String identifying the first spectrum. Intended format is (type) (freq1)x(freq2), e.g. “TT 150x150”
- spec_str_2str
String identifying the second spectrum. Intended format is (type) (freq1)x(freq2), e.g. “TT 150x220”
- data_CMB_onlyarray (float)
The data bandpowers with all transformations undone.
- likecandl.Like
The likelihood.
- blindedbool
Whether to operate in blinded space.
- Returns:
- dict
“diff spec” : Difference spectrum (array (float)) “diff cov” : Difference covariance (array (float))
- candl.tools.make_frequency_conditional(spec_str, like, best_fit_model_binned, blinded=False)#
Generate frequency conditional prediction. See Section 3.6.3 here https://arxiv.org/pdf/1907.12875.pdf for details.
- Parameters:
- spec_strstr
String identifying the spectrum. Intended format is (type) (freq1)x(freq2), e.g. “TT 150x150”
- likecandl.Like
The likelihood.
- best_fit_model_binnedarray (float)
The binned best fit spectrum.
- blindedbool
Whether to operate in blinded space or not.
- Returns:
- dict
“cond spec” : Conditional spectrum (array (float)) “cond cov” : Conditional covariance (array (float))
- candl.tools.newton_raphson_minimiser(like_deriv, like_hess, starting_pars, pars_for_min, N_newton=10, step_size=0.2, show_progress=False)#
Take Newton-Raphson steps towards the minimum.
- Parameters:
- like_derivfunc
Derivative of the likelihood.
- like_hessfunc
Hessian of the likelihood.
- starting_parsdict
Start parameters.
- pars_for_minlist
List of parameters to minimise.
- N_newtonint
Number of steps to take.
- step_sizefloat
Scaling for step size.
- show_progressbool
Show tqdm progress bar.
- Returns:
- list
List of all evaluation points
- list
List of parameter covariance matrix at all evaluation points.
- candl.tools.newton_raphson_minimiser_bdp(like_deriv, like_hess, starting_pars, pars_for_min, bdp, N_newton=10, step_size=1, show_progress=False)#
Same as newton_raphson_minimiser() but allows for the input of data band powers. This can be useful if dealing with the same likelihood but lots of mock data realisations to avoid the jitting penalty of new likelihoods for each realisation.
- candl.tools.pars_to_model_specs(like, pars, pars_to_theory_specs)#
Helper to move parameters to transformed model spectra and bin them. For lensing likelihoods only binned model spectra are returned.
- Parameters:
- likecandl.Like
Likelihood to take care of transformation and binning.
- parsdict
Parameters for evaluation.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- Returns:
- array:
Best-fit model unbinned.
- array:
Best-fit model binned.
- candl.tools.pars_to_model_specs_partial_transformation(like, pars, pars_to_theory_specs, i_tr_stop)#
Helper to move parameters to transformed model spectra and crop them. Only applies some of the transformations.
- Parameters:
- likecandl.Like
Likelihood to take care of transformation and binning.
- parsdict
Parameters for evaluation.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- i_tr_stopint
Index of the transformation at which to stop
- Returns:
- array:
Model unbinned.
- array:
Model binned.
- candl.tools.test_statistic_MV_consistency(like, MV_dict, data_CMB_only, design_matrix, blinded=False)#
Test consistency by comparing multifrequency band powers to MV combination
- Parameters:
- likecandl.Like
The likelihood.
- MV_dictdict
Dictionary containing information about MV combination (intended is output from make_MV_combination).
- data_CMB_onlyarray (float)
The data bandpowers with all transformations undone.
- design_matrixarray (int)
The design matrix.
- blindedbool
Whether operations are carried out in blinded or unblinded space (applies additional factor of the blinding function to the covariance).
- Returns:
- float:
chisq
- float:
PTE
- candl.tools.test_statistic_conditional(spec_str, cond_dict, like, blinded=False)#
Test conditional prediction against measured band powers.
- Parameters:
- spec_strstr
String identifying the spectrum. Intended format is (type) (freq1)x(freq2), e.g. “TT 150x150”
- cond_dictdict
Dictionary containing information about the conditional prediction (intended is output from make_frequency_conditional).
- likecandl.Like
The likelihood.
- blindedbool
Whether to operate on blinded band powers or not.
- Returns:
- float:
chisq
- float:
PTE
- candl.tools.test_statistic_difference(diff_dict)#
Test difference spectrum against zero.
- Parameters:
- diff_dictdict
Dictionary containing information about the difference spectrum (intended is output from make_difference_spectra).
- Returns:
- float:
chisq
- float:
PTE
- candl.tools.undo_transformations(like, pars, pars_to_theory_specs)#
Undo (best-fit) transformations from data vector. The order of the operations in the transformation matters - they do not commute. Hence, when undoing them, we must be careful to proceed in the correct order. The attribute ‘operation_hint’ of Transformation is used in order, if this attribute is not properly set this function will not return the correct results. In general, we need to undo calibration first, and then subtract the additive foregrounds. Special care needs to be taken for transformations that take Dls as their input too (e.g. super-sample lensing).
- Parameters:
- likecandl.Like
Likelihood to take care of transformation and binning.
- parsdict
Parameters for evaluation.
- pars_to_theory_specs: func
function moving dictionary of cosmological parameters to dictionary of CMB Dls
- Returns:
- array:
Data band power with all transformations undone.