Fitting with monte carlo in python
<p>Hello I use a python package called <code>emcee</code> 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 this: <a href="http://i.stack.imgur.com/t0cpX.png" rel="nofollow"><img src="http://i.stack.imgur.com/t0cpX.png" alt="enter image description here"></a>. In their example (with a different function and data points) they get this: <a href="http://i.stack.imgur.com/qiXaH.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/qiXaH.jpg" alt="enter image description here"></a>. 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? Here is their code for fitting: <a href="http://dan.iel.fm/emcee/current/user/line/" rel="nofollow">http://dan.iel.fm/emcee/current/user/line/</a> I used the same code with the obvious modifications for my data points and fitting function.</p>

<p>I would not say that your function is converging faster than the <code>emcee</code> line-fitting example you're linked to. In the example, the walkers start exploring the most likely values in the parameter space almost immediately, whereas in your case it takes more than 200 iterations to reach the high probability region.</p>

<p>The jump in your trace plots looks like a burn-in. It is a common feature of MCMC sampling algorithms where your walkers are given starting points away from the bulk of the posterior and then must find their way to it. It looks like in your case the likelihood function is fairly smooth so you only need a hundred or so iterations to achieve this, giving your function this "weird shape" you're talking about.</p>

<p>If you can constrain the starting points better, do so; if not, you might consider discarding this initial length before further analysis is made (see <a href="http://stats.stackexchange.com/questions/88819/mcmc-methods-burning-samples">here</a> and <a href="http://stats.stackexchange.com/questions/88256/what-does-sampling-period-mean-in-mcmc/88262#88262">here</a> for discussions on burn in lengths).</p>

<p>As for whether the errors are realistic or not, you need to inspect the resulting posterior models for that, because the ratio of the actual parameter value to its uncertainties does not have a say in this. For example, if we take your linked <a href="http://dan.iel.fm/emcee/current/user/line/" rel="nofollow">example</a> and change the true value of <code>b</code> to 10^10, the resulting errors will be ten magnitudes smaller while remaining perfectly valid.</p>

python curve-fitting montecarlo