如果参数完全符合,为什么 `curve_fit` 不能估计参数的协方差?

Why isn't `curve_fit` able to estimate the covariance of the parameter if the parameter fits exactly?

我不明白 curve_fit 无法估计参数的协方差,因此提高了下面的 OptimizeWarning。以下 MCVE 解释了我的问题:

MCVE python 片段

from scipy.optimize import curve_fit
func = lambda x, a: a * x
popt, pcov = curve_fit(f = func, xdata = [1], ydata = [1])
print(popt, pcov)

输出

\python-3.4.4\lib\site-packages\scipy\optimize\minpack.py:715:
OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)

[ 1.] [[ inf]]

对于 a = 1,该函数完全适合 xdataydata。为什么不是 error/variance 0 或接近 0 的东西,而是 inf

引用自 curve_fit SciPy Reference Guide:

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.

那么,潜在的问题是什么?为什么解的雅可比矩阵没有满秩?

参数协方差的公式 (Wikipedia) has the number of degrees of freedom in the denominator. The degrees of freedoms are computed as (number of data points) - (number of parameters), which is 1 - 1 = 0 in your example. And this 是 SciPy 在除以自由度之前检查自由度数的地方。

使用 xdata = [1, 2], ydata = [1, 2] 你会得到零协方差(请注意模型仍然精确拟合:精确拟合不是问题)。

如果样本大小 N 为 1(样本方差的公式的分母中有 (N-1)),这与未定义样本方差是同一类问题。如果我们只从总体中取出 size=1 个样本,我们不会将方差估计为零,我们对方差一无所知。