API Autodoc Sampler

Contents

API Autodoc Sampler#

class OLE.sampler.base.Sampler(name=None, **kwargs)#

Base sampler class. It connects the theory, the likelihood, and the emulator. It can be also used to interact with the emulator also without giving a specific theory oder likelihood instance.

Key features are: - Initialize the sampler with the theory, the likelihood, and the emulator. - Transform/Retransform the parameters into the normalized eigenspace. - Generate the covariance matrix. - Implement priors. - Interacts with the emulator.

Attributes:
theoryTheory

The theory instance.

likelihood_collectionDictionary of all used likelihoods. Has form of {name: likelihood_instance, name2: likelihood_instance2,…}

The likelihood instance.

emulatorEmulator

The emulator instance.

emulator_settingsdict

The settings for the emulator. Will be passed to the emulator when intialized.

likelihood_collection_settingsdict

The settings for the likelihood. Will be passed to the likelihood when intialized. has the form of {name: likelihood_settings, name2: likelihood_settings2,…}

theory_settingsdict

The settings for the theory. Will be passed to the theory when intialized.

sampling_settingsdict

The settings for the sampler. Will be passed to the sampler.

Methods

initialize(parameters[, ...])

Initialises the sampler.

update_covmat(covmat)

Updates the parameter covariance matrix and computed eigenvalues and eigenvectors in order to transform the parameters into the normalized eigenspace.

calculate_data_covmat(parameters)#

This function computes the hessian of the likelihood with respect to a certain point in parameter space. This can be interpreded as the data covariance matrix. The hessian is calculated by jax.hessian. Note that this can only be done for differentiable likelihoods. Additionally, it tends to be numerically unstable for large parameter spaces.

It is particularly useful for the emulator to normalize the data by the data covariance matrix. For example data points with large uncertainties are less important for the emulator training.

Parameters:
parametersndarray

The parameters for which the hessian is calculated.

Returns:
dict

The data covariance matrix for each observable.

compute_loglike_uncertainty_for_differentiable_likelihood_from_normalized_parameters(parameters, include_error_tolerance=False)#

This function computes the uncertainty of the loglikelihoods for given normalized parameters. It is used in the NUTS sampler to accelerate the initial burn-in phase by minimizing the nuisance parameters for the theory code.

Parameters:
parametersndarray

The normalized parameters for which the uncertainty is computed.

Returns:
float

The uncertainty of the loglikelihood.

compute_logposterior_from_normalized_parameters(parameters)#

This function computes the logposteriors for given normalized parameters. If possible it uses the emulator to speed up the computation, otherwise the theory code is used.

It checks the emulator for its performance and decides whether to use the emulator or the theory code. Eventually, the logposterior is computed and returned.

Parameters:
parametersndarray

The parameters for which the logposterior is computed.

Returns:
float

The logposterior.

compute_logprior(state)#

This function computes the logprior for a given state. It uses the parameter_dict to compute the logprior. If the prior is not defined, it uses a flat prior. Possible priors are: ‘uniform’, ‘gaussian’, ‘jeffreys’, log-normal, etc.

Parameters:
statedict

The state for which the logprior is computed.

Returns:
float

The logprior.

compute_logprior_with_border(state)#

This function computes the logprior for a given state. It uses the parameter_dict to compute the logprior. If the prior is not defined, it uses a flat prior. Possible priors are: ‘uniform’, ‘gaussian’, ‘jeffreys’, log-normal, etc. If we exceed the borders of the prior, we project the parameters to the border and add a penalty term to the logprior.

Parameters:
statedict

The state for which the logprior is computed.

Returns:
float

The logprior.

compute_theory_from_normalized_parameters(parameters)#

This function computes only the theory (and thus the observational data) for given normalized parameters. It is used in the NUTS sampler to accelerate to initial burn-in phase by minimizing the nuisance parameters for the theory code.

Parameters:
parametersndarray

The normalized parameters for which the theory is computed.

