I am working with scipy’s curve_fit function, and using pcov to calculate the parameter errors. I understand pcov produce the variance in the parameters which it produces. Variance is obviously calculated from a set of numbers, which are the estimates for the respective parameters. I am wondering if it is possible to produce this set of estimates so that I can see it and try to calculate the variance by hand. Additionally, I would like to plot a histogram from these values. Does anyone know how I could do this, or if there is a separate function which might be able to help?
Here is my current code:
import matplotlib.pyplot as plt
import numpy as np
from scipy.optimize import curve_fit
x_data = np.array([1, 2, 3, 4, 5])
y_data = np.array([2, 4, 9, 16, 25])
def function(x, a, b):
return a * np.exp(-b*x)
popt, pcov = curve_fit(function, x_data, y_data)
a, b = popt
print("pcov =", pcov)
print("a +/- standard deviation =", popt[0], "+/-", pcov[0,0]**0.5)
print("b +/- standard deviation =", popt[1], "+/-", pcov[1,1]**0.5)
print("a +/- standard error =", popt[0], "+/-", ((pcov[0,0]**0.5)/np.sqrt(5)))
print("b +/- standard error =", popt[1], "+/-", ((pcov[1,1]**0.5)/np.sqrt(5)))
print("Confidence Interval:", CI)
plt.plot(x_data, function(x_data, *popt), "k--", label='Curve Fit')
plt.plot(x_data, y_data, 'o', label='Raw Data')
plt.xlabel("Time")
plt.ylabel("AB")
plt.legend()
which produces:
To reiterate, I want to produce the set of values used to calculate the variance of each parameter. Thank you!