Efficient Monte Carlo Fitting with Python Package Emcee and MCMC Analysis

2 replies · 2K views · last reply
Silviu
Messages
612
Reaction score
11
Hello I use a python package called emcee to fit a function to some data points. The fit looks great, but when I want to plot the value of each parameter at each step I get the plot.png below. In their example (with a different function and data points) they get the plot1.png image. Why is my function converging so fast, and why does it have that weird shape in the beginning. I apply MCMC using likelihood and posterior probability. And even if the fit looks very good, the error on parameters of function are very small (10^10 smaller than the actual value) and I think it is because of the random walks. Any idea how to fix it?
 
Attachments
  • Three stacked line plots of MCMC walker traces against step number from 0 to 1000: top y-axis labelled rho r^2 rising from about 1072000 to 1088000, middle labelled r falling from 55 to 50, bottom labelled alpha peaking near 0.24 then settling at 0.18.
    plot.png
    9.1 KB · Views: 585
  • Three stacked panels of MCMC walker traces plotted against step number from 0 to 500, showing parameters m (about -1.4 to -0.6), b (about 3.2 to 5.6) and f (0.2 to 1.0), with horizontal reference lines.
    plot1.png
    56 KB · Views: 624
That looks like it is just the burn in period. Usually people just discard the first few thousand points in order to avoid that.

I have done all of my MCMC work in R, so I am not familiar with the Python package you mention, but it looks like it is either set with a burn in of 0 or it plots the burn in samples.
 
If you're using emcee something like this will help overcome the burn in:
sampler = EnsembleSampler(...)

pos, prob, state = sampler.run_mcmc(...)

sampler.reset()

sampler.run_mcmc(pos, 1000, rstate0=state)