Returns:
dict

The state after the theory computation.

compute_total_logposterior_from_normalized_parameters(parameters)#

This function computes the total loglikelihood for given normalized parameters. It sums up the loglikelihoods for each observable. It calls the function compute_logposterior_from_normalized_parameters for each observable.

Parameters:
parametersndarray

The parameters for which the loglikelihood is computed.

Returns:
float

The total loglikelihood.

compute_total_minuslogposterior_from_normalized_parameters(parameters)#

This function computes the total minus logposterior for given normalized parameters.

denormalize_inv_hessematrix(matrix)#

This function denormalizes the inverse hessian matrix according to the eigenspace.

emulate_loglikelihood_from_parameters_differentiable(parameters)#

This function emulates the loglikelihoods for given parameters. Note that it does not consider the posterior. It does not automatically add the state to the emulator. Thus it is differentiable.

Parameters:
parametersndarray

The parameters for which the loglikelihood is computed.

Returns:
float

The loglikelihood.

emulate_logposterior_from_normalized_parameters_differentiable(parameters_norm)#

This function emulates the logposteriors for given parameters. It does not automatically add the state to the emulator. Thus it is differentiable.

Parameters:
parameters_normndarray

The normalized parameters for which the logposterior is computed.

Returns:
float

The logposterior.

emulate_logposterior_from_parameters_differentiable(parameters)#

This function emulates the logposteriors for given parameters. It does not automatically add the state to the emulator. Thus it is differentiable.

Parameters:
parametersndarray

The parameters for which the logposterior is computed.

Returns:
float

The logposterior.

emulate_total_logposterior_from_normalized_parameters_differentiable(parameters)#

This function emulates the total loglikelihood for given normalized parameters.

emulate_total_minusloglikelihood_from_parameters_differentiable(parameters)#

This function emulates the total minus loglikelihood for given parameters.

emulate_total_minuslogposterior_from_normalized_parameters_differentiable(parameters)#

This function emulates the total minus logposterior for given normalized parameters.

estimate_effective_sample_size(chain)#

This function estimates the effective sample size of the chain. It is computed for each parameter.

Parameters:
chainndarray

The current status of the chain.

Returns:
ndarray

The effective sample size for each parameter.

generate_covmat()#

This function generates the covariance matrix either by loading it from a file or by using the proposal lengths. It is called during the initialization of the sampler. Note that the covmat might miss some entries which are to be filled with the proposal lengths.

Returns:
ndarray

The covariance matrix.

get_bounds(normalized=False)#

This function returns the bounds of the parameters. It is used for the minimizer.

get_initial_position(N=1, normalized=False)#

This function returns N initial positions for the sampler according to the parameter informations. It samples from the ‘ref’ values of the parameters.

Parameters:
Nint

The number of initial positions.

normalizedbool

If True, the parameters are returned in the normalized eigenspace.

Returns:
ndarray

The initial positions.

initialize(parameters, likelihood_collection=None, theory=None, emulator=None, emulator_settings={}, likelihood_collection_settings={}, theory_settings={}, sampling_settings={}, **kwargs)#

Initialises the sampler. It takes the parameters, the likelihood, the theory, the emulator, and the settings as input.

Parameters:
parametersdict

A dictionary containing the parameters and their properties such as priors, proposals, etc.

likelihood_collectiondictionary of Likelihoods

The likelihood instance.

theoryTheory

The theory instance.

emulatorEmulator

The emulator instance.

emulator_settingsdict

The settings for the emulator. Will be passed to the emulator when intialized.

likelihood_settingsdict

The settings for the likelihood. Will be passed to the likelihood when intialized.

theory_settingsdict

The settings for the theory. Will be passed to the theory when intialized.

sampling_settingsdict

The settings for the sampler. Will be passed to the sampler.

**kwargsdict

Catch all rubbish.

logposterior_function(parameter_values, local_state)#

