Allow more deeply nested mappings in params while retaining tight typing.#237
Allow more deeply nested mappings in params while retaining tight typing.#237hmgaudecker merged 61 commits intomainfrom
params while retaining tight typing.#237Conversation
Instead of wrapping each function to accept an internal_regime_params dict and extracting parameters at call time, use dags.signature.rename_arguments() to qualify parameter names with function prefixes (e.g., risk_aversion becomes utility__risk_aversion). This makes the parameter flow explicit and removes the internal_regime_params indirection from all function signatures. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…at_regime_params. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ParamsTemplate. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…s Callable in result.py. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…mstances than REGIME_SEPARATOR. Some small improvements.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…eatment of dynamic IrregSpacedGrids, in particular ShockGrids.
Three bugs in _partial_fixed_params_into_regimes: 1. internal_functions.regime_transition_probs was not updated alongside the top-level attribute, causing simulation to use un-partialled functions for regime transitions. 2. regime_transition_probs was partialled with ALL regime fixed params, but it only accepts a subset — breaking inspect.signature used by dags.concatenate_functions in to_dataframe. 3. to_dataframe additional_targets built DAGs from raw functions that still expected fixed params no longer present in runtime params. Fixes: update internal_functions, filter kwargs per function signature, store resolved_fixed_params on InternalRegime for target computation. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
for more information, see https://pre-commit.ci
Add the NestedMappingParams type.
timmens
left a comment
There was a problem hiding this comment.
Very nice! I can see how this is useful/necessary for GETTSIM etc.
Only two minor remarks. The user would need to import NestedMappingParams and use that in her params definition, correct? In this case:
- Should we potentially find a name that fits better with what this object actually does, i.e., tells PyLCM to treat this Mapping as a leaf (not sure about the wording yet, but NestedMappingParams confused me)?
- Should this object live in either the top-level namespace or in
lcm.params?
…rocesses throughout. Stick to rho - mu - sigma for all shock parameters.
…ecture-dependent.
…ters need to be passed at runtime.
NestedMappingParams type.MappingLeaf type.
|
@timmens @MImmesberger -- do you like |
|
I think that is much better. I have one question (that might be stupid because I don't understand the application that well), but is there a reason why we don't simply want: lcm.params.LeafIn the sense that I have never read "MappingLeaf" before, but I have read "Leaf" frequently. And anyone who worked with JAX before will ---hopefully--- directly understand what is meant by "Leaf". Or is solving the "more general" problem simply not necessary here, and we can restrict ourselves completely to the Mapping type? Another thought that occurred to me, don't we actually want to export something like a as-function? gettsim_params = {"a": [0, 1], "b": jnp.arange(4), "c": -1231}
lcm_params = {"d": {"e": 2, "f": 3}, "gettsim_params": lcm.params.as_leaf(gettsim_params)}where |
…ternal_regime.state_action_space(flat_regime_params).
Yes, we want to have a mapping that is a leaf, useful for typing to distinguish this from other leaves. Since it was cheap and symmetric, I added
|
|
@MImmesberger, can you switch to |
MappingLeaf type.params while retaining tight typing.
|
@timmens -- I thought you had approved already, but either I misremember or it got lost because of subsequent changes. Anything left? |
Summary
Small update in order to work sensibly with GETTSIM functions.
MappingLeaf, a JAX-pytree-compatible wrapper that preventsflatten_regime_namespacefrom recursing into its contents, allowing nested parameter dicts to be passed through the params pipeline as opaque leaves. Same forSequenceLeaf.as_leaffunction, wich is how these will typically be generated, make it available vialcm.params.as_leaf_make_immutable/_make_mutableto handleMappingLeaf(converting inner data while preserving the wrapper)Test plan
test_mapping_leaf.py— construction, repr, equality, JAX pytree round-trips, immutable/mutable conversions,flatten_regime_namespaceleaf behaviortest_model.py::test_state_only_in_transitions_with_terminal_regime— reproducer for BUG: State variable only used in transitions causes ValueError in vmap_1d at last regime period #236.Somewhat unrelated, just because the bug crept up in the model where we are already using this branch.
This passes out of the box; must have been fixed along the way somewhere.