第一次写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