寻根算法的 Python 类型错误使用 QuantEcon 包

问题描述

Python 初学者,将其用于经济研究。我目前在使用此包 (https://quanteconpy.readthedocs.io/en/latest/optimize/root_finding.html) 中的 python 函数查找函数根时遇到问题。我之前发布了一个关于它的查询 (Python TypeError for Root-Finding Code(QuantEcon Package)),但我似乎仍然无法解决这个问题。

我的代码附在下面:

from numba import jit
import timeit

####################QuantEcon Methods#######################
from quantecon.optimize.root_finding import newton,newton_halley,newton_secant\,bisect,brentq

##Coded Up Functions (Jitted)##

@jit 
def ces(x,y,rho):
    U = (x**rho + y**rho)**(1/rho)
    return U # original CES Function
               
@jit
def ces_d(x,rho):
    U_x = (x**(rho-1))* ((x**rho + y**rho)**((1-rho)/rho)) 
    return U_x # CES Derivative

@jit
def ces_dd(x,rho,ud):
    U_obj = ces_d(x,rho) - ud
    return U_obj # New Objective Function = CES Derivative - ud

@jit
def ces_d2(x,rho):
    U_xx = (rho-1) * (x**(rho-2)) *(y**rho) * ((x**rho + y**rho)**(1/rho-2))
    return U_xx #The derivative of the new objective function (ud drops out)

##Default Newton-Raphson##

@jit
def ces_inv(y,ud):
 args = (y,ud) #order must be preserved
 x_bar = newton(ces_dd,ces_d2,args) 
 return x_bar

print(ces_inv(2,0.5,1)) #TypingError Occurs Here

相关的堆栈跟踪在这里

TypingError: Internal error at <numba.core.typeinfer.CallConstraint object at 0x000002619E33BB20>.
too many positional arguments
During: resolving callee type: type(cpudispatcher(<function ces_d2 at 0x000002619E1B15E0>))
During: typing of call at C:\Users\Orphanides\anaconda3\lib\site-packages\quantecon\optimize\root_finding.py (89)

Enable logging at debug level for details.

整个 StackTrace 历史记录:

<ipython-input-1-1251fd61b2b2>:32: NumbaWarning: 
Compilation is falling back to object mode WITH looplifting enabled because Function "ces_inv" Failed type inference due to: Failed in nopython mode pipeline (step: nopython frontend)
Internal error at <numba.core.typeinfer.CallConstraint object at 0x000002619E2727F0>.
too many positional arguments
During: resolving callee type: type(cpudispatcher(<function ces_d2 at 0x000002619E1B15E0>))
During: typing of call at C:\Users\Orphanides\anaconda3\lib\site-packages\quantecon\optimize\root_finding.py (89)

Enable logging at debug level for details.

File "..\..\..\..\..\..\anaconda3\lib\site-packages\quantecon\optimize\root_finding.py",line 89:
def newton(func,x0,fprime,args=(),tol=1.48e-8,maxiter=50,<source elided>
            break
        fder = fprime(p0,*args)
        ^

During: resolving callee type: type(cpudispatcher(<function newton at 0x000002619E15C5E0>))
During: typing of call at <ipython-input-1-1251fd61b2b2> (35)

During: resolving callee type: type(cpudispatcher(<function newton at 0x000002619E15C5E0>))
During: typing of call at <ipython-input-1-1251fd61b2b2> (35)


File "<ipython-input-1-1251fd61b2b2>",line 35:
def ces_inv(y,ud):
    <source elided>
 args = (y,args) 
 ^

  @jit
C:\Users\Orphanides\anaconda3\lib\site-packages\numba\core\object_mode_passes.py:177: NumbaWarning: Function "ces_inv" was compiled in object mode without forceobj=True.

File "<ipython-input-1-1251fd61b2b2>",line 33:
@jit
def ces_inv(y,ud):
^

  warnings.warn(errors.NumbaWarning(warn_msg,C:\Users\Orphanides\anaconda3\lib\site-packages\numba\core\object_mode_passes.py:187: NumbaDeprecationWarning: 
Fall-back from the nopython compilation path to the object mode compilation path has been detected,this is deprecated behavIoUr.

For more @R_904_4045@ion visit https://numba.pydata.org/numba-doc/latest/reference/deprecation.html#deprecation-of-object-mode-fall-back-behavIoUr-when-using-jit

File "<ipython-input-1-1251fd61b2b2>",ud):
^

  warnings.warn(errors.NumbaDeprecationWarning(msg,Traceback (most recent call last):

  File "<ipython-input-1-1251fd61b2b2>",line 38,in <module>
    print(ces_inv(2,1)) #TypingError Occurs Here

  File "C:\Users\Orphanides\anaconda3\lib\site-packages\numba\core\dispatcher.py",line 415,in _compile_for_args
    error_rewrite(e,'typing')

  File "C:\Users\Orphanides\anaconda3\lib\site-packages\numba\core\dispatcher.py",line 358,in error_rewrite
    reraise(type(e),e,None)

  File "C:\Users\Orphanides\anaconda3\lib\site-packages\numba\core\utils.py",line 80,in reraise
    raise value.with_traceback(tb)

TypingError: Internal error at <numba.core.typeinfer.CallConstraint object at 0x000002619E33BB20>.
too many positional arguments
During: resolving callee type: type(cpudispatcher(<function ces_d2 at 0x000002619E1B15E0>))
During: typing of call at C:\Users\Orphanides\anaconda3\lib\site-packages\quantecon\optimize\root_finding.py (89)

Enable logging at debug level for details.

任何帮助将不胜感激,谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)