Skip to content

Commit

Permalink
use age-varying parameters in MonteCarloSimulator
Browse files Browse the repository at this point in the history
  • Loading branch information
sbenthall committed Aug 10, 2023
1 parent 4ea021a commit 411703a
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions HARK/simulation/monte_carlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,45 @@ def simulate_dynamics(

return vals

def parameters_by_age(ages, parameters):
"""
Returns parameters for this model, but with vectorized
values which map age-varying values to agent ages.
Parameters
----------
ages: np.array
An array of agent ages.
parameters: dict
A parameters dictionary
Returns
--------
aged_parameters: dict
A dictionary of parameter values.
If a parameter is age-varying, the value is a vector
corresponding to the values for each input age.
"""
def aged_param(ages, p_value):
if isinstance(p_value, float) or isinstance(p_value, int):
return p_value
elif isinstance(p_value, list) and len(p_value) > 1:
pv_array = np.array(p_value)
return np.apply_along_axis(

Check warning on line 125 in HARK/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

HARK/simulation/monte_carlo.py#L123-L125

Added lines #L123 - L125 were not covered by tests
lambda a: pv_array[a],
0,
ages
)
else:
return np.empty(ages.size)

Check warning on line 131 in HARK/simulation/monte_carlo.py

View check run for this annotation

Codecov / codecov/patch

HARK/simulation/monte_carlo.py#L131

Added line #L131 was not covered by tests

return {
p : aged_param(ages, parameters[p])
for p
in parameters
}

class Simulator():
pass

Expand Down Expand Up @@ -207,8 +246,6 @@ def initialize_sim(self):
if self.vars_now[var] is None:
self.vars_now[var] = copy(blank_array)

# elif self.state_prev[var] is None:
# self.state_prev[var] = copy(blank_array)
self.t_age = np.zeros(
self.agent_count, dtype=int
) # Number of periods since agent entry
Expand Down Expand Up @@ -268,8 +305,8 @@ def sim_one_period(self):
### BIG CHANGES HERE from HARK.core.AgentType
shocks_now = draw_shocks(self.shocks, self.t_age)

# maybe need to time index the parameters here somehow?
pre = copy(self.parameters)
pre = parameters_by_age(self.t_age, self.parameters)

pre.update(self.vars_prev)
pre.update(shocks_now)
#Won't work for 3.8: self.parameters | self.vars_prev | shocks_now
Expand Down

0 comments on commit 411703a

Please sign in to comment.