Skip to content

Emit one terminal-regime row per subject in to_dataframe - #396

Merged
hmgaudecker merged 31 commits into
mainfrom
feat/terminal-rows
Jul 6, 2026
Merged

hmgaudecker merged 31 commits into
mainfrom
feat/terminal-rows

Conversation

@hmgaudecker

@hmgaudecker hmgaudecker commented Jul 2, 2026 •

Copy link
Copy Markdown
Member

Closes #386.

What this PR does

SimulationResult.to_dataframe gains a terminal_rows knob:

df = result.to_dataframe()                      # terminal_rows="first" (default)
df = result.to_dataframe(terminal_rows="all")   # absorbing representation

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"

subject_id age regime_name
0 60 alive
70 dead
80 dead

terminal_rows="first" (default)

subject_id age regime_name
0 60 alive
70 dead

Implementation

A post-hoc filter on the assembled flat frame (_keep_first_terminal_row in lcm/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 under use_labels=False. No change to the simulation internals — the fixed-population kernels and the n_subjects invariant stay intact.

Tests

On the Epstein–Zin mortality example (multi-age terminal dead regime, certain death by the horizon): exactly one dead row 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 on main via the usual cascade.

🤖 Generated with Claude Code

hmgaudecker and others added 24 commits June 21, 2026 19:09
…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>
@read-the-docs-community

read-the-docs-community Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Jul 2, 2026 •

Copy link
Copy Markdown

Benchmark results (HEAD only — no baseline comparison available)

Benchmark (0a8e428) Statistic Value
aca-baseline execution time 13.163 s
peak GPU mem 588 MB
compilation time 375.70 s
peak CPU mem 7.04 GB
aca-baseline-debug execution time 56.366 s
peak GPU mem 587 MB
compilation time 450.99 s
peak CPU mem 7.81 GB
Mahler-Yum execution time 4.541 s
peak GPU mem 520 MB
compilation time 11.33 s
peak CPU mem 1.59 GB
Precautionary Savings - Solve execution time 23.5 ms
peak GPU mem 8 MB
compilation time 1.55 s
peak CPU mem 1.16 GB
Precautionary Savings - Simulate execution time 64.8 ms
peak GPU mem 157 MB
compilation time 3.46 s
peak CPU mem 1.33 GB
Precautionary Savings - Solve & Simulate execution time 95.7 ms
peak GPU mem 566 MB
compilation time 4.74 s
peak CPU mem 1.31 GB
Precautionary Savings - Solve & Simulate (irreg) execution time 202.0 ms
peak GPU mem 2.18 GB
compilation time 5.06 s
peak CPU mem 1.36 GB
IskhakovEtAl2017Simulate execution time 188.1 ms
compilation time 4.27 s
peak CPU mem 1.29 GB
IskhakovEtAl2017Solve execution time 45.1 ms
compilation time 0.71 s
peak CPU mem 1.15 GB
IskhakovEtAl2017SimulateGpuPeakMem peak GPU mem 281 MB
IskhakovEtAl2017SolveGpuPeakMem peak GPU mem 67 MB

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
hmgaudecker force-pushed the feat/terminal-rows branch from 99165d1 to 346b5d0 Compare July 3, 2026 06:56
…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
hmgaudecker force-pushed the feat/terminal-rows branch 2 times, most recently from 6859de6 to c42565e Compare July 3, 2026 08:48
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
hmgaudecker force-pushed the feat/certainty-equivalent branch from bd99589 to c0dab41 Compare July 3, 2026 09:49
@hmgaudecker
hmgaudecker force-pushed the feat/terminal-rows branch from c42565e to cbbdf1e Compare July 3, 2026 09:49
hmgaudecker and others added 3 commits July 3, 2026 11:59
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
hmgaudecker force-pushed the feat/terminal-rows branch from cbbdf1e to ec6e8b5 Compare July 3, 2026 09:59
Base automatically changed from feat/certainty-equivalent to main July 6, 2026 05:13
@hmgaudecker
hmgaudecker merged commit deb2ebc into main Jul 6, 2026
10 of 11 checks passed
@hmgaudecker
hmgaudecker deleted the feat/terminal-rows branch July 6, 2026 05:15
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: to_dataframe option to keep only the first terminal-regime row per subject

2 participants