Broc
Broc

Reputation: 23

Python - fitting data with exponential function

I am aware that there are a few questions about a similar subject, although I couldn't find a proper answer.

I would like to fit some data with a function (called Bastenaire) and iget the parameters values. Here is the code:

import numpy as np
from matplotlib import pyplot as plt
from scipy import optimize

def bastenaire(s, A,B, C,sd):

    logNB=np.log(A)-C*(s-sd)-np.log(s-sd)

    return np.exp(logNB)-B

S=np.array([659,646,634,623,613,595,580,565,551,535,515,493,473,452,432,413,394,374,355,345])
N=np.array([46963,52934,59975,65522,74241,87237,101977,116751,133665,157067,189426,233260,281321,355558,428815,522582,630257,768067,902506,1017280])


fitmb,fitmob=optimize.curve_fit(bastenaire,S,N,p0=(30000,2000000000,0.2,250))


plt.scatter(N,S)
plt.plot(bastenaire(S,*fitmb),S,label='bastenaire')
plt.legend()

plt.show()

However, the curve fit cannot identify the correct parameters and I get: OptimizeWarning: Covariance of the parameters could not be estimated. Same results when I give no input parameters values.

Figure

Is there any way to tweak something and get results? Should my dataset cover a wider range and values?

Thank you!

Broc

Upvotes: 2

Views: 269

Answers (1)

Silmathoron
Silmathoron

Reputation: 1981

Fitting is tough, you need to restrain the parameter space using bounds and (often) check a bit your initial values.

To make it work, I search for an initial value where the function had the correct look, then estimated some constraints:

bounds = np.array([(1e4, 1e12), (-np.inf, np.inf), (1e-20, 1e-2), (-2000., 20000)]).T
fitmb, fitmob = optimize.curve_fit(bastenaire,S, N,p0=(1e7,-100.,1e-5,250.), bounds=bounds)

returns

(array([ 1.00000000e+10,  1.03174824e+04,  7.53169772e-03, -7.32901325e+01]), array([[ 2.24128391e-06,  6.17858390e+00, -1.44693602e-07,
        -5.72040842e-03],
       [ 6.17858390e+00,  1.70326029e+07, -3.98881486e-01,
        -1.57696515e+04],
       [-1.44693602e-07, -3.98881486e-01,  1.14650323e-08,
         4.68707940e-04],
       [-5.72040842e-03, -1.57696515e+04,  4.68707940e-04,
         1.93358414e+01]]))

Upvotes: 1

Related Questions