Skip to content Skip to sidebar Skip to footer

Mcmc Sampling A Maxwellian Curve Using Python's Emcee

I am trying to introduce myself to MCMC sampling with emcee. I want to simply take a sample from a Maxwell Boltzmann distribution using a set of example code on github, https://git

Solution 1:

I think there are a couple of problems that I see. The main one is that emcee wants you to give it the natural logarithm of the probability distribution function that you want to sample. So, rather than having:

def lnprob(x, a, icov):
    pi = np.pireturn np.sqrt(2/pi)*x**2*np.exp(-x**2/(2.*a**2))/a**3

you would instead want, e.g.

def lnprob(x, a):
    pi = np.piif x < 0:
        return -np.inf
    else:
        return0.5*np.log(2./pi) + 2.*np.log(x) - (x**2/(2.*a**2)) - 3.*np.log(a)

where the if...else... statement is to explicitly say that negative values of x have zero probability (or -infinity in log-space).

You also shouldn't have to calculate icov and pass it to lnprob as that's only needed for the Gaussian case in the example you link to.

When you call:

sampler = emcee.EnsembleSampler(nwalkers, ndim, lnprob, args=[means, icov])

the args value should just be any additional arguments that your lnprob function requires, so in your case this would be the value of a that you want to set your Maxwell-Boltxmann distribution up with. This should be a single value rather than the two randomly initialised values you have set when creating mean.

Overall, the following should work for you:

from __future__ import print_function

import emcee
import numpy as np
from numpy import pi as pi

# define the natural log of the Maxwell-Boltzmann distributiondeflnprob(x, a):               
    if x < 0:                                                                
        return -np.inf
    else:
        return0.5*np.log(2./pi) + 2.*np.log(x) - (x**2/(2.*a**2)) - 3.*np.log(a)

# choose a value of 'a' for the distributions
a = 5.# I'm choosing 5!# choose the number of walkers
nwalkers = 50# set some initial points at which to calculate the lnprob
p0 = [np.random.rand(1) for i in xrange(nwalkers)]

# initialise the sampler
sampler = emcee.EnsembleSampler(nwalkers, 1, lnprob, args=[a])

# Run 5000 steps as a burn-in.
pos, prob, state = sampler.run_mcmc(p0, 5000)

# Reset the chain to remove the burn-in samples.
sampler.reset()

# Starting from the final position in the burn-in chain, sample for 100000 steps.
sampler.run_mcmc(pos, 100000, rstate0=state)

# lets check the samples look right
mbmean = 2.*a*np.sqrt(2./pi) # mean of Maxwell-Boltzmann distributionprint("Sample mean = {}, analytical mean = {}".format(np.mean(sampler.flatchain[:,0]), mbmean))
mbstd = np.sqrt(a**2*(3*np.pi-8.)/np.pi) # std. dev. of M-B distributionprint("Sample standard deviation = {}, analytical = {}".format(np.std(sampler.flatchain[:,0]), mbstd))

Post a Comment for "Mcmc Sampling A Maxwellian Curve Using Python's Emcee"