pybop.samplers.base_sampler#

Classes#

BaseSampler

Base class for Monte Carlo samplers.

SamplerOptions

Base options for the sampler.

SamplingResult

Stores the result of the sampling.

Module Contents#

class pybop.samplers.base_sampler.BaseSampler(log_pdf: pybop.problems.log_pdf.LogPDF, options: SamplerOptions | None = None)[source]#

Base class for Monte Carlo samplers.

Parameters:
  • log_pdf (pybop.LogPDF) – The negative unnormalised posterior distribution.

  • options (SamplerOptions, optional) – Options for the sampler. If None, default options are used.

_validate_covariance_matrix() None[source]#

Check or create the initial covariance matrix.

static default_options() SamplerOptions[source]#

Get the default options for the sampler.

abstractmethod run() SamplingResult[source]#

Sample from the posterior distribution.

Returns:

Samples from the posterior distribution.

Return type:

np.ndarray

set_initial_phase_iterations(iterations: int = 250)[source]#

Set the number of iterations for the initial phase of the sampler.

set_max_iterations(iterations: int = 500)[source]#

Set the maximum number of iterations for the sampler.

set_warm_up_iterations(iterations: int = 250)[source]#

Set the number of warm up iterations for the sampler.

_cov0#
_log_pdf#
_logger = None#
_mean0#
_n_parameters#
_options#
property cov0: numpy.ndarray#
property log_pdf: pybop.problems.log_pdf.LogPDF#
property logger: pybop._logging.Logger | None#
property mean0: numpy.ndarray#
property options: SamplerOptions#
class pybop.samplers.base_sampler.SamplerOptions[source]#

Base options for the sampler.

n_chains#

The number of chains to concurrently sample from.

Type:

int

validate()[source]#

Validate the options.

Raises:

ValueError – If the options are invalid.

n_chains: int = 1#
class pybop.samplers.base_sampler.SamplingResult(sampler: BaseSampler, time: float, chains: numpy.ndarray, method_name: str | None = None, message: str | None = None)[source]#

Bases: pybop._result.Result

Stores the result of the sampling.

sampler#

The sampler used to generate the results.

Type:

pybop.BaseSampler

time#

The time taken.

Type:

float

chains#

An array containing the samples from the posterior distribution, or None.

Type:

np.ndarray, optional

method_name#

The name of the sampler.

Type:

str

message#

The reason for stopping given by the sampler.

Type:

str

_autocorrelate_negative(autocorrelation)[source]#

Returns the index of the first negative entry in autocorrelation, or len(autocorrelation) if a negative entry is not found.

_calculate_statistics(fun, attr_name, *args, **kwargs)[source]#

Calculate statistics from callable fun.

autocorrelation(x: numpy.ndarray) numpy.ndarray[source]#

Computes the autocorrelation (Pearson correlation coefficient) of a numpy array representing samples.

effective_sample_size(mixed_chains=False)[source]#

Computes the effective sample size (ESS) for each parameter in each chain.

Parameters:

mixed_chains (bool, optional) – If True, the ESS is computed for all samplers mixed into a single chain. Defaults to False.

Returns:

A list of effective sample sizes for each parameter in each chain, or for the mixed chain if mixed_chains is True.

Return type:

list

Raises:

ValueError – If there are fewer than two samples in the data.

get_summary_statistics(significant_digits: int = 4)[source]#

Calculate summary statistics for the posterior samples.

Parameters:

significant_digits (int) – Number of significant digits to display for summary statistics.

Returns:

Summary statistics including mean, median, standard deviation, and 95% credible interval.

Return type:

dict

plot_chains(**kwargs)[source]#

Plot posterior distributions for each chain.

plot_posterior(**kwargs)[source]#

Plot the summed posterior distribution across chains.

plot_trace(**kwargs)[source]#

Plot trace plots for the posterior samples.

rhat()[source]#

Computes the Gelman-Rubin statistic which diagnoses MCMC convergence. For well-mixed and stationary chains R-hat will be close to one, otherwise it is higher.

signif(x, p: int)[source]#

Rounds array x to p significant digits.

summary_table(**kwargs)[source]#

Display summary statistics in a table.

all_samples#
chains#
n_parameters#