Skip to content

Commit

Permalink
remove sim_death from MonteCarloSimulator, just use 'live' state
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenthall committed Jul 27, 2023
1 parent dde1e88 commit 4ea021a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
45 changes: 14 additions & 31 deletions HARK/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ def draw_shocks(

for shock_var in shocks:
shock = shocks[shock_var]
if isinstance(shock, Aggregate):

if isinstance(shock, (int, float)):
draws[shock_var] = np.ones(len(conditions)) * shock
elif isinstance(shock, Aggregate):
draws[shock_var] = shock.dist.draw(1)[0]
elif isinstance(shock, IndexDistribution) \
or isinstance(shock, TimeVaryingDiscreteDistribution):
Expand Down Expand Up @@ -321,10 +324,15 @@ def make_shock_history(self):
def get_mortality(self):
"""
Simulates mortality or agent turnover according to some model-specific rules named sim_death
and sim_birth (methods of an AgentType subclass). sim_death takes no arguments and returns
and sim_birth (methods of an AgentType subclass).
Agents die when their states `live` is less than or equal to zero.
a Boolean array of size agent_count, indicating which agents of this type have "died" and
must be replaced. sim_birth takes such a Boolean array as an argument and generates initial
post-decision states for those agent indices.
must be replaced.
sim_birth takes such a Boolean array as an argument and generates initial
states for those agent indices.
Parameters
----------
Expand All @@ -334,13 +342,13 @@ def get_mortality(self):
-------
None
"""
who_dies = self.sim_death()
who_dies = self.vars_now['live'] <= 0

if self.read_shocks:
# Instead of simulating births, assign the saved newborn initial conditions
if np.sum(who_dies) > 0:
for var_name in self.initial:
self.state_now[var_name][
self.vars_now[var_name][
who_dies
] = self.newborn_init_history[var_name][
self.t_sim, who_dies
Expand All @@ -355,31 +363,6 @@ def get_mortality(self):
self.who_dies = who_dies
return None

def sim_death(self):
"""
# TODO: This should mainly just track the 'who_dies' var, which can be a shock or endogenous.
Determines which agents in the current population "die" or should be replaced. Takes no
inputs, returns a Boolean array of size self.agent_count, which has True for agents who die
and False for those that survive. Returns all False by default, must be overwritten by a
subclass to have replacement events.
Parameters
----------
None
Returns
-------
who_dies : np.array
Boolean array of size self.agent_count indicating which agents die and are replaced.
"""

#if self.read_shocks:
# who_dies = self.shock_history["who_dies"][self.t_sim, :]

who_dies = np.zeros(self.agent_count, dtype=bool)
return who_dies

def sim_birth(self, which_agents):
"""
Makes new agents for the simulation. Takes a boolean array as an input, indicating which
Expand Down
11 changes: 7 additions & 4 deletions HARK/simulation/test_monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import unittest

from HARK.distribution import MeanOneLogNormal, IndexDistribution
from HARK.distribution import Bernoulli, MeanOneLogNormal, IndexDistribution
from HARK.simulation.monte_carlo import *

cons_shocks = {
Expand All @@ -13,7 +13,8 @@
{
'sigma' : [1.0, 1.1]
}),
'theta' : MeanOneLogNormal(1)
'theta' : MeanOneLogNormal(1),
'live' : Bernoulli(p=0.98)
}

cons_pre = {
Expand Down Expand Up @@ -61,11 +62,13 @@ def setUp(self):
## TODO: Add an aggregate shock
## TODO: Add a time varying shock.
'theta' : MeanOneLogNormal(1),
'agg_R' : Aggregate(MeanOneLogNormal(1))
'agg_R' : Aggregate(MeanOneLogNormal(1)),
'live' : Bernoulli(p=0.98)
}

self.initial = {
'a' : MeanOneLogNormal(1)
'a' : MeanOneLogNormal(1),
'live' : 1
}

self.parameters = { # TODO
Expand Down

0 comments on commit 4ea021a

Please sign in to comment.