Emit one terminal-regime row per subject in to_dataframe - #396
Merged
Merged
Conversation
…river) Two tests on the 4-CPU distributed seam: - test_distributed_solve_matches_single_device_per_type: the F6 correctness contract — a sharded solve must equal the single-device solve per type slice. Green now; the co-map fix must preserve it (catches a wrong device-local index). - test_distributed_solve_kernel_does_not_all_gather_continuation_v: the red driver — the backward-induction kernel currently all-gathers the continuation V across the type shard; it must read only its device-local slice. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
vmap_1d gains co_mapped_in_axes: an optional per-argument in_axes override so a pytree argument's leading axis can be mapped in lockstep with the mapped variables. The backward-induction co-map uses it to slice each next_regime_to_V_arr leaf to the device-local type, so the continuation-V interpolation reads only its own shard. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Fixed, distributed states (e.g. a permanent type sharded one block per device) never transition, so a regime's continuation value depends only on its own slice of the next-period V-array. The grid-search solve kernel now co-maps each such state with the matching axis of every next_regime_to_V_arr leaf that carries it: an outer vmap peels the leading axis off both the state grid and the continuation V, so the interpolation reads only the device-local slice and XLA inserts no all-gather of the full V-array onto every device. - max_Q_over_a splits the co-mapped (leading) states from the inner productmap and wraps it in per-state co-map vmaps; the V-interpolator drops those coordinates and Q_and_F omits the sliced next-states. - processing detects the co-mappable states (distributed and identity-transition) and builds per-state, per-leaf in_axes so a target regime that prunes the state keeps its full leaf. - Simulation is unchanged: it keeps the full continuation V (subjects are not type-aligned across devices), so the co-map applies to the solve path only. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…seam (#385) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ectations Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- Add 'age' entry to initial_conditions in epstein_zin.md Run section - Document certainty_equivalent parameter in get_model docstring - Add docstrings to _power_transform and _power_inverse helpers Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…model Rename `TransformedExpectation` to `QuasiArithmeticMean` and `PowerCertaintyEquivalent` to `PowerMean`; the `certainty_equivalent` field, params pseudo-function, and `risk_aversion` parameter are unchanged. Move the engine-side implementation (`CE_VALUE_ARG`, the power transform pair, and `resolve_certainty_equivalent`) into `_lcm/certainty_equivalent.py` so the public module is a thin, deep-module namespace and the solver seam in `Q_and_F.py` only imports the resolver. Collapse the toy `tests/test_models/epstein_zin_health.py` and the example into a single parametrized `lcm_examples.epstein_zin` (`EZRegimeId`, `get_model` with a required `certainty_equivalent` and grid-size knobs whose defaults reproduce the toy numerically). The numpy pinning references pass unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Replace `docs/examples/epstein_zin.md` with `docs/examples/epstein_zin.ipynb`: the same recursion, mapping, and pitfalls as markdown cells, plus code cells that solve and simulate a 20-period model for two risk-aversion values and render a plotly figure of mean wealth by age (grey vs accent, direct labels). Update the toctree in `myst.yml` and the link in `examples/index.md`. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…he example - lcm.aggregators exposes the two standard Koopmans aggregators; the default H is now the public H_linear, and H_epstein_zin is parametrized by the intertemporal elasticity of substitution (curvature rho = 1 - 1/psi computed inside, psi = 1 as the Cobb-Douglas limit). - PowerMean handles risk_aversion = 1 as the geometric-mean (log) limit exp(E[log V']) instead of rejecting it; the numpy reference and pinning tests cover it. - Example notebook: merged the redundant positivity pitfalls, added an expected-utility baseline to the wealth figure, log_level='off'. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
… trace Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Benchmark results (HEAD only — no baseline comparison available)
No merge-base results found locally. Run benchmarks on main first for a comparison. |
The example model gains three parameters, all defaulting to the previous behavior: income (above the consumption floor, saving becomes possible), health_cost (an out-of-pocket expense while in bad health - uninsurable expense risk in the spirit of the Atal et al. medical spending), and bequest_scale (prices the bequest in consumption-equivalent units; H_epstein_zin is a weighted power mean, so the alive value sits at the scale of per-period consumption and an unscaled sqrt(wealth) bequest makes death the good branch of the certainty equivalent). next_wealth clips to the wealth grid so none of the knobs can push states off-grid. The docs page is rewritten around the fixed model: - Atal, Fang, Karlsson & Ziebarth (2025) is now characterized correctly: their baseline is time-separable CARA expected utility; their robustness specification has exactly the CES-aggregator-around-a- certainty-equivalent structure used here, with a CARA certainty equivalent (expressible as a QuasiArithmeticMean). - The pylcm-mapping section reflects the shipped H_linear/H_epstein_zin and explains that per-period utility must live in consumption units because H is a power mean. - A new pitfall documents the bequest-scaling trap. - The figure simulates 1,000 subjects with saving, health costs, and a gentler mortality hazard, so survivor counts stay meaningful at every plotted age, and the closing text explains the economics: both types dissave early out of impatience, the risk-averse type holds and rebuilds a larger precautionary buffer, and the ranking flips when the bad-health spell becomes too expensive to self-insure. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
hmgaudecker
force-pushed
the
feat/terminal-rows
branch
from
July 3, 2026 06:56
99165d1 to
346b5d0
Compare
…IES/RA sweep The intro now describes the original Atal, Fang, Karlsson & Ziebarth (2025) model accurately: an annual Yaari life-cycle savings problem over ages 25-94, a seven-category health Markov chain driving expenditure and mortality, and the guaranteed-renewable GLTHI vs short-term insurance contracts. Their baseline is time-separable expected utility (CARA gamma=4e-4, CRRA sigma=4 robustness, delta=0.966); the headline is that GLTHI reaches ~96% of first-best welfare, robust to disentangling risk aversion and the IES (sec. VI.D.1, within 0.7%). A 'what this keeps and drops' paragraph is explicit that the equilibrium contract/premium layer is out of scope, so the paper's headline welfare gap is not something this consumer-block example reproduces. A new closing section makes the disentangling concrete: a 2D sweep of the welfare cost of the uninsurable health-expense risk over a log-2 grid of risk aversion and the IES, with expected utility marked as the exact anti-diagonal (IES = 1/gamma). The cost is a risk premium — it roughly triples down the risk-aversion axis and barely moves along the IES axis — so an EU model, confined to the anti-diagonal, confounds the two. This is exactly the degree of freedom the certainty_equivalent seam adds and the lever the paper's robustness section pulls. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
hmgaudecker
force-pushed
the
feat/terminal-rows
branch
2 times, most recently
from
July 3, 2026 08:48
6859de6 to
c42565e
Compare
The example model enters at age 25 rather than 60, matching the annual horizon of Atal et al. instead of a retirement-only slice. Only the entry age changes and periods stay annual, so the discount factor needs no recompounding. The docs page runs the full 25-to-85 lifecycle: 2,000 subjects under a Gompertz-like mortality hazard (low when young, rising with age), which keeps a well-populated surviving cohort through midlife so both figures read cleanly. The risk-averse agent holds a persistently larger precautionary buffer, and the welfare cost of the health-expense risk roughly doubles down the risk-aversion axis while barely moving along the IES axis. Prose and quoted numbers track the new lifecycle. The notebook executes in about ten seconds on CPU, within the Read-the-Docs build budget. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
hmgaudecker
force-pushed
the
feat/certainty-equivalent
branch
from
July 3, 2026 09:49
bd99589 to
c0dab41
Compare
hmgaudecker
force-pushed
the
feat/terminal-rows
branch
from
July 3, 2026 09:49
c42565e to
cbbdf1e
Compare
The intro markdown cell had lost its newlines and collapsed into a single
line, so its headings and paragraphs ran together on the rendered page.
Rebuild it with one array element per line. Also switch the recursion's
display equation from a ```{math}``` directive — which this project's MyST
does not process inside notebook cells, leaking raw LaTeX — to the $$ form
used by every other notebook, so it typesets.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A subject that enters a terminal regime is carried with frozen state through every later period the regime is active. to_dataframe now emits only the entry row per subject (terminal_rows="first", the default); terminal_rows="all" keeps the absorbing representation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The stored regression frames capture the absorbing representation, so the comparison must request it explicitly rather than inherit the collapsed default. Only the mortality model exposed this on CI — its subjects die before the horizon under float32 draws — but all four shape-pinning tests get the explicit knob. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
hmgaudecker
force-pushed
the
feat/terminal-rows
branch
from
July 3, 2026 09:59
cbbdf1e to
ec6e8b5
Compare
mj023
approved these changes
Jul 4, 2026
hmgaudecker
added a commit
that referenced
this pull request
Jul 7, 2026
Integrate the 28 commits feat/dcegm advanced since the branch point: the Epstein-Zin certainty-equivalent seam (#395), device-local continuation-V (#391), persistent-compilation-cache fix (#397), per-subject terminal rows (#396), and the CI/tooling bumps (ty prek hook, action/pixi pins, main merges). Conflict resolution: - src/lcm/__init__.py: keep both the NB-EGM case_piece exports and the new certainty_equivalent exports. - src/_lcm/regime_building/Q_and_F.py: take feat/dcegm's version — its #395 refactor relocated the continuation-operator logic into _lcm/certainty_equivalent.py, superseding nb-egm's inline copy. The MappingLeaf-payload concern nb-egm's deleted unit test guarded is covered by the Q-bundle threading contract and the solve-level nbegm_mappingleaf_threshold agreement tests. - tests/regime_building/test_continuation_operator.py: accept the deletion (replaced by tests/test_certainty_equivalent.py). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016PCdJtoqhhjBWhGAo7AXuz
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #386.
What this PR does
SimulationResult.to_dataframegains aterminal_rowsknob:Terminal regimes are absorbing: a subject that enters one is carried with frozen state through every later period the regime is active, so its one-shot terminal payoff (e.g. a bequest) is re-emitted again and again. With
"first"(the new default), each subject keeps only its terminal entry row — a dead subject appears once at death, like leaving a panel."all"reproduces the previous output.For a subject that dies at age 70:
terminal_rows="all"terminal_rows="first"(default)Implementation
A post-hoc filter on the assembled flat frame (
_keep_first_terminal_rowinlcm/result.py): group by subject, keep every row up to and including the first period in a terminal regime; subjects that never enter one keep all rows. The filter runs before the categorical conversion, so it applies identically underuse_labels=False. No change to the simulation internals — the fixed-population kernels and then_subjectsinvariant stay intact.Tests
On the Epstein–Zin mortality example (multi-age terminal
deadregime, certain death by the horizon): exactly onedeadrow per subject;"first"equals"all"with each subject's post-entry rows dropped (assert_frame_equal);"all"emits one row per subject and period;"first"is the default; the filter composes with integer codes. Full suite green — no existing test relied on the duplicated rows.Base branch
Targets
feat/certainty-equivalent(#395), whose Epstein–Zin example provides the multi-age terminal test model; lands onmainvia the usual cascade.🤖 Generated with Claude Code