Hi all and Matt Newville,
I'm beginner, and I'm doing a very basic thing about exponential fitting.
However, i have some difficulties that i couldn't find the answer, although I try many suggestions from previous post or in stackoverflow.
Here is the model:
def mod_exp(x, amplitude, decay, x0, y0):
return amplitude*np.exp( -(x-x0)/decay)+y0
mod = lt.Model(mod_exp)
mod.set_param_hint('amplitude', value=0.8, vary=True, min =0.0)
mod.set_param_hint('decay', value=2, vary=True, min=0.1)
mod.set_param_hint('x0', value=4, vary=True, min=0.0)
mod.set_param_hint('y0', value=0, vary=True)
pars = mod.make_params()
out = mod.fit(eps, pars, x=x_eps)
The example data is attached. X is the independent variable.
If you do the data with above example, you will get the result quite ok..but:
1. As you might notice, there's a possibility the -(x-x0)/decay, becomes positive and very big. First if 'decay' too small and if the (x-x0) coincidentally becomes <0.
if let say change value of 'decay' = 1, you will get the nan_policy warning.
if i modified
mod.set_param_hint('x0', value=4, vary=True, min=np.min(X))
It prevents negative value of x-x0.
But the fit result of x0 stuck on np.min(X)... I think it is not the best fitting, although it works better.
example of fit report:
Qc=4.0,Qd=1.6
[[Model]]
Model(mod_exp)
[[Fit Statistics]]
# fitting method = leastsq
# function evals = 26
# data points = 30
# variables = 4
chi-square = 0.01068116
reduced chi-square = 4.1081e-04
Akaike info crit = -230.214133
Bayesian info crit = -224.609343
## Warning: uncertainties could not be estimated:
x0: at initial value
x0: at boundary
[[Variables]]
amplitude: 0.53622335 (init = 0.8)
decay: 0.38896822 (init = 1)
x0: 4.16521739 (init = 4.165217)
y0: 0.29091852 (init = 0)
2. using python 'min' as suggested from other example:
amplitude*np.exp(min(700, -(x-x0)/decay))+y0
i got this error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().
3. I tried using:
mod.set_param_hint('decay', value=1, vary=True, min=np.min(x_eps)/700)
I think x0 should be counted on the minimum value, but x0 is another parameter, I don't know how to do it.
I'm not sure about this. Any suggestion?
I'm sorry if I this is trivial/beginner question, but I want to understand about this.
Thank you very much!!
Best regards,
Teko