Question

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 
Était-ce utile?

La solution

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.

Autres conseils

you have a negative number (x) which is getting passed into the function. You are then raising it to the alpha (non-integer) power. A negative number raised to a non-integer exponent will necessarily result in a complex number -- Something which apparently python doesn't like unless the types involved are complex.

>>> (-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)

Print state, and you'll see that it becomes negative in the third iterate. Then, in olgss, you have x ** (1 - alpha), which means you are raising a negative number (x) to a fractional power (1-alpha). This is not allowed by **.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top