Scipy curve_fit does not estimate offset values and throws initial guess as the final estimate for every fit.

Hello everyone, I am currently working on a curve_fitting problem that needs to estimate 4 parameters namely time to peak (tmax), maximum amplitude (ymax), shape (b) and offset. I am using Scipy's curve_fit library that is giving reliable results for the first three parameters but not for offset. My objective function looks like this: def objective2(t,tmax, ymax, b, offset): offInt = math.ceil(offset) offArr = np.zeros(offInt) offArr = np.append(offArr,ymax*((t[:(len(t)-offInt)]/tmax)*np.exp((b*tmax)))* 2.713*np.exp((tmax-t[:(len(t)-offInt)])*b)) return offArr I am fitting a 4 parameter gamma function in this objective function. If my initial guess for these parameters is p0 = np.array([100, 5, 0.005, 10]), all my estimated fits have an offset of 10 which is not right. Can someone help me with this or is there some issue with scipy curve_fit that can be corrected?

Hi, I am sorry but this mailing list is not intended for user questions, but for development questions related to SciPy itself. Please consider another forum such as StackOverflow using the SciPy tag. Still, to your question. You might want to scale your inputs to have similar order of magnitude. Optimizers and other ML operations usually don’t like that. Cheers, Pamphile
On 28.09.2022, at 19:40, amk2358@columbia.edu wrote:
Hello everyone,
I am currently working on a curve_fitting problem that needs to estimate 4 parameters namely time to peak (tmax), maximum amplitude (ymax), shape (b) and offset. I am using Scipy's curve_fit library that is giving reliable results for the first three parameters but not for offset. My objective function looks like this: def objective2(t,tmax, ymax, b, offset): offInt = math.ceil(offset) offArr = np.zeros(offInt) offArr = np.append(offArr,ymax*((t[:(len(t)-offInt)]/tmax)*np.exp((b*tmax)))* 2.713*np.exp((tmax-t[:(len(t)-offInt)])*b)) return offArr I am fitting a 4 parameter gamma function in this objective function. If my initial guess for these parameters is p0 = np.array([100, 5, 0.005, 10]), all my estimated fits have an offset of 10 which is not right. Can someone help me with this or is there some issue with scipy curve_fit that can be corrected? _______________________________________________ SciPy-Dev mailing list -- scipy-dev@python.org To unsubscribe send an email to scipy-dev-leave@python.org https://mail.python.org/mailman3/lists/scipy-dev.python.org/ Member address: roy.pamphile@gmail.com

On Wed, Sep 28, 2022, at 10:54, Pamphile Roy wrote:
Still, to your question. You might want to scale your inputs to have similar order of magnitude. Optimizers and other ML operations usually don’t like that.
I have not looked at your cost function in detail, but it could also be that the cost function value is relatively insensitive to the offset parameter. Stéfan
participants (3)
-
amk2358@columbia.edu
-
Pamphile Roy
-
Stefan van der Walt