我不明白 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
,该函数完全适合 xdata
和 ydata
。为什么误差/方差不是 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 ) 在分母中包含自由度数。自由度计算为(数据点数)-(参数数),在您的示例中为 1 - 1 = 0。和 this是 SciPy 在除以它之前检查自由度数的地方。
使用 xdata = [1, 2], ydata = [1, 2]
您将获得零协方差(请注意,模型仍然完全适合:完全适合不是问题)。
如果样本大小 N 为 1(样本方差的公式在分母中有 (N-1)),这与未定义样本方差是同一类问题。如果我们只从总体中取出 size=1 个样本,我们不会将方差估计为零,我们对方差一无所知。
关于python - 如果参数完全符合,为什么 `curve_fit` 不能估计参数的协方差?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41725377/