Reputation: 65
I am having what appears to be a common problem getting the covariance of the parameters using scipy.optimize.curve_fit(), but the common solutions haven't been much help to me. (Not giving a good initial guess, not having enough data points, not inputting a numpy array, not using float64.) This reproduced my error:
x = np.array([103.15, 113.15, 269.3, 273.15, 273.15, 283.15, 290.0, 293.15, 293.15, 296.15, 296.15, 303.15, 303.15, 303.15, 310.0, 313.15, 313.15, 318.15, 323.15, 323.15, 330.0, 333.15, 348.16, 350.0, 353.15])
y = np.array([-0.2468283, -0.19681169, -0.01856526, -0.01707074, -0.01698771, -0.01333444, -0.01072733, -0.00986383, -0.00974759, -0.00830289, -0.00896712, -0.00699103, -0.00667552, -0.0063102, -0.00440053, -0.00368648, -0.0034374, -0.00199269, -0.00092992, -0.00066423, 0.00106277, 0.00166058, 0.00464962, 0.0060445,0.00650947])
((u0, u1, r0, r1), pcov) = scipy.optimize.curve_fit(f, x, y, p0=np.array([1.0, 0.01, 0.3, 0.5]))
print(u0, u1, r0, r1)
print(pcov)
Where my function is:
def f(x, u0, u1, r0, r1):
k = 8.617333262145E-5
term1 = (np.exp(-u0/(k*x)) - np.exp(u1/(k*x))) * r0**3
term2 = (np.exp(u1/(k*x)) - 1) * r1**3
return -(2 * np.pi / 3) * (term1 + term2)
The output is
OptimizeWarning: Covariance of the parameters could not be estimated
warnings.warn('Covariance of the parameters could not be estimated',
1.0 0.010062693926842859 0.3093891634616651 0.4627801922742623
[[inf inf inf inf]
[inf inf inf inf]
[inf inf inf inf]
[inf inf inf inf]]
Plotting the data points with the best fit shows an excellent match. I am not really sure what to make of this issue with the covariance. Any ideas?
Upvotes: 0
Views: 756
Reputation: 2802
According to the documentation
If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.
I tried to use method='trf'
on your data and seemed to work. I don't know why the Jacobian is non-invertable in this case though.
Upvotes: 0