openscm_calibration.emcee_utils#
Helpers for emcee
Classes:
| Name | Description |
|---|---|
AutoCorrelationInfo |
Information about auto-correlation within an MCMC chain |
ChainProgressInfo |
Information about the progress of MCMC chains |
Functions:
| Name | Description |
|---|---|
get_acceptance_fractions |
Get acceptance fraction in each chain of an MCMC ensemble of chains |
get_autocorrelation_info |
Get info about autocorrelation in chains |
get_labelled_chain_data |
Get labelled chain data |
get_neg_log_prior |
Get a function for calculating the negative log prior |
get_start |
Get starting point for emcee sampling |
neg_log_info |
Get negative log probability and likelihood information |
AutoCorrelationInfo #
Information about auto-correlation within an MCMC chain
Methods:
| Name | Description |
|---|---|
any_non_nan_tau |
Check if any of the tau information is not nan |
Attributes:
| Name | Type | Description |
|---|---|---|
converged |
NDArray[bool]
|
Whether, based on |
convergence_ratio |
float
|
Convergence ratio used to assess convergence. |
steps_post_burnin |
int
|
The number of steps in the chains post burn-in. |
tau |
NDArray[float64]
|
Auto-correlation for each parameter |
Source code in src/openscm_calibration/emcee_utils.py
converged
instance-attribute
#
Whether, based on convergence_ratio, the chains for each parameter have converged
The first value is whether the chain for the first parameter has converged, the second value is whether the chain for the second parameter has converged, ... nth value is whether the chain for the nth parameter has converged, ...
convergence_ratio
instance-attribute
#
convergence_ratio: float
Convergence ratio used to assess convergence.
For details, see
get_autocorrelation_info.
steps_post_burnin
instance-attribute
#
steps_post_burnin: int
The number of steps in the chains post burn-in.
tau
instance-attribute
#
Auto-correlation for each parameter
I.e. the first value is the auto-correlation for the first parameter, second value is the auto-correlation for the second parameter, ... nth value is the auto-correlation for the nth parameter, ...
ChainProgressInfo #
Information about the progress of MCMC chains
Attributes:
| Name | Type | Description |
|---|---|---|
acceptance_fraction |
float
|
Acceptance fraction of proposed steps |
steps |
int
|
Number of steps in the chain |
steps_post_burnin |
int
|
Number of steps after the chain's burn-in period |
Source code in src/openscm_calibration/emcee_utils.py
get_acceptance_fractions #
Get acceptance fraction in each chain of an MCMC ensemble of chains
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chains
|
NDArray[floating[Any] | integer[Any]]
|
Chains. We expected that the the axes are ["step", "chain", "parameter"] |
required |
Returns:
| Type | Description |
|---|---|
Acceptance fraction in each chain
|
|
Source code in src/openscm_calibration/emcee_utils.py
get_autocorrelation_info #
get_autocorrelation_info(
inp: Backend | EnsembleSampler,
burnin: int,
thin: int = 1,
autocorr_tol: int = 0,
convergence_ratio: float = 50,
) -> AutoCorrelationInfo
Get info about autocorrelation in chains
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Backend | EnsembleSampler
|
Object of which to check autocorrelation |
required |
burnin
|
int
|
Number of iterations to treat as burn-in |
required |
thin
|
int
|
Thinning to apply to the chains. Emcee handles this such that the returned output is in steps, not thinned steps. |
1
|
autocorr_tol
|
int
|
Tolerance for auto-correlation calculations. Set to zero to force calculation to never fail |
0
|
convergence_ratio
|
float
|
Convergence ratio to apply when checking for convergence If the number of iterations (excluding burn-in)
is greater than |
50
|
Returns:
| Type | Description |
|---|---|
AutoCorrelationInfo
|
Results of calculation |
Source code in src/openscm_calibration/emcee_utils.py
get_labelled_chain_data #
get_labelled_chain_data(
inp: Backend | EnsembleSampler,
parameter_order: tuple[str, ...],
neg_log_likelihood_name: str | None = None,
burnin: int = 0,
thin: int = 0,
) -> dict[str, NDArray[floating[Any] | integer[Any]]]
Get labelled chain data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
inp
|
Backend | EnsembleSampler
|
Object from which to plot the state |
required |
parameter_order
|
tuple[str, ...]
|
Order of model parameters. This must match the order used by |
required |
neg_log_likelihood_name
|
str | None
|
Name to use for the negative log likelihood data. If not provided, negative log likelihood information is not returned. |
None
|
burnin
|
int
|
Number of iterations to treat as burn in |
0
|
thin
|
int
|
Thinning to use when sampling the chains |
0
|
Returns:
| Type | Description |
|---|---|
dict[str, NDArray[floating[Any] | integer[Any]]]
|
Chain data, labelled with parameter names
and, if requested, |
Source code in src/openscm_calibration/emcee_utils.py
get_neg_log_prior #
get_neg_log_prior(
parameter_order: ParameterOrder,
kind: Literal["uniform"] = "uniform",
) -> SupportsLikelihoodCalculation
Get a function for calculating the negative log prior
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameter_order
|
ParameterOrder
|
The parameters being calibrated |
required |
kind
|
Literal['uniform']
|
The kind of log prior to create. Options:
|
'uniform'
|
Returns:
| Type | Description |
|---|---|
SupportsLikelihoodCalculation
|
Function that, given a parameter vector, returns the negative log prior |
Source code in src/openscm_calibration/emcee_utils.py
get_start #
Get starting point for emcee sampling
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sampler
|
EnsembleSampler
|
Sampler which will do the sampling |
required |
start
|
NDArray[float64] | None
|
Starting point to use. This is only used if the sampler has not already performed some iterations. |
None
|
Returns:
| Type | Description |
|---|---|
NDArray[float64]
|
Starting point for the sampling |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Warns:
| Type | Description |
|---|---|
UserWarning
|
In this case, we use the sampler's last iteration
and ignore the value of |
Source code in src/openscm_calibration/emcee_utils.py
neg_log_info #
neg_log_info(
x: NDArray[float64],
neg_log_prior: Callable[[NDArray[float64]], float],
model_runner: SupportsModelRun[DataContainer],
negative_log_likelihood_calculator: SupportsNegativeLogLikelihoodCalculation[
DataContainer
],
) -> tuple[float, float, float | None]
Get negative log probability and likelihood information
Specifically, negative log probability, log prior and log likelihood for a given parameter vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
NDArray[float64]
|
Parameter vector. Be careful with the order and units of parameters. No checks of parameter order or units are performed in this function. |
required |
neg_log_prior
|
Callable[[NDArray[float64]], float]
|
Function that calculates the negative log prior for a given value of |
required |
model_runner
|
SupportsModelRun[DataContainer]
|
Runner of the model for a given value of |
required |
negative_log_likelihood_calculator
|
SupportsNegativeLogLikelihoodCalculation[DataContainer]
|
Calculator of negative log likelihood based on the outputs of the model run |
required |
Returns:
| Type | Description |
|---|---|
tuple[float, float, float | None]
|
Negative log probability of If the negative log probability of If the log likelihood calculation raises a |