Skip to main content

Adaptive Parallel Tempering MCMC Ensemble Sampler

Project description

reddemcee

png

Adaptive Parallel Tempering MCMC Ensemble Sampler, made for the exoplanet finder algorithm EMPEROR. This sampler works as a stand-alone program, so the community might find it useful.

Overview

reddemcee is an Adaptive Parallel Tempering MCMC implementation based on the excellent emcee code, and a modified version of the Vousden et al. implementation.

It's coded in such a way that minimal differences in input are required compared to emcee (v. 3.1.3). Make sure to check reddemcee's documentation !

Dependencies

This code makes use of:

tqdm is used to display progress bars on the terminal. emcee is used for calculating the autocorrelation times.

Installation

In the console simply type:

pip install reddemcee

Usage

For a complete tutorial please refer to the documentation's tutorial or the test file in the tests folder.

import numpy as np
import reddemcee

def log_like(x, ivar):
    return -0.5 * np.sum(ivar * x ** 2)

def log_prior(x):
    return 0.0

ndim = 2
ntemps, nwalkers, nsweeps, nsteps = 5, 50, 100, 2

ivar = 1. / np.random.rand(ndim)
p0 = np.random.randn(10, nwalkers, ndim)
sampler = reddemcee.PTSampler(nwalkers,
                             ndim,
                             log_like,
                             log_prior,
                             ntemps=ntemps,
                             loglargs=[ivar],
                             )
                             
sampler.run_mcmc(p0, nsweeps, nsteps)  # starting coords, nsweeps, nsteps

Configuring the Sampler

When setting up PTSampler, you can use the arguments:

Arg Type Description
ntemps int The number of temperatures. If None, determined from betas.
betas list The inverse temperatures of the parallel chains.
pool Pool A pool object for parallel processing.
loglargs list Positional arguments for the log-likelihood function.
loglkwargs list Keyword arguments for the log-likelihood function.
logpargs list Positional arguments for the log-prior function.
logpkwargs list Keyword arguments for the log-prior function.
backend Backend A backend object for storing the chain.
smd_history bool Whether to store swap mean distance history.
tsw_history bool Whether to store temperature swap history.
adapt_tau float Halflife of adaptation hyper-parameter.
adapt_nu float Rate of adaptation hyper-paramter.
adapt_mode 0-4 Mode of adaptation.

The adaptation modes try to equalise the following quantity:

Mode Description
0 Temperature Swap Rate
1 Swap Mean Distance
2 Specific Heat
3 dE/sig
4 Thermodynamic Length

Additional Functions

Additional functions on the sampler include:

Function Description
thermodynamic_integration_classic Calculates evidence using trapezoidal rule.
thermodynamic_integration Interpolates, uses Monte-Carlo integration.
stepping_stones Stepping Stones method
hybrid_evidence Hybrid method
get_autocorr_time Auto-correlation time.
get_betas Returns beta history
get_chain Returns chain
get_logprob Returns log posteriors

All these functions accept as arguments:

Arg Description
flat Flatten the walkers.
thin Take one every thin samples.
discard Drop the first discard steps in samples.

For example, the previous chain would have shape (5, 200, 50, 2), for 5 temperatures, 200 steps (nsweeps*nsteps), 50 walkers, and 2 dimensions.

sampler.get_chain(discard=100)

Would return the samples with shape (5, 100, 50, 2). Dropping the first 100 for every walker.

sampler.get_chain(discard=100, flat=True)

Would return the samples with shape (5, 5000, 2), 'linearising' the walkers.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reddemcee-0.9.0.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reddemcee-0.9.0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file reddemcee-0.9.0.tar.gz.

File metadata

  • Download URL: reddemcee-0.9.0.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for reddemcee-0.9.0.tar.gz
Algorithm Hash digest
SHA256 c34a86c58b3689bb1925b91483ac0f690742fa304ca46dcb0b3bd1242887727a
MD5 24077b332ea8bb66f822de5404f13f99
BLAKE2b-256 4c7673c9f1baac35558216667f33e648f513656ef23d3d27a324b9411cfb41a7

See more details on using hashes here.

File details

Details for the file reddemcee-0.9.0-py3-none-any.whl.

File metadata

  • Download URL: reddemcee-0.9.0-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.4

File hashes

Hashes for reddemcee-0.9.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ba7f53e9c67f3b7143bd443133fcfbb5c03508157164c4eafe66574ed0e2c4c
MD5 d07b931f74004c9b4a5579587955bc85
BLAKE2b-256 30e81320e890241cfbcf418994e97c5ca586f2dd8b2816ffdf6d2534f39bf4f1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page