-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
[WIP] Transition matrices (general, life-cycle) #1286
Closed
Closed
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
6c87cf5
Shock distribution maker
Mv77 23f46bd
Make args work
Mv77 a3a74a6
Create transition matrix method sketch
Mv77 0430b26
Construct transition matrix from conditional distribution
Mv77 a73f6dc
Support masking states
Mv77 b8e6fda
Shock engine for consportfolio
Mv77 9310d25
State grid for consportfolio
Mv77 88b71cd
Transition equation for ConsPortfolio
Mv77 ff42bdf
Test for frictionless and calvo agent
Mv77 c4c5064
Black
Mv77 11d7059
Add a life-cycle example
Mv77 bf705bb
Merge branch 'plumbing/labeled-dist-of-fun' into lc-transition
Mv77 eed2d01
Start working xrrays in
Mv77 c7a816f
Use xarray
Mv77 d3539d3
Create conditional policyfuncs
Mv77 0f1f453
Merge branch 'lc-transition-xarray' into lc-transition
Mv77 d3238a8
Compare with current methods in ConsIndShock
Mv77 6b502aa
Implement deaths (but results don't match)
Mv77 f155fe6
Fix treatment of newborns in infinite horizon models
Mv77 516f4a0
Add transition matrix class
Mv77 f2d73ad
Merge branch 'master' into lc-transition
Mv77 cb583c8
Add a bruteforce LC transition matrix
Mv77 4678d60
Proof of concept of simulating LC distributions
Mv77 d4e37de
Fix and test infinite horizon simulator
Mv77 fea5c2c
Add tool to find the steady state
Mv77 126e8c5
Use tool for evaluating outcomes of interest as functions
Mv77 10210f1
Compare with Will in more detail
Mv77 6aa5f34
Typo
Mv77 41f6ee2
Merge branch 'master' into lc-transition
Mv77 9555210
Implement pre- and post-multiplication by transition mats
Mv77 d96fe71
Fix pre-multiplication bug caught with 3x3 test
Mv77 497a7f2
Update distribution iterator
Mv77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ | |
from HARK.parallel import multi_thread_commands, multi_thread_commands_fake | ||
from HARK.utilities import NullFunc, get_arg_names | ||
|
||
from HARK.mat_methods import mass_to_grid | ||
|
||
|
||
class Model: | ||
""" | ||
|
@@ -850,6 +852,121 @@ | |
self.history[var_name] = np.empty((self.T_sim, self.AgentCount)) | ||
self.history[var_name].fill(np.nan) | ||
|
||
def make_shock_distributions(self): | ||
# Calculate number of periods per cycle, defaults to 1 if all variables are time invariant | ||
if len(self.time_vary) > 0: | ||
# name = agent.time_vary[0] | ||
# T = len(eval('agent.' + name)) | ||
T = len(self.__dict__[self.time_vary[0]]) | ||
else: | ||
T = 1 | ||
|
||
dstn_dict = {parameter: self.__dict__[parameter] for parameter in self.time_inv} | ||
dstn_dict.update({parameter: None for parameter in self.time_vary}) | ||
|
||
if hasattr(self.shock_dstn_engine, "dstn_args"): | ||
these_args = self.shock_dstn_engine.dstn_args | ||
else: | ||
these_args = get_arg_names(self.shock_dstn_engine) | ||
|
||
these_args = tuple(filter(lambda x: x != "self", these_args)) | ||
|
||
# Initialize the list of shock distributions for this cycle, | ||
# then iterate on periods | ||
shock_dstns = [] | ||
|
||
cycles_range = [0] + list(range(T - 1, 0, -1)) | ||
for k in range(T - 1, -1, -1) if self.cycles == 1 else cycles_range: | ||
# Update time-varying single period inputs | ||
for name in self.time_vary: | ||
if name in these_args: | ||
dstn_dict[name] = self.__dict__[name][k] | ||
|
||
# Make a temporary dictionary for this period | ||
temp_dict = {name: dstn_dict[name] for name in these_args} | ||
|
||
# Construct this period's shock distribution one period | ||
# Add it to the solution, and move to the next period | ||
dstn_t = self.shock_dstn_engine(**temp_dict) | ||
shock_dstns.insert(0, dstn_t) | ||
|
||
# Save list of shock distributions | ||
self.full_shock_dstns = shock_dstns | ||
|
||
def find_transition_matrices(self): | ||
# Calculate number of periods per cycle, defaults to 1 if all variables are time invariant | ||
if len(self.time_vary) > 0: | ||
# name = agent.time_vary[0] | ||
# T = len(eval('agent.' + name)) | ||
T = len(self.__dict__[self.time_vary[0]]) | ||
else: | ||
T = 1 | ||
|
||
trans_dict = { | ||
parameter: self.__dict__[parameter] for parameter in self.time_inv | ||
} | ||
trans_dict.update({parameter: None for parameter in self.time_vary}) | ||
|
||
if hasattr(self.state_to_state_trans, "trans_args"): | ||
these_args = self.state_to_state_trans.trans_args | ||
else: | ||
these_args = get_arg_names(self.state_to_state_trans) | ||
|
||
exclude_args = ["self", "shocks_next", "state", "solution"] | ||
these_args = tuple(filter(lambda x: x not in exclude_args, these_args)) | ||
|
||
# Extract state grid data | ||
grids = [ | ||
self.state_grid.attrs["grids"][x].astype(float) | ||
for x in self.state_grid.attrs["mesh_order"] | ||
] | ||
# Find values and indices of non-trivial grids | ||
nt_inds, nt_grids = zip(*[[i, x] for i, x in enumerate(grids) if len(x) > 1]) | ||
|
||
# Number of points in full grid | ||
mesh_size = self.state_grid.coords["mesh"].size | ||
|
||
# Initialize the list transition matrices | ||
trans_mats = [] | ||
|
||
cycles_range = [0] + list(range(T - 1, 0, -1)) | ||
for k in range(T - 1, -1, -1) if self.cycles == 1 else cycles_range: | ||
# Update time-varying single period inputs | ||
for name in self.time_vary: | ||
if name in these_args: | ||
trans_dict[name] = self.__dict__[name][k] | ||
|
||
# Make a temporary dictionary for this period | ||
temp_dict = {name: trans_dict[name] for name in these_args} | ||
|
||
shock_dstn = self.full_shock_dstns[k] | ||
|
||
def trans_wrapper(shocks_next, solution, state_points): | ||
return self.state_to_state_trans( | ||
shocks_next, solution, state_points, **temp_dict | ||
) | ||
|
||
state_dstn = shock_dstn.dist_of_func( | ||
trans_wrapper, solution=self.solution[k], state_points=self.state_grid | ||
) | ||
|
||
state_points = state_dstn.dataset.to_array().values[nt_inds, :, :] | ||
pmv = state_dstn.probability.values | ||
|
||
# Construct transition matrix from the object above | ||
tmat = np.zeros((mesh_size, mesh_size)) | ||
for i in range(mesh_size): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
tmat[i, :] = mass_to_grid( | ||
points=state_points[:, i, :].T, | ||
mass=pmv, | ||
grids=nt_grids, | ||
) | ||
# Prepend | ||
trans_mats.insert(0, tmat) | ||
|
||
# Save matrices | ||
self.trans_mats = trans_mats | ||
|
||
|
||
def solve_agent(agent, verbose): | ||
""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just updated the methods to use
xarrays
and labeled distributions. They now take more expressive transition equations like these.