diff --git a/MANIFEST b/MANIFEST index 1b5743941..f995c1bef 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,4 +1,6 @@ # file GENERATED by distutils, do NOT edit +LICENSE.txt +README.md setup.cfg setup.py quantecon/__init__.py @@ -35,6 +37,11 @@ quantecon/models/jv.py quantecon/models/lucastree.py quantecon/models/odu.py quantecon/models/optgrowth.py +quantecon/models/solow/__init__.py +quantecon/models/solow/ces.py +quantecon/models/solow/cobb_douglas.py +quantecon/models/solow/impulse_response.py +quantecon/models/solow/model.py quantecon/tests/__init__.py quantecon/tests/test_arma.py quantecon/tests/test_cartesian.py diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 000000000..7ea28439f --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +include LICENSE.txt \ No newline at end of file diff --git a/examples/LucasStokey.py b/examples/LucasStokey.py new file mode 100644 index 000000000..687f94899 --- /dev/null +++ b/examples/LucasStokey.py @@ -0,0 +1,437 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Feb 18 15:43:37 2015 + +@author: dgevans +""" +import numpy as np +from scipy.optimize import root +from scipy.optimize import fmin_slsqp +from scipy.interpolate import UnivariateSpline +from quantecon import compute_fixed_point +from quantecon.mc_tools import mc_sample_path + + +class Planners_Allocation_Sequential(object): + ''' + Class returns planner's allocation as a function of the multiplier on the + implementability constraint mu + ''' + def __init__(self,Para): + ''' + Initializes the class from the calibration Para + ''' + self.beta = Para.beta + self.Pi = Para.Pi + self.G = Para.G + self.S = len(Para.Pi) # number of states + self.Theta = Para.Theta + self.Para = Para + #now find the first best allocation + self.find_first_best() + + def find_first_best(self): + ''' + Find the first best allocation + ''' + Para = self.Para + S,Theta,Uc,Un,G = self.S,self.Theta,Para.Uc,Para.Un,self.G + + def res(z): + c = z[:S] + n = z[S:] + return np.hstack( + [Theta*Uc(c,n)+Un(c,n), Theta*n - c - G] + ) + res = root(res,0.5*np.ones(2*S)) + + if not res.success: + raise Exception('Could not find first best') + + self.cFB = res.x[:S] + self.nFB = res.x[S:] + self.XiFB = Uc(self.cFB,self.nFB) #multiplier on the resource constraint. + self.zFB = np.hstack([self.cFB,self.nFB,self.XiFB]) + + def time1_allocation(self,mu): + ''' + Computes optimal allocation for time t\geq 1 for a given \mu + ''' + Para = self.Para + S,Theta,G,Uc,Ucc,Un,Unn = self.S,self.Theta,self.G,Para.Uc,Para.Ucc,Para.Un,Para.Unn + def FOC(z): + c = z[:S] + n = z[S:2*S] + Xi = z[2*S:] + return np.hstack([ + Uc(c,n) - mu*(Ucc(c,n)*c+Uc(c,n)) -Xi, #foc c + Un(c,n) - mu*(Unn(c,n)*n+Un(c,n)) + Theta*Xi, #foc n + Theta*n - c - G #resource constraint + ]) + + #find the root of the FOC + res = root(FOC,self.zFB) + if not res.success: + raise Exception('Could not find LS allocation.') + z = res.x + c,n,Xi = z[:S],z[S:2*S],z[2*S:] + + #now compute x + I = Uc(c,n)*c + Un(c,n)*n + x = np.linalg.solve(np.eye(S) - self.beta*self.Pi, I ) + + return c,n,x,Xi + + def time0_allocation(self,B_,s_0): + ''' + Finds the optimal allocation given initial government debt B_ and state s_0 + ''' + Para,Pi,Theta,G,beta = self.Para,self.Pi,self.Theta,self.G,self.beta + Uc,Ucc,Un,Unn = Para.Uc,Para.Ucc,Para.Un,Para.Unn + + #first order conditions of planner's problem + def FOC(z): + mu,c,n,Xi = z + xprime = self.time1_allocation(mu)[2] + return np.hstack([ + Uc(c,n)*(c-B_) + Un(c,n)*n + beta*Pi[s_0].dot(xprime), + Uc(c,n) - mu*(Ucc(c,n)*(c-B_) + Uc(c,n)) - Xi, + Un(c,n) - mu*(Unn(c,n)*n+Un(c,n)) + Theta[s_0]*Xi, + (Theta*n - c - G)[s_0] + ]) + + #find root + res = root(FOC,np.array([0.,self.cFB[s_0],self.nFB[s_0],self.XiFB[s_0]])) + if not res.success: + raise Exception('Could not find time 0 LS allocation.') + + return res.x + + def time1_value(self,mu): + ''' + Find the value associated with multiplier mu + ''' + c,n,x,Xi = self.time1_allocation(mu) + U = self.Para.U(c,n) + V = np.linalg.solve(np.eye(self.S) - self.beta*self.Pi, U ) + return c,n,x,V + + def Tau(self,c,n): + ''' + Computes Tau given c,n + ''' + Para = self.Para + Uc,Un = Para.Uc(c,n),Para.Un(c,n) + + return 1+Un/(self.Theta * Uc) + + def simulate(self,B_,s_0,T,sHist=None): + ''' + Simulates planners policies for T periods + ''' + Para,Pi,beta = self.Para,self.Pi,self.beta + Uc = Para.Uc + + + if sHist == None: + sHist = mc_sample_path(Pi,s_0,T) + + cHist,nHist,Bhist,TauHist,muHist = np.zeros((5,T)) + RHist = np.zeros(T-1) + #time0 + mu,cHist[0],nHist[0],_ = self.time0_allocation(B_,s_0) + TauHist[0] = self.Tau(cHist[0],nHist[0])[s_0] + Bhist[0] = B_ + muHist[0] = mu + + #time 1 onward + for t in range(1,T): + c,n,x,Xi = self.time1_allocation(mu) + Tau = self.Tau(c,n) + u_c = Uc(c,n) + s = sHist[t] + Eu_c = Pi[sHist[t-1]].dot(u_c) + + cHist[t],nHist[t],Bhist[t],TauHist[t] = c[s],n[s],x[s]/u_c[s],Tau[s] + + RHist[t-1] = Uc(cHist[t-1],nHist[t-1])/(beta*Eu_c) + muHist[t] = mu + + return cHist,nHist,Bhist,TauHist,sHist,muHist,RHist + + + + +class Planners_Allocation_Bellman(object): + ''' + Compute the planner's allocation by solving Bellman + equation. + ''' + def __init__(self,Para,mugrid): + ''' + Initializes the class from the calibration Para + ''' + self.beta = Para.beta + self.Pi = Para.Pi + self.G = Para.G + self.S = len(Para.Pi) # number of states + self.Theta = Para.Theta + self.Para = Para + self.mugrid = mugrid + + #now find the first best allocation + self.solve_time1_bellman() + self.T.time_0 = True #Bellman equation now solves time 0 problem + + def solve_time1_bellman(self): + ''' + Solve the time 1 Bellman equation for calibration Para and initial grid mugrid0 + ''' + Para,mugrid0 = self.Para,self.mugrid + S = len(Para.Pi) + + #First get initial fit + PP = Planners_Allocation_Sequential(Para) + c,n,x,V = map(np.vstack, zip(*map(lambda mu: PP.time1_value(mu),mugrid0)) ) + + Vf,cf,nf,xprimef = {},{},{},{} + for s in range(2): + cf[s] = UnivariateSpline(x[:,s],c[:,s]) + nf[s] = UnivariateSpline(x[:,s],n[:,s]) + Vf[s] = UnivariateSpline(x[:,s],V[:,s]) + for sprime in range(S): + xprimef[s,sprime] = UnivariateSpline(x[:,s],x[:,s]) + policies = [cf,nf,xprimef] + + + #create xgrid + xbar = [x.min(0).max(),x.max(0).min()] + xgrid = np.linspace(xbar[0],xbar[1],len(mugrid0)) + self.xgrid = xgrid + + #Now iterate on bellman equation + T = BellmanEquation(Para,xgrid,policies) + diff = 1. + while diff > 1e-5: + PF = T(Vf) + + Vfnew,policies = self.fit_policy_function(PF) + + diff = 0. + for s in range(S): + diff = max(diff, np.abs((Vf[s](xgrid)-Vfnew[s](xgrid))/Vf[s](xgrid)).max() ) + + print diff + Vf = Vfnew + + #store value function policies and Bellman Equations + self.Vf = Vf + self.policies = policies + self.T = T + + def fit_policy_function(self,PF): + ''' + Fits the policy functions PF using the points xgrid using UnivariateSpline + ''' + xgrid,S = self.xgrid,self.S + + Vf,cf,nf,xprimef = {},{},{},{} + for s in range(S): + PFvec = np.vstack(map(lambda x:PF(x,s),xgrid)) + Vf[s] = UnivariateSpline(xgrid,PFvec[:,0],s=0) + cf[s] = UnivariateSpline(xgrid,PFvec[:,1],s=0,k=1) + nf[s] = UnivariateSpline(xgrid,PFvec[:,2],s=0,k=1) + for sprime in range(S): + xprimef[s,sprime] = UnivariateSpline(xgrid,PFvec[:,3+sprime],s=0,k=1) + + return Vf,[cf,nf,xprimef] + + def Tau(self,c,n): + ''' + Computes Tau given c,n + ''' + Para = self.Para + Uc,Un = Para.Uc(c,n),Para.Un(c,n) + + return 1+Un/(self.Theta * Uc) + + def time0_allocation(self,B_,s0): + ''' + Finds the optimal allocation given initial government debt B_ and state s_0 + ''' + PF = self.T(self.Vf) + + z0 = PF(B_,s0) + c0,n0,xprime0 = z0[1],z0[2],z0[3:] + return c0,n0,xprime0 + + def simulate(self,B_,s_0,T,sHist=None): + ''' + Simulates Ramsey plan for T periods + ''' + Para,Pi = self.Para,self.Pi + Uc = Para.Uc + cf,nf,xprimef = self.policies + + if sHist == None: + sHist = mc_sample_path(Pi,s_0,T) + + cHist,nHist,Bhist,TauHist,muHist = np.zeros((5,T)) + RHist = np.zeros(T-1) + #time0 + cHist[0],nHist[0],xprime = self.time0_allocation(B_,s_0) + TauHist[0] = self.Tau(cHist[0],nHist[0])[s_0] + Bhist[0] = B_ + muHist[0] = 0. + + #time 1 onward + for t in range(1,T): + s,x = sHist[t],xprime[sHist[t]] + c,n,xprime = np.empty(self.S),nf[s](x),np.empty(self.S) + for shat in range(self.S): + c[shat] = cf[shat](x) + for sprime in range(self.S): + xprime[sprime] = xprimef[s,sprime](x) + + Tau = self.Tau(c,n)[s] + u_c = Uc(c,n) + Eu_c = Pi[sHist[t-1]].dot(u_c) + muHist[t] = self.Vf[s](x,1) + + RHist[t-1] = Uc(cHist[t-1],nHist[t-1])/(self.beta*Eu_c) + + cHist[t],nHist[t],Bhist[t],TauHist[t] = c[s],n,x/u_c[s],Tau + + return cHist,nHist,Bhist,TauHist,sHist,muHist,RHist + +class BellmanEquation(object): + ''' + Bellman equation for the continuation of the Lucas-Stokey Problem + ''' + def __init__(self,Para,xgrid,policies0): + ''' + Initializes the class from the calibration Para + ''' + self.beta = Para.beta + self.Pi = Para.Pi + self.G = Para.G + self.S = len(Para.Pi) # number of states + self.Theta = Para.Theta + self.Para = Para + + self.xbar = [min(xgrid),max(xgrid)] + self.time_0 = False + + self.z0 = {} + cf,nf,xprimef = policies0 + for s in range(self.S): + for x in xgrid: + xprime0 = np.empty(self.S) + for sprime in range(self.S): + xprime0[sprime] = xprimef[s,sprime](x) + self.z0[x,s] = np.hstack([cf[s](x),nf[s](x),xprime0]) + + self.find_first_best() + + def find_first_best(self): + ''' + Find the first best allocation + ''' + Para = self.Para + S,Theta,Uc,Un,G = self.S,self.Theta,Para.Uc,Para.Un,self.G + + def res(z): + c = z[:S] + n = z[S:] + return np.hstack( + [Theta*Uc(c,n)+Un(c,n), Theta*n - c - G] + ) + res = root(res,0.5*np.ones(2*S)) + if not res.success: + raise Exception('Could not find first best') + + self.cFB = res.x[:S] + self.nFB = res.x[S:] + IFB = Uc(self.cFB,self.nFB)*self.cFB + Un(self.cFB,self.nFB)*self.nFB + + self.xFB = np.linalg.solve(np.eye(S) - self.beta*self.Pi, IFB) + + self.zFB = {} + for s in range(S): + self.zFB[s] = np.hstack([self.cFB[s],self.nFB[s],self.xFB]) + + + + def __call__(self,Vf): + ''' + Given continuation value function next period return value function this + period return T(V) and optimal policies + ''' + if not self.time_0: + PF = lambda x,s: self.get_policies_time1(x,s,Vf) + else: + PF = lambda B_,s0: self.get_policies_time0(B_,s0,Vf) + return PF + + def get_policies_time1(self,x,s,Vf): + ''' + Finds the optimal policies + ''' + Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi + U,Uc,Un = Para.U,Para.Uc,Para.Un + + def objf(z): + c,n,xprime = z[0],z[1],z[2:] + Vprime = np.empty(S) + for sprime in range(S): + Vprime[sprime] = Vf[sprime](xprime[sprime]) + + return -(U(c,n)+beta*Pi[s].dot(Vprime)) + + def cons(z): + c,n,xprime = z[0],z[1],z[2:] + return np.hstack([ + x - Uc(c,n)*c-Un(c,n)*n - beta*Pi[s].dot(xprime), + (Theta*n - c - G)[s] + ]) + + + out,fx,_,imode,smode = fmin_slsqp(objf,self.z0[x,s],f_eqcons=cons, + bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0) + + if imode >0: + raise Exception(smode) + + self.z0[x,s] = out + return np.hstack([-fx,out]) + + def get_policies_time0(self,B_,s0,Vf): + ''' + Finds the optimal policies + ''' + Para,beta,Theta,G,S,Pi = self.Para,self.beta,self.Theta,self.G,self.S,self.Pi + U,Uc,Un = Para.U,Para.Uc,Para.Un + + def objf(z): + c,n,xprime = z[0],z[1],z[2:] + Vprime = np.empty(S) + for sprime in range(S): + Vprime[sprime] = Vf[sprime](xprime[sprime]) + + return -(U(c,n)+beta*Pi[s0].dot(Vprime)) + + def cons(z): + c,n,xprime = z[0],z[1],z[2:] + return np.hstack([ + -Uc(c,n)*(c-B_)-Un(c,n)*n - beta*Pi[s0].dot(xprime), + (Theta*n - c - G)[s0] + ]) + + + out,fx,_,imode,smode = fmin_slsqp(objf,self.zFB[s0],f_eqcons=cons, + bounds=[(0.,100),(0.,100)]+[self.xbar]*S,full_output=True,iprint=0) + + if imode >0: + raise Exception(smode) + + return np.hstack([-fx,out]) \ No newline at end of file diff --git a/examples/calibrations/BGP.py b/examples/calibrations/BGP.py new file mode 100644 index 000000000..44c30cdac --- /dev/null +++ b/examples/calibrations/BGP.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Feb 18 15:32:07 2015 + +@author: dgevans +""" +import numpy as np + +class baseline(object): + beta = 0.9 + psi = 0.69 + + Pi = 0.5 *np.ones((2,2)) + + G = np.array([0.1,0.2]) + + Theta = np.ones(2) + + transfers = False + + #derivatives of utiltiy function + def U(self,c,n): + return np.log(c) + self.psi*np.log(1-n) + + def Uc(self,c,n): + return 1./c + + def Ucc(self,c,n): + return -c**(-2) + + def Un(self,c,n): + return -self.psi/(1-n) + + def Unn(self,c,n): + return -self.psi/(1-n)**2 + + +#Model 1 +M1 = baseline() + +#Model 2 + +M2 = baseline() +M2.G = np.array([0.15]) +M2.Pi = np.ones((1,1)) +M2.Theta = np.ones(1) + +#Model 3 with time varying + +M_time_example = baseline() + +M_time_example.Pi = np.array([[0., 1., 0., 0., 0., 0.], + [0., 0., 1., 0., 0., 0.], + [0., 0., 0., 0.5, 0.5, 0.], + [0., 0., 0., 0., 0., 1.], + [0., 0., 0., 0., 0., 1.], + [0., 0., 0., 0., 0., 1.]]) + +M_time_example.G = np.array([0.1, 0.1, 0.1, 0.2, 0.1, 0.1]) +M_time_example.Theta = np.ones(6) # Theta can in principle be random \ No newline at end of file diff --git a/examples/calibrations/CES.py b/examples/calibrations/CES.py new file mode 100644 index 000000000..6c8144ef8 --- /dev/null +++ b/examples/calibrations/CES.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +""" +Created on Tue Mar 10 08:55:03 2015 + +@author: dgevans +""" + +import numpy as np + +class baseline(object): + beta = 0.9 + + sigma = 2. + + gamma = 2. + + Pi = 0.5 *np.ones((2,2)) + + G = np.array([0.1,0.2]) + + Theta = np.ones(2) + + transfers = False + + #derivatives of utiltiy function + def U(self,c,n): + sigma = self.sigma + if sigma == 1.: + U = np.log(c) + else: + U = (c**(1-sigma)-1)/(1-sigma) + return U - n**(1+self.gamma)/(1+self.gamma) + + def Uc(self,c,n): + return c**(-self.sigma) + + def Ucc(self,c,n): + return -self.sigma*c**(-self.sigma-1.) + + def Un(self,c,n): + return -n**self.gamma + + def Unn(self,c,n): + return -self.gamma * n**(self.gamma-1.) + +#Model 1 +M1 = baseline() + +#Model 2 + +M2 = baseline() +M2.G = np.array([0.15]) +M2.Pi = np.ones((1,1)) +M2.Theta = np.ones(1) + +#Model 3 with time varying + +M_time_example = baseline() + +M_time_example.Pi = np.array([[0., 1., 0., 0., 0., 0.], + [0., 0., 1., 0., 0., 0.], + [0., 0., 0., 0.5, 0.5, 0.], + [0., 0., 0., 0., 0., 1.], + [0., 0., 0., 0., 0., 1.], + [0., 0., 0., 0., 0., 1.]]) + +M_time_example.G = np.array([0.1, 0.1, 0.1, 0.2, 0.1, 0.1]) +M_time_example.Theta = np.ones(6) # Theta can in principle be random \ No newline at end of file diff --git a/examples/calibrations/__init__.py b/examples/calibrations/__init__.py new file mode 100644 index 000000000..a0a54464b --- /dev/null +++ b/examples/calibrations/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +""" +Created on Wed Feb 18 16:13:07 2015 + +@author: dgevans +""" + diff --git a/examples/main_LS.py b/examples/main_LS.py new file mode 100644 index 000000000..90ae5cceb --- /dev/null +++ b/examples/main_LS.py @@ -0,0 +1,162 @@ +# -*- coding: utf-8 -*- +""" +Created on Fri Feb 20 14:07:56 2015 + +@author: dgevans +""" +import matplotlib.pyplot as plt +import numpy as np +import LucasStokey as LS +from calibrations.BGP import M1 +from calibrations.CES import M2 +from calibrations.CES import M_time_example + + +''' +Time Varying Example +''' + +PP_seq_time = LS.Planners_Allocation_Sequential(M_time_example) #solve sequential problem + +sHist_h = np.array([0,1,2,3,5,5,5]) +sHist_l = np.array([0,1,2,4,5,5,5]) + +sim_seq_h = PP_seq_time.simulate(1.,0,7,sHist_h) +sim_seq_l = PP_seq_time.simulate(1.,0,7,sHist_l) + +plt.figure(figsize=[14,10]) +plt.subplot(3,2,1) +plt.title('Consumption') +plt.plot(sim_seq_l[0],'-ok') +plt.plot(sim_seq_h[0],'-or') +plt.subplot(3,2,2) +plt.title('Labor Supply') +plt.plot(sim_seq_l[1],'-ok') +plt.plot(sim_seq_h[1],'-or') +plt.subplot(3,2,3) +plt.title('Government Debt') +plt.plot(sim_seq_l[2],'-ok') +plt.plot(sim_seq_h[2],'-or') +plt.subplot(3,2,4) +plt.title('Taxe Rate') +plt.plot(sim_seq_l[3],'-ok') +plt.plot(sim_seq_h[3],'-or') +plt.subplot(3,2,5) +plt.title('Government Spending') +plt.plot(M_time_example.G[sHist_l],'-ok') +plt.plot(M_time_example.G[sHist_h],'-or') +plt.subplot(3,2,6) +plt.title('Output') +plt.plot(M_time_example.Theta[sHist_l]*sim_seq_l[1],'-ok') +plt.plot(M_time_example.Theta[sHist_h]*sim_seq_h[1],'-or') + +plt.tight_layout() +plt.savefig('TaxSequence_time_varying.png') + +plt.figure(figsize=[8,5]) +plt.title('Gross Interest Rate') +plt.plot(sim_seq_l[-1],'-ok') +plt.plot(sim_seq_h[-1],'-or') +plt.tight_layout() +plt.savefig('InterestRate_time_varying.png') + +''' +Time 0 example +''' +PP_seq_time0 = LS.Planners_Allocation_Sequential(M2) #solve sequential problem + +B_vec = np.linspace(-1.5,1.,100) +taxpolicy = np.vstack([PP_seq_time0.simulate(B_,0,2)[3] for B_ in B_vec]) +interest_rate = np.vstack([PP_seq_time0.simulate(B_,0,3)[-1] for B_ in B_vec]) + +plt.figure(figsize=[14,6]) +plt.subplot(211) +plt.plot(B_vec,taxpolicy[:,0],linewidth=2.) +plt.plot(B_vec,taxpolicy[:,1],linewidth=2.) + +plt.title('Tax Rate') +plt.legend((r'Time $t=0$', 'Time $t\geq1$'),loc=2,shadow=True) +plt.subplot(212) +plt.title('Gross Interest Rate') +plt.plot(B_vec,interest_rate[:,0],linewidth=2.) +plt.plot(B_vec,interest_rate[:,1],linewidth=2.) +plt.xlabel('Initial Government Debt') +plt.tight_layout() + +plt.savefig('Time0_taxpolicy.png') + + + + +#compute the debt entered with at time 1 +B1_vec = np.hstack([PP_seq_time0.simulate(B_,0,2)[2][1] for B_ in B_vec]) +#now compute the optimal policy if the government could reset +tau1_reset = np.hstack([PP_seq_time0.simulate(B1,0,1)[3] for B1 in B1_vec]) + +plt.figure(figsize=[10,6]) +plt.plot(B_vec,taxpolicy[:,1],linewidth=2.) +plt.plot(B_vec,tau1_reset,linewidth=2.) +plt.xlabel('Initial Government Debt') +plt.title('Tax Rate') +plt.legend((r'$\tau_1$', r'$\tau_1^R$'),loc=2,shadow=True) +plt.tight_layout() + +plt.savefig('Time0_inconsistent.png') + + +''' +BGP Example +''' +#initialize mugrid for value function iteration +muvec = np.linspace(-0.6,0.0,200) + + +PP_seq = LS.Planners_Allocation_Sequential(M1) #solve sequential problem +PP_bel = LS.Planners_Allocation_Bellman(M1,muvec) #solve recursive problem + +T = 20 +#sHist = utilities.simulate_markov(M1.Pi,0,T) +sHist = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0],dtype=int) + +#simulate +sim_seq = PP_seq.simulate(0.5,0,T,sHist) +sim_bel = PP_bel.simulate(0.5,0,T,sHist) + +#plot policies +plt.figure(figsize=[14,10]) +plt.subplot(3,2,1) +plt.title('Consumption') +plt.plot(sim_seq[0],'-ok') +plt.plot(sim_bel[0],'-xk') +plt.legend(('Sequential','Recursive'),loc='best') +plt.subplot(3,2,2) +plt.title('Labor Supply') +plt.plot(sim_seq[1],'-ok') +plt.plot(sim_bel[1],'-xk') +plt.subplot(3,2,3) +plt.title('Government Debt') +plt.plot(sim_seq[2],'-ok') +plt.plot(sim_bel[2],'-xk') +plt.subplot(3,2,4) +plt.title('Tax Rate') +plt.plot(sim_seq[3],'-ok') +plt.plot(sim_bel[3],'-xk') +plt.subplot(3,2,5) +plt.title('Government Spending') +plt.plot(M1.G[sHist],'-ok') +plt.plot(M1.G[sHist],'-xk') +plt.plot(M1.G[sHist],'-^k') +plt.subplot(3,2,6) +plt.title('Output') +plt.plot(M1.Theta[sHist]*sim_seq[1],'-ok') +plt.plot(M1.Theta[sHist]*sim_bel[1],'-xk') + +plt.tight_layout() +plt.savefig('TaxSequence_LS.png') + +plt.figure(figsize=[8,5]) +plt.title('Gross Interest Rate') +plt.plot(sim_seq[-1],'-ok') +plt.plot(sim_bel[-1],'-xk') +plt.legend(('Sequential','Recursive'),loc='best') +plt.tight_layout() diff --git a/quantecon/__init__.py b/quantecon/__init__.py index 69d37431a..b1ea9f8db 100644 --- a/quantecon/__init__.py +++ b/quantecon/__init__.py @@ -23,6 +23,7 @@ from .robustlq import RBLQ from .tauchen import approx_markov from . import quad as quad +from .utilities import searchsorted #Add Version Attribute from .version import version as __version__ diff --git a/quantecon/common_messages.py b/quantecon/common_messages.py new file mode 100644 index 000000000..bf3065e3e --- /dev/null +++ b/quantecon/common_messages.py @@ -0,0 +1,10 @@ +""" +Warnings Module +=============== + +Contains a collection of warning messages for consistent package wide notifications + +""" + +#-Numba-# +numba_import_fail_message = "Numba import failed. Falling back to non-optimized routine." \ No newline at end of file diff --git a/quantecon/external.py b/quantecon/external.py new file mode 100644 index 000000000..ec21eac24 --- /dev/null +++ b/quantecon/external.py @@ -0,0 +1,22 @@ +""" +External Module +=============== + +This module is an import location for flexible dependancies on external packages such as numba + +Packages +-------- + 1. numba + +""" + +#-Setup Installed Indicator and Import JIT function for NUMBA-# + +numba_installed = True +try: + from numba import jit +except ImportError: + numba_installed = False + jit = None + from .common_messages import numba_import_fail_message + warnings.warn(numba_import_fail_message, UserWarning) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index 2211545d5..4f3de1064 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -8,14 +8,17 @@ """ import numpy as np +from numba import jit -try: - xrange -except: # python3 - xrange = range +from .external import numba_installed, jit +if not numba_installed: + try: + xrange + except: # python3 + xrange = range -def gth_solve(A, overwrite=False): +def gth_solve(A): r""" This routine computes the stationary distribution of an irreducible Markov transition matrix (stochastic matrix) or transition rate @@ -42,8 +45,6 @@ def gth_solve(A, overwrite=False): ---------- A : array_like(float, ndim=2) Stochastic matrix or generator matrix. Must be of shape n x n. - overwrite : bool, optional(default=False) - Whether to overwrite `A`; may improve performance. Returns ------- @@ -60,34 +61,85 @@ def gth_solve(A, overwrite=False): Simulation, Princeton University Press, 2009. """ - A1 = np.array(A, dtype=float, copy=not overwrite) + A1 = np.array(A, dtype=float, copy=True, order='C') + # `order='C'` is for use with Numba <= 0.18.2 + # See issue github.com/numba/numba/issues/1103 if len(A1.shape) != 2 or A1.shape[0] != A1.shape[1]: raise ValueError('matrix must be square') n = A1.shape[0] - x = np.zeros(n) + if numba_installed: + _gth_solve_jit(A1, x) + return x + + # if not numba_installed # === Reduction === # - for i in xrange(n-1): - scale = np.sum(A1[i, i+1:n]) + for k in xrange(n-1): + scale = np.sum(A1[k, k+1:n]) if scale <= 0: # There is one (and only one) recurrent class contained in - # {0, ..., i}; + # {0, ..., k}; # compute the solution associated with that recurrent class. - n = i+1 + n = k+1 break - A1[i+1:n, i] /= scale + A1[k+1:n, k] /= scale - A1[i+1:n, i+1:n] += np.dot(A1[i+1:n, i:i+1], A1[i:i+1, i+1:n]) + A1[k+1:n, k+1:n] += np.dot(A1[k+1:n, k:k+1], A1[k:k+1, k+1:n]) # === Backward substitution === # x[n-1] = 1 - for i in xrange(n-2, -1, -1): - x[i] = np.dot(x[i+1:n], A1[i+1:n, i]) + for k in xrange(n-2, -1, -1): + x[k] = np.dot(x[k+1:n], A1[k+1:n, k]) # === Normalization === # x /= np.sum(x) return x + + +if numba_installed: + @jit(nopython=True) + def _gth_solve_jit(A, out): + """ + JIT complied version of the main routine of gth_solve. + + Parameters + ---------- + A : numpy.ndarray(float, ndim=2) + Stochastic matrix or generator matrix. Must be of shape n x n. + Data will be overwritten. + + out : numpy.ndarray(float, ndim=1) + Output array in which to place the stationary distribution of A. + + """ + n = A.shape[0] + + # === Reduction === # + for k in range(n-1): + scale = np.sum(A[k, k+1:n]) + if scale <= 0: + # There is one (and only one) recurrent class contained in + # {0, ..., k}; + # compute the solution associated with that recurrent class. + n = k+1 + break + for i in range(k+1, n): + A[i, k] /= scale + + for j in range(k+1, n): + A[i, j] += A[i, k] * A[k, j] + + # === Backward substitution === # + out[n-1] = 1 + for k in range(n-2, -1, -1): + for i in range(k+1, n): + out[k] += out[i] * A[i, k] + + # === Normalization === # + norm = np.sum(out) + for k in range(n): + out[k] /= norm diff --git a/quantecon/lss.py b/quantecon/lss.py index 488df145d..daa823c84 100644 --- a/quantecon/lss.py +++ b/quantecon/lss.py @@ -4,20 +4,15 @@ Computes quantities associated with the Gaussian linear state space model. """ + from textwrap import dedent import numpy as np from numpy.random import multivariate_normal from scipy.linalg import solve import warnings -numba_installed = True -try: - from numba import jit -except ImportError: - numba_installed = False - numba_warning_message = "Numba import failed. Falling back to non-optimized routine." - warnings.warn(numba_warning_message, UserWarning) - +#-Check if Numba is Available-# +from .external import numba_installed, jit def simulate_linear_model(A, x0, v, ts_length): """ diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 6e383ec5e..31da5a44a 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -88,11 +88,13 @@ import numpy as np from fractions import gcd import sys -from .discrete_rv import DiscreteRV from .graph_tools import DiGraph from .gth_solve import gth_solve from warnings import warn +#-Check if Numba is Available-# +from .external import numba_installed, jit +from .utilities import searchsorted class MarkovChain(object): """ @@ -257,7 +259,6 @@ def stationary_distributions(self): def simulate(self, init=0, sample_size=1000): X = mc_sample_path(self.P, init, sample_size) - return X @@ -277,31 +278,69 @@ def mc_compute_stationary(P): def mc_sample_path(P, init=0, sample_size=1000): + """ + See Section: DocStrings below + """ + n = len(P) + + # CDFs, one for each row of P + cdfs = np.empty((n, n), order='C') # see issue #137#issuecomment-96128186 + np.cumsum(P, axis=-1, out=cdfs) + + # Random values, uniformly sampled from [0, 1) + u = np.random.random(size=sample_size) + # === set up array to store output === # X = np.empty(sample_size, dtype=int) if isinstance(init, int): X[0] = init else: - X[0] = DiscreteRV(init).draw() + cdf0 = np.cumsum(init) + X[0] = searchsorted(cdf0, u[0]) - # === turn each row into a distribution === # - # In particular, let P_dist[i] be the distribution corresponding to the - # i-th row P[i,:] - n = len(P) - P_dist = [DiscreteRV(P[i, :]) for i in range(n)] + # === generate the sample path === # + for t in range(sample_size-1): + X[t+1] = searchsorted(cdfs[X[t]], u[t+1]) + + return X + + +def mc_sample_path_numpy(P, init=0, sample_size=1000): + """ + See Section: DocStrings below + """ + # CDFs, one for each row of P + cdfs = np.cumsum(P, axis=-1) + + # Random values, uniformly sampled from [0, 1) + u = np.random.random(size=sample_size) + + # === set up array to store output === # + X = np.empty(sample_size, dtype=int) + if isinstance(init, int): + X[0] = init + else: + cdf0 = np.cumsum(init) + X[0] = cdf0.searchsorted(u[0], side='right') # === generate the sample path === # - for t in range(sample_size - 1): - X[t+1] = P_dist[X[t]].draw() + for t in range(sample_size-1): + X[t+1] = cdfs[X[t]].searchsorted(u[t+1], side='right') return X +if numba_installed: + mc_sample_path = jit(mc_sample_path) +else: + mc_sample_path = mc_sample_path_numpy + + -# ------------------------------------------------------------------- # -# Set up the docstrings for the functions -# ------------------------------------------------------------------- # +#------------# +#-DocStrings-# +#------------# -# For drawing a sample path +#-mc_sample_path() function and MarkovChain.simulate() method-# _sample_path_docstr = \ """ Generates one sample path from the Markov chain represented by (n x n) @@ -309,7 +348,8 @@ def mc_sample_path(P, init=0, sample_size=1000): Parameters ---------- -{p_arg}init : array_like(float ndim=1) or scalar(int) +{p_arg} +init : array_like(float ndim=1) or scalar(int), optional(default=0) If init is an array_like, then it is treated as the initial distribution across states. If init is a scalar, then it treated as the deterministic initial state. @@ -324,14 +364,21 @@ def mc_sample_path(P, init=0, sample_size=1000): """ -# set docstring for functions +#-Functions-# + +#-mc_sample_path-# mc_sample_path.__doc__ = _sample_path_docstr.format(p_arg=""" - P : array_like(float, ndim=2) +P : array_like(float, ndim=2) + A Markov transition matrix. +""") +mc_sample_path_numpy.__doc__ = _sample_path_docstr.format(p_arg=""" +P : array_like(float, ndim=2) A Markov transition matrix. - """) +""") -# set docstring for methods +#-Methods-# +#-Markovchain.simulate()-# if sys.version_info[0] == 3: MarkovChain.simulate.__doc__ = _sample_path_docstr.format(p_arg="") elif sys.version_info[0] == 2: diff --git a/quantecon/tests/test_gth_solve.py b/quantecon/tests/test_gth_solve.py index 0e1181a58..0ef736f25 100644 --- a/quantecon/tests/test_gth_solve.py +++ b/quantecon/tests/test_gth_solve.py @@ -7,9 +7,8 @@ """ from __future__ import division -import sys import numpy as np -import nose +from numpy.testing import assert_array_equal from nose.tools import eq_, ok_, raises from quantecon.gth_solve import gth_solve @@ -118,9 +117,9 @@ def test_kmr_matrix(): yield StationaryDistLeftEigenVec(), matrix_dict['A'], x -def test_gen_solve(): +def test_gen_matrix(): """Test with generator matrices""" - print(__name__ + '.' + test_gen_solve.__name__) + print(__name__ + '.' + test_gen_matrix.__name__) matrices = Matrices() for matrix_dict in matrices.gen_matrix_dicts: x = gth_solve(matrix_dict['A']) @@ -154,6 +153,29 @@ def __call__(self, y, x): ok_(np.allclose(y, x, atol=TOL)) +def test_matrices_with_C_F_orders(): + """ + Test matrices with C- and F-contiguous orders + See the issue and fix on Numba: + github.com/numba/numba/issues/1103 + github.com/numba/numba/issues/1104 + + Fix in gth_solve(A): + added `order='C'` when `A1` copies the input `A` + """ + P_C = np.array([[0.5, 0.5], [0, 1]], order='C') + P_F = np.array([[0.5, 0.5], [0, 1]], order='F') + stationary_dist = [0., 1.] + + computed_C_and_F = gth_solve(np.array([[1]])) + assert_array_equal(computed_C_and_F, [1]) + + computed_C = gth_solve(P_C) + computed_F = gth_solve(P_F) + assert_array_equal(computed_C, stationary_dist) + assert_array_equal(computed_F, stationary_dist) + + @raises(ValueError) def test_raises_value_error_non_2dim(): """Test with non 2dim input""" @@ -167,6 +189,9 @@ def test_raises_value_error_non_square(): if __name__ == '__main__': + import sys + import nose + argv = sys.argv[:] argv.append('--verbose') argv.append('--nocapture') diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index d6931335d..259a22fd8 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -9,13 +9,15 @@ """ from __future__ import division -import sys import numpy as np from numpy.testing import assert_allclose, assert_array_equal -import nose from nose.tools import eq_, raises -from quantecon.mc_tools import MarkovChain, mc_compute_stationary +from quantecon.mc_tools import ( + MarkovChain, mc_compute_stationary, mc_sample_path, mc_sample_path_numpy +) + +from ..external import numba_installed, jit def list_of_array_equal(s, t): @@ -204,6 +206,47 @@ def test_left_eigen_vec(self): assert_allclose(np.dot(curr_v, mc.P), curr_v, atol=self.TOL) +def test_simulate_for_matrices_with_C_F_orders(): + """ + Test MarkovChasin.simulate for matrices with C- and F-orders + See the issue and fix on Numba: + github.com/numba/numba/issues/1103 + github.com/numba/numba/issues/1104 + """ + P_C = np.array([[0.5, 0.5], [0, 1]], order='C') + P_F = np.array([[0.5, 0.5], [0, 1]], order='F') + init = 1 + sample_size = 10 + sample_path = np.ones(sample_size, dtype=int) + + computed_C_and_F = \ + MarkovChain(np.array([[1.]])).simulate(init=0, sample_size=sample_size) + assert_array_equal(computed_C_and_F, np.zeros(sample_size, dtype=int)) + + computed_C = MarkovChain(P_C).simulate(init, sample_size) + computed_F = MarkovChain(P_F).simulate(init, sample_size) + assert_array_equal(computed_C, sample_path) + assert_array_equal(computed_F, sample_path) + + +def test_mc_sample_path_functions(): + """ + Test Numba and Numpy Versions of the mc_sample_path() Functions + """ + if not numba_installed: + raise ImportError("This test requires numba to be installed!") + #-Test Data-# + P = np.array([[0.4, 0.6], [0.2, 0.8]]) + init = (0.25, 0.75) + sample_size = 10 + #-Core-# + np.random.seed(1234) + numba_result = mc_sample_path(P, init=init, sample_size=sample_size) + np.random.seed(1234) + numpy_result = mc_sample_path_numpy(P, init=init, sample_size=sample_size) + assert_array_equal(numba_result, numpy_result) + + @raises(ValueError) def test_raises_value_error_non_2dim(): """Test with non 2dim input""" @@ -229,6 +272,9 @@ def test_raises_value_error_non_sum_one(): if __name__ == '__main__': + import sys + import nose + argv = sys.argv[:] argv.append('--verbose') argv.append('--nocapture') diff --git a/quantecon/tests/test_utilities.py b/quantecon/tests/test_utilities.py new file mode 100644 index 000000000..a28bae5b9 --- /dev/null +++ b/quantecon/tests/test_utilities.py @@ -0,0 +1,40 @@ +""" +Tests for utilities.py + +Functions +--------- +searchsorted + +""" +import numpy as np +from nose.tools import eq_ +from quantecon.utilities import searchsorted + + +def test_searchsorted(): + a = np.array([0.2, 0.4, 1.0]) + eq_(searchsorted(a, 0.1), 0) + eq_(searchsorted(a, 0.4), 2) + eq_(searchsorted(a, 2), 3) + + a = np.ones(0) + for (v, i) in zip([0, 1, 2], [0, 0, 0]): + eq_(searchsorted(a, v), i) + + a = np.ones(1) + for (v, i) in zip([0, 1, 2], [0, 1, 1]): + eq_(searchsorted(a, v), i) + + a = np.ones(2) + for (v, i) in zip([0, 1, 2], [0, 2, 2]): + eq_(searchsorted(a, v), i) + + +if __name__ == '__main__': + import sys + import nose + + argv = sys.argv[:] + argv.append('--verbose') + argv.append('--nocapture') + nose.main(argv=argv, defaultTest=__file__) diff --git a/quantecon/utilities.py b/quantecon/utilities.py new file mode 100644 index 000000000..db05a2cc1 --- /dev/null +++ b/quantecon/utilities.py @@ -0,0 +1,73 @@ +""" +Utilities Module for QuantEcon +============================== + +An assortment of utilities that are useful throughout QuantEcon + +Array +----- +searchsorted + +""" + +import numpy as np +from .external import numba_installed, jit + +# ----------------- # +# -ARRAY UTILITIES- # +# ----------------- # + + +def _searchsorted(a, v): + """ + Custom version of np.searchsorted. Return the largest index `i` such + that `a[i-1] <= v < a[i]` (for `i = 0`, `v < a[0]`); if `v[n-1] <= + v`, return `n`, where `n = len(a)`. + + Parameters + ---------- + a : ndarray(float, ndim=1) + Input array. Must be sorted in ascending order. + + v : scalar(float) + Value to be compared with elements of `a`. + + Returns + ------- + scalar(int) + Largest index `i` such that `a[i-1] <= v < a[i]`, or len(a) if + no such index exists. + + Notes + ----- + This routine is jit-complied if the module Numba is vailable; if + not, it is an alias of np.searchsorted(a, v, side='right'). + + Examples + -------- + >>> a = np.array([0.2, 0.4, 1.0]) + >>> searchsorted(a, 0.1) + 0 + >>> searchsorted(a, 0.4) + 2 + >>> searchsorted(a, 2) + 3 + + """ + lo = -1 + hi = len(a) + while(lo < hi-1): + m = (lo + hi) // 2 + if v < a[m]: + hi = m + else: + lo = m + return hi + +if numba_installed: + searchsorted = jit(nopython=True)(_searchsorted) +else: + def searchsorted(a, v): + return np.searchsorted(a, v, side='right') + +searchsorted.__doc__ = _searchsorted.__doc__ diff --git a/quantecon/version.py b/quantecon/version.py index c9aac5938..a6feb5ecb 100644 --- a/quantecon/version.py +++ b/quantecon/version.py @@ -1,4 +1,4 @@ """ This is a VERSION file and should NOT be manually altered """ -version = '0.1.8' \ No newline at end of file +version = '0.1.9' \ No newline at end of file diff --git a/setup.py b/setup.py index 2a04901a8..27919b8ff 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ #-Write Versions File-# #~~~~~~~~~~~~~~~~~~~~~# -VERSION = '0.1.8' +VERSION = '0.1.9' def write_version_py(filename=None): """