Skip to content

Commit

Permalink
fix bugs on bequest model when BeqShift == 0
Browse files Browse the repository at this point in the history
  • Loading branch information
alanlujan91 committed Apr 6, 2024
1 parent 47ce83a commit db9facd
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 297 deletions.
73 changes: 9 additions & 64 deletions HARK/ConsumptionSaving/ConsBequestModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
ConsumerSolution,
IndShockConsumerType,
init_idiosyncratic_shocks,
init_lifecycle,
)
from HARK.ConsumptionSaving.ConsPortfolioModel import (
PortfolioConsumerType,
Expand All @@ -43,14 +42,7 @@


class BequestWarmGlowConsumerType(IndShockConsumerType):
time_inv_ = IndShockConsumerType.time_inv_ + [
"BeqCRRA",
"BeqShift",
]

time_vary_ = IndShockConsumerType.time_vary_ + [
"BeqFac",
]
time_inv_ = IndShockConsumerType.time_inv_ + ["BeqCRRA", "BeqShift", "BeqFac"]

def __init__(self, **kwds):
params = init_accidental_bequest.copy()
Expand All @@ -60,26 +52,6 @@ def __init__(self, **kwds):

self.solve_one_period = solve_one_period_ConsWarmBequest

def update(self):
super().update()
self.update_parameters()

def update_parameters(self):
if not isinstance(self.BeqCRRA, (int, float)):
raise ValueError("Bequest CRRA parameter must be a single value.")

if isinstance(self.BeqFac, (int, float)):
self.BeqFac = [self.BeqFac] * self.T_cycle
elif len(self.BeqFac) == 1:
self.BeqFac *= self.T_cycle
elif len(self.BeqFac) != self.T_cycle:
raise ValueError(
"Bequest relative value parameter must be a single value or a list of length T_cycle",
)

if not isinstance(self.BeqShift, (int, float)):
raise ValueError("Bequest Stone-Geary parameter must be a single value.")

def update_solution_terminal(self):
if self.BeqFacTerm == 0.0: # No terminal bequest
super().update_solution_terminal()
Expand Down Expand Up @@ -117,15 +89,7 @@ def update_solution_terminal(self):


class BequestWarmGlowPortfolioType(PortfolioConsumerType):
time_inv_ = IndShockConsumerType.time_inv_ + [
"BeqCRRA",
"BeqShift",
"DiscreteShareBool",
]

time_vary_ = IndShockConsumerType.time_vary_ + [
"BeqFac",
]
time_inv_ = PortfolioConsumerType.time_inv_ + ["BeqCRRA", "BeqShift", "BeqFac"]

def __init__(self, **kwds):
params = init_portfolio_bequest.copy()
Expand All @@ -137,26 +101,6 @@ def __init__(self, **kwds):

self.solve_one_period = solve_one_period_ConsPortfolioWarmGlow

def update(self):
super().update()
self.update_parameters()

def update_parameters(self):
if not isinstance(self.BeqCRRA, (int, float)):
raise ValueError("Bequest CRRA parameter must be a single value.")

if isinstance(self.BeqFac, (int, float)):
self.BeqFac = [self.BeqFac] * self.T_cycle
elif len(self.BeqFac) == 1:
self.BeqFac *= self.T_cycle
elif len(self.BeqFac) != self.T_cycle:
raise ValueError(
"Bequest relative value parameter must be a single value or a list of length T_cycle",
)

if not isinstance(self.BeqShift, (int, float)):
raise ValueError("Bequest Stone-Geary parameter must be a single value.")

def update_solution_terminal(self):
if self.BeqFacTerm == 0.0: # No terminal bequest
super().update_solution_terminal()
Expand Down Expand Up @@ -477,7 +421,6 @@ def calc_vPPnext(S, a, R):

def solve_one_period_ConsPortfolioWarmGlow(
solution_next,
ShockDstn,
IncShkDstn,
RiskyDstn,
LivPrb,
Expand All @@ -492,7 +435,6 @@ def solve_one_period_ConsPortfolioWarmGlow(
ShareLimit,
vFuncBool,
DiscreteShareBool,
IndepDstnBool,
BeqCRRA,
BeqFac,
BeqShift,
Expand Down Expand Up @@ -596,7 +538,9 @@ def solve_one_period_ConsPortfolioWarmGlow(

# Set a flag for whether the natural borrowing constraint is zero, which
# depends on whether the smallest transitory income shock is zero
BoroCnstNat_iszero = np.min(IncShkDstn.atoms[1]) == 0.0
BoroCnstNat_iszero = (np.min(IncShkDstn.atoms[1]) == 0.0) or (
BeqFac != 0.0 and BeqShift == 0.0
)

# Prepare to calculate end-of-period marginal values by creating an array
# of market resources that the agent could have next period, considering
Expand Down Expand Up @@ -737,7 +681,8 @@ def EndOfPrddvds_dist(S, a, z):
EndOfPrd_dvda = DiscFacEff * expected(
calc_EndOfPrd_dvda, RiskyDstn, args=(aNrmNow, ShareNext)
)
EndOfPrd_dvda += warm_glow.der(aNrmNow)
warm_glow_der = warm_glow.der(aNrmNow)
EndOfPrd_dvda += np.where(np.isnan(warm_glow_der), 0.0, warm_glow_der)
EndOfPrd_dvdaNvrs = uFunc.derinv(EndOfPrd_dvda)

# Calculate end-of-period marginal value of risky portfolio share by taking expectations
Expand Down Expand Up @@ -997,12 +942,12 @@ def calc_EndOfPrd_v(S, a, z):
init_accidental_bequest["BeqShiftTerm"] = 0.0

init_warm_glow_terminal_only = init_accidental_bequest.copy()
init_warm_glow_terminal_only["BeqCRRATerm"] = init_lifecycle["CRRA"]
init_warm_glow_terminal_only["BeqCRRATerm"] = init_idiosyncratic_shocks["CRRA"]
init_warm_glow_terminal_only["BeqFacTerm"] = 40.0 # kid lives 40yr after bequest
init_warm_glow_terminal_only["BeqShiftTerm"] = 0.0

init_warm_glow = init_warm_glow_terminal_only.copy()
init_warm_glow["BeqCRRA"] = init_lifecycle["CRRA"]
init_warm_glow["BeqCRRA"] = init_idiosyncratic_shocks["CRRA"]
init_warm_glow["BeqFac"] = 40.0
init_warm_glow["BeqShift"] = 0.0

Expand Down
7 changes: 1 addition & 6 deletions HARK/ConsumptionSaving/ConsPortfolioModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import numpy as np

from HARK import AgentType, NullFunc
from HARK import NullFunc
from HARK.ConsumptionSaving.ConsIndShockModel import (
IndShockConsumerType,
init_idiosyncratic_shocks,
Expand Down Expand Up @@ -171,11 +171,6 @@ def __init__(self, verbose=False, quiet=False, **kwds):

# Set the solver for the portfolio model, and update various constructed attributes
self.solve_one_period = solve_one_period_ConsPortfolio
self.update()

def pre_solve(self):
AgentType.pre_solve(self)
self.update_solution_terminal()

def update(self):
RiskyAssetConsumerType.update(self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
"outputs": [],
"source": [
"from HARK.ConsumptionSaving.ConsBequestModel import BequestWarmGlowConsumerType\n",
"from HARK.ConsumptionSaving.ConsIndShockModel import (\n",
" IndShockConsumerType,\n",
" init_idiosyncratic_shocks,\n",
")\n",
"from HARK.ConsumptionSaving.ConsIndShockModel import IndShockConsumerType\n",
"from HARK.utilities import plot_funcs"
]
},
Expand All @@ -31,7 +28,7 @@
"metadata": {},
"outputs": [],
"source": [
"ind_agent = IndShockConsumerType(**init_idiosyncratic_shocks)\n",
"ind_agent = IndShockConsumerType()\n",
"ind_agent.cycles = 0\n",
"ind_agent.solve()"
]
Expand Down
114 changes: 0 additions & 114 deletions examples/ConsBequestModel/example_AccidentalBequestPort.ipynb

This file was deleted.

109 changes: 109 additions & 0 deletions examples/ConsBequestModel/example_AccidentalBequestPortComp.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/ConsBequestModel/example_TerminalBequest.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Solving a lifecycle consumer took 0.06389713287353516 seconds.\n"
"Solving a lifecycle consumer took 0.04110527038574219 seconds.\n"
]
}
],
Expand Down
101 changes: 57 additions & 44 deletions examples/ConsBequestModel/example_TerminalBequestPort.ipynb

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions examples/ConsBequestModel/example_WarmGlowBequest.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 1,
"id": "19c5f531",
"metadata": {},
"outputs": [],
Expand All @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 2,
"id": "8a2d828c",
"metadata": {
"title": "Alter calibration"
Expand Down Expand Up @@ -82,7 +82,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 3,
"id": "fbfff075",
"metadata": {
"title": "Create and solve agent"
Expand All @@ -97,15 +97,15 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 4,
"id": "5f41049a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solving a lifecycle consumer took 0.04669356346130371 seconds.\n"
"Solving a lifecycle consumer took 0.04212594032287598 seconds.\n"
]
}
],
Expand All @@ -119,7 +119,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 5,
"id": "27d1663e",
"metadata": {},
"outputs": [
Expand Down Expand Up @@ -149,7 +149,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 6,
"id": "a16b7032",
"metadata": {
"lines_to_next_cell": 2,
Expand Down Expand Up @@ -220,7 +220,7 @@
" 7.7341921 , 2.70711694]])}"
]
},
"execution_count": 14,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -240,7 +240,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 7,
"id": "7c7979ec",
"metadata": {
"title": "Extract and format simulation results"
Expand All @@ -261,7 +261,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 8,
"id": "1d8269db",
"metadata": {
"title": "Plots"
Expand Down
130 changes: 77 additions & 53 deletions examples/ConsBequestModel/example_WarmGlowBequestPort.ipynb

Large diffs are not rendered by default.

0 comments on commit db9facd

Please sign in to comment.