问题
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
"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))
And the error I get is
"TypeError: unsupported operand type(s) for -: 'CPUDispatcher' and 'int'"
EDIT: 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'
EDIT2: 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:
Code:
###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))
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.
回答1:
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)
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.
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.
来源:https://stackoverflow.com/questions/65434506/python-typeerror-for-root-finding-codequantecon-package