寻根代码的 Python 类型错误(QuantEcon 包) [英] Python TypeError for Root-Finding Code(QuantEcon Package)

查看:39
本文介绍了寻根代码的 Python 类型错误(QuantEcon 包)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这里是 Python 的初学者,将其用于经济研究.我目前正在尝试运行代码以使用 Newton-Ralphson 方法(https://quanteconpy.readthedocs.io/en/latest/optimize/root_finding.html).

So beginner here at Python, using it for economic research. I am currently trying to run code to find the roots of a CES Function using the Newton-Ralphson Method (https://quanteconpy.readthedocs.io/en/latest/optimize/root_finding.html).

但是,我在这里遇到了一个错误

However, I am running into an error here where it says

类型错误:不支持 - 的操作数类型:‘CPUDispatcher’和‘int’".我不知道这意味着什么(我使用 Spyder for Python 3.8)所以任何帮助将不胜感激.我的代码附在下面.

"TypeError: unsupported operand type(s) for -: 'CPUDispatcher' and 'int'". I have no clue what this means (I am using Spyder for Python 3.8) so any help would be much appreciated. My code is attached below.

###QuantEcon Method###
from quantecon.optimize.root_finding import newton
from numba import jit

@jit 
def ces(x,y,s):
    U = (x**s + y**s)**(1/s)
    return U

@jit
def ces_d(x,y,s):
    U_x = (x**(s-1))*(x**s + y**s)**((1-s)/s) #analytical form hand-derived
    return U_x

@jit
def ces_d2(x,y,s):
    U_xx = (s-1)*x**(s-2)*y**s*(x**s + y**s)**(1/s-2)
    return U_xx

#root/inv of ces
def ces_inv(y,s,ud):
 args = (y,s) #order must be preserved
 x_bar = newton(ces_d - ud, 0 ,ces_d2, args)
 return x_bar

print(ces_inv(2,3,4))

我得到的错误是

"TypeError: unsupported operand type(s) for -: 'CPUDispatcher' and 'int'"

谢谢你让我知道这一切,我忘了附上 stracktrace.我已经把它贴在下面了(我还是个初学者,所以如果它正确,请告诉我).

Thanks for letting me know all, I have forgotten to attach the stracktrace. I've pasted it below (I am still a beginner so let me know if it is right).

    runcell(0, 'C:/Users/Orphanides/Documents/XXXX Projects/XXXX/Python/Draft.py')
    Traceback (most recent call last):

    File "C:\Users\Orphanides\Documents\XXXX\XXXX\Python\Draft.py", line 27, in <module>
print(ces_inv(2,3,4))

    File "C:\Users\Orphanides\Documents\XXXX Projects\XXXX\Python\Draft.py", line 24, in ces_inv
x_bar = newton(ces_d - ud, 0 ,ces_d2, args)

    TypeError: unsupported operand type(s) for -: 'CPUDispatcher' and 'int'

谢谢大家帮我解决这个问题.我已经修复了下面的代码,但现在唯一的问题是 jitted 函数 ces_d2:

Thank you all for helping me out with this. I have fixed up the code as below, but the only problem now is the jitted function ces_d2:

代码:

###QuantEcon Method###
from quantecon.optimize.root_finding import newton
from numba import jit

@jit 
def ces(x,y,s):
    U = (x**s + y**s)**(1/s)
    return U

@jit
def ces_d(x,y,s):
    U_x = (x**(s-1))*(x**s + y**s)**((1-s)/s) #analytical form hand-derived
    return U_x

@jit
def ces_dd(x,y,s,ud):
    U_xd = ces_d(x,y,s) - ud
    return U_xd 

@jit
def ces_d2(x,y,s):
    U_xx = (s-1)*x**(s-2)*y**s*(x**s + y**s)**(1/s-2) 
    return U_xx

#root/inv of ces
def ces_inv(y,s,ud):
 args = (y,s,ud) #order must be preserved
 x_bar = newton(ces_dd, 0 ,ces_d2, args)
 return x_bar

print(ces_inv(2,3,4))

堆栈跟踪:runfile('C:/Users/Orphanides/Documents/RA Projects/Isabella/Python/CES Functions/untitled1.py', wdir='C:/Users/Orphanides/Documents/RA Projects/Isabella/Python/CES Functions')回溯(最近一次调用):

StackTrace: runfile('C:/Users/Orphanides/Documents/RA Projects/Isabella/Python/CES Functions/untitled1.py', wdir='C:/Users/Orphanides/Documents/RA Projects/Isabella/Python/CES Functions') Traceback (most recent call last):

  File "C:\Users\Orphanides\Documents\RA Projects\Isabella\Python\CES Functions\untitled1.py", line 32, in <module>
    print(ces_inv(2,3,4))

  File "C:\Users\Orphanides\Documents\RA Projects\Isabella\Python\CES Functions\untitled1.py", line 29, in ces_inv
    x_bar = newton(ces_dd, 0 ,ces_d2, args)

  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 0x000001A489CE40D0>.
too many positional arguments
During: resolving callee type: type(CPUDispatcher(<function ces_d2 at 0x000001A488BF3670>))
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.

推荐答案

仅凭您提供的信息(即没有堆栈跟踪)很难确定,但我想我知道发生了什么.我猜错误是由这行代码抛出的:

It's hard to tell for sure with just the information you've provided (ie. without a stack trace), but I think I see what's going on. I'm guessing that the error is thrown by this line of code:

x_bar = newton(ces_d - ud, 0 ,ces_d2, args)

查看其余代码,ces_d 是一个函数引用,而 ud 是一个 int.从函数引用中减去整数值是没有意义的.这就是此错误消息告诉您的内容.我不明白为什么 ces_d 函数被报告为 CPUDispatcher 类型,但我希望它与 @jit装饰器.在任何情况下,表达式 ces_d - ud 都没有意义.

Looking at the rest of your code, ces_d is a function reference and ud is an int. It doesn't make sense to subtract an integer value from a function reference. That's what this error message is telling you. I don't understand why the ces_d function is being reported as being of type CPUDispatcher, but I expect that it has something to do with the @jit decorator. In any case, the expression ces_d - ud does not make sense.

我查看了 newton 方法,它接受的第一个参数是可调用的,因此将 ces_d 作为第一个参数.但是,为什么在将它传递给 newton 方法之前尝试从中减去 ud 呢?正如我之前解释的那样,这似乎不正确,而且在逻辑上毫无意义.

I took a look at the newton method, and the first parameter it accepts is a callable, so it seems to make sense for you to be passing ces_d as the first parameter. But why are you attempting to subtract ud from that before passing it to the newton method? That doesn't seem right, and makes no sense logically, as I explained earlier.

这篇关于寻根代码的 Python 类型错误(QuantEcon 包)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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