engine+api: plumb life events into the simulator
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled

Until now life events were stored but ignored by the engine — pure
metadata. Now they actually move portfolios.

Engine:
- simulator.simulate() takes optional cashflow_adjustments: a (n_years,)
  real-GBP array applied each year *after* savings + return but
  *before* withdrawal. Positive = inflow, negative = outflow.
- New fire_planner/life_events.py with EventInput dataclass +
  events_to_cashflow_array(events, horizon). Handles ranged deltas,
  one-time amounts, disabled events, year clipping past horizon,
  negative year_start (clipped to 0), and summing multiple events.

API:
- /simulate accepts optional life_events list. Server converts each
  to EventInput, builds cashflow_adjustments, passes to simulate().
- Frontend Run-now on scenario detail now fetches the scenario's
  life events and includes them in the request — projections finally
  reflect "retire at 50, kid born at y3, inheritance at y22".

Tests: 11 events helper + 4 end-to-end engine + 1 API integration =
16 new tests. 188 total (was 172). mypy strict + ruff clean.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-05-09 22:30:33 +00:00
parent b82770b5c4
commit 2fc92c12f5
9 changed files with 335 additions and 4 deletions

View file

@ -203,6 +203,15 @@ class GoalCreate(BaseModel):
# ── simulate / compare ─────────────────────────────────────────────── # ── simulate / compare ───────────────────────────────────────────────
class LifeEventInput(BaseModel):
"""Engine-level event shape — same as the DB row's relevant fields."""
year_start: int = Field(ge=0, le=100)
year_end: int | None = Field(default=None, ge=0, le=100)
delta_gbp_per_year: Decimal = Decimal("0")
one_time_amount_gbp: Decimal | None = None
enabled: bool = True
class SimulateRequest(BaseModel): class SimulateRequest(BaseModel):
"""Sync, non-persisted simulate. Used by the React UI for what-if.""" """Sync, non-persisted simulate. Used by the React UI for what-if."""
jurisdiction: str jurisdiction: str
@ -216,6 +225,7 @@ class SimulateRequest(BaseModel):
floor_gbp: Decimal | None = None floor_gbp: Decimal | None = None
n_paths: int = Field(ge=100, le=50_000, default=5_000) n_paths: int = Field(ge=100, le=50_000, default=5_000)
seed: int = 42 seed: int = 42
life_events: list[LifeEventInput] = Field(default_factory=list)
class SimulateResult(BaseModel): class SimulateResult(BaseModel):

View file

@ -26,6 +26,7 @@ from fire_planner.api.schemas import (
SimulateResult, SimulateResult,
) )
from fire_planner.glide_path import get as get_glide from fire_planner.glide_path import get as get_glide
from fire_planner.life_events import EventInput, events_to_cashflow_array
from fire_planner.returns.bootstrap import block_bootstrap from fire_planner.returns.bootstrap import block_bootstrap
from fire_planner.returns.shiller import load_from_csv, synthetic_returns from fire_planner.returns.shiller import load_from_csv, synthetic_returns
from fire_planner.scenarios import build_regime_schedule, build_strategy from fire_planner.scenarios import build_regime_schedule, build_strategy
@ -47,6 +48,21 @@ def _project(req: SimulateRequest) -> tuple[SimulationResult, float]:
annual_savings = (np.full(req.horizon_years, float(req.savings_per_year_gbp), dtype=np.float64) annual_savings = (np.full(req.horizon_years, float(req.savings_per_year_gbp), dtype=np.float64)
if req.savings_per_year_gbp > 0 else None) if req.savings_per_year_gbp > 0 else None)
floor = float(req.floor_gbp) if req.floor_gbp is not None else None floor = float(req.floor_gbp) if req.floor_gbp is not None else None
cashflow_adjustments = None
if req.life_events:
engine_events = [
EventInput(
year_start=ev.year_start,
year_end=ev.year_end,
delta_gbp_per_year=float(ev.delta_gbp_per_year),
one_time_amount_gbp=(float(ev.one_time_amount_gbp)
if ev.one_time_amount_gbp is not None else None),
enabled=ev.enabled,
) for ev in req.life_events
]
cashflow_adjustments = events_to_cashflow_array(engine_events, req.horizon_years)
started = time.perf_counter() started = time.perf_counter()
result = simulate( result = simulate(
paths=paths, paths=paths,
@ -57,6 +73,7 @@ def _project(req: SimulateRequest) -> tuple[SimulationResult, float]:
regime=build_regime_schedule(req.jurisdiction, req.leave_uk_year), regime=build_regime_schedule(req.jurisdiction, req.leave_uk_year),
horizon_years=req.horizon_years, horizon_years=req.horizon_years,
annual_savings=annual_savings, annual_savings=annual_savings,
cashflow_adjustments=cashflow_adjustments,
) )
elapsed = time.perf_counter() - started elapsed = time.perf_counter() - started
return result, elapsed return result, elapsed

