EnsembleSampler

class inference.mcmc.EnsembleSampler(posterior: callable, starting_positions: ndarray, alpha: float = 2.0, bounds: Bounds = None, display_progress=True)

EnsembleSampler is an implementation of the affine-invariant ensemble sampler proposed by Goodman & Weare. This algorithm is based on an ‘ensemble’ of points in the parameter space referred to as ‘walkers’. Proposed updates to the position of each walker are generated based on the positions of the other walkers in the ensemble in such a way that the performance of the algorithm is unaffected by affine-transformations of the parameter space.

Parameters
  • posterior (callable) – A callable which takes a numpy.ndarray of the model parameters as its only argument, and returns the posterior log-probability.

  • starting_positions – The starting positions of each walker as a 2D numpy.ndarray with shape (n_walkers, n_parameters).

  • alpha (float) – Parameter controlling the width of the distribution of stretch-move jump distances. A larger value of alpha results in a larger average jump distance, and therefore a lower jump acceptance rate. alpha must be greater than 1, as the jump distance becomes zero when alpha = 1.

  • bounds – An instance of the inference.mcmc.Bounds class, or a sequence of two numpy.ndarray specifying the lower and upper bounds for the parameters in the form (lower_bounds, upper_bounds).

  • display_progress (bool) – If set as True, a message is displayed during sampling showing the current progress and an estimated time until completion.

advance(iterations: int)

Advance the ensemble sampler a chosen number of iterations.

Parameters

iterations (int) – The number of sets of walker positions which will be stored as samples. The total number of samples generated is therefore iterations times the number of walkers.

get_parameter(index: int, burn=0, thin=1) ndarray

Return sample values for a chosen parameter.

Parameters
  • index (int) – Index of the parameter for which samples are to be returned.

  • burn (int) – Number of steps from the start of the chain which are ignored.

  • thin (int) – Sets the factor by which the sample is ‘thinned’ before returning the parameter values. If thin is set to some integer value m, then only every m’th sample is used, and the remainder are ignored.

Returns

Samples for the chosen parameter as a numpy.ndarray.

get_probabilities(burn=0, thin=1) ndarray

Return the log-probability values for each step in the chain.

Parameters
  • burn (int) – Number of steps from the start of the chain which are ignored.

  • thin (int) – Sets the factor by which the sample is ‘thinned’ before returning corresponding log-probabilities. If thin is set to some integer value m, then only every m’th sample is used, and the remainder are ignored.

Returns

Log-probability values as a numpy.ndarray.

get_sample(burn=0, thin=1) ndarray

Return the sample as a 2D numpy.ndarray.

Parameters
  • burn (int) – Number of steps from the start of the chain which are ignored.

  • thin (int) – Sets the factor by which the sample is ‘thinned’ before being returned. If thin is set to some integer value m, then only every m’th sample is used, and the remainder are ignored.

Returns

The sample as a numpy.ndarray of shape (n_samples, n_parameters).

matrix_plot(params: list[int] = None, burn: int = 0, thin: int = 1, **kwargs)

Construct a ‘matrix plot’ of the parameters (or a subset) which displays all 1D and 2D marginal distributions. See the documentation of inference.plotting.matrix_plot for a description of other allowed keyword arguments.

Parameters
  • params – A list of integers specifying the indices of parameters which are to be plotted. If not specified, all parameters are plotted.

  • burn (int) – Sets the number of samples from the beginning of the chain which are ignored when generating the plot.

  • thin (int) – Sets the factor by which the sample is ‘thinned’ before generating the plot. If thin is set to some integer value m, then only every m’th sample is used, and the remainder are ignored.

mode() ndarray

Return the sample with the current highest posterior probability.

Returns

The model parameters corresponding to the highest observed posterior probability as a numpy.ndarray.

plot_diagnostics()

Plot the acceptance rate and log-probability of each walker as a function of the number of iterations.

trace_plot(params: list[int] = None, burn: int = 0, thin: int = 1, **kwargs)

Construct a ‘trace plot’ of the parameters (or a subset) which displays the value of the parameters as a function of step number in the chain. See the documentation of inference.plotting.trace_plot for a description of other allowed keyword arguments.

Parameters
  • params – A list of integers specifying the indices of parameters which are to be plotted. If not specified, all parameters are plotted.

  • burn (int) – Sets the number of samples from the beginning of the chain which are ignored when generating the plot.

  • thin (int) – Sets the factor by which the sample is ‘thinned’ before generating the plot. If thin is set to some integer value m, then only every m’th sample is used, and the remainder are ignored.