def solve_time1_bellman(self): ''' Solve the time 1 Bellman equation for calibration Para and initial grid mugrid0 ''' Para, mugrid0 = self.Para, self.mugrid Pi = Para.Pi S = len(Para.Pi) #First get initial fit from lucas stockey solution. #Need to change things to be ex_ante PP = LS.Planners_Allocation_Sequential(Para) interp = utilities.interpolator_factory(2, None) def incomplete_allocation(mu_, s_): c, n, x, V = PP.time1_value(mu_) return c, n, Pi[s_].dot(x), Pi[s_].dot(V) cf, nf, xgrid, Vf, xprimef = [], [], [], [], [] for s_ in range(S): c, n, x, V = zip( *map(lambda mu: incomplete_allocation(mu, s_), mugrid0)) c, n = np.vstack(c).T, np.vstack(n).T x, V = np.hstack(x), np.hstack(V) xprimes = np.vstack([x] * S) cf.append(interp(x, c)) nf.append(interp(x, n)) Vf.append(interp(x, V)) xgrid.append(x) xprimef.append(interp(x, xprimes)) cf, nf, xprimef = utilities.fun_vstack(cf), utilities.fun_vstack( nf), utilities.fun_vstack(xprimef) Vf = utilities.fun_hstack(Vf) policies = [cf, nf, xprimef] #create xgrid x = np.vstack(xgrid).T 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-6: PF = T(Vf) Vfnew, policies = self.fit_policy_function(PF) diff = np.abs((Vf(xgrid) - Vfnew(xgrid)) / Vf(xgrid)).max() print diff Vf = Vfnew #store value function policies and Bellman Equations self.Vf = Vf self.policies = policies self.T = T
def solve_time1_bellman(self): ''' Solve the time 1 Bellman equation for calibration Para and initial grid mugrid0 ''' Para,mugrid0 = self.Para,self.mugrid Pi = Para.Pi S = len(Para.Pi) #First get initial fit from lucas stockey solution. #Need to change things to be ex_ante PP = LS.Planners_Allocation_Sequential(Para) interp = utilities.interpolator_factory(2,None) def incomplete_allocation(mu_,s_): c,n,x,V = PP.time1_value(mu_) return c,n,Pi[s_].dot(x),Pi[s_].dot(V) cf,nf,xgrid,Vf,xprimef = [],[],[],[],[] for s_ in range(S): c,n,x,V = zip(*map(lambda mu: incomplete_allocation(mu,s_),mugrid0)) c,n = np.vstack(c).T,np.vstack(n).T x,V = np.hstack(x),np.hstack(V) xprimes = np.vstack([x]*S) cf.append(interp(x,c)) nf.append(interp(x,n)) Vf.append(interp(x,V)) xgrid.append(x) xprimef.append(interp(x,xprimes)) cf,nf,xprimef = utilities.fun_vstack(cf), utilities.fun_vstack(nf),utilities.fun_vstack(xprimef) Vf = utilities.fun_hstack(Vf) policies = [cf,nf,xprimef] #create xgrid x = np.vstack(xgrid).T 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-6: PF = T(Vf) Vfnew,policies = self.fit_policy_function(PF) diff = np.abs((Vf(xgrid)-Vfnew(xgrid))/Vf(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 ''' S,xgrid = len(self.Pi),self.xgrid interp = utilities.interpolator_factory(3,0) cf,nf,xprimef,Tf,Vf = [],[],[],[],[] for s_ in range(S): PFvec = np.vstack([PF(x,s_) for x in self.xgrid]).T Vf.append(interp(xgrid,PFvec[0,:])) cf.append(interp(xgrid,PFvec[1:1+S])) nf.append(interp(xgrid,PFvec[1+S:1+2*S])) xprimef.append(interp(xgrid,PFvec[1+2*S:1+3*S])) Tf.append(interp(xgrid,PFvec[1+3*S:])) policies = utilities.fun_vstack(cf), utilities.fun_vstack(nf),utilities.fun_vstack(xprimef),utilities.fun_vstack(Tf) Vf = utilities.fun_hstack(Vf) return Vf,policies