MCMC Utility Functions

mcmcUtils.py - Markov Chain Monte Carlo Utility Functions

MCMC utility functions for validating emcee MCMC runs within approxposterior.

approxposterior.mcmcUtils.validateMCMCKwargs(ap, samplerKwargs, mcmcKwargs, verbose=False)[source]

Validates emcee.EnsembleSampler parameters/kwargs.

Parameters
  • ap (approxposterior.ApproxPosterior) – Initialized ApproxPosterior object

  • samplerKwargs (dict) – dictionary containing parameters intended for emcee.EnsembleSampler object

  • mcmcKwargs (dict) – dictionary containing parameters intended for emcee.EnsembleSampler.run_mcmc/.sample object

  • verbose (bool, optional) – verboisty level. Defaults to False (no output)

Returns

  • samplerKwargs (dict) – Sanitized dictionary containing parameters intended for emcee.EnsembleSampler object

  • mcmcKwargs (dict) – Sanitized dictionary containing parameters intended for emcee.EnsembleSampler.run_mcmc/.sample object

approxposterior.mcmcUtils.batchMeansMCSE(samples, bins=None, fn=None)[source]

Estimate the Monte Carlo Standard Error of MCMC samples using the non-overlapping batch means methods. See Flegal, Haran, & Jones (2008) for more info: https://arxiv.org/pdf/math/0703746.pdf

Parameters
  • samples (array) – nsamples x ndim array of MCMC samples

  • bins (int, optional) – Number of bins. Defaults to int(sqrt(len(samples)))

  • fn (function, optional) – Function used to compute posterior summary statistic on each chunk. Defaults to None to compute the simple expected value, aka the mean.

Returns

RMSE – RMSE for each dimension of the chain

Return type

float/array

approxposterior.mcmcUtils.estimateBurnin(sampler, estBurnin=True, thinChains=True, verbose=False)[source]

Estimate the integrated autocorrelation length on the MCMC chain associated with an emcee sampler object. With the integrated autocorrelation length, we can then estimate the burn-in length for the MCMC chain. This procedure follows the example outlined here: https://emcee.readthedocs.io/en/stable/tutorials/autocorr/

Parameters
  • sampler (emcee.EnsembleSampler) – emcee MCMC sampler object/backend handler, given a complete chain

  • estBurnin (bool, optional) – Estimate burn-in time using integrated autocorrelation time heuristic. Defaults to True. In general, we recommend users inspect the chains and calculate the burnin after the fact to ensure convergence, but this function works pretty well.

  • thinChains (bool, optional) – Whether or not to thin chains. Useful if running long chains. Defaults to True. If true, estimates a thin cadence via int(0.5*np.min(tau)) where tau is the intergrated autocorrelation time.

  • verbose (bool, optional) – Output all the diagnostics? Defaults to False.

Returns

  • iburn (int) – burn-in index estimate. If estBurnin == False, returns 0.

  • ithin (int) – thin cadence estimate. If thinChains == False, returns 1.