def test_liquidity_constraint(self): KinkyExample = KinkedRconsumerType() KinkyExample.cycles = 0 # The consumer cannot borrow more than 0.4 # times their permanent income KinkyExample.BoroCnstArt = -0.4 # Solve the consumer's problem KinkyExample.solve() self.assertAlmostEqual( KinkyExample.solution[0].cFunc(1).tolist(), 0.9623337593984276 ) self.assertAlmostEqual( KinkyExample.solution[0].cFunc(4).tolist(), 1.343766303734248 ) KinkyExample.BoroCnstArt = -0.2 KinkyExample.solve() self.assertAlmostEqual( KinkyExample.solution[0].cFunc(1).tolist(), 0.9346895908550565 ) self.assertAlmostEqual( KinkyExample.solution[0].cFunc(4).tolist(), 1.3401428646781697 )
def test_KinkedRconsumerType(self): try: model = KinkedRconsumerType(**init_kinked_R) except: self.fail( "KinkedRconsumerType failed to initialize with Params.init_kinked_R." )
def getRfree(self): # Specify which getRfree to use return KinkedRconsumerType.getRfree(self)
# Plot the consumption functions for the cyclical consumer type print("Quarterly consumption functions:") mMin = min([X.mNrmMin for X in CyclicalExample.solution]) plotFuncs(CyclicalExample.cFunc, mMin, 5) # %% # Simulate some data; results stored in cHist, mHist, bHist, aHist, MPChist, and pHist if do_simulation: CyclicalExample.T_sim = 480 CyclicalExample.track_vars = ["mNrmNow", "cNrmNow", "pLvlNow", "t_cycle"] CyclicalExample.initializeSim() CyclicalExample.simulate() # %% # Make and solve an agent with a kinky interest rate KinkyExample = KinkedRconsumerType() KinkyExample.cycles = 0 # Make the Example infinite horizon # %% start_time = time() KinkyExample.solve() end_time = time() print("Solving a kinky consumer took " + mystr(end_time - start_time) + " seconds.") KinkyExample.unpackcFunc() print("Kinky consumption function:") KinkyExample.timeFwd() plotFuncs(KinkyExample.cFunc[0], KinkyExample.solution[0].mNrmMin, 5) # %% if do_simulation:
## We create the instance of the consumer type by calling IndShockConsumerType() ## We use the default parameter values by passing **Params.init_idiosyncratic_shocks as an argument BaselineExample = IndShockConsumerType(**Params.init_idiosyncratic_shocks) # %% {"code_folding": []} # Note: we've created an instance of a very standard consumer type, and many assumptions go # into making this kind of consumer. As with any structural model, these assumptions matter. # For example, this consumer pays the same interest rate on # debt as she earns on savings. If instead we wanted to solve the problem of a consumer # who pays a higher interest rate on debt than she earns on savings, this would be really easy, # since this is a model that is also solved in HARK. All we would have to do is import that model # and instantiate an instance of that ConsumerType instead. As a homework assignment, we leave it # to you to uncomment the two lines of code below, and see how the results change! from HARK.ConsumptionSaving.ConsIndShockModel import KinkedRconsumerType BaselineExample = KinkedRconsumerType(**Params.init_kinked_R) # %% [markdown] # The next step is to change the values of parameters as we want. # # To see all the parameters used in the model, along with their default values, see $\texttt{ConsumerParameters.py}$ # # Parameter values are stored as attributes of the $\texttt{ConsumerType}$ the values are used for. For example, the risk-free interest rate $\texttt{Rfree}$ is stored as $\texttt{BaselineExample.Rfree}$. Because we created $\texttt{BaselineExample}$ using the default parameters values at the moment $\texttt{BaselineExample.Rfree}$ is set to the default value of $\texttt{Rfree}$ (which, at the time this demo was written, was 1.03). Therefore, to change the risk-free interest rate used in $\texttt{BaselineExample}$ to (say) 1.02, all we need to do is: # # %% {"code_folding": [0]} # Change the Default Riskfree Interest Rate BaselineExample.Rfree = 1.02 # %% {"code_folding": [0]} ## Change some parameter values
"T_sim": 500, # Number of periods to simulate "aNrmInitMean": -6.0, # Mean of log initial assets "aNrmInitStd": 1.0, # Standard deviation of log initial assets "pLvlInitMean": 0.0, # Mean of log initial permanent income "pLvlInitStd": 0.0, # Standard deviation of log initial permanent income "PermGroFacAgg": 1.0, # Aggregate permanent income growth factor "T_age": None, # Age after which simulated agents are automatically killed } # %% [markdown] # ## Solving and examining the solution of the "kinked R" model # # The cell below creates an infinite horizon instance of $\texttt{KinkedRconsumerType}$ and solves its model by calling its $\texttt{solve}$ method. # %% KinkyExample = KinkedRconsumerType(**KinkedRdict) KinkyExample.cycles = 0 # Make the example infinite horizon KinkyExample.solve() # %% [markdown] # An element of a $\texttt{KinkedRconsumerType}$'s solution will have all the same attributes as that of a $\texttt{IndShockConsumerType}$; see that notebook for details. # # We can plot the consumption function of our "kinked R" example, as well as the MPC: # %% print("Kinked R consumption function:") plotFuncs(KinkyExample.solution[0].cFunc, KinkyExample.solution[0].mNrmMin, 5) print("Kinked R marginal propensity to consume:") plotFuncsDer(KinkyExample.solution[0].cFunc, KinkyExample.solution[0].mNrmMin, 5)
## We create the instance of the consumer type by calling IndShockConsumerType() ## We use the default parameter values by passing **Params.init_idiosyncratic_shocks as an argument BaselineExample = IndShockConsumerType() # %% {"code_folding": []} # Note: we've created an instance of a very standard consumer type, and many assumptions go # into making this kind of consumer. As with any structural model, these assumptions matter. # For example, this consumer pays the same interest rate on # debt as she earns on savings. If instead we wanted to solve the problem of a consumer # who pays a higher interest rate on debt than she earns on savings, this would be really easy, # since this is a model that is also solved in HARK. All we would have to do is import that model # and instantiate an instance of that ConsumerType instead. As a homework assignment, we leave it # to you to uncomment the two lines of code below, and see how the results change! from HARK.ConsumptionSaving.ConsIndShockModel import KinkedRconsumerType BaselineExample = KinkedRconsumerType() # %% [markdown] """ The next step is to change the values of parameters as we want. To see all the parameters used in the model, along with their default values, see $\texttt{ConsumerParameters.py}$ Parameter values are stored as attributes of the $\texttt{ConsumerType}$ the values are used for. For example, the risk-free interest rate $\texttt{Rfree}$ is stored as $\texttt{BaselineExample.Rfree}$. Because we created $\texttt{BaselineExample}$ using the default parameters values at the moment $\texttt{BaselineExample.Rfree}$ is set to the default value of $\texttt{Rfree}$ (which, at the time this demo was written, was 1.03). Therefore, to change the risk-free interest rate used in $\texttt{BaselineExample}$ to (say) 1.02, all we need to do is: """ # %% {"code_folding": [0]} # Change the Default Riskfree Interest Rate BaselineExample.Rfree = 1.02
mystr = lambda number: "{:.4f}".format(number) import matplotlib.pyplot as plt # - # ## What Happens To the Consumption Function When A Liquidity Constraint is Tightened? # # (This example builds on the ConsIndShockModel notebook; digest that before proceeding) # # The $\texttt{KinkedRconsumerType}$ class solves the problem of a consumer for whom the interest rate on borrowing is higher than the rate that the consumer will receive on any positive saving they do. The default calibration is meant to capture a case where the borrowing occurs at an interest rate typical of credit cards. # # (Fuller discussion of the issues here can be found in [A Theory of the Consumption Function, With or Without Liquidity Constraints](http://econ.jhu.edu/people/ccarroll/ATheoryv3JEP.pdf)) # + # Create an instance of the type of consumer we are interested in KinkyExample = KinkedRconsumerType(**Params.init_kinked_R) # Make the example infinite horizon (not a life cycle model) KinkyExample.cycles = 0 # The consumer cannot borrow more than 0.4 times their permanent income KinkyExample.BoroCnstArt = -0.4 # Solve the consumer's problem KinkyExample.solve() # Plot the results plt.ylabel('Consumption c') plt.xlabel('Market Resources m') plotFuncs([KinkyExample.solution[0].cFunc], KinkyExample.solution[0].mNrmMin, 5)
# # The $\texttt{KinkedRconsumerType}$ class solves the problem of a consumer for whom the interest rate on borrowing is higher than the rate that the consumer will receive on any positive saving they do. The default calibration is meant to capture a case where the borrowing occurs at an interest rate typical of credit cards. # # (Fuller discussion of the issues here can be found in [A Theory of the Consumption Function, With or Without Liquidity Constraints](http://econ.jhu.edu/people/ccarroll/ATheoryv3JEP.pdf)) # + {"code_folding": [0]} from copy import deepcopy from HARK.ConsumptionSaving.ConsIndShockModel import KinkedRconsumerType from HARK.utilities import plotFuncsDer, plotFuncs mystr = lambda number: "{:.4f}".format(number) import matplotlib.pyplot as plt # + # Create an instance of the type of consumer we are interested in KinkyExample = KinkedRconsumerType() # Make the example infinite horizon (not a life cycle model) KinkyExample.cycles = 0 # The consumer cannot borrow more than 0.4 times their permanent income KinkyExample.BoroCnstArt = -0.4 # Solve the consumer's problem KinkyExample.solve() # Plot the results plt.ylabel('Consumption c') plt.xlabel('Market Resources m') plotFuncs([KinkyExample.solution[0].cFunc], KinkyExample.solution[0].mNrmMin, 5)
"T_sim": 500, # Number of periods to simulate "aNrmInitMean": -6.0, # Mean of log initial assets "aNrmInitStd": 1.0, # Standard deviation of log initial assets "pLvlInitMean": 0.0, # Mean of log initial permanent income "pLvlInitStd": 0.0, # Standard deviation of log initial permanent income "PermGroFacAgg": 1.0, # Aggregate permanent income growth factor "T_age": None, # Age after which simulated agents are automatically killed } # %% [markdown] # ## Solving and examining the solution of the "kinked R" model # # The cell below creates an infinite horizon instance of `KinkedRconsumerType` and solves its model by calling its `solve` method. # %% KinkyExample = KinkedRconsumerType(**KinkedRdict) KinkyExample.cycles = 0 # Make the example infinite horizon KinkyExample.solve() # %% [markdown] # An element of a `KinkedRconsumerType`'s solution will have all the same attributes as that of a `IndShockConsumerType`; see that notebook for details. # # We can plot the consumption function of our "kinked R" example, as well as the MPC: # %% print("Kinked R consumption function:") plot_funcs(KinkyExample.solution[0].cFunc, KinkyExample.solution[0].mNrmMin, 5) print("Kinked R marginal propensity to consume:") plot_funcs_der(KinkyExample.solution[0].cFunc, KinkyExample.solution[0].mNrmMin, 5)