Skip to content

Commit

Permalink
Fix tests and notebooks
Browse files Browse the repository at this point in the history
Adjustments for new constructor.
  • Loading branch information
mnwhite committed Aug 16, 2024
1 parent 36aa16f commit 13fa927
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 403 deletions.
7 changes: 2 additions & 5 deletions HARK/ConsumptionSaving/tests/test_ConsMarkovModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@ def setUp(self):

init_serial_unemployment = copy(init_indshk_markov)
init_serial_unemployment["MrkvArray"] = [MrkvArray]
init_serial_unemployment["UnempPrb"] = np.zeros(
2
) # Income process is overwritten below
# to make income distribution when employed
init_serial_unemployment["UnempPrb"] = np.zeros(2)
# Income process is overwritten below to make income distribution when employed
init_serial_unemployment["global_markov"] = False
init_serial_unemployment["Rfree"] = np.array([1.03, 1.03, 1.03, 1.03])
init_serial_unemployment["LivPrb"] = [np.array([0.98, 0.98, 0.98, 0.98])]
Expand All @@ -70,7 +68,6 @@ def setUp(self):
self.model = MarkovConsumerType(**init_serial_unemployment)
self.model.cycles = 0
self.model.vFuncBool = False # for easy toggling here
self.model.MrkvArray = [MrkvArray]

