I am using Python lmfit to do least square fit with monthly average data from 2005-2016. I have constructed the function like below:
equation
and the original code shown as below:
# t is in fractional years, e.g. 2017+122./365.
def fun(t, a, b, c, A1, A2, A3, A4, B1, B2, B3, B4):
An=[A1,A2,A3,A4]
Bn=[B1,B2,B3,B4]
sum=np.sum([An[i] * np.sin(2 * np.pi * (i + 1) * t+Bn[i]) for i in range(len(An))])
return a+b*t+c*t*t+sum
mod = Model(fun)
pars = mod.make_params(a=-10, b=0.003, c=0.01, A1=-1., A2=1., A3=1., A4=1., B1=-1., B2=1., B3=1., B4=1.)
result = mod.fit(y, pars, t=t)
print(result.fit_report())
plt.plot(t, y, 'bo')
plt.plot(t, result.best_fit, 'r-')
plt.show()
fitted line and the original data dots
It seems that the Fourier terms didn't work. Therefore, I am curious that how to give a suitable initial estimation on the function parameters such as A1, A2,A3...?
sumas variable name. - mikuszefskiimports. A minimal data set would be nice as well. - mikuszefskiacan be checked frommean. TheBs are probably best set to zero.A1to something like0.5 * ( max() - min() ). The otherA's to zero. (Assuming that all your data looks similar to what you show in the link),bandcto zero as well. - mikuszefski