View file

@ -0,0 +1,58 @@
"""Convert life-event records into per-year cashflow adjustments.
Two event shapes the engine understands:
- **Ranged delta**: `delta_gbp_per_year != 0` applied each year in
`[year_start, year_end]` (inclusive). Use a negative delta for
expenses (childcare, sabbatical), positive for income (rental,
pension that hasn't started yet).
- **One-time amount**: `one_time_amount_gbp` applied once at
`year_start`. Inheritance, house sale proceeds, lump-sum gift.
Disabled events (`enabled=False`) are skipped. Year ranges that
extend past the simulation horizon are clipped events beyond year
H simply don't happen in this run.
"""
from __future__ import annotations
from collections.abc import Iterable
from dataclasses import dataclass
import numpy as np
import numpy.typing as npt
@dataclass(frozen=True)
class EventInput:
"""Engine-level event shape — decoupled from the SQLAlchemy ORM and
the API Pydantic schema so callers can construct them however."""
year_start: int
year_end: int | None = None
delta_gbp_per_year: float = 0.0
one_time_amount_gbp: float | None = None
enabled: bool = True
def events_to_cashflow_array(
events: Iterable[EventInput],
horizon_years: int,
) -> npt.NDArray[np.float64]:
"""Sum a list of events into a `(horizon_years,)` real-GBP array."""
out = np.zeros(horizon_years, dtype=np.float64)
for ev in events:
if not ev.enabled:
continue
start = max(0, int(ev.year_start))
if start >= horizon_years:
continue
if ev.delta_gbp_per_year:
end = ev.year_end if ev.year_end is not None else ev.year_start
end = min(int(end), horizon_years - 1)
if end >= start:
out[start:end + 1] += float(ev.delta_gbp_per_year)
if ev.one_time_amount_gbp:
out[start] += float(ev.one_time_amount_gbp)
return out

View file

