为什么`curve_fit`无法估计参数的协方差,即使参数完全匹配?

2024-04-29 10:34:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我不明白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。为什么错误/方差不是0,或者接近0,而是inf

这是来自^{} 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.

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


Tags: the参数scipymatrixfitinfoptimizefunc
1条回答
网友
1楼 · 发布于 2024-04-29 10:34:07

参数的协方差公式(Wikipedia)具有分母中的自由度数。自由度计算为(数据点数量)-(参数数量),在您的示例中为1-1=0。而this是SciPy在除以自由度之前检查自由度的地方。

使用xdata = [1, 2], ydata = [1, 2]可以得到零协方差(注意,模型仍然完全适合:精确匹配不是问题)。

如果样本量N为1(样本方差的公式在分母中有(N-1))则这与样本方差未定义是同一类问题。如果我们只从总体中抽取size=1的样本,我们不会用0来估计方差,我们对方差一无所知。

相关问题 更多 >