def test_mc_sample_path(): P = [[0.4, 0.6], [0.2, 0.8]] Ps = [P, sparse.csr_matrix(P)] init = 0 sample_size = 10 seed = 42 for P in Ps: # init: integer expected = [0, 0, 1, 1, 1, 0, 0, 0, 1, 1] computed = mc_sample_path(P, init=init, sample_size=sample_size, random_state=seed) assert_array_equal(computed, expected) # init: distribution distribution = (0.5, 0.5) expected = [0, 1, 1, 1, 0, 0, 0, 1, 1, 1] computed = mc_sample_path(P, init=distribution, sample_size=sample_size, random_state=seed) assert_array_equal(computed, expected)
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
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
def test_mc_sample_path(): P = [[0.4, 0.6], [0.2, 0.8]] init = 0 sample_size = 10 seed = 42 # init: integer expected = [0, 0, 1, 1, 1, 0, 0, 0, 1, 1] computed = mc_sample_path(P, init=init, sample_size=sample_size, random_state=seed) assert_array_equal(computed, expected) # init: distribution distribution = (0.5, 0.5) expected = [0, 1, 1, 1, 0, 0, 0, 1, 1, 1] computed = mc_sample_path(P, init=distribution, sample_size=sample_size, random_state=seed) assert_array_equal(computed, expected)
def test_mc_sample_path_lln(): P = [[0.4, 0.6], [0.2, 0.8]] stationary_dist = [0.25, 0.75] init = 0 seed = 4433 sample_size = 10**4 tol = 0.02 frequency_1 = mc_sample_path(P, init=init, sample_size=sample_size, random_state=seed).mean() ok_(np.abs(frequency_1 - stationary_dist[1]) < tol)
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
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