scipy.integrate.quad 如何知道何时停止? [英] How does scipy.integrate.quad know when to stop?

查看:73
本文介绍了scipy.integrate.quad 如何知道何时停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码,我正在使用 scipy.integrate.quad.积分的极限是从负无穷到无穷大.它运行正常,但我希望它更快.

I have a piece of code that I am using scipy.integrate.quad. The limits of integration are minus infinity to infinity. It runs OK, but I would like it faster.

问题的本质是被积分的函数是三个函数的乘积:(1) 一个窄函数(介于 0 和 (2) 一个宽函数(例如,在 200,000 和 500,000 之间),以及(3) 下降为 1/abs(x).

The nature of the problem is that the function being integrated is the product of three functions: (1) one that is narrow (between zero and (2) one that is wide (between, say, 200,000 and 500,000), and (3) one that falls off as 1/abs(x).

如果那样的话,我只需要精确到 0.1%.

I only need accuracy to .1%, if that.

我可以做很多工作并实际确定实数的积分限制,这样就不会进行多余的计算;在函数 1 和 2 的区域之外,它们都为零,因此 1/x 在那里甚至不起作用.但这将是大量容易出错的代码计算.

I could do a lot of work and actually determine integration limits that are real numbers so no excess computation gets done; outside the regions of functions 1 and 2 they are both zero, so the 1/x doesn't even come into play there. But it would be a fair amount of error-prone code calculations.

  1. 这个函数是如何知道如何优化的,它是否很擅长,有无限边界?

  1. How does this function know how to optimize, and is it pretty good at it, with infinite bounds?

我可以通过传递指导(如容错)来调整它吗?

Can I tune it through passing in guidance (like error tolerance)?

或者,是否值得尝试给它有限的集成范围?

Or, would it be worthwhile to try to give it limited integration bounds?

推荐答案

quad 对有限区间和无限区间使用不同的算法,但大体思路是一样的:积分是使用两种相关的方法计算的(例如,7 点高斯规则和 15 点 Kronrod 规则),这些结果之间的差异提供了对它们的准确程度的估计.如果准确度低,则将间隔一分为二,并对子间隔重复该过程.详细解释超出了 Stack Overflow 答案的范围;数值积分很复杂.

quad uses different algorithms for finite and infinite intervals, but the general idea is the same: the integral is computed using two related methods (for example, 7-point Gauss rule and 15-point Kronrod rule), and the difference between those results provides an estimate for how accurate they are. If the accuracy is low, the interval is bisected and the process repeats for subintervals. A detailed explanation is beyond the scope of a Stack Overflow answer; numerical integration is complicated.

对于大的或无限的积分边界,准确度和效率取决于算法能够定位函数的主要特征.将边界作为 -np.inf, np.inf 传递是有风险的.例如,

For large or infinite integration bounds, the accuracy and efficiency depend on the algorithm being able to locate the main features of the function. Passing the bounds as -np.inf, np.inf is risky. For example,

quad(lambda x: np.exp(-(x-20)**2), -np.inf, np.inf)

返回错误的结果(基本上是零而不是 1.77),因为它没有注意到高斯函数在 20 附近的凹凸.

returns a wrong result (essentially zero instead of 1.77) because it does not notice the bump of the Gaussian function near 20.

另一方面,任意强加有限区间是有问题的,因为您放弃了对错误的任何控制(不估计您切断的无限尾部中包含的内容).我的建议如下:

On the other hand, arbitrarily imposing a finite interval is questionable in that you give up any control over error (no estimate on what was contained in the infinite tails that you cut off). I suggest the following:

  1. 将积分分成三部分:(-np.inf, A)(A, B)(B, np.inf) 其中,例如,A 是 -1e6,B 是 1e6.

  1. Split the integral into three: (-np.inf, A), (A, B), and (B, np.inf) where, say, A is -1e6 and B is 1e6.

对于(A, B)上的积分,提供points参数,定位函数的特征(窄部分").例如,

For the integral over (A, B), provide points parameter, which locates the features ("narrow parts") of the function. For example,

quad(lambda x: np.exp(-(x-20)**2), -1e6, 1e6, points=[10, 30])

应该返回 1.77.

returns 1.77 as it should.

如果您发现默认精度要求过高,请将 epsabs(绝对误差)和 epsrel(相对误差)调整到所需精度范围内.

Adjust epsabs (absolute error) and epsrel (relative error) to within desired accuracy, if you find that the default accuracy is too demanding.

这篇关于scipy.integrate.quad 如何知道何时停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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