economics - Error in Python Code. How do I fix it? -
first time writing python code. need help in graphing function. overlapping growth model function. keeps giving error code though sure equation correct. help appreciated!
from numpy import * pylab import * scipy import optimize 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 homecoming olgk # set parameter values alpha = .3 # share of capital income in gross domestic product = 1.0 # productivity parameter beta = 0.8 # discount factor n = 0.01 # rate of growth of population sigma = 0.9 # intertemporal elasticity of substitution utility function # set inital status state= 0.2 xt = [] # x_t valudebuge # iterate few time steps niterates = 10 # plot lines, showing how iteration reflected off of identity 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 phone 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 raised fractional powerfulness
if add together print statements olgss(x)
, 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 homecoming olgk
i next output:
alpha 0.3 x 0.2 alpha 0.3 x 0.0126300785572 alpha 0.3 x -0.0251898297413 traceback (most recent phone 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 raised fractional powerfulness
so, looks 3rd phone call olgss()
returning negative value, feeds next phone call , causes error.
python economics
No comments:
Post a Comment