From d0575235d73b2ae19c6363e7cae8a08d580e3636 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Fri, 28 Nov 2014 12:34:19 +0900 Subject: [PATCH 01/34] GTH_SOLVE: Replaced with its Numba version --- quantecon/gth_solve.py | 63 +++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index 2211545d5..424dd132c 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -8,14 +8,10 @@ """ import numpy as np +from numba import jit -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 +38,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 +54,59 @@ 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=np.float64) 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, dtype=np.float64) + + _gth_solve_jit(A1, n, x) + + return x - x = np.zeros(n) +@jit('void(float64[:,:], int64, float64[:])', nopython=True) +def _gth_solve_jit(A, n, out): + """ + Main body 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. + + n : int + Value of A.shape[0]. + + out : numpy.ndarray(float, ndim=1) + Output array in which to place the stationary distribution of A. + + """ # === Reduction === # - for i in xrange(n-1): - scale = np.sum(A1[i, i+1:n]) + 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, ..., i}; + # {0, ..., k}; # compute the solution associated with that recurrent class. - n = i+1 + n = k+1 break - A1[i+1:n, i] /= scale + for i in range(k+1, n): + A[i, 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]) + for j in range(k+1, n): + A[i, j] += A[i, k] * A[k, j] # === 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]) + 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 === # - x /= np.sum(x) - - return x + norm = np.sum(out) + for k in range(n): + out[k] /= norm From 0bbb5f987f6ffa89ef375d8b8fc9790c75e31588 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Fri, 28 Nov 2014 12:35:46 +0900 Subject: [PATCH 02/34] Minor edit --- quantecon/tests/test_gth_solve.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantecon/tests/test_gth_solve.py b/quantecon/tests/test_gth_solve.py index 0e1181a58..b4a683a4f 100644 --- a/quantecon/tests/test_gth_solve.py +++ b/quantecon/tests/test_gth_solve.py @@ -118,9 +118,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']) From 5316070e931c0f39aff7ad0c9a252093e64f78d6 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Tue, 7 Apr 2015 20:48:14 +1000 Subject: [PATCH 03/34] changes generated by version update --- MANIFEST | 5 +++++ quantecon/version.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/MANIFEST b/MANIFEST index 1b5743941..7a90e3bbb 100644 --- a/MANIFEST +++ b/MANIFEST @@ -35,6 +35,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/quantecon/version.py b/quantecon/version.py index 640621751..c9aac5938 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.7-2' \ No newline at end of file +version = '0.1.8' \ No newline at end of file From fee2fb8a545d10dc019b99eb1ca016b062dff269 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Sat, 11 Apr 2015 17:41:23 +1000 Subject: [PATCH 04/34] Adding Manifest In File to Distribute LICENSE and README.md --- MANIFEST.in | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 MANIFEST.in diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 000000000..74215c3ee --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +include README.md +include LICENSE \ No newline at end of file From 4ebc1eeddcbb356bfc2b3a3454b1bf7c247286f4 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Sat, 11 Apr 2015 17:47:33 +1000 Subject: [PATCH 05/34] added full file name for LICENSE --- MANIFEST.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MANIFEST.in b/MANIFEST.in index 74215c3ee..7ea28439f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,2 +1,2 @@ include README.md -include LICENSE \ No newline at end of file +include LICENSE.txt \ No newline at end of file From c339f1219e901aa110cb793bcb91da44f68b9c29 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Sat, 18 Apr 2015 06:48:55 +1000 Subject: [PATCH 06/34] updated to 0.1.9 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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): """ From 2fe35e9e7517bce0ef5b8144daf16794960f8f98 Mon Sep 17 00:00:00 2001 From: John Stachurski Date: Sat, 18 Apr 2015 16:33:05 +1000 Subject: [PATCH 07/34] reorganizing lucas stokey code --- MANIFEST | 2 + examples/LucasStokey.py | 437 ++++++++++++++++++++++++++++++ examples/calibrations/BGP.py | 60 ++++ examples/calibrations/CES.py | 68 +++++ examples/calibrations/__init__.py | 7 + examples/main_LS.py | 162 +++++++++++ quantecon/version.py | 2 +- 7 files changed, 737 insertions(+), 1 deletion(-) create mode 100644 examples/LucasStokey.py create mode 100644 examples/calibrations/BGP.py create mode 100644 examples/calibrations/CES.py create mode 100644 examples/calibrations/__init__.py create mode 100644 examples/main_LS.py diff --git a/MANIFEST b/MANIFEST index 7a90e3bbb..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 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/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 From 53a345130dc35c6b13ca10be779d61ae559acc73 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Tue, 21 Apr 2015 21:47:04 +0900 Subject: [PATCH 08/34] Numba version of mc_sample_path: Preliminary version --- quantecon/mc_tools.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 6e383ec5e..55da51a3c 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -92,6 +92,7 @@ from .graph_tools import DiGraph from .gth_solve import gth_solve from warnings import warn +from numba import jit class MarkovChain(object): @@ -276,27 +277,45 @@ def mc_compute_stationary(P): return MarkovChain(P).stationary_distributions +@jit def mc_sample_path(P, init=0, sample_size=1000): + # 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: - X[0] = DiscreteRV(init).draw() - - # === 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)] + cdf0 = np.cumsum(init) + X[0] = search_cdf(cdf0, u[0]) # === generate the sample path === # - for t in range(sample_size - 1): - X[t+1] = P_dist[X[t]].draw() + n = len(cdfs) + for t in range(sample_size-1): + X[t+1] = search_cdf(cdfs[X[t]], u[t+1]) return X +@jit(nopython=True) +def search_cdf(cdf, v): + n = len(cdf) + + lo = -1 + hi = n - 1 + while(lo < hi-1): + m = (lo + hi) // 2 + if v < cdf[m]: + hi = m + else: + lo = m + return hi + + # ------------------------------------------------------------------- # # Set up the docstrings for the functions # ------------------------------------------------------------------- # From cb37391bd8a30ce5c92712509c4493cef97330c5 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Thu, 23 Apr 2015 13:51:38 +1000 Subject: [PATCH 09/34] Update LSS with common numba warning message --- quantecon/common_messages.py | 10 ++++++++++ quantecon/lss.py | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 quantecon/common_messages.py 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/lss.py b/quantecon/lss.py index 488df145d..bf31704cd 100644 --- a/quantecon/lss.py +++ b/quantecon/lss.py @@ -4,6 +4,7 @@ Computes quantities associated with the Gaussian linear state space model. """ + from textwrap import dedent import numpy as np from numpy.random import multivariate_normal @@ -15,8 +16,8 @@ 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) + from .common_messages import numba_import_fail_message + warnings.warn(numba_import_fail_message, UserWarning) def simulate_linear_model(A, x0, v, ts_length): From 02f7664660401bb7706ab708536f270031c4de8d Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Thu, 23 Apr 2015 14:38:42 +1000 Subject: [PATCH 10/34] Adding numpy version and adjusting @jit to using jit() for the case where numba is not installed --- quantecon/mc_tools.py | 77 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 55da51a3c..395cdbf8e 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -91,9 +91,15 @@ from .discrete_rv import DiscreteRV from .graph_tools import DiGraph from .gth_solve import gth_solve -from warnings import warn -from numba import jit +from warnings import warn +numba_installed = True +try: + from numba import jit +except ImportError: + numba_installed = False + from .common_messages import numba_import_fail_message + warnings.warn(numba_import_fail_message, UserWarning) class MarkovChain(object): """ @@ -277,7 +283,6 @@ def mc_compute_stationary(P): return MarkovChain(P).stationary_distributions -@jit def mc_sample_path(P, init=0, sample_size=1000): # CDFs, one for each row of P cdfs = np.cumsum(P, axis=-1) @@ -300,8 +305,69 @@ def mc_sample_path(P, init=0, sample_size=1000): return X +## --> Previous Implementation discussed: +## --> http://nbviewer.ipython.org/github/oyamad/mc_sample_path_numba/blob/master/mc_sample_path_numba02.ipynb <-- ## + +@jit +def mc_sample_path_jit(P, init, sample_size): + # 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 === # + n = len(cdfs) + for t in range(sample_size-1): + lo = -1 + hi = n - 1 + while(lo < hi-1): + m = (lo + hi) // 2 + if u[t+1] < cdfs[X[t], m]: + hi = m + else: + lo = m + X[t+1] = hi + + return X + +## --> END <-- ## + +def mc_sample_path_numpy(P, init=0, sample_size=1000): + # 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] = 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 + -@jit(nopython=True) def search_cdf(cdf, v): n = len(cdf) @@ -315,6 +381,9 @@ def search_cdf(cdf, v): lo = m return hi +if numba_installed: + search_cdf = jit(search_cdf) + # ------------------------------------------------------------------- # # Set up the docstrings for the functions From c51ce3fb1db66265dfecc4a4d3e9b7cb1212e6ef Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Thu, 23 Apr 2015 23:04:51 +0900 Subject: [PATCH 11/34] Numpy version added back `numba_installed` flag introduced --- quantecon/gth_solve.py | 113 +++++++++++++++++++++++++++-------------- 1 file changed, 75 insertions(+), 38 deletions(-) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index 424dd132c..95d94eb94 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -9,6 +9,19 @@ """ import numpy as np from numba import jit +from warnings import warn + +numba_installed = True +try: + from numba import jit +except ImportError: + numba_installed = False + from .common_messages import numba_import_fail_message + warnings.warn(numba_import_fail_message, UserWarning) + try: + xrange + except: # python3 + xrange = range def gth_solve(A): @@ -54,59 +67,83 @@ def gth_solve(A): Simulation, Princeton University Press, 2009. """ - A1 = np.array(A, dtype=np.float64) + A1 = np.array(A, dtype=float, copy=True) if len(A1.shape) != 2 or A1.shape[0] != A1.shape[1]: - raise ValueError('matrix must be square') + raise ValueError # ('matrix must be square') n = A1.shape[0] - x = np.zeros(n, dtype=np.float64) - - _gth_solve_jit(A1, n, x) + x = np.zeros(n) - return x + if numba_installed: + _gth_solve_jit(A1, x) + return x - -@jit('void(float64[:,:], int64, float64[:])', nopython=True) -def _gth_solve_jit(A, n, out): - """ - Main body 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. - - n : int - Value of A.shape[0]. - - out : numpy.ndarray(float, ndim=1) - Output array in which to place the stationary distribution of A. - - """ + # if not numba_installed # === Reduction === # - for k in range(n-1): - scale = np.sum(A[k, k+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, ..., k}; # compute the solution associated with that recurrent class. n = k+1 break - for i in range(k+1, n): - A[i, k] /= scale + A1[k+1:n, k] /= scale - for j in range(k+1, n): - A[i, j] += A[i, k] * A[k, j] + 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 === # - 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] + x[n-1] = 1 + for k in xrange(n-2, -1, -1): + x[k] = np.dot(x[k+1:n], A1[k+1:n, k]) # === Normalization === # - norm = np.sum(out) - for k in range(n): - out[k] /= norm + 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 From 8ea1206a8edf67d6c2a4602cd4b95c326bb36604 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Fri, 24 Apr 2015 00:15:30 +0900 Subject: [PATCH 12/34] Change the order of some of the dict elements in `testset` --- quantecon/tests/test_mc_tools.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index d6931335d..ff354b7e6 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -96,14 +96,6 @@ def test_markovchain_pmatrices(): 'is_aperiodic': False, 'cyclic_classes': [np.array([0]), np.array([1])], }, - {'P': np.eye(2), - 'stationary_dists': np.array([[1, 0], [0, 1]]), - 'comm_classes': [np.array([0]), np.array([1])], - 'rec_classes': [np.array([0]), np.array([1])], - 'is_irreducible': False, - 'period': 1, - 'is_aperiodic': True, - }, {'P': Q, 'stationary_dists': Q_stationary_dists, 'comm_classes': [np.array([0, 1]), np.array([2]), np.array([3, 4, 5])], @@ -111,6 +103,14 @@ def test_markovchain_pmatrices(): 'is_irreducible': False, 'period': 6, 'is_aperiodic': False, + }, + {'P': np.eye(2), + 'stationary_dists': np.array([[1, 0], [0, 1]]), + 'comm_classes': [np.array([0]), np.array([1])], + 'rec_classes': [np.array([0]), np.array([1])], + 'is_irreducible': False, + 'period': 1, + 'is_aperiodic': True, } ] From 0ace6ef94dd93b6db369fef2bc34605181a8f094 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 08:39:09 +0900 Subject: [PATCH 13/34] Revert "Change the order of some of the dict elements in `testset`" This reverts commit 8ea1206a8edf67d6c2a4602cd4b95c326bb36604. --- quantecon/tests/test_mc_tools.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index ff354b7e6..d6931335d 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -96,14 +96,6 @@ def test_markovchain_pmatrices(): 'is_aperiodic': False, 'cyclic_classes': [np.array([0]), np.array([1])], }, - {'P': Q, - 'stationary_dists': Q_stationary_dists, - 'comm_classes': [np.array([0, 1]), np.array([2]), np.array([3, 4, 5])], - 'rec_classes': [np.array([0, 1]), np.array([3, 4, 5])], - 'is_irreducible': False, - 'period': 6, - 'is_aperiodic': False, - }, {'P': np.eye(2), 'stationary_dists': np.array([[1, 0], [0, 1]]), 'comm_classes': [np.array([0]), np.array([1])], @@ -111,6 +103,14 @@ def test_markovchain_pmatrices(): 'is_irreducible': False, 'period': 1, 'is_aperiodic': True, + }, + {'P': Q, + 'stationary_dists': Q_stationary_dists, + 'comm_classes': [np.array([0, 1]), np.array([2]), np.array([3, 4, 5])], + 'rec_classes': [np.array([0, 1]), np.array([3, 4, 5])], + 'is_irreducible': False, + 'period': 6, + 'is_aperiodic': False, } ] From 2055102e716a30e735f6d9c8e163da7905221142 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 09:26:25 +0900 Subject: [PATCH 14/34] Add test for matrices with C- and F-contiguous orders Following the issue and fix numba/numba#1103 and numba/numba#1104 Test should fail for this commit --- quantecon/tests/test_gth_solve.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/quantecon/tests/test_gth_solve.py b/quantecon/tests/test_gth_solve.py index b4a683a4f..02a5726d5 100644 --- a/quantecon/tests/test_gth_solve.py +++ b/quantecon/tests/test_gth_solve.py @@ -9,6 +9,7 @@ import sys import numpy as np +from numpy.testing import assert_array_equal import nose from nose.tools import eq_, ok_, raises @@ -154,6 +155,27 @@ 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 + This should fail with Numba <= 0.18.2 + """ + 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""" From 3665910cf2fe2bf4cea65bb826581ec1ac41f719 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 11:01:58 +0900 Subject: [PATCH 15/34] Fix: Add `order='C'` when making a copy `A1` of the input `A` --- quantecon/gth_solve.py | 4 +++- quantecon/tests/test_gth_solve.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index 95d94eb94..d6b4da776 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -67,7 +67,9 @@ def gth_solve(A): Simulation, Princeton University Press, 2009. """ - A1 = np.array(A, dtype=float, copy=True) + 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') diff --git a/quantecon/tests/test_gth_solve.py b/quantecon/tests/test_gth_solve.py index 02a5726d5..835ad0c0d 100644 --- a/quantecon/tests/test_gth_solve.py +++ b/quantecon/tests/test_gth_solve.py @@ -161,7 +161,9 @@ def test_matrices_with_C_F_orders(): See the issue and fix on Numba: github.com/numba/numba/issues/1103 github.com/numba/numba/issues/1104 - This should fail with Numba <= 0.18.2 + + 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') From 4cd7e7a7eff824ee3b956e5afd9b4189c574005b Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 11:04:51 +0900 Subject: [PATCH 16/34] Minor edits --- quantecon/gth_solve.py | 2 +- quantecon/tests/test_gth_solve.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index d6b4da776..73b82c3dd 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -72,7 +72,7 @@ def gth_solve(A): # 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') + raise ValueError('matrix must be square') n = A1.shape[0] x = np.zeros(n) diff --git a/quantecon/tests/test_gth_solve.py b/quantecon/tests/test_gth_solve.py index 835ad0c0d..0ef736f25 100644 --- a/quantecon/tests/test_gth_solve.py +++ b/quantecon/tests/test_gth_solve.py @@ -7,10 +7,8 @@ """ from __future__ import division -import sys import numpy as np from numpy.testing import assert_array_equal -import nose from nose.tools import eq_, ok_, raises from quantecon.gth_solve import gth_solve @@ -191,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') From 3127ca24dc6d3756bfff5d62853a41971aac36f0 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Sat, 25 Apr 2015 13:10:18 +1000 Subject: [PATCH 17/34] Updated numba import statements to use the common external module --- quantecon/external.py | 22 ++++++++++++++++++++++ quantecon/lss.py | 10 ++-------- quantecon/mc_tools.py | 9 ++------- 3 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 quantecon/external.py 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/lss.py b/quantecon/lss.py index bf31704cd..daa823c84 100644 --- a/quantecon/lss.py +++ b/quantecon/lss.py @@ -11,14 +11,8 @@ from scipy.linalg import solve import warnings -numba_installed = True -try: - from numba import jit -except ImportError: - numba_installed = False - from .common_messages import numba_import_fail_message - warnings.warn(numba_import_fail_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 395cdbf8e..a54ae28ae 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -93,13 +93,8 @@ from .gth_solve import gth_solve from warnings import warn -numba_installed = True -try: - from numba import jit -except ImportError: - numba_installed = False - from .common_messages import numba_import_fail_message - warnings.warn(numba_import_fail_message, UserWarning) +#-Check if Numba is Available-# +from .external import numba_installed, jit class MarkovChain(object): """ From 45004f0a48008adaca4fde5dca4951fd89d0aeef Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Sat, 25 Apr 2015 13:12:12 +1000 Subject: [PATCH 18/34] Removing mc_sample_path() numba 0.17 version in preference for split version that works in 0.18.2 --- quantecon/mc_tools.py | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index a54ae28ae..7f6ca8f12 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -300,41 +300,6 @@ def mc_sample_path(P, init=0, sample_size=1000): return X -## --> Previous Implementation discussed: -## --> http://nbviewer.ipython.org/github/oyamad/mc_sample_path_numba/blob/master/mc_sample_path_numba02.ipynb <-- ## - -@jit -def mc_sample_path_jit(P, init, sample_size): - # 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 === # - n = len(cdfs) - for t in range(sample_size-1): - lo = -1 - hi = n - 1 - while(lo < hi-1): - m = (lo + hi) // 2 - if u[t+1] < cdfs[X[t], m]: - hi = m - else: - lo = m - X[t+1] = hi - - return X - -## --> END <-- ## def mc_sample_path_numpy(P, init=0, sample_size=1000): # CDFs, one for each row of P From 617ab4303f2b6df795f6b853e34f2a4e4a2f1b14 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 14:19:48 +0900 Subject: [PATCH 19/34] Add test for `simulate` with matrices with C- and F-orders Test should fail for this commit --- quantecon/tests/test_mc_tools.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index d6931335d..ea412feff 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -204,6 +204,29 @@ 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) + + @raises(ValueError) def test_raises_value_error_non_2dim(): """Test with non 2dim input""" From 2eaec7df51cc5a3dd0e8b95b665f1d8f0824ed39 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 14:21:22 +0900 Subject: [PATCH 20/34] Minor edit --- quantecon/tests/test_mc_tools.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index ea412feff..8aa1e0989 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -9,10 +9,8 @@ """ 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 @@ -252,6 +250,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') From 0e24d870deb9c4d399f2c9ccebbb83673d596645 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 14:28:01 +0900 Subject: [PATCH 21/34] Fix: Preallocate `cdfs` with `order='C'` For use with Numba <= 0.18.2 --- quantecon/mc_tools.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 7f6ca8f12..9ec984010 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -91,11 +91,12 @@ from .discrete_rv import DiscreteRV from .graph_tools import DiGraph from .gth_solve import gth_solve -from warnings import warn +from warnings import warn #-Check if Numba is Available-# from .external import numba_installed, jit + class MarkovChain(object): """ Class for a finite-state discrete-time Markov chain. It stores @@ -279,8 +280,11 @@ def mc_compute_stationary(P): def mc_sample_path(P, init=0, sample_size=1000): + n = len(P) + # CDFs, one for each row of P - cdfs = np.cumsum(P, axis=-1) + 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) @@ -304,10 +308,10 @@ def mc_sample_path(P, init=0, sample_size=1000): def mc_sample_path_numpy(P, init=0, sample_size=1000): # 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): From d726b1495b7ffe65c7d03cf7d4a2812aba1ffbff Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 21:43:13 +0900 Subject: [PATCH 22/34] Minor edit --- quantecon/mc_tools.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 9ec984010..bc5abc5f4 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -88,7 +88,6 @@ 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 @@ -298,7 +297,6 @@ def mc_sample_path(P, init=0, sample_size=1000): X[0] = search_cdf(cdf0, u[0]) # === generate the sample path === # - n = len(cdfs) for t in range(sample_size-1): X[t+1] = search_cdf(cdfs[X[t]], u[t+1]) @@ -346,7 +344,7 @@ def search_cdf(cdf, v): return hi if numba_installed: - search_cdf = jit(search_cdf) + search_cdf = jit(search_cdf) # ------------------------------------------------------------------- # From f0bae7fcb4ce857099320811063b316ef7644df1 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sat, 25 Apr 2015 23:48:23 +0900 Subject: [PATCH 23/34] `search_cdf` renamed to `searchsorted`; work properly for inputs s.t. a[n-1] <= v Docstring and test added --- quantecon/mc_tools.py | 49 +++++++++++++++++++++++++++----- quantecon/tests/test_mc_tools.py | 23 ++++++++++++++- 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index bc5abc5f4..0541631e1 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -294,11 +294,11 @@ def mc_sample_path(P, init=0, sample_size=1000): X[0] = init else: cdf0 = np.cumsum(init) - X[0] = search_cdf(cdf0, u[0]) + X[0] = searchsorted(cdf0, u[0]) # === generate the sample path === # for t in range(sample_size-1): - X[t+1] = search_cdf(cdfs[X[t]], u[t+1]) + X[t+1] = searchsorted(cdfs[X[t]], u[t+1]) return X @@ -330,21 +330,56 @@ def mc_sample_path_numpy(P, init=0, sample_size=1000): mc_sample_path = mc_sample_path_numpy -def search_cdf(cdf, v): - n = len(cdf) +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 = n - 1 + hi = len(a) while(lo < hi-1): m = (lo + hi) // 2 - if v < cdf[m]: + if v < a[m]: hi = m else: lo = m return hi if numba_installed: - search_cdf = jit(search_cdf) + searchsorted = jit(nopython=True)(searchsorted) +else: + searchsorted = lambda a, v: np.searchsorted(a, v, side='right') # ------------------------------------------------------------------- # diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index 8aa1e0989..074948d25 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -13,7 +13,9 @@ from numpy.testing import assert_allclose, assert_array_equal from nose.tools import eq_, raises -from quantecon.mc_tools import MarkovChain, mc_compute_stationary +from quantecon.mc_tools import ( + MarkovChain, mc_compute_stationary, searchsorted +) def list_of_array_equal(s, t): @@ -225,6 +227,25 @@ def test_simulate_for_matrices_with_C_F_orders(): assert_array_equal(computed_F, sample_path) +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) + + @raises(ValueError) def test_raises_value_error_non_2dim(): """Test with non 2dim input""" From 17e710068a732f79e0b522b380f99c9b7e8e0d53 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Mon, 27 Apr 2015 17:43:34 +1000 Subject: [PATCH 24/34] Adding test for numpy and numba functions ... needs discussion on random seeds --- quantecon/tests/test_mc_tools.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index 074948d25..e633d3329 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -14,9 +14,11 @@ from nose.tools import eq_, raises from quantecon.mc_tools import ( - MarkovChain, mc_compute_stationary, searchsorted + MarkovChain, mc_compute_stationary, searchsorted, mc_sample_path, mc_sample_path_numpy ) +from ..external import numba_installed, jit + def list_of_array_equal(s, t): """ @@ -246,6 +248,28 @@ def test_searchsorted(): eq_(searchsorted(a, v), i) +def test_mc_sample_path_functions(): + """ + Test Numba and Numpy Versions of the mc_sample_path() Functions + """ + + ##-!!-ERROR-!!-## + #-Set Seed-# + np.random.seed(0) + ##-!!-END ERROR-!!-## + + 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-# + numba_result = mc_sample_path(P, init=init, sample_size=sample_size) + 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""" From 52a67f63d4e7214af6c1a6d15b33da6e4a3dc668 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Mon, 27 Apr 2015 21:31:12 +1000 Subject: [PATCH 25/34] Adding Utilities Module and migrated searchsorted --- quantecon/__init__.py | 1 + quantecon/mc_tools.py | 52 +------------------------------- quantecon/utilities.py | 68 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 quantecon/utilities.py 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/mc_tools.py b/quantecon/mc_tools.py index 0541631e1..31d359ec0 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -94,7 +94,7 @@ #-Check if Numba is Available-# from .external import numba_installed, jit - +from .utilities import searchsorted class MarkovChain(object): """ @@ -330,56 +330,6 @@ def mc_sample_path_numpy(P, init=0, sample_size=1000): mc_sample_path = mc_sample_path_numpy -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: - searchsorted = lambda a, v: np.searchsorted(a, v, side='right') # ------------------------------------------------------------------- # diff --git a/quantecon/utilities.py b/quantecon/utilities.py new file mode 100644 index 000000000..a1b554c11 --- /dev/null +++ b/quantecon/utilities.py @@ -0,0 +1,68 @@ +""" +Utilities Module for QuantEcon +============================== + +An assortment of utilities that are useful throughout QuantEcon + +Array +----- +searchsorted + +""" + +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: + searchsorted = lambda a, v: np.searchsorted(a, v, side='right') \ No newline at end of file From 4dec740f058d56fd7e6ff9e3ffa3ffc1144fc43e Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Tue, 28 Apr 2015 19:45:28 +1000 Subject: [PATCH 26/34] Updates to docstrings for both mc_sample_path functions --- quantecon/mc_tools.py | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 31d359ec0..df5ab89ff 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -259,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 @@ -279,6 +278,9 @@ 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 @@ -304,6 +306,9 @@ def mc_sample_path(P, init=0, sample_size=1000): 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) @@ -331,12 +336,11 @@ def mc_sample_path_numpy(P, init=0, sample_size=1000): +#------------# +#-DocStrings-# +#------------# -# ------------------------------------------------------------------- # -# Set up the docstrings for the functions -# ------------------------------------------------------------------- # - -# 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) @@ -344,7 +348,8 @@ def mc_sample_path_numpy(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. @@ -359,14 +364,21 @@ def mc_sample_path_numpy(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: From 972e25854355c2fc9deb79a17b5f3441d6f37072 Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Tue, 28 Apr 2015 19:46:15 +1000 Subject: [PATCH 27/34] Removing Tabs in Preference for Spaces --- quantecon/mc_tools.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index df5ab89ff..254629df5 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -278,9 +278,9 @@ def mc_compute_stationary(P): def mc_sample_path(P, init=0, sample_size=1000): - """ - See Section: DocStrings below - """ + """ + See Section: DocStrings below + """ n = len(P) # CDFs, one for each row of P @@ -306,9 +306,9 @@ def mc_sample_path(P, init=0, sample_size=1000): def mc_sample_path_numpy(P, init=0, sample_size=1000): - """ - See Section: DocStrings below - """ + """ + See Section: DocStrings below + """ # CDFs, one for each row of P cdfs = np.cumsum(P, axis=-1) From 16fec8805b253d71f81327e529c2425429ae4e7d Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Tue, 28 Apr 2015 19:49:46 +1000 Subject: [PATCH 28/34] Bug fix - forgot an underscore --- quantecon/mc_tools.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantecon/mc_tools.py b/quantecon/mc_tools.py index 254629df5..31da5a44a 100644 --- a/quantecon/mc_tools.py +++ b/quantecon/mc_tools.py @@ -371,7 +371,7 @@ def mc_sample_path_numpy(P, init=0, sample_size=1000): P : array_like(float, ndim=2) A Markov transition matrix. """) -mc_sample_path_numpy.__doc__ = sample_path_docstr.format(p_arg=""" +mc_sample_path_numpy.__doc__ = _sample_path_docstr.format(p_arg=""" P : array_like(float, ndim=2) A Markov transition matrix. """) From cdf0b09072c40dd2c379192c5151b07bc112cd3a Mon Sep 17 00:00:00 2001 From: Matthew McKay Date: Tue, 28 Apr 2015 21:04:40 +1000 Subject: [PATCH 29/34] Updating test with 1 x seed for each function run --- quantecon/tests/test_mc_tools.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index e633d3329..c31445b16 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -252,12 +252,6 @@ def test_mc_sample_path_functions(): """ Test Numba and Numpy Versions of the mc_sample_path() Functions """ - - ##-!!-ERROR-!!-## - #-Set Seed-# - np.random.seed(0) - ##-!!-END ERROR-!!-## - if not numba_installed: raise ImportError("This test requires numba to be installed!") #-Test Data-# @@ -265,7 +259,9 @@ def test_mc_sample_path_functions(): 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) From 6e951e3d8dec5e5d883de0eccefbef0a8bd0ec34 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Thu, 30 Apr 2015 22:12:50 +0900 Subject: [PATCH 30/34] test_searchsorted moved from test_mc_tools.py to test_utilities.py --- quantecon/tests/test_mc_tools.py | 25 +++---------------- quantecon/tests/test_utilities.py | 40 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 quantecon/tests/test_utilities.py diff --git a/quantecon/tests/test_mc_tools.py b/quantecon/tests/test_mc_tools.py index c31445b16..259a22fd8 100644 --- a/quantecon/tests/test_mc_tools.py +++ b/quantecon/tests/test_mc_tools.py @@ -14,7 +14,7 @@ from nose.tools import eq_, raises from quantecon.mc_tools import ( - MarkovChain, mc_compute_stationary, searchsorted, mc_sample_path, mc_sample_path_numpy + MarkovChain, mc_compute_stationary, mc_sample_path, mc_sample_path_numpy ) from ..external import numba_installed, jit @@ -229,25 +229,6 @@ def test_simulate_for_matrices_with_C_F_orders(): assert_array_equal(computed_F, sample_path) -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) - - def test_mc_sample_path_functions(): """ Test Numba and Numpy Versions of the mc_sample_path() Functions @@ -259,9 +240,9 @@ def test_mc_sample_path_functions(): init = (0.25, 0.75) sample_size = 10 #-Core-# - np.random.seed(1234) + np.random.seed(1234) numba_result = mc_sample_path(P, init=init, sample_size=sample_size) - np.random.seed(1234) + np.random.seed(1234) numpy_result = mc_sample_path_numpy(P, init=init, sample_size=sample_size) assert_array_equal(numba_result, numpy_result) 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__) From 36e751866c13e6003b46a5b9d6947bad7fb783fe Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Thu, 30 Apr 2015 22:13:21 +0900 Subject: [PATCH 31/34] Missing import added; minor edits to comply with PEP 8 --- quantecon/utilities.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/quantecon/utilities.py b/quantecon/utilities.py index a1b554c11..164503dff 100644 --- a/quantecon/utilities.py +++ b/quantecon/utilities.py @@ -10,11 +10,13 @@ """ +import numpy as np from .external import numba_installed, jit - -#-----------------# -#-ARRAY UTILITIES-# -#-----------------# + +# ----------------- # +# -ARRAY UTILITIES- # +# ----------------- # + def searchsorted(a, v): """ @@ -65,4 +67,4 @@ def searchsorted(a, v): if numba_installed: searchsorted = jit(nopython=True)(searchsorted) else: - searchsorted = lambda a, v: np.searchsorted(a, v, side='right') \ No newline at end of file + searchsorted = lambda a, v: np.searchsorted(a, v, side='right') From 99ad7577897304bf8e43f6748be4c83822851286 Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Fri, 1 May 2015 23:13:34 +0900 Subject: [PATCH 32/34] The first `searchsorted` renamed to `_searchsorted`; docstring copied --- quantecon/utilities.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/quantecon/utilities.py b/quantecon/utilities.py index 164503dff..7daab0b81 100644 --- a/quantecon/utilities.py +++ b/quantecon/utilities.py @@ -18,7 +18,7 @@ # ----------------- # -def searchsorted(a, v): +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] <= @@ -65,6 +65,8 @@ def searchsorted(a, v): return hi if numba_installed: - searchsorted = jit(nopython=True)(searchsorted) + searchsorted = jit(nopython=True)(_searchsorted) else: searchsorted = lambda a, v: np.searchsorted(a, v, side='right') + +searchsorted.__doc__ = _searchsorted.__doc__ From 2c70760602a955fc40eb6e8a625ecf7cd32045be Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Fri, 1 May 2015 23:15:07 +0900 Subject: [PATCH 33/34] PEP 8 compliance --- quantecon/utilities.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/quantecon/utilities.py b/quantecon/utilities.py index 7daab0b81..db05a2cc1 100644 --- a/quantecon/utilities.py +++ b/quantecon/utilities.py @@ -67,6 +67,7 @@ def _searchsorted(a, v): if numba_installed: searchsorted = jit(nopython=True)(_searchsorted) else: - searchsorted = lambda a, v: np.searchsorted(a, v, side='right') + def searchsorted(a, v): + return np.searchsorted(a, v, side='right') searchsorted.__doc__ = _searchsorted.__doc__ From 89302ec824d0e080906ded35f101f13440e97b8a Mon Sep 17 00:00:00 2001 From: Daisuke Oyama Date: Sun, 3 May 2015 17:01:02 +0900 Subject: [PATCH 34/34] Numba import statement changed to use the `external` module --- quantecon/gth_solve.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/quantecon/gth_solve.py b/quantecon/gth_solve.py index 73b82c3dd..4f3de1064 100644 --- a/quantecon/gth_solve.py +++ b/quantecon/gth_solve.py @@ -9,15 +9,9 @@ """ import numpy as np from numba import jit -from warnings import warn - -numba_installed = True -try: - from numba import jit -except ImportError: - numba_installed = False - from .common_messages import numba_import_fail_message - warnings.warn(numba_import_fail_message, UserWarning) + +from .external import numba_installed, jit +if not numba_installed: try: xrange except: # python3