Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing t indexing for inputs for cyclical models #1039

Merged
merged 3 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Release Data: TBD

#### Major Changes

* Input parameters for cyclical models now indexed by t [#1039](https://github.com/econ-ark/HARK/pull/1039)
* A IndexDistribution class for representing time-indexed probability distributions [#1018](https://github.com/econ-ark/pull/1018/).
* Adds new consumption-savings-portfolio model `RiskyContrib`, which represents an agent who can save in risky and risk-free assets but faces
frictions to moving funds between them. To circumvent these frictions, he has access to an income-deduction scheme to accumulate risky assets.
Expand Down
13 changes: 9 additions & 4 deletions HARK/ConsumptionSaving/ConsIndShockModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1729,7 +1729,7 @@ def sim_death(self):
# Determine who dies
DiePrb_by_t_cycle = 1.0 - np.asarray(self.LivPrb)
DiePrb = DiePrb_by_t_cycle[
self.t_cycle - 1
self.t_cycle - 1 if self.cycles == 1 else self.t_cycle
] # Time has already advanced, so look back one

# In finite-horizon problems the previous line gives newborns the
Expand Down Expand Up @@ -2173,12 +2173,17 @@ def get_shocks(self):
newborn = self.t_age == 0
for t in range(self.T_cycle):
these = t == self.t_cycle

# temporary, see #1022
if self.cycles == 1:
t = t - 1

N = np.sum(these)
if N > 0:
IncShkDstnNow = self.IncShkDstn[
t - 1
t
] # set current income distribution
PermGroFacNow = self.PermGroFac[t - 1] # and permanent growth factor
PermGroFacNow = self.PermGroFac[t] # and permanent growth factor
# Get random draws of income shocks from the discrete distribution
IncShks = IncShkDstnNow.draw(N)

Expand Down Expand Up @@ -3047,7 +3052,7 @@ def construct_assets_grid(parameters):

# Make a dictionary to specify an infinite consumer with a four period cycle
init_cyclical = copy(init_idiosyncratic_shocks)
init_cyclical['PermGroFac'] = [1.082251, 2.8, 0.3, 1.1]
init_cyclical['PermGroFac'] = [1.1, 1.082251, 2.8, 0.3]
Copy link
Contributor

@Mv77 Mv77 Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern comes from my unfamiliarity with cyclical models in HARK.

For a finite horizon, non-cyclical model with two periods, I think HARK requires len(PermGroFac) == 1. The single element in the list is the growth factor of permanent income between the first and second periods. The terminal period is, in this sense, "left out" from the list which has length 1 for a 2-period model.

How does this logic change when I'm considering an infinite-horizon model with a cycle of two periods? I know Chris, Matt and you had some lengthy discussion on how to treat the "terminal" period in cyclical vs non-cyclical models, which I did not follow, but I wanted to make sure this change (which looks good to me) is consistent with whatever you settled on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that when cycles == 0, it's infinite horizon and so there is no terminal period solution.

When cycles == N, it's a finite model repeated many times. If a terminal solution is given, the solver returns N * T + 1 solutions.

init_cyclical['PermShkStd'] = [0.1, 0.1, 0.1, 0.1]
init_cyclical['TranShkStd'] = [0.1, 0.1, 0.1, 0.1]
init_cyclical['LivPrb'] = 4*[0.98]
Expand Down
7 changes: 6 additions & 1 deletion HARK/ConsumptionSaving/tests/test_IndShockConsumerType.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def test_lifecyle(self):
"Rfree": 1.03, # Interest factor on assets
"DiscFac": 0.96, # Intertemporal discount factor
"LivPrb": 4 * [0.98], # Survival probability
"PermGroFac": [1.082251, 2.8, 0.3, 1.1],
"PermGroFac": [1.1, 1.082251, 2.8, 0.3],
# Parameters that specify the income distribution over the lifecycle
"PermShkStd": [0.1, 0.1, 0.1, 0.1],
"PermShkCount": 7, # Number of points in discrete approximation to permanent income shocks
Expand Down Expand Up @@ -358,6 +358,11 @@ def test_cyclical(self):
CyclicalExample.solution[3].cFunc(3).tolist(), 1.5958390056965004
)

CyclicalExample.initialize_sim()
CyclicalExample.simulate()

self.assertAlmostEqual(CyclicalExample.state_now['aLvl'][1], 0.41839957)

# %% Tests of 'stable points'


Expand Down
9 changes: 5 additions & 4 deletions HARK/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,12 @@ def solve_one_cycle(agent, solution_last):
# Initialize the solution for this cycle, then iterate on periods
solution_cycle = []
solution_next = solution_last
for t in range(T):

cycles_range = [0] + list(range(T - 1, 0, -1))
for k in (range(T-1, -1, -1) if agent.cycles == 1 else cycles_range):
# Update which single period solver to use (if it depends on time)
if hasattr(agent.solve_one_period, "__getitem__"):
solve_one_period = agent.solve_one_period[T - 1 - t]
solve_one_period = agent.solve_one_period[k]
else:
solve_one_period = agent.solve_one_period

Expand All @@ -1019,8 +1021,7 @@ def solve_one_cycle(agent, solution_last):
# Update time-varying single period inputs
for name in agent.time_vary:
if name in these_args:
# solve_dict[name] = eval('agent.' + name + '[t]')
solve_dict[name] = agent.__dict__[name][T - 1 - t]
solve_dict[name] = agent.__dict__[name][k]
solve_dict["solution_next"] = solution_next

# Make a temporary dictionary for this period
Expand Down