【问题标题】:Error in Python Code. How do I fix it?Python 代码中的错误。我如何解决它?
【发布时间】:2013-02-07 01:25:45
【问题描述】:

第一次写 Python 代码。在绘制此函数时需要一些帮助。它是一个重叠增长模型函数。即使我确定等式是正确的,也会不断给出错误代码。任何帮助将不胜感激!

from numpy import *
from pylab import *
from scipy import optimize
from scipy.optimize import fsolve
def olgss(x) :
    numg = ((1-alpha)*A*x**alpha)/(1+n)
    deng = (1+(1/(beta**(sigma)))*(1+alpha*A*x**(alpha-1))**(1-sigma))
    olgk = x - numg/deng
    return olgk

# Set the parameter values

alpha = .3 # share of capital income in GDP
A = 1.0 # productivity parameter
beta = 0.8 # discount factor
n = 0.01 # rate of growth of population
sigma = 0.9 # intertemporal elasticity of substitution from the utility function

# Set the inital condition
state= 0.2

xt = [] # The x_t valudebuge

# Iterate for a few time steps
nIterates = 10
# Plot lines, showing how the iteration is reflected off of the identity
for n in xrange(nIterates):
    xt.append(state)
    state = olgss(state)    
plot(xrange(nIterates), xt, 'b')
xlabel('Time')
ylabel('k$t$')
title('Time Path of k$t$')
#savefig('OLGTimePath', dpi=100)
show()

错误是:

Traceback (most recent call last): 
File "C:\Users\AChia\Documents\untitled1.py", line 37, in <module> 
   state = olgss(state) 
File "C:\Users\AChia\Documents\untitled1.py", line 14, in olgss 
   numg = ((1-alpha)*A*x**alpha)/(1+n) 
ValueError: negative number cannot be raised to a fractional power 

【问题讨论】:

  • Traceback(最近一次调用最后):文件“C:\Users\AChia\Documents\untitled1.py”,第 37 行,在 state = olgss(state) 文件“C:\ Users\AChia\Documents\untitled1.py",第 14 行,在 olgss 中 numg = ((1-alpha)*Ax*alpha)/(1+n) ValueError: 不能提高负数分数幂
  • 您是否注意到您将“n”用于两个不同的目的? 'for n in xrange(...)' 将覆盖你所说的 n = 0.1

标签: python economics


【解决方案1】:

如果我向olgss(x) 添加打印语句,如下所示:

def olgss(x) :
    print "alpha is", alpha
    print "x is", x
    numg = ((1-alpha)*A*x**alpha)/(1+n)
    deng = (1+(1/(beta**(sigma)))*(1+alpha*A*x**(alpha-1))**(1-sigma))
    olgk = x - numg/deng
    return olgk

我得到以下输出:

alpha is 0.3
x is 0.2
alpha is 0.3
x is 0.0126300785572
alpha is 0.3
x is -0.0251898297413
Traceback (most recent call last):
  File "globals.py", line 36, in ?
    state = olgss(state)
  File "globals.py", line 13, in olgss
    numg = ((1-alpha)*A*x**alpha)/(1+n)
ValueError: negative number cannot be raised to a fractional power

因此,对olgss() 的第三次调用似乎返回了一个负值,然后反馈到下一次调用并导致错误。

【讨论】:

  • 这意味着我需要的图最多可以迭代两次?我知道它应该以递减的速度增加,但只有两点,图表不会反映这一点。有没有办法绕过负数指数?
  • 我想我在这里帮不了你。如果你确定你的等式是正确的,那么它就是这样。
【解决方案2】:

您有一个负数 (x) 正在传递给函数。然后你将它提升到alpha(非整数)幂。将负数提升到非整数指数必然会导致复数——除非涉及的类型很复杂,否则显然 python 不喜欢的东西。

>>> (-0.9) ** -0.9
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> (-0.9+0j) ** -0.9
(-1.0456541539072963-0.3397536300522355j)

【讨论】:

  • 1 - alpha 只会在 alpha &gt; 1 时为负数
  • 你不能只从左到右; ** 的优先级高于 *。问题是x 有时是否定的。
  • @Cairnarvon -- 是的,当然。太累了。现在我需要弄清楚为什么x 有时是负面的。
  • @MatthewD -- 是的,当我最初阅读问题时(在 OP 提供回溯之前),我认为错误出现在 alpha - 1 的下一行。我想出了解释并没有注意到在它转到1 - alpha之前就上线了。感谢您指出这一点。
【解决方案3】:

打印state,你会看到它在第三次迭代中变成了负数。然后,在olgss 中,您有x ** (1 - alpha),这意味着您将负数 (x) 提高到分数幂 (1-alpha)。 ** 不允许这样做。

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 2023-01-12
    相关资源
    最近更新 更多