Show the planner's Monte Carlo run in a lazy frame

JA
5d ago
+477-4

Base: up/retirement-plan-ui-streams (stacked) | Head: up/retirement-plan-ui-monte-carlo

Closes #5567

Fork pre-flight: jaysbeekay/sure#976 (same commits)

Stacked on #5754

Last of five UI PRs: the Monte Carlo section. With it the planner covers every slice #5567 proposed.

Problem

The year-by-year plan answers on expected returns only. #5611 added a seeded Monte Carlo run that says how likely the money is to last when returns vary, but nothing shows it.

Resulting behaviour

The planner gains a "Chance of success" card:

  • the chance the money lasts;
  • the worst-years-first stress rate;
  • in FIRE mode, the earliest retirement year at the plan's confidence target, or that none reaches it;
  • the 5 x 5 grid over expected return and savings rate (DS::Table). Savings steps that clamp to the same rate show as one column.

The three figures use DS::Stat. The settings modal gains the volatility and the confidence target.

A departure from #5611. #5611 said a full result belongs off the request, in a job with a cache. This PR runs it inline instead, on its own request:

  • The section is a lazy Turbo frame (loading: :lazy, GET /retirement_plan/monte_carlo), so the page renders without waiting for it.
  • A job needs a cache that every process shares. Development's default null_store, or a per-process memory_store, would never show the result, and the page would enqueue a new run on every poll.
  • Inline, the cache is best-effort. RetirementPlan::MonteCarloCache keeps the result for a day under a SHA-256 of every input the run reads: the settings and streams, income and spending, the funded assets, the count of streams left out for want of a rate, the date, the seed, the path count and a version. A store that keeps nothing just runs it again.

What it costs, measured on one fast machine (engine only, 5,000 paths, one run per case):

CaseMeasured totalPercentilesConfident-year searchHeatmapThe frame now (no percentiles)
FIRE, #251's figures (ages 41 to 90)0.94 s0.18 s0.24 s0.45 s0.76 s
FIRE, ages 30 to 95, demo spending1.11 s0.20 s0.38 s0.45 s0.91 s
FIRE, ages 30 to 95, target reached only in 20841.80 s0.21 s1.03 s0.47 s1.59 s
FIRE, ages 30 to 95, no year reaches the target (worst case)1.98 s0.21 s1.14 s0.54 s1.77 s

A traditional plan has no confident-year search; with #251's figures a full result measured 0.6 s, percentiles included.

The percentile paths are left out of the frame's result: the page doesn't draw them, and they cost a further full run. The confident-year search stays a linear scan. As #5611 says, lasting is not monotonic in the year: before retiring only the savings contribution goes in, while once retired, income above spending is reinvested, so a large pension can make an earlier year last where a later one doesn't. A bisection could therefore give a different year from the scan.

  • The frame runs on the page's own date, passed in its URL, so a frame that loads after midnight matches the figures beside it. Only today or yesterday is taken; anything else is today.
  • Tenancy: the frame is the signed-in user's own saved plan or 404. A plan that can't be simulated gets no section, and its frame says so.

Size: 217 non-test lines added, 1 removed.

Validation

  • Full suite bin/rails test at this head ca9fb80f3 (which merges #5754's head a065de3d4): 18727 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest, which needs node, missing from the test image.
  • System tests (test/system/retirement_plans_test.rb, 4 runs, 0 failures at ca9fb80f3): the lazy frame loads once scrolled into view and shows the result.
  • bin/rubocop, erb_lint and Brakeman: clean.
  • Tests:
    • the page holds a lazy frame and runs nothing itself (MonteCarlo.expects(:new).never);
    • the frame shows the result;
    • a cached result is shown again without running (expects(...).once across two requests);
    • no saved plan gives 404, whoever else has one;
    • the frame runs on the page's date and only a recent one;
    • the key changes with each input, including a stream's rate arriving;
    • nothing is cached for a plan with nothing to run;
    • the clamp merging;
    • the FIRE confident year or its absence.
  • Mutations, each caught by a failing assertion:
    • a frame for an unsaved plan;
    • the cache bypassed;
    • a nil result cached;
    • the frame not lazy;
    • the page running the result itself;
    • a section for a plan that can't be simulated;
    • unconverted streams left out of the key;
    • the version left out of the key;
    • percentiles computed;
    • Integer clamp bounds;
    • the no-confident-year sentence missing;
    • the page date not passed, ignored, or taken from any day.

Blast radius

A new member route and action, a component, a view, and RetirementPlan::MonteCarloCache, which reads only RetirementPlan's public API. That API gains one reader, RetirementPlan#converted_streams, which returns the converted streams the simulation already builds, so the cache key hashes exactly what the run reads. The settings form and its permitted parameters gain the two Monte Carlo fields. No other model changes.

Migration notes

No migration.

Screenshots

Before: the planner from the previous PR, with no Monte Carlo section:

Planner before Monte Carlo

After, desktop and phone:

Planner with Monte Carlo

Planner with Monte Carlo, phone

The section, traditional and FIRE (confident year):

Monte Carlo, traditional

Monte Carlo, FIRE


Summary by cubic

Shows the planner's Monte Carlo run in a lazy Turbo frame so the page renders without waiting for the simulation.

  • The "Chance of success" card shows the probability the money lasts, the worst-years-first stress rate, the earliest FIRE retirement year reaching the confidence target (or that none does), and a 5×5 heatmap over return and savings rate, with clamped savings steps merged into one column.
  • The settings form gains return volatility and confidence target fields.
  • The run executes inline on its own GET /retirement_plan/monte_carlo request rather than in a background job, which would need a shared cache that development's default store doesn't provide; RetirementPlan::MonteCarloCache keys the result for a day under a SHA-256 digest of every input it reads, so an empty store just runs again.
  • The key hashes the converted streams the run actually reads through a new RetirementPlan#converted_streams reader, so a rate that arrives or is revised changes the key, while a rate for a pair no stream uses does not.
  • The cached result omits percentile paths, which the page doesn't draw and which cost a further full run.
  • The frame uses the page's date (today or yesterday only), serves only the signed-in user's saved plan, and is hidden for plans that cannot be simulated.

Summary by CodeRabbit

  • New Features
    • Retirement plans now include Monte Carlo simulations with success rates, stress results, and a savings-rate heatmap.
    • Set return volatility and a success target when creating or updating a plan.
    • FIRE plans can display a confidence-based retirement year, and unavailable simulation results are clearly indicated.
    • Simulation results load on demand and may be reused for the same day.
    • View the assumptions used for return volatility alongside simulation results.