-
Notifications
You must be signed in to change notification settings - Fork 2
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
Refactor solution and simulation #108
base: main
Are you sure you want to change the base?
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
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.
Very nice! Only managed to look at the files I have comments in so far, will try to do more on the weekend but no guarantees.
@@ -30,7 +34,7 @@ def get_lcm_function( | |||
targets: Literal["solve", "simulate", "solve_and_simulate"], | |||
debug_mode: bool = True, | |||
jit: bool = True, | |||
) -> tuple[Callable[..., list[Array] | pd.DataFrame], ParamsDict]: | |||
) -> tuple[Callable[..., dict[int, Array] | pd.DataFrame], ParamsDict]: |
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.
I don't see from the docstring how "requested targets" are reflected in the output type. Could you change that? Also, in the summary line we don't need need to repeat entry_point
. Maybe that could be "Return the solve/simulate functions and a template for the model's parameters." ?
) | ||
|
||
target_func: Callable[..., list[Array] | pd.DataFrame] | ||
target_func: Callable[..., dict[int, Array] | pd.DataFrame] |
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.
Maybe add a comment that this is just repeating the signature to get rid of mypy complaints? (I suppose)
) | ||
|
||
solve_and_simulate_model = partial( | ||
solve_and_simulate, |
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.
I would define the function solve_and_simulate
here, I think. Or in a separate module of the same name. But I would not look for it in the simulation.simulate
module...
If there was a clever way of telling simulate_model
that the partialled argument to simulate(vf_arr_dict)
will be the output of solve_model
(2nd order partialling, I guess), that would be even better...
logger=logger, | ||
) | ||
|
||
_next_state_simulate = get_next_state_function( |
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.
Move below solve_model = ...
. I appreciate having both jit calls next to each other but having a "solve" and a "simulate" block would be even better.
period=period, | ||
is_last_period=is_last_period, | ||
) | ||
|
||
compute_ccv = create_compute_conditional_continuation_value( | ||
compute_ccv = get_compute_conditional_continuation_value( |
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.
compute_ccv = get_compute_conditional_continuation_value( | |
compute_cc_val = get_compute_conditional_continuation_value( |
Symmetry because I'd like ccp
to be cc_pol
|
||
compute_ccv_argmax = create_compute_conditional_continuation_policy( | ||
compute_ccp = get_compute_conditional_continuation_policy( |
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.
compute_ccp = get_compute_conditional_continuation_policy( | |
compute_cc_pol = get_compute_conditional_continuation_policy( |
Since we have CCP estimators and there P
stands for probability
, this is too confusing IMO.
compute_ccv_functions=compute_ccv_functions, | ||
emax_calculators=emax_calculators, | ||
emax_calculators=solve_discrete_problem_functions, |
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.
Would there be a value in renaming the emax_calculators
argument, too?
@@ -6,6 +7,7 @@ | |||
|
|||
from lcm.grids import ContinuousGrid, DiscreteGrid, Grid | |||
from lcm.typing import InternalUserFunction, ParamsDict, ShockType | |||
from lcm.utils import first_non_none | |||
|
|||
|
|||
@dataclass(frozen=True) |
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.
Does this make the mutable attributes frozen, too? Else, would there be a value in using FrozenDicts
or the like?
*, | ||
is_last_period: bool, | ||
) -> StateSpaceInfo: | ||
"""Create a state-space information for the model solution. |
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.
"""Create a state-space information for the model solution. | |
"""Collect information on the state space for the model solution. |
) | ||
|
||
|
||
def _validate_initial_states( |
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.
def _validate_initial_states( | |
def _validate_initial_state_names( |
This does not do validation of values, right? With the earlier name I'd expect that kind of checks.
In this PR, I try to make clear where simulation and solution code differs, and where it does not. I also perform some additional refactoring along the way.
Specifically, I
model_functions
module toutility_and_feasibility
and rewrite for clarityproductmap
instead ofspacemap
in the solutionspacemap
and adjust it so that it's usage during the simulation is clearconditional_continuation
moduletest_get_lcm_function_with_simulate_target
(test did not account for stochastics...)solve_and_simulate
function