def setUp(self): """ Prepare to compare the models by initializing and solving them """ # Set up and solve infinite type import ConsumerParameters as Params InfiniteType = IndShockConsumerType(**Params.init_idiosyncratic_shocks) InfiniteType.assignParameters(LivPrb = [1.], DiscFac = 0.955, PermGroFac = [1.], PermShkStd = [0.], TempShkStd = [0.], T_total = 1, T_retire = 0, BoroCnstArt = None, UnempPrb = 0., cycles = 0) # This is what makes the type infinite horizon InfiniteType.updateIncomeProcess() InfiniteType.solve() InfiniteType.timeFwd() InfiniteType.unpackcFunc() # Make and solve a perfect foresight consumer type with the same parameters PerfectForesightType = deepcopy(InfiniteType) PerfectForesightType.solveOnePeriod = solvePerfForesight PerfectForesightType.solve() PerfectForesightType.unpackcFunc() PerfectForesightType.timeFwd() self.InfiniteType = InfiniteType self.PerfectForesightType = PerfectForesightType
def setUp(self): # Set up and solve infinite type import ConsumerParameters as Params InfiniteType = IndShockConsumerType(**Params.init_idiosyncratic_shocks) InfiniteType.assignParameters( LivPrb=[1.], DiscFac=0.955, PermGroFac=[1.], PermShkStd=[0.], TempShkStd=[0.], T_total=1, T_retire=0, BoroCnstArt=None, UnempPrb=0., cycles=0) # This is what makes the type infinite horizon InfiniteType.updateIncomeProcess() InfiniteType.solve() InfiniteType.timeFwd() InfiniteType.unpackcFunc() # Make and solve a perfect foresight consumer type PerfectForesightType = deepcopy(InfiniteType) PerfectForesightType.solveOnePeriod = solvePerfForesight PerfectForesightType.solve() PerfectForesightType.unpackcFunc() PerfectForesightType.timeFwd() self.InfiniteType = InfiniteType self.PerfectForesightType = PerfectForesightType
def perturbParameterToGetcPlotList(base_dictionary, param_name, param_min, param_max, N=20, time_vary=False): param_vec = np.linspace( param_min, param_max, num=N, endpoint=True ) # vector of alternative values of the parameter to examine thisConsumer = IndShockConsumerType( **my_dictionary) # create an instance of the consumer type thisConsumer.cycles = 0 # Make this type have an infinite horizon x = np.linspace( mMinVal, mMaxVal, xPoints, endpoint=True ) # Define a vector of x values that span the range from the minimum to the maximum values of m for j in range( N): # loop from the first to the last values of the parameter if time_vary: # Some parameters are time-varying; others are not setattr(thisConsumer, param_name, [param_vec[j]]) else: setattr(thisConsumer, param_name, param_vec[j]) thisConsumer.update( ) # set up the preliminaries required to solve the problem thisConsumer.solve() # solve the problem y = thisConsumer.solution[0].cFunc( x ) # Get the values of the consumption function at the points in the vector of x points pylab.plot( x, y, label=str(round(param_vec[j], 3)) ) # plot it and generate a label indicating the rounded value of the parameter pylab.legend(loc='upper right') # put the legend in the upper right return pylab # return the figure
# Declare how much we want to increase credit by credit_change = 0.001 # Now increase the consumer's credit limit. # We do this by decreasing the artificial borrowing constraint. XtraCreditExample.BoroCnstArt = BaselineExample.BoroCnstArt - credit_change #################################################################################################### """ Now we are ready to solve the consumers' problems. In HARK, this is done by calling the solve() method of the ConsumerType. """ ### First solve the baseline example. BaselineExample.solve() ### Now solve the comparison example of the consumer with a bit more credit XtraCreditExample.solve() #################################################################################################### """ Now that we have the solutions to the 2 different problems, we can compare them """ ## We are going to compare the consumption functions for the two different consumers. ## Policy functions (including consumption functions) in HARK are stored as attributes ## of the *solution* of the ConsumerType. The solution, in turn, is a list, indexed by the time ## period the solution is for. Since in this demo we are working with infinite-horizon models ## where every period is the same, there is only one time period and hence only one solution.
############################################################################### if __name__ == '__main__': import ConsumerParameters as Params from time import clock mystr = lambda number: "{:.4f}".format(number) # Make and solve an example consumer with idiosyncratic income shocks IndShockExample = IndShockConsumerType(**Params.init_idiosyncratic_shocks) IndShockExample.cycles = 0 # Make this type have an infinite horizon IndShockExample.DiePrb = 1 - IndShockExample.LivPrb[ 0] # Not sure why this is not already in the object start_time = clock() IndShockExample.solve() end_time = clock() print('Solving a consumer with idiosyncratic shocks took ' + mystr(end_time - start_time) + ' seconds.') IndShockExample.unpackcFunc() IndShockExample.timeFwd() PermShk = 1.1 TranShk = 1 cPermImpulseResponse = AggCons_impulseResponseInd(IndShockExample, PermShk, TranShk, 100, 25) plt.plot(cPermImpulseResponse) PermShk = 1 TranShk = 1.1 cTranImpulseResponse = AggCons_impulseResponseInd(IndShockExample, PermShk,
credit_change = .001 # Now increase the consumer's credit limit. # We do this by decreasing the artificial borrowing constraint. XtraCreditExample.BoroCnstArt = BaselineExample.BoroCnstArt - credit_change #################################################################################################### """ Now we are ready to solve the consumers' problems. In HARK, this is done by calling the solve() method of the ConsumerType. """ ### First solve the baseline example. BaselineExample.solve() ### Now solve the comparison example of the consumer with a bit more credit XtraCreditExample.solve() #################################################################################################### """ Now that we have the solutions to the 2 different problems, we can compare them """ ## We are going to compare the consumption functions for the two different consumers. ## Policy functions (including consumption functions) in HARK are stored as attributes ## of the *solution* of the ConsumerType. The solution, in turn, is a list, indexed by the time ## period the solution is for. Since in this demo we are working with infinite-horizon models
PFexample.timeFwd() # Plot the perfect foresight consumption function print('Linear consumption function:') mMin = PFexample.solution[0].mNrmMin #plotFuncs(PFexample.cFunc[0],mMin,mMin+10) ############################################################################### # Make and solve an example consumer with idiosyncratic income shocks IndShockExample = IndShockConsumerType(**Params.init_idiosyncratic_shocks) IndShockExample.cycles = 0 # Make this type have an infinite horizon start_time = clock() IndShockExample.solve() end_time = clock() print('Solving a consumer with idiosyncratic shocks took ' + mystr(end_time - start_time) + ' seconds.') IndShockExample.unpackcFunc() IndShockExample.timeFwd() # Plot the consumption function and MPC for the infinite horizon consumer #print('Concave consumption function:') #plotFuncs(IndShockExample.cFunc[0],IndShockExample.solution[0].mNrmMin,5) #print('Marginal consumption function:') #plotFuncsDer(IndShockExample.cFunc[0],IndShockExample.solution[0].mNrmMin,5) # Compare the consumption functions for the perfect foresight and idiosyncratic # shock types. Risky income cFunc asymptotically approaches perfect foresight cFunc. print('Consumption functions for perfect foresight vs idiosyncratic shocks:')