# Replace the default (lognormal) income distribution with a custom one
employed_income_dist = DiscreteDistributionLabeled(
Expand Down
10 changes: 7 additions & 3 deletions HARK/ConsumptionSaving/tests/test_modelInits.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ def test_MarkovConsumerType(self):
# Make a consumer with serially correlated unemployment, subject to boom and bust cycles
init_serial_unemployment = {}
init_serial_unemployment["MrkvArray"] = [MrkvArray]
init_serial_unemployment["UnempPrb"] = (
0.0 # to make income distribution when employed
)
init_serial_unemployment["UnempPrb"] = np.zeros(2)
# Income process is overwritten below to make income distribution when employed
init_serial_unemployment["global_markov"] = False
init_serial_unemployment["Rfree"] = np.array([1.03, 1.03, 1.03, 1.03])
init_serial_unemployment["LivPrb"] = [np.array([0.98, 0.98, 0.98, 0.98])]
init_serial_unemployment["PermGroFac"] = [
np.array([1.01, 1.01, 1.01, 1.01])
]
SerialUnemploymentExample = MarkovConsumerType(**init_serial_unemployment)
except:
self.fail(
Expand Down
8 changes: 4 additions & 4 deletions HARK/ConsumptionSaving/tests/test_modelcomparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,14 @@ def setUp(self):
"Mrkv_p11": [1.0 - base_primitives["UnempPrb"]],
"Mrkv_p22": [1.0],
"BoroCnstArt": None,
"PermShkStd": [0.0],
"PermShkStd": np.array([[0.0, 0.0]]),
"PermShkCount": 1,
"TranShkStd": [0.0],
"TranShkStd": np.array([[0.0, 0.0]]),
"TranShkCount": 1,
"UnempPrb": 0.0,
"UnempPrb": np.array([[0.0, 0.0]]), # This will be overwritten
"UnempPrbRet": 0.0,
"T_retire": 0,
"IncUnemp": 0.0,
"IncUnemp": np.array([[0.0, 0.0]]), # This will be overwritten
"IncUnempRet": 0.0,
"aXtraMin": 0.001,
"aXtraMax": TBSType.mUpperBnd,
Expand Down
334 changes: 47 additions & 287 deletions examples/ConsumptionSaving/example_ConsMarkovModel.ipynb

Large diffs are not rendered by default.

134 changes: 30 additions & 104 deletions examples/ConsumptionSaving/example_TractableBufferStockModel.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,7 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:37.397658Z",
"iopub.status.busy": "2024-07-11T15:28:37.397488Z",
"iopub.status.idle": "2024-07-11T15:28:38.138893Z",
"shell.execute_reply": "2024-07-11T15:28:38.138246Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"# An alternative, much longer way to solve the TBS model\n",
Expand All @@ -38,14 +31,7 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.140862Z",
"iopub.status.busy": "2024-07-11T15:28:38.140575Z",
"iopub.status.idle": "2024-07-11T15:28:38.143289Z",
"shell.execute_reply": "2024-07-11T15:28:38.142819Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"# Define the model primitives\n",
Expand All @@ -61,14 +47,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.144857Z",
"iopub.status.busy": "2024-07-11T15:28:38.144604Z",
"iopub.status.idle": "2024-07-11T15:28:38.147174Z",
"shell.execute_reply": "2024-07-11T15:28:38.146723Z"
}
},
"metadata": {},
"outputs": [],
"source": [
"# Define a dictionary to be used in case of simulation\n",
Expand All @@ -84,20 +63,13 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.148655Z",
"iopub.status.busy": "2024-07-11T15:28:38.148424Z",
"iopub.status.idle": "2024-07-11T15:28:38.153501Z",
"shell.execute_reply": "2024-07-11T15:28:38.153035Z"
}
},
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solving a tractable consumption-savings model took 0.0018313999999999275 seconds.\n"
"Solving a tractable consumption-savings model took 0.0 seconds.\n"
]
}
],
Expand All @@ -118,14 +90,7 @@
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.155159Z",
"iopub.status.busy": "2024-07-11T15:28:38.154710Z",
"iopub.status.idle": "2024-07-11T15:28:38.452503Z",
"shell.execute_reply": "2024-07-11T15:28:38.451910Z"
}
},
"metadata": {},
"outputs": [
{
"data": {
Expand All @@ -150,12 +115,6 @@
"cell_type": "code",
"execution_count": 6,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.454367Z",
"iopub.status.busy": "2024-07-11T15:28:38.454005Z",
"iopub.status.idle": "2024-07-11T15:28:38.615600Z",
"shell.execute_reply": "2024-07-11T15:28:38.615039Z"
},
"lines_to_next_cell": 2
},
"outputs": [],
Expand All @@ -172,22 +131,12 @@
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.617489Z",
"iopub.status.busy": "2024-07-11T15:28:38.617241Z",
"iopub.status.idle": "2024-07-11T15:28:38.624528Z",
"shell.execute_reply": "2024-07-11T15:28:38.624049Z"
}
},
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# Now solve the same model using backward induction rather than the analytic method of TBS.\n",
"# The TBS model is equivalent to a Markov model with two states, one of them absorbing (permanent unemployment).\n",
"MrkvArray = np.array(\n",
" [[1.0 - base_primitives[\"UnempPrb\"], base_primitives[\"UnempPrb\"]], [0.0, 1.0]],\n",
") # Define the two state, absorbing unemployment Markov array\n",
"init_consumer_objects = {\n",
" \"CRRA\": base_primitives[\"CRRA\"],\n",
" \"Rfree\": np.array(\n",
Expand All @@ -199,16 +148,18 @@
" ),\n",
" ], # Unemployment-compensated permanent growth factor\n",
" \"BoroCnstArt\": None, # Artificial borrowing constraint\n",
" \"PermShkStd\": [0.0], # Permanent shock standard deviation\n",
" \"PermShkStd\": np.array([[0.0, 0.0]]), # Permanent shock standard deviation\n",
" \"PermShkCount\": 1, # Number of shocks in discrete permanent shock distribution\n",
" \"TranShkStd\": [0.0], # Transitory shock standard deviation\n",
" \"TranShkStd\": np.array([[0.0, 0.0]]), # Transitory shock standard deviation\n",
" \"TranShkCount\": 1, # Number of shocks in discrete permanent shock distribution\n",
" \"T_cycle\": 1, # Number of periods in cycle\n",
" \"UnempPrb\": 0.0, # Unemployment probability (not used, as the unemployment here is *permanent*, not transitory)\n",
" \"UnempPrbRet\": 0.0, # Unemployment probability when retired (irrelevant here)\n",
" \"UnempPrb\": np.array(\n",
" [[0.0, 0.0]]\n",
" ), # Unemployment probability (not used, as the unemployment here is *permanent*, not transitory)\n",
" \"UnempPrbRet\": None, # Unemployment probability when retired (irrelevant here)\n",
" \"T_retire\": 0, # Age at retirement (turned off)\n",
" \"IncUnemp\": 0.0, # Income when unemployed (irrelevant)\n",
" \"IncUnempRet\": 0.0, # Income when unemployed and retired (irrelevant)\n",
" \"IncUnemp\": np.array([[0.0, 0.0]]), # Income when unemployed (irrelevant)\n",
" \"IncUnempRet\": None, # Income when unemployed and retired (irrelevant)\n",
" \"aXtraMin\": 0.001, # Minimum value of assets above minimum in grid\n",
" \"aXtraMax\": ExampleType.mUpperBnd, # Maximum value of assets above minimum in grid\n",
" \"aXtraCount\": 48, # Number of points in assets grid\n",
Expand All @@ -220,22 +171,18 @@
" \"tax_rate\": 0.0, # Tax rate on labor income (irrelevant)\n",
" \"vFuncBool\": False, # Whether to calculate the value function\n",
" \"CubicBool\": True, # Whether to use cubic splines (False --> linear splines)\n",
" \"MrkvArray\": [MrkvArray], # State transition probabilities\n",
" \"Mrkv_p11\": [\n",
" 1.0 - base_primitives[\"UnempPrb\"]\n",
" ], # Define the two state, absorbing unemployment Markov array\n",
" \"Mrkv_p22\": [1.0], # Define the two state, absorbing unemployment Markov array\n",
"}\n",
"MarkovType = MarkovConsumerType(**init_consumer_objects) # Make a basic consumer type"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.625993Z",
"iopub.status.busy": "2024-07-11T15:28:38.625752Z",
"iopub.status.idle": "2024-07-11T15:28:38.629449Z",
"shell.execute_reply": "2024-07-11T15:28:38.628984Z"
}
},
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"employed_income_dist = DiscreteDistributionLabeled(\n",
Expand All @@ -252,15 +199,8 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.630942Z",
"iopub.status.busy": "2024-07-11T15:28:38.630695Z",
"iopub.status.idle": "2024-07-11T15:28:38.633030Z",
"shell.execute_reply": "2024-07-11T15:28:38.632580Z"
}
},
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"MarkovType.IncShkDstn = [\n",
Expand All @@ -271,15 +211,8 @@
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.634478Z",
"iopub.status.busy": "2024-07-11T15:28:38.634249Z",
"iopub.status.idle": "2024-07-11T15:28:38.764369Z",
"shell.execute_reply": "2024-07-11T15:28:38.763860Z"
}
},
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stderr",
Expand All @@ -299,21 +232,14 @@
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"execution": {
"iopub.execute_input": "2024-07-11T15:28:38.765970Z",
"iopub.status.busy": "2024-07-11T15:28:38.765719Z",
"iopub.status.idle": "2024-07-11T15:28:38.882997Z",
"shell.execute_reply": "2024-07-11T15:28:38.882470Z"
}
},
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solving the same model \"the long way\" took 0.12803889999999996 seconds.\n"
"Solving the same model \"the long way\" took 0.28125 seconds.\n"
]
},
{
Expand Down Expand Up @@ -390,7 +316,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.10.14"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
Expand Down

0 comments on commit 13fa927

Please sign in to comment.