mcmc

PBjam uses MC sampling at several points during the peakbagging process. Samplers added to PBjam should be called from this module.

class pbjam.mcmc.mcmc(start, likelihood, prior, nwalkers=50)[source]

Class for MCMC sampling using `emcee’

Uses `emcee’ to sample the parameterspace of a provided spectrum model.

Parameters:
  • start (ndarray) – An array of starting position of the parameters.
  • likelihood (function) – Function to call that returns the log likelihood when passed the parameters.
  • prior (function) – Function to call that returns the log prior probability when passed the parameters.
  • nwalkers (int, optional) – The number of walkers that `emcee’ will use.
ndim

Number of model parameters (length of start input).

Type:int
sampler

A `emcee’ sampler class instance initialized with the number of walkers, number of parameters, and the posterior comprised of the likelihood and prior input functions.

Type:emcee.EnsembleSampler class instance
chain

Sampled locations in parameters space of each walker at each step.

Type:ndarray
lnlike

Likelihood at the sampled locations in parameter space.

Type:ndarray
flatchain

Flattened chain.

Type:ndarray
flatlnlike

Flattened likelihoods

Type:ndarray
acceptance

Acceptance fraction at each step.

Type:ndarray
fold(pos, accept_lim=0.2, spread=0.1)[source]

Fold low acceptance walkers into main distribution

At the end of the burn-in, some walkers appear stuck with low acceptance fraction. These can be selected using a threshold, and folded back into the main distribution, estimated based on the median of the walkers with an acceptance fraction above the threshold.

The stuck walkers are redistributed with multivariate Gaussian, with mean equal to the median of the high acceptance walkers, and a standard deviation equal to the median absolute deviation of these.

Parameters:
  • pos (ndarray, optional) – The positions of the walkers after the burn-in phase.
  • accept_lim (float, optional) – The value below which walkers will be labelled as bad and/or hence stuck.
  • spread (float, optional) – Factor by which to scatter the folded walkers.
Returns:

pos – The positions of the walkers after the low accepatance walkers have been folded into high acceptance distribution.

Return type:

ndarray

logpost(p)[source]

Evaluate the likelihood and prior

Returns the log posterior probability given parameters p. Evaluates first the prior function and then the likelihood function. In the event that the prior returns -inf, the function exits.

Parameters:p (list) – Fit parameters
Returns:log_posterior – log posterior of the model given parameters p and the observed quantities.
Return type:float
stationarity(nfactor=20)[source]

Tests to see if stationarity metrics are satified.

Uses the autocorrelation timescale to estimate whether the MC chains have reached a stationary state.

Parameters:nfactor (int, optional) – Factor used to test stationary. If the number of steps in the MC chain exceeds nfactor*tau, where tau is the autocorrelation timescale of the chain, the sampling is considered stationary.
class pbjam.mcmc.nested(names, bounds, likelihood, prior, path)[source]

Runs CPnest to performed nested sampling from

log P(theta | D) ~ likelihood + prior

Note both likelihood and prior are in natural log.

names

A list of names of the model parameters

Type:list, strings
bounds

The bounds of the model parameters as [(0, 10), (-1, 1), …]

Type:list of tuples
likelihood

Function that will return the log likelihood when called as likelihood(params)

Type:func
prior

Function that will return the log prior when called as prior(params)

Type:func
log_likelihood(param)[source]

Wrapper for log likelihood

log_prior(p)[source]

Wrapper for log prior