符合scipy.optimize参数的错误 [英] Errors to fit parameters of scipy.optimize

查看:158
本文介绍了符合scipy.optimize参数的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用scipy.optimize.minimize( https://docs.scipy .org/doc/scipy/reference/tutorial/optimize.html )功能与method='L-BFGS-B.

上面返回的示例如下:

      fun: 32.372210618549758
 hess_inv: <6x6 LbfgsInvHessProduct with dtype=float64>
     jac: array([ -2.14583906e-04,   4.09272616e-04,  -2.55795385e-05,
         3.76587650e-05,   1.49213975e-04,  -8.38440428e-05])
  message: 'CONVERGENCE: REL_REDUCTION_OF_F_<=_FACTR*EPSMCH'
     nfev: 420
      nit: 51
   status: 0
  success: True
        x: array([ 0.75739412, -0.0927572 ,  0.11986434,  1.19911266,  0.27866406,
       -0.03825225])

x值正确包含适合的参数.如何计算与这些参数相关的误差?

The x value correctly contains the fitted parameters. How do I compute the errors associated to those parameters?

推荐答案

TL; DR:您实际上可以将最小化例程的精确度设置为上限参数的最佳值.请参见此答案末尾的代码片段,其中显示了如何直接执行此操作,而无需诉诸其他最小化例程.

TL;DR: You can actually place an upper bound on how precisely the minimization routine has found the optimal values of your parameters. See the snippet at the end of this answer that shows how to do it directly, without resorting to calling additional minimization routines.

该方法的文档说,

(f^k - f^{k+1})/max{|f^k|,|f^{k+1}|,1} <= ftol时,迭代停止.

大致来说,当您要最小化的函数f的值被最小化到最佳值的ftol以内时,最小化停止. (如果f大于1,这是一个相对错误,否则为绝对错误;为简单起见,我认为这是一个绝对错误.)在更标准的语言中,您可能会把函数f视为chi.平方值.因此,这大致表明您会期望

Roughly speaking, the minimization stops when the value of the function f that you're minimizing is minimized to within ftol of the optimum. (This is a relative error if f is greater than 1, and absolute otherwise; for simplicity I'll assume it's an absolute error.) In more standard language, you'll probably think of your function f as a chi-squared value. So this roughly suggests that you would expect

当然,实际上,您正在应用这样的最小化例程的事实是,假设您的函数运行良好,在某种意义上说,函数相当平滑,并且找到的最优值很好地接近了接近最优值

Of course, just the fact that you're applying a minimization routine like this assumes that your function is well behaved, in the sense that it's reasonably smooth and the optimum being found is well approximated near the optimum by a quadratic function of the parameters xi:

其中Δ x i 是参数 x i 的发现值与其最佳值之间的差,并且 H ij 是Hessian矩阵.一个小的(令人惊讶的是非平凡的)线性代数会让您得到一个相当标准的结果,以估计任何数量 X 中的不确定性,这是参数 x i 的函数:

where Δxi is the difference between the found value of parameter xi and its optimal value, and Hij is the Hessian matrix. A little (surprisingly nontrivial) linear algebra gets you to a pretty standard result for an estimate of the uncertainty in any quantity X that's a function of your parameters xi:

这让我们写

通常,这是最有用的公式,但是对于此处的特定问题,我们只有 X = x i ,因此可以简化为

That's the most useful formula in general, but for the specific question here, we just have X = xi, so this simplifies to

最后,要完全明确,假设您已将优化结果存储在名为res的变量中.逆Hessian可作为res.hess_inv使用,该函数接受一个向量并返回该逆Hessian与该向量的乘积.因此,例如,我们可以使用以下代码段显示优化的参数以及不确定性估计值:

Finally, to be totally explicit, let's say you've stored the optimization result in a variable called res. The inverse Hessian is available as res.hess_inv, which is a function that takes a vector and returns the product of the inverse Hessian with that vector. So, for example, we can display the optimized parameters along with the uncertainty estimates with a snippet like this:

ftol = 2.220446049250313e-09
tmp_i = np.zeros(len(res.x))
for i in range(len(res.x)):
    tmp_i[i] = 1.0
    hess_inv_i = res.hess_inv(tmp_i)[i]
    uncertainty_i = np.sqrt(max(1, abs(res.fun)) * ftol * hess_inv_i)
    tmp_i[i] = 0.0
    print('x^{0} = {1:12.4e} ± {2:.1e}'.format(i, res.x[i], uncertainty_i))

请注意,假设f^kf^{k+1}基本上与最终输出值res.fun相同,我已经从文档中合并了max行为,这实际上应该是一个很好的近似值.另外,对于小问题,您可以使用np.diag(res.hess_inv.todense())来获取完整的逆并立即提取对角线.但是对于大量变量,我发现这是一个慢得多的选择.最后,我添加了默认值ftol,但是如果在minimize的参数中对其进行更改,则显然需要在此处进行更改.

Note that I've incorporated the max behavior from the documentation, assuming that f^k and f^{k+1} are basically just the same as the final output value, res.fun, which really ought to be a good approximation. Also, for small problems, you can just use np.diag(res.hess_inv.todense()) to get the full inverse and extract the diagonal all at once. But for large numbers of variables, I've found that to be a much slower option. Finally, I've added the default value of ftol, but if you change it in an argument to minimize, you would obviously need to change it here.

这篇关于符合scipy.optimize参数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