def simBirth(self, which_agents): ''' Makes new consumers for the given indices. Slightly extends base method by also setting pLvlErrNow = 1.0 for new agents, indicating that they correctly perceive their productivity. Parameters ---------- which_agents : np.array(Bool) Boolean array of size self.AgentCount indicating which agents should be "born". Returns ------- None ''' AggShockConsumerType.simBirth(self, which_agents) if hasattr(self, 'pLvlErrNow'): self.pLvlErrNow[which_agents] = 1.0 #self.pSocial[which_agents] = (self.pSocial[which_agents]*float(2*self.NetSiz+1)-self.Pcvd[which_agents]+self.PlvlAggNow)/(2*self.NetSiz+1) self.PSocial[which_agents] = self.PlvlAggNow self.Pcvd[which_agents] = self.PlvlAggNow self.Pcvd_pre[which_agents] = self.PlvlAggNow self.pInd[which_agents] = 1.0 self.pLvlNow[which_agents] = self.PlvlAggNow else: self.pLvlErrNow = np.ones(self.AgentCount) self.PSocial = np.ones(self.AgentCount) self.Pcvd = np.ones(self.AgentCount) self.Pcvd_pre = np.ones(self.AgentCount) self.pInd = np.ones(self.AgentCount)
def getPostStates(self): ''' Slightly extends the base version of this method by recalculating aLvlNow to account for the consumer's (potential) misperception about their productivity level. Parameters ---------- None Returns ------- None ''' AggShockConsumerType.getPostStates(self) self.cLvlNow = self.cNrmNow * self.pLvlNow # True consumption level self.aLvlNow = self.mLvlTrueNow - self.cLvlNow # True asset level self.aNrmNow = self.aLvlNow / self.pLvlNow # Perceived normalized assets
def simBirth(self, which_agents): ''' Makes new consumers for the given indices. Slightly extends base method by also setting pLvlErrNow = 1.0 for new agents, indicating that they correctly perceive their productivity. Parameters ---------- which_agents : np.array(Bool) Boolean array of size self.AgentCount indicating which agents should be "born". Returns ------- None ''' AggShockConsumerType.simBirth(self, which_agents) if hasattr(self, 'pLvlErrNow'): self.pLvlErrNow[which_agents] = 1.0 else: self.pLvlErrNow = np.ones(self.AgentCount)
def initializeSim(self, a_init=None, p_init=None, t_init=0, sim_prds=None): ''' Readies this type for simulation by clearing its history, initializing state variables, and setting time indices to their correct position. Parameters ---------- a_init : np.array Array of initial end-of-period assets at the beginning of the sim- ulation. Should be of size self.Nagents. If omitted, will default to values in self.a_init (which are all 0 by default). p_init : np.array Array of initial permanent income levels at the beginning of the sim- ulation. Should be of size self.Nagents. If omitted, will default to values in self.p_init (which are all 1 by default). t_init : int Period of life in which to begin the simulation. Defaults to 0. sim_prds : int Number of periods to simulate. Defaults to the length of the trans- itory income shock history. Returns ------- none ''' AggShockConsumerType.initializeSim(self, a_init, p_init, t_init, sim_prds) blank_history = np.zeros_like(self.pHist) + np.nan # After doing the same initialization as AggShockConsumerType, add in beliefs self.pBeliefHist = copy(blank_history) self.bBeliefHist = copy(blank_history) self.mBeliefHist = copy(blank_history) self.aBeliefHist = copy(blank_history) self.aBeliefNow = self.a_init self.pBeliefNow = self.p_init self.KtoLBeliefNow = self.KtoLBeliefNow_init self.updateBeliefNow = np.ones_like( self.p_init ) #beliefs must be up to date at the start of simulation
def makeIncShkHist(self): ''' Makes histories of simulated income shocks for this consumer type by drawing from the discrete income distributions, storing them as attributes of self for use by simulation methods. Parameters ---------- None Returns ------- None ''' # After running the routine for AggShockConsumerType, just add in updateBeliefHist AggShockConsumerType.makeIncShkHist(self) orig_time = self.time_flow self.timeFwd() self.resetRNG() # Initialize the shock histories updateBeliefHist = np.zeros((self.sim_periods, self.Nagents)) + np.nan updateBeliefHist[0, :] = 1.0 t_idx = 0 # Loop through each simulated period for t in range(1, self.sim_periods): updateBeliefHist[t, :] = drawBernoulli(self.Nagents, self.updateBeliefProb, seed=self.RNG.randint( 0, 2**31 - 1)) # Advance the time index, looping if we've run out of income distributions t_idx += 1 if t_idx >= len(self.IncomeDstn): t_idx = 0 # Store the results as attributes of self and restore time to its original flow self.updateBeliefHist = updateBeliefHist if not orig_time: self.timeRev()
def getEconomyData(self, Economy): ''' Imports economy-determined objects into self from a Market. Instances of AggShockConsumerType "live" in some macroeconomy that has attributes relevant to their microeconomic model, like the relationship between the capital-to-labor ratio and the interest and wage rates; this method imports those attributes from an "economy" object and makes them attributes of the ConsumerType. Parameters ---------- Economy : Market The "macroeconomy" in which this instance "lives". Might be of the subclass CobbDouglasEconomy, which has methods to generate the relevant attributes. Returns ------- None ''' AggShockConsumerType.getEconomyData(self, Economy) self.KtoLBeliefNow_init = Economy.KtoLnow_init * np.ones(self.Nagents)
PermShk = 1 TranShk = 1.1 cTranImpulseResponse = AggCons_impulseResponseInd(IndShockExample, PermShk, TranShk, 100, 25) plt.plot(cTranImpulseResponse) print( 'Impulse response to a one time permanent and transitive shock to income of 10%:' ) plt.show() ########################################## # Now do aggregate shocks of a market # Make an aggregate shocks consumer AggShockExample = AggShockConsumerType(**Params.init_agg_shocks) AggShockExample.cycles = 0 AggShockExample.sim_periods = 3000 AggShockExample.makeIncShkHist( ) # Simulate a history of idiosyncratic shocks # Make a Cobb-Douglas economy for the agents EconomyExample = CobbDouglasEconomy(agents=[AggShockExample], act_T=AggShockExample.sim_periods, **Params.init_cobb_douglas) EconomyExample.makeAggShkHist() # Simulate a history of aggregate shocks # Have the consumers inherit relevant objects from the economy AggShockExample.getEconomyData(EconomyExample) # Solve the microeconomic model for the aggregate shocks example type (and display results) t_start = clock()
if Params.do_sensitivity[7]: # interest rate sensitivity analysis Rfree_list = (np.linspace(1.0, 1.04, 17) / InfiniteType.survival_prob[0]).tolist() sensitivityAnalysis('Rfree', Rfree_list, False) # ======================================================================= # ========= FBS aggregate shocks model ================================== #======================================================================== if Params.do_agg_shocks: # These are the perpetual youth estimates in case we want to skip estimation (and we do) beta_point_estimate = 0.989142 beta_dist_estimate = 0.985773 nabla_estimate = 0.0077 # Make a set of consumer types for the FBS aggregate shocks model BaseAggShksType = AggShockConsumerType(**Params.init_agg_shocks) agg_shocks_type_list = [] for j in range(Params.pref_type_count): new_type = deepcopy(BaseAggShksType) new_type.seed = j new_type.resetRNG() new_type.makeIncShkHist() agg_shocks_type_list.append(new_type) if Params.do_beta_dist: beta_agg = beta_dist_estimate nabla_agg = nabla_estimate else: beta_agg = beta_point_estimate nabla_agg = 0.0 DiscFac_list_agg = approxUniform(N=Params.pref_type_count, bot=beta_agg - nabla_agg,
if not orig_time: self.timeRev() ############################################################################### if __name__ == '__main__': import ConsumerParameters as Params from time import clock from HARKutilities import plotFuncs mystr = lambda number: "{:.4f}".format(number) # Make an aggregate shocks sticky expectations consumer StickyExample = AggShockStickyExpectationsConsumerType( **Params.init_sticky_shocks) NotStickyExample = AggShockConsumerType(**Params.init_sticky_shocks) StickyExample.cycles = 0 NotStickyExample.cycles = 0 StickyExample.sim_periods = 3000 NotStickyExample.sim_periods = 3000 StickyExample.makeIncShkHist( ) # Simulate a history of idiosyncratic shocks NotStickyExample.makeIncShkHist( ) # Simulate a history of idiosyncratic shocks # Make a Cobb-Douglas economy for the agents StickyEconomyExample = CobbDouglasEconomy(agents=[StickyExample], act_T=StickyExample.sim_periods, **Params.init_cobb_douglas) NotStickyEconomyExample = CobbDouglasEconomy( agents=[NotStickyExample],