hierarc.Likelihood.KDELikelihood package

Submodules

hierarc.Likelihood.KDELikelihood.chain module

class hierarc.Likelihood.KDELikelihood.chain.Chain(kw, probe, params, default_weights, cosmology, loglsamples=None, rescale=True)[source]

Bases: object

Chain class to have a convenient way to manipulate posteriors distributions of some experiments.

create_param(param_key)[source]

Add a new parameter to the Chain.

Parameters:

param_key – (str). Parameter name.

fill_default(param, default_val, nsamples=None, verbose=False)[source]

Fill an empty default param with a default value.

Parameters:
  • param – (string) Name of the parameter to fill with default value

  • default_val – (float). Default value.

  • nsamples – (int). Number of samples in the Chain. If None, it will take the same number of samples as for the other parameters

  • verbose – (bool).

fill_default_array(param, default_array, verbose=False)[source]

Fill an empty default param with a default array.

Parameters:
  • param – (str). Name of the parameter

  • default_array – (numpy array). Must have the same dimension as the samples.

  • verbose – (bool).

list_params()[source]

List the cosmo parameters that are not empty :return: List of parameter name.

list_weights()[source]

List the existing weights :return: Array of weights.

rescale_from_unity(verbose=False)[source]

Rescale all parameter chains to their original values.

Parameters:

verbose – (bool).

rescale_to_unity(verbose=False)[source]

Rescale all parameter chains between 0 and 1.

Parameters:

verbose – (bool).

hierarc.Likelihood.KDELikelihood.chain.import_Planck_chain(datapath, kw, probe, params, cosmology, rescale=True)[source]

Special function to parse Planck files. Return a Chain object. For keywords in the kw parameter, see https://wiki.cosmos.esa.int/planck-legacy-archive/index.php/Cosmological_Parameters

Parameters:
  • datapath – (str). Path to the Planck chain

  • kw – (str). Planck base cosmology keyword. For example, “base” or “base_omegak”.

  • probe – (str). Planck probe combination. For example, “plikHM_TTTEEE_lowl_lowE” for default Planck results

  • params – (list). List of cosmological parameters. [“h0”, “om”] for FLCDM.

  • cosmology – (str). Astropy cosmology

  • rescale – (bool). Rescale the chains between 0 and 1 for all parameters. This is absolutely necessary if you want to evaluate a KDE on these chains.

Returns:

Chain object.

hierarc.Likelihood.KDELikelihood.chain.rescale_vector_from_unity(vector, rescale_dic, keys)[source]

Restore the original scaling of the samples, given the value in rescale_dic.

Parameters:
  • vector – (numpy array). Vector to be rescaled.

  • rescale_dic – (dictionnary). Contains the min and max value for each parameters.

  • keys – (list). Contain the name of the parameter to be rescaled. all keys must be in the rescale_dic.

Returns:

hierarc.Likelihood.KDELikelihood.chain.rescale_vector_to_unity(vector, rescale_dic, keys)[source]

Rescale a vector accroding to the min adn max value provided in rescale_dic.

Parameters:
  • vector – (numpy array). Vector to be rescaled.

  • rescale_dic – (dictionnary). Contains the min and max value for each parameters.

  • keys – Contain the name of the parameter to be rescaled. all keys must be in the rescale_dic.

Returns:

hierarc.Likelihood.KDELikelihood.kde_likelihood module

class hierarc.Likelihood.KDELikelihood.kde_likelihood.KDELikelihood(chain, likelihood_type='kde_hist_nd', weight_type='default', kde_kernel='gaussian', bandwidth=0.01, nbins_hist=30)[source]

Bases: object

KDE likelihood class. Provide a Chain object that will be used as your likelihood. __warning:: This class is not fully tested for more than 5 free parameters. Use at your own risk.

__note:: Parameters need to be rescaled between 0 and 1 for this to work optimally. Think about rescaling your Chain with the “rescale = True” option.

init_kernel_full(kde_kernel, bandwidth)[source]
Parameters:
  • kde_kernel – kde_kernel: (str). Kernel type to be passed to scikit-learn. Default : ‘gaussian’.

  • bandwidth – (float). Bandwidth of the kernel. Default : 0.01. Works well if parameters are rescaled between 0 and 1.

Returns:

scikit-learn KernelDensity

init_kernel_kdelikelihood_hist_nd(kde_kernel, bandwidth, nbins_hist)[source]

Evaluates the likelihood from a Kernel Density Estimator. The KDE is constructed using a binned version of the full samples. Greatly improves speed at the cost of a (tiny) loss in precision.

__warning:: you should adjust bandwidth and nbins_hist to the spacing and size of your samples chain!

__note:: nbins_hist refer to the number of bins per dimension. Hence, the final number of bins will be nbins_hist**n

Parameters:
  • kde_kernel – kde_kernel: (str). Kernel type to be passed to scikit-learn. Default : ‘gaussian’.

  • bandwidth – (float). Bandwidth of the kernel. Default : 0.01. Works well if parameters are rescaled between 0 and 1.

  • nbins_hist – (float). Number of bins to use before fitting KDE. Used only if likelihood_type = ‘kde_hist_nd’.

Returns:

scikit-learn KernelDensity

init_loglikelihood()[source]

Initialisation of the KDE, depending on loglikelihood_type.

kdelikelihood()[source]

Evaluates the likelihood. Return a function.

kdelikelihood_samples(samples)[source]

Evaluates the likelihood on an array. Return an array.

Module contents