Skip to content Skip to sidebar Skip to footer

Getting Standard Error Associated With Parameter Estimates From Scipy.optimize.curve_fit

I am using scipy.optimize.curve_fit to fit a curve to some data i have. The curves, for the most part, seem to fit very well. For some reason, pcov = inf when i print it off. What

Solution 1:

The variance of parameters are the diagonal elements of the variance-co variance matrix, and the standard error is the square root of it. np.sqrt(np.diag(pcov))

Regarding getting inf, see and compare these two examples:

In [129]:
import numpy as np
def func(x, a, b, c, d):
    return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5, 1)
ydata = y + 0.2 * np.random.normal(size=len(xdata))
popt, pcov = so.curve_fit(func, xdata, ydata)
print np.sqrt(np.diag(pcov))
[ inf  inf  inf  inf]

And:

In [130]:

def func(x, a, b, c):
    return a * np.exp(-b * x) + c

xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
ydata = y + 0.2 * np.random.normal(size=len(xdata))
popt, pcov = so.curve_fit(func, xdata, ydata)
print np.sqrt(np.diag(pcov))
[ 0.110976460.118491070.05230711]

In this extreme example, d has no effect on the function func, hence it will be associated with variance of +inf, or in another word, it can be just about any value. Removing d from func will get what will make sense.

In reality, if parameters are of very different scale, say:

def func(x, a, b, c, d):
    #return a * np.exp(-b * x) + c
    return a * np.exp(-b * x) + c + d*1e-10

You will also get inf due to float point overflow/underflow.

In your case, I think you never used a and b. So it is just like the first example here.

Post a Comment for "Getting Standard Error Associated With Parameter Estimates From Scipy.optimize.curve_fit"