First time writing Python code. Need some help in graphing this function. It is an Overlapping Growth Model function. Keeps giving an error code even though I am sure the equation is correct. Any help would be appreciated!
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()
The error is:
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
If I add print statements to olgss(x)
, like so:
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
I get the following output:
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
So, it looks like the third call to olgss()
is returning a negative value, which then feeds back into the next call and causes the error.