Python 尝试使用scipy quad集成函数并使用brentq求解时出现数学范围错误

Python 尝试使用scipy quad集成函数并使用brentq求解时出现数学范围错误,python,Python,我想让scipy为我计算以下积分: def F(x, theta, mu, sigma, r): #equation 1 def integrand(u): return u**(r/mu - 1) * exp(sqrt(2*mu / sigma**2) * (x-theta)*u - u**2/2) return scipy.integrate.quad(integrand, 1e-5, np.inf)[0] def Prime(f, x, theta

我想让scipy为我计算以下积分:

def F(x, theta, mu, sigma, r):
    #equation 1
    def integrand(u):
        return u**(r/mu - 1) * exp(sqrt(2*mu / sigma**2) * (x-theta)*u - u**2/2)
    return scipy.integrate.quad(integrand, 1e-5, np.inf)[0]

def Prime(f, x, theta, mu, sigma, r, h=1e-5):
    # given f, estimates f'(x) using the difference quotient formula 
    return (f(x+h, theta, mu, sigma, r) - f(x, theta, mu, sigma, r)) / h

def b_star(theta, mu, sigma, r, c):
    #equation 2
    def func(b):
        return F(b, theta, mu, sigma, r) - (b-c)*Prime(F, b, theta, mu, sigma, r)
    
    return scipy.optimize.brentq(func, -2, 3)
与下列方程式对应 ,

其中,我解方程2中的b。 这似乎在大多数情况下都有效,但在某些情况下会出现以下错误:

In: b_star(0.573085,3.213728,0.137655,0,0.001)
Out:
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-164-1aeb31cb3219> in <module>
----> 1 b_star(0.573085,3.213728,0.137655,0,0.001)

<ipython-input-163-14ce7e008b20> in b_star(theta, mu, sigma, r, c)
     38         return F(b, theta, mu, sigma, r) - (b-c)*Prime(F, b, theta, mu, sigma, r)
     39 
---> 40     return so.brentq(func, -2, 3)
     41 
     42 def V(x, theta, mu, sigma, r, c):

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/optimize/zeros.py in brentq(f, a, b, args, xtol, rtol, maxiter, full_output, disp)
    778     if rtol < _rtol:
    779         raise ValueError("rtol too small (%g < %g)" % (rtol, _rtol))
--> 780     r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
    781     return results_c(full_output, r)
    782 

<ipython-input-163-14ce7e008b20> in func(b)
     36 
     37     def func(b):
---> 38         return F(b, theta, mu, sigma, r) - (b-c)*Prime(F, b, theta, mu, sigma, r)
     39 
     40     return so.brentq(func, -2, 3)

<ipython-input-163-14ce7e008b20> in F(x, theta, mu, sigma, r)
     18     def integrand(u):
     19         return u**(r/mu - 1) * exp(sqrt(2*mu / sigma**2) * (x-theta)*u - u**2/2)
---> 20     return si.quad(integrand, 1e-5, np.inf)[0]
     21 
     22 def G(x, theta, mu, sigma, r):

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/integrate/quadpack.py in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    339 
    340     if weight is None:
--> 341         retval = _quad(func, a, b, args, full_output, epsabs, epsrel, limit,
    342                        points)
    343     else:

/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/integrate/quadpack.py in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
    453             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    454         else:
--> 455             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
    456     else:
    457         if infbounds != 0:

<ipython-input-163-14ce7e008b20> in integrand(u)
     17     # equation 3.3
     18     def integrand(u):
---> 19         return u**(r/mu - 1) * exp(sqrt(2*mu / sigma**2) * (x-theta)*u - u**2/2)
     20     return si.quad(integrand, 1e-5, np.inf)[0]
     21 

OverflowError: math range error

In:b_星(0.573085,3.213728,0.137655,0,0.001)
输出:
---------------------------------------------------------------------------
溢出错误回溯(上次最近调用)
在里面
---->1颗b_星(0.573085,3.213728,0.137655,0,0.001)
b_星(θ,μ,σ,r,c)
38返回F(b,θ,μ,σ,r)-(b-c)*素数(F,b,θ,μ,σ,r)
39
--->40返回so.brentq(func,-2,3)
41
42 def V(x,θ,μ,σ,r,c):
/brentq中的Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/optimize/zeros.py(f、a、b、args、xtol、rtol、maxiter、full_输出、disp)
778如果rtol<\u rtol:
779提升值错误(“rtol太小(%g<%g)”%(rtol,_rtol))
-->780 r=_0._brentq(f,a,b,xtol,rtol,maxiter,args,full_输出,disp)
781返回结果(满输出,r)
782
在func(b)
36
37 def func(b):
--->38返回F(b,θ,μ,σ,r)-(b-c)*素数(F,b,θ,μ,σ,r)
39
40返回so.brentq(func,-2,3)
F(x,θ,μ,σ,r)
18 def被积函数(u):
19返回u**(r/mu-1)*exp(sqrt(2*mu/sigma**2)*(x-θ)*u-u**2/2)
--->20返回四进制(被积函数,1e-5,np.inf)[0]
21
22 def G(x,θ,μ,σ,r):
/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/integrate/quadpack.py四元组(func、a、b、args、full_输出、epsabs、epsrel、limit、points、weight、wvar、wopts、maxp1、limlst)
339
340如果重量为零:
-->341 retval=_quad(func、a、b、args、full_输出、epsabs、epsrel、limit、,
(342分)
343其他:
/库/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/scipy/integrate/quadpack.py in_quad(func、a、b、args、full_输出、epsabs、epsrel、limit、points)
453返回四块封装(函数、a、b、参数、全输出、epsabs、epsrel、极限)
454其他:
-->455返回_quadpack._qagie(func、bound、inbounds、args、full_输出、epsabs、epsrel、limit)
456其他:
457如果输入!=0:
在被积函数(u)中
17#方程式3.3
18 def被积函数(u):
--->19返回u**(r/mu-1)*exp(sqrt(2*mu/sigma**2)*(x-θ)*u-u**2/2)
20返回四进制(被积函数,1e-5,np.inf)[0]
21
溢出错误:数学范围错误
我尝试了几种解决方案,比如使用Decimal包或gmpy包来允许更大的浮点数,但在这些情况下,输出变为0.0。 我还尝试将输入转换为np.float128数据类型,这也没有解决问题


是否对此进行了修复?

如果
gmpy
给出了一个零值,您如何确定这是错误的?你认为这个值应该是多少?因为根据我的论文,从输出中得到的方程应该大于用作输入的θ。我认为在你报告的行中出现数学范围错误的唯一可能原因是
exp()
的参数太大。限值介于1e2和1e3之间。如果
gmpy
给出的值为零值,您如何确定该值不正确?你认为这个值应该是多少?因为根据我的论文,从输出中得到的方程应该大于用作输入的θ。我认为在你报告的行中出现数学范围错误的唯一可能原因是
exp()
的参数太大。限值介于1e2和1e3之间。