Integrate.quad无法正常工作

时间:2016-04-10 14:46:21

标签: python lambda scipy

我想对此进行编码:

http://latex.codecogs.com/gif.latex?%5Cint_%7B0.322411516161%7D%5E%5Cinfty%20%28108e%5Ex%20-%20150%29function%5Bselection%5D%28%29%20dx

,其中

params = (-0.00019942774322628663, 0.017096351295537309)
functions = {'1': lambda x:norm.cdf(x,loc=params[0],scale=params[1]), '2': lambda x:laplace.cdf(x,loc=params[0],scale=params[1])}

所以我写了这个:

print integrate.quad(lambda x : ((108*numpy.exp(x))-150)*functions[selection](), 0.322411516161, numpy.inf)

用户给出选择。我收到这个错误:

TypeError: <lambda>() takes exactly 1 argument (0 given)

编辑:我在阅读答案后做了以下更改:

print integrate.quad(lambda x : ((108*numpy.exp(x))-150)*functions[selection](x), 0.322411516161, numpy.inf)

我得到了

 IntegrationWarning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved.  The error may be underestimated.warnings.warn(msg, IntegrationWarning)
 (inf, nan)

1 个答案:

答案 0 :(得分:1)

您收到错误的原因是因为lambda

lambda x : ((108*numpy.exp(x))-150)*functions[selection]()

什么时候应该

lambda x : ((108*numpy.exp(x))-150)*functions[selection](your_arg_here)

其中your_arg_here是您要传递给选择函数的内容。

换句话说,生成错误的lambda无法使用lambda引用selection而不是lambda integrate.quad您已为x定义。

不确定您希望传递给您的选择功能,或者我会给出更完整的答案。不过,我假设它是lambda,在这种情况下,您的lambda x : ((108*numpy.exp(x))-150)*functions[selection](x) 将是:

IntegrationWarning: The occurrence of roundoff error is detected, which prevents the requested tolerance from being achieved.  The error may be underestimated.warnings.warn(msg, IntegrationWarning)
 (inf, nan)

回应更新后的问题:

function[selection]

只是让你知道积分发散(变为无穷大),并且发生了一些浮点错误。积分发散是有意义的,因为e ^ x是无界的,因为x ---&gt; inf,function[selection]正在计算cdf值,这意味着它受[0,1]的限制。基于一些快速测试,对于大于1的值,function[selection]看起来总是为1。因此,当x - > inf时,SELECT ST_Distance( ST_Transform(ST_GeomFromText('POINT(23.7104 90.4074)',4326),26986), ST_Transform(ST_GeomFromText('POINT(55.75222 37.61556)', 4326),26986) ); 为1,并且e ^ x变为无穷大,因此分歧。