Parallel Tempering

The tempering is controlled from eryn.moves.tempering.TemperatureControl.

class eryn.moves.tempering.TemperatureControl(effective_ndim, nwalkers, ntemps=1, betas=None, Tmax=None, adaptive=True, adaptation_lag=10000, adaptation_time=100, stop_adaptation=-1, permute=True, skip_swap_supp_names=[])

Bases: object

Controls the temperature ladder and operations in the sampler.

All of the tempering features within Eryn are controlled from this class. This includes the evaluation of the tempered posterior, swapping between temperatures, and the adaptation of the temperatures over time. The adaptive tempering model can be found in the Eryn paper as well as the paper for ptemcee, which acted as a basis for the code below.

Parameters:
  • effective_ndim (int) – Effective dimension used to determine temperatures if betas not given.

  • nwalkers (int) – Number of walkers in the sampler. Must maintain proper order of branches.

  • ntemps (int, optional) – Number of temperatures. If this is provided rather than betas, make_ladder() will be used to generate the temperature ladder. (default: 1)

  • betas (np.ndarray[ntemps], optional) – If provided, will use as the array of inverse temperatures. (default: None).

  • Tmax (float, optional) – If provided and betas is not provided, this will be included with ntemps when determing the temperature ladder with make_ladder(). See that functions docs for more information. (default: None)

  • adaptive (bool, optional) – If True, adapt the temperature ladder during sampling. (default: True).

  • adaptation_lag (int, optional) – lag parameter from arXiv:1501.05823. adaptation_lag must be much greater than adapation_time. (default: 10000)

  • adaptation_time (int, optional) –

    initial amplitude of adjustments from arXiv:1501.05823. adaptation_lag must be much greater than adapation_time. (default: 100)

  • stop_adaptation (int, optional) – If stop_adaptation > 0, the adapating will stop after stop_adaption steps. The number of steps is counted as the number times adaptation has happened which is generally once per sampler iteration. For example, if you only want to adapt temperatures during burn-in, you set stop_adaption = burn. This can become complicated when using the repeating proposal options, so the user must be careful and verify constant temperatures in the backend. (default: -1)

  • permute (bool, optional) – If True, permute the walkers in each temperature during swaps. (default: True)

  • skip_swap_supp_names (list, optional) – List of strings that indicate supplemental keys that are not to be swapped. (default: [])

compute_log_posterior_tempered(logl, logp, betas=None)

Compute the log of the tempered posterior

Parameters:
  • logl (np.ndarray) – Log of the Likelihood. Can be 1D or 2D array. If 2D, must have shape (ntemps, nwalkers). If 1D, betas must be provided with the same shape.

  • logp (np.ndarray) – Log of the Prior. Can be 1D or 2D array. If 2D, must have shape (ntemps, nwalkers). If 1D, betas must be provided with the same shape.

  • betas (np.ndarray[ntemps]) – If provided, inverse temperatures as 1D array. If not provided, it will use self.betas. (default: None)

Returns:

Log of the temperated posterior.

Return type:

np.ndarray

Raises:

AssertionError – Inputs are incorrectly shaped.

tempered_likelihood(logl, betas=None)

Compute the log of the tempered Likelihood

From ptemcee: “This is usually a mundane multiplication, except for the special case where beta == 0 and we’re outside the likelihood support. Here, we find a singularity that demands more careful attention; we allow the likelihood to dominate the temperature, since wandering outside the likelihood support causes a discontinuity.”

Parameters:
  • logl (np.ndarray) – Log of the Likelihood. Can be 1D or 2D array. If 2D, must have shape (ntemps, nwalkers). If 1D, betas must be provided with the same shape.

  • betas (np.ndarray[ntemps]) – If provided, inverse temperatures as 1D array. If not provided, it will use self.betas. (default: None)

Returns:

Log of the temperated Likelihood.

Return type:

np.ndarray

Raises:

ValueError – betas not provided if needed.

temperature_swaps(x, logP, logl, logp, inds=None, blobs=None, supps=None, branch_supps=None)

Perform parallel-tempering temperature swaps

This function performs the swapping between neighboring temperatures. It cascades from high temperature down to low temperature.

Parameters:
  • x (dict) – Dictionary with keys as branch names and values as coordinate arrays.

  • logP (np.ndarray[ntemps, nwalkers]) – Log of the posterior probability.

  • logl (np.ndarray[ntemps, nwalkers]) – Log of the Likelihood.

  • logp (np.ndarray[ntemps, nwalkers]) – Log of the prior probability.

  • inds (dict, optional) – Dictionary with keys as branch names and values as the index arrays indicating which leaves are used. (default: None)

  • blobs (object, optional) – Blobs associated with each walker. (default: None)

  • supps (object, optional) – eryn.state.BranchSupplemental object. (default: None)

  • branch_supps (dict, optional) – Dictionary with keys as branch names and values as eryn.state.BranchSupplemental objects for each branch (can be None for some branches). (default: None)

Returns:

All of the information that was input now swapped (output in the same order as input).

Return type:

tuple

temper_comps(state, adapt=True)

Perfrom temperature-related operations on a state.

This includes making swaps and then adapting the temperatures for the next round.

Parameters:
  • state (object) – Filled State object.

  • adapt (bool, optional) – If True, swaps are to be performed, but no adaptation is made. In this case, self.time does not increase by 1. (default: True)

Returns:

State object after swaps.

Return type:

eryn.state.State

eryn.moves.tempering.make_ladder(ndim, ntemps=None, Tmax=None)

Returns a ladder of \(\beta \equiv 1/T\) under a geometric spacing that is determined by the arguments ntemps and Tmax. The temperature selection algorithm works as follows: Ideally, Tmax should be specified such that the tempered posterior looks like the prior at this temperature. If using adaptive parallel tempering, per arXiv:1501.05823, choosing Tmax = inf is a safe bet, so long as ntemps is also specified.

This function is originally from ptemcee github.com/willvousden/ptemcee.

Temperatures are chosen according to the following algorithm: * If neither ntemps nor Tmax is specified, raise an exception (insufficient information). * If ntemps is specified but not Tmax, return a ladder spaced so that a Gaussian posterior would have a 25% temperature swap acceptance ratio. * If Tmax is specified but not ntemps: * If Tmax = inf, raise an exception (insufficient information). * Else, space chains geometrically as above (for 25% acceptance) until Tmax is reached. * If Tmax and ntemps are specified: * If Tmax = inf, place one chain at inf and ntemps-1 in a 25% geometric spacing. * Else, use the unique geometric spacing defined by ntemps and Tmax.`

Parameters:
  • ndim (int) – The number of dimensions in the parameter space.

  • ntemps (int, optional) – If set, the number of temperatures to generate.

  • Tmax (float, optional) – If set, the maximum temperature for the ladder.

Returns:

Output inverse temperature (beta) array.

Return type:

np.ndarray[ntemps]

Raises:

ValueError – Improper inputs.