Passing arguments to emcce sample when using lmfit

3k views Asked by At

I am trying to fit a function to two datasets using lmfit with the emcee minner

My code looks like this:

minner = lmfit.Minimizer(function_2min, params, fcn_args=([x1,x2], [y1,y2],[y_err1,y_err2]))
result = minner.minimize(method='emcee')

This works well for some datasets, but sometimes I get an error that looks like this:

"ValueError: Initial state has a large condition number. Make sure that your walkers are linearly independent for the best performance"

From what I googled it seems that the solution to skip this error is to set skip_initial_state_check parameter to True in the emcce sample

I have tried to do that by passing this setting to the minimizer this way:

result = minner.minimize(method='emcee',**{'skip_initial_state_check':True})

But with that I get an error:

"TypeError: emcee() got an unexpected keyword argument 'skip_initial_state_check'"

However, when I tried to do that by passing any other parameters to emcee it seems to work. For example this works fine:

result = minner.minimize(method='emcee',**{'nwalkers':5000})

So my conclusion is that I am not passing the parameter to the emcee sample, but to emcee in general. Anyone would be so kind to suggest a solution? Unfortunately I cannot use least square fitting, as it tends to get stuck on a local minimum frequently.

1

There are 1 answers

0
M Newville On

lmfit.emcee creates an emcee.EnsembleSampler, and then runs that samplers run_mcmc() method. You can pass in optional arguments to that with lmfit.emcee(...., run_mcmc_kwargs={})

I don't know if that will do what you want, though. I would guess that you actually want to pay attention to the error message emcee is giving before trying to skip the check.

More importantly, you're opening premise of "I am trying to fit a function to two datasets using lmfit with the emcee" suggests that you are on the wrong track. emcee does not -- and cannot -- actually do a fit. It can explore parameter space, which can for sure be useful, but it never has any intention of finding an improved solution.

If doing an actual fit finds false minima then you may want a more "global" solver (emcee is not in that category, either!) or to understand why your problem is prone to such false minima -- maybe they aren't so false after all, or maybe it points to a flawed model or metric of "goodness of fit".