This function computes the loglikelihoods for given parameters and a given theory state. It is used in the minimization of the nuisance parameters for the NUTS sampler.

Parameters:
parameter_valuesndarray

The parameters for which the loglikelihood is computed.

local_statedict

The state of the theory code.

Returns:
float

The loglikelihood.

retranform_parameters_from_normalized_eigenspace(parameters)#

This function transforms the parameters back from the normalized eigenspace.

sample_emulate_logposterior_from_normalized_parameters(parameters, N=1, RNGkey=Array([0, 1740496339], dtype=uint32), noise=0.0)#

This function samples the logposteriors for given normalized parameters from the emulator in order to test its performance.

Parameters:
parametersndarray

The normalized parameters for which the logposterior is computed.

Nint

The number of samples.

RNGkeyjax.random.PRNGKey

The random key for the sampling.

noisefloat

The noise level for the sampling.

Returns:
ndarray

The logposteriors for the states.

sample_emulate_logposterior_from_normalized_parameters_differentiable(parameters, N=1, RNGkey=Array([0, 1740496339], dtype=uint32), noise=0.0)#

This function samples the logposteriors for given normalized parameters from the emulator in order to test its performance.

Parameters:
parametersndarray

The normalized parameters for which the logposterior is computed.

Nint

The number of samples.

RNGkeyjax.random.PRNGKey

The random key for the sampling.

noisefloat

The noise level for the sampling.

Returns:
ndarray

The logposteriors for the states.

sample_emulate_logposterior_from_parameters(parameters, N=1, RNGkey=Array([405240883, 203883015], dtype=uint32), noise=0.0)#

This function samples the logposteriors for given parameters from the emulator in order to test its performance.

Parameters:
parametersndarray

The normalized parameters for which the logposterior is computed.

Nint

The number of samples.

RNGkeyjax.random.PRNGKey

The random key for the sampling.

noisefloat

The noise level for the sampling.

Returns:
ndarray

The logposteriors for the states.

sample_emulate_logposterior_from_parameters_differentiable(parameters, N=1, RNGkey=Array([0, 1740496339], dtype=uint32), noise=0.0)#

This function samples the logposteriors for given parameters from the emulator in order to test its performance.

Parameters:
parametersndarray

The normalized parameters for which the logposterior is computed.

Nint

The number of samples.

RNGkeyjax.random.PRNGKey

The random key for the sampling.

noisefloat

The noise level for the sampling.

Returns:
ndarray

The logposteriors for the states.

sample_emulate_total_logposterior_from_normalized_parameters(parameters, noise=0.0)#

This function samples the total loglikelihood for given normalized parameters from the emulator in order to test its performance.

sample_emulate_total_logposterior_from_normalized_parameters_differentiable(parameters, noise=0.0)#

This function samples the total loglikelihood for given normalized parameters from the emulator in order to test its performance.

sample_emulate_total_logposterior_from_parameters(parameters, noise=0.0)#

This function samples the total loglikelihood for given parameters from the emulator in order to test its performance.

sample_emulate_total_logposterior_from_parameters_differentiable(parameters, noise=0.0)#

This function samples the total loglikelihood for given parameters from the emulator in order to test its performance.

stitch_parameters(parameters)#

This function stitches the parameters to the allowed parameter space. If the parameters exceed the borders, they are projected to the border.

Parameters:
parametersdict

The parameters for which the logprior is computed.

Returns:
dict

The stitched parameters.

test_pipeline()#

This function creates a test state from the given parameters. It is used to test the pipeline when initializing the sampler in order to ensure that the emulator, the theory, and the likelihood are working correctly.

transform_parameters_into_normalized_eigenspace(parameters)#

This function transforms the parameters into the normalized eigenspace.

update_covmat(covmat)#

Updates the parameter covariance matrix and computed eigenvalues and eigenvectors in order to transform the parameters into the normalized eigenspace. It also checks for negative eigenvalues and nans or infs in the matrix.

Parameters:
covmatndarray

The new covariance matrix.