@ -155,6 +155,7 @@ def simulate(
regime: TaxRegime | RegimeFn, regime: TaxRegime | RegimeFn,
horizon_years: int | None = None, horizon_years: int | None = None,
annual_savings: npt.NDArray[np.float64] | None = None, annual_savings: npt.NDArray[np.float64] | None = None,
cashflow_adjustments: npt.NDArray[np.float64] | None = None,
bucket_split: _BucketSplit = default_bucket_split, bucket_split: _BucketSplit = default_bucket_split,
) -> SimulationResult: ) -> SimulationResult:
"""Run the MC simulation. `paths` shape: (n_paths, n_years, 3). """Run the MC simulation. `paths` shape: (n_paths, n_years, 3).
@ -163,6 +164,12 @@ def simulate(
decided by the strategy. `annual_savings`, if given, is a (n_years,) decided by the strategy. `annual_savings`, if given, is a (n_years,)
real-GBP array added at the start of each year while accumulating. real-GBP array added at the start of each year while accumulating.
`cashflow_adjustments`, if given, is a (n_years,) real-GBP array of
per-year deltas applied **after** savings + return but **before**
withdrawal. Positive = inflow (e.g. inheritance, rental income),
negative = extra outflow (e.g. childcare, sabbatical). Used to plumb
`life_event` rows into the projection.
`regime` may be a single `TaxRegime` (constant for all years) or a `regime` may be a single `TaxRegime` (constant for all years) or a
callable `(year_idx) -> TaxRegime` to model jurisdiction switches callable `(year_idx) -> TaxRegime` to model jurisdiction switches
e.g. UK for years 0..N-1, then Cyprus from year N onward. e.g. UK for years 0..N-1, then Cyprus from year N onward.
@ -181,6 +188,8 @@ def simulate(
if annual_savings is None: if annual_savings is None:
annual_savings = np.zeros(n_years, dtype=np.float64) annual_savings = np.zeros(n_years, dtype=np.float64)
if cashflow_adjustments is None:
cashflow_adjustments = np.zeros(n_years, dtype=np.float64)
for y in range(n_years): for y in range(n_years):
alloc = glide(y) alloc = glide(y)
@ -192,8 +201,11 @@ def simulate(
real_bond = (1 + nominal_bond) / (1 + cpi) - 1 real_bond = (1 + nominal_bond) / (1 + cpi) - 1
port_return = alloc * real_stock + (1 - alloc) * real_bond port_return = alloc * real_stock + (1 - alloc) * real_bond
# Add savings at year start, then apply year's return. # Add savings at year start, apply year's return, then apply
# life-event cashflow adjustments. Adjustments don't compound
# this year's returns (they're treated as end-of-year events).
portfolio = (portfolio + annual_savings[y]) * (1 + port_return) portfolio = (portfolio + annual_savings[y]) * (1 + port_return)
portfolio = portfolio + cashflow_adjustments[y]
# Strategy is per-path Python — 600k iterations at 60y × 10k paths. # Strategy is per-path Python — 600k iterations at 60y × 10k paths.
# Profiled: ~3 seconds for the full Trinity / GK / VPW set. # Profiled: ~3 seconds for the full Trinity / GK / VPW set.

View file

@ -229,6 +229,13 @@ export interface SimulateRequest {
floor_gbp?: string | null; floor_gbp?: string | null;
n_paths?: number; n_paths?: number;
seed?: number; seed?: number;
life_events?: Array<{
year_start: number;
year_end?: number | null;
delta_gbp_per_year?: string;
one_time_amount_gbp?: string | null;
enabled?: boolean;
}>;
} }
export interface SimulateResult { export interface SimulateResult {

View file

@ -7,7 +7,7 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { Link, useNavigate, useParams } from 'react-router-dom'; import { Link, useNavigate, useParams } from 'react-router-dom';
import { api, type Scenario, type SimulateRequest } from '@/api/client'; import { api, lifeEventsApi, type Scenario, type SimulateRequest } from '@/api/client';
import { ApiError } from '@/api/client'; import { ApiError } from '@/api/client';
import { FanChart } from '@/components/FanChart'; import { FanChart } from '@/components/FanChart';
import { GoalsSection } from '@/components/GoalsSection'; import { GoalsSection } from '@/components/GoalsSection';
@ -53,7 +53,9 @@ export function ScenarioDetail() {
del.mutate(); del.mutate();
}; };
const onRunNow = (s: Scenario) => const onRunNow = async (s: Scenario) => {
// Pull events fresh so the run reflects whatever the user just edited.
const events = await lifeEventsApi.list(s.id);
sim.mutate({ sim.mutate({
jurisdiction: s.jurisdiction, jurisdiction: s.jurisdiction,
strategy: s.strategy, strategy: s.strategy,
@ -65,7 +67,15 @@ export function ScenarioDetail() {
horizon_years: s.horizon_years, horizon_years: s.horizon_years,
n_paths: 5000, n_paths: 5000,
seed: 42, seed: 42,
life_events: events.map((e) => ({
year_start: e.year_start,
year_end: e.year_end,
delta_gbp_per_year: e.delta_gbp_per_year,
one_time_amount_gbp: e.one_time_amount_gbp,
enabled: e.enabled,
})),
}); });
};
if (!Number.isFinite(id)) { if (!Number.isFinite(id)) {
return <p className="text-red-700">Invalid scenario id.</p>; return <p className="text-red-700">Invalid scenario id.</p>;
@ -104,9 +114,10 @@ export function ScenarioDetail() {
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<button <button
type="button" type="button"
onClick={() => onRunNow(s)} onClick={() => void onRunNow(s)}
disabled={sim.isPending} disabled={sim.isPending}
className="rounded-md border border-slate-300 bg-white text-sm px-3 py-1.5 hover:bg-slate-50 disabled:opacity-60" className="rounded-md border border-slate-300 bg-white text-sm px-3 py-1.5 hover:bg-slate-50 disabled:opacity-60"
title="Run a fresh MC including this scenario's life events"
> >
{sim.isPending ? 'Running…' : 'Run now'} {sim.isPending ? 'Running…' : 'Run now'}
</button> </button>

View file

@ -113,6 +113,42 @@ async def test_compare_runs_two_scenarios(client: AsyncClient) -> None:
assert all(len(r["yearly"]) == 20 for r in results) assert all(len(r["yearly"]) == 20 for r in results)
async def test_simulate_with_life_events_changes_outcome(client: AsyncClient) -> None:
"""Same params with vs without a £500k inheritance at year 5 — the
inheritance run must end with strictly more median NW."""
base_req = {
"jurisdiction": "uk",
"strategy": "trinity",
"leave_uk_year": 0,
"glide_path": "static_60_40",
"spending_gbp": "60000",
"nw_seed_gbp": "1500000",
"horizon_years": 30,
"n_paths": 200,
"seed": 42,
}
base = await client.post("/simulate", json=base_req)
assert base.status_code == 200, base.text
enhanced = await client.post(
"/simulate",
json={
**base_req,
"life_events": [
{
"year_start": 5,
"one_time_amount_gbp": "500000",
}
],
},
)
assert enhanced.status_code == 200, enhanced.text
base_p50 = float(base.json()["p50_ending_gbp"])
enhanced_p50 = float(enhanced.json()["p50_ending_gbp"])
assert enhanced_p50 > base_p50
async def test_compare_rejects_single_scenario(client: AsyncClient) -> None: async def test_compare_rejects_single_scenario(client: AsyncClient) -> None:
resp = await client.post( resp = await client.post(
"/compare", "/compare",

107
tests/test_life_events.py Normal file
View file

@ -0,0 +1,107 @@
"""Tests for the life-events → cashflow-array helper."""
from __future__ import annotations
import numpy as np
import pytest
from fire_planner.life_events import EventInput, events_to_cashflow_array
def test_empty_events_yield_zero_array() -> None:
arr = events_to_cashflow_array([], horizon_years=5)
np.testing.assert_array_equal(arr, np.zeros(5))
def test_one_time_event_lands_at_year_start() -> None:
arr = events_to_cashflow_array(
[EventInput(year_start=3, one_time_amount_gbp=250_000)],
horizon_years=10,
)
expected = np.zeros(10)
expected[3] = 250_000
np.testing.assert_array_equal(arr, expected)
def test_ranged_delta_applied_inclusive() -> None:
arr = events_to_cashflow_array(
[EventInput(year_start=2, year_end=5, delta_gbp_per_year=-10_000)],
horizon_years=10,
)
expected = np.zeros(10)
expected[2:6] = -10_000 # 2,3,4,5 inclusive
np.testing.assert_array_equal(arr, expected)
def test_year_end_none_is_one_time() -> None:
"""Ranged events default year_end == year_start."""
arr = events_to_cashflow_array(
[EventInput(year_start=4, year_end=None, delta_gbp_per_year=-5_000)],
horizon_years=10,
)
expected = np.zeros(10)
expected[4] = -5_000
np.testing.assert_array_equal(arr, expected)
def test_disabled_events_skipped() -> None:
arr = events_to_cashflow_array(
[
EventInput(year_start=0, one_time_amount_gbp=1_000_000, enabled=False),
EventInput(year_start=1, delta_gbp_per_year=-50_000, year_end=3, enabled=False),
],
horizon_years=5,
)
np.testing.assert_array_equal(arr, np.zeros(5))
def test_events_past_horizon_clipped() -> None:
"""Events starting at or beyond the horizon don't apply at all;
ranged events that overlap the horizon get clipped to the last year."""
arr = events_to_cashflow_array(
[
EventInput(year_start=10, one_time_amount_gbp=100_000),
EventInput(year_start=8, year_end=15, delta_gbp_per_year=-5_000),
],
horizon_years=10,
)
# First event: year 10 is outside (horizon 0..9), so nothing.
# Second event: clipped to years 8..9.
expected = np.zeros(10)
expected[8] = -5_000
expected[9] = -5_000
np.testing.assert_array_equal(arr, expected)
def test_multiple_events_sum() -> None:
arr = events_to_cashflow_array(
[
EventInput(year_start=0, year_end=4, delta_gbp_per_year=-12_000),
EventInput(year_start=2, one_time_amount_gbp=50_000),
EventInput(year_start=3, delta_gbp_per_year=20_000, year_end=10),
],
horizon_years=10,
)
expected = np.zeros(10)
expected[0:5] += -12_000 # event 1: years 0..4
expected[2] += 50_000 # event 2: year 2 lump sum
expected[3:10] += 20_000 # event 3: years 3..9 (clipped from 3..10)
np.testing.assert_array_equal(arr, expected)
def test_negative_year_start_clipped_to_zero() -> None:
arr = events_to_cashflow_array(
[EventInput(year_start=-2, year_end=2, delta_gbp_per_year=-1_000)],
horizon_years=5,
)
expected = np.zeros(5)
expected[0:3] = -1_000 # 0,1,2
np.testing.assert_array_equal(arr, expected)
@pytest.mark.parametrize("amount", [0, 0.0, None])
def test_zero_or_none_one_time_amount_skipped(amount: float | None) -> None:
arr = events_to_cashflow_array(
[EventInput(year_start=2, one_time_amount_gbp=amount)],
horizon_years=5,
)
np.testing.assert_array_equal(arr, np.zeros(5))

View file

@ -0,0 +1,73 @@
"""End-to-end: cashflow_adjustments make portfolios bigger or smaller."""
from __future__ import annotations
import numpy as np
from fire_planner.glide_path import static
from fire_planner.life_events import EventInput, events_to_cashflow_array
from fire_planner.simulator import simulate
from fire_planner.strategies.trinity import TrinityStrategy
from fire_planner.tax.malaysia import MalaysiaTaxRegime
from tests.test_simulator import fixed_paths
def _baseline_kwargs() -> dict[str, object]:
"""0% real returns, 25y, Trinity 4%, no taxes (Malaysia) — predictable."""
paths = fixed_paths(n_paths=1, n_years=25, stock_ret=0.0, bond_ret=0.0, cpi=0.0)
return dict(
paths=paths,
initial_portfolio=1_000_000.0,
spending_target=40_000.0,
glide=static(0.6),
strategy=TrinityStrategy(initial_rate=0.04),
regime=MalaysiaTaxRegime(),
)
def test_no_adjustments_matches_baseline() -> None:
base = simulate(**_baseline_kwargs()) # type: ignore[arg-type]
with_zero = simulate(**_baseline_kwargs(), cashflow_adjustments=np.zeros(25)) # type: ignore[arg-type]
np.testing.assert_allclose(base.portfolio_real, with_zero.portfolio_real)
def test_one_time_inheritance_lifts_portfolio() -> None:
kwargs = _baseline_kwargs()
adj = events_to_cashflow_array(
[EventInput(year_start=10, one_time_amount_gbp=250_000)],
horizon_years=25,
)
base = simulate(**kwargs) # type: ignore[arg-type]
enhanced = simulate(**kwargs, cashflow_adjustments=adj) # type: ignore[arg-type]
# Year 11 onward should be exactly £250k higher under 0% returns +
# constant Trinity withdrawal.
delta = enhanced.portfolio_real[0, 11:] - base.portfolio_real[0, 11:]
assert np.all(delta > 0)
# Year 11 specifically: +£250k landed at end of year 10, withdrawn
# nothing extra in y10. By y11 just propagated forward.
assert enhanced.portfolio_real[0, 11] - base.portfolio_real[0, 11] == 250_000
def test_ongoing_expense_drains_portfolio() -> None:
kwargs = _baseline_kwargs()
adj = events_to_cashflow_array(
[EventInput(year_start=0, year_end=5, delta_gbp_per_year=-20_000)],
horizon_years=25,
)
base = simulate(**kwargs) # type: ignore[arg-type]
drained = simulate(**kwargs, cashflow_adjustments=adj) # type: ignore[arg-type]
# 6 years × £20k expense = £120k less by end of year 6, 0% growth.
delta = base.portfolio_real[0, 6] - drained.portfolio_real[0, 6]
assert delta == 120_000
def test_event_can_force_failure() -> None:
"""A massive expense early on can ruin an otherwise-successful run."""
kwargs = _baseline_kwargs()
adj = events_to_cashflow_array(
[EventInput(year_start=2, one_time_amount_gbp=-1_500_000)],
horizon_years=25,
)
base = simulate(**kwargs) # type: ignore[arg-type]
ruined = simulate(**kwargs, cashflow_adjustments=adj) # type: ignore[arg-type]
assert base.success_rate == 1.0
assert ruined.success_rate == 0.0