engine+api: plumb life events into the simulator
Some checks failed
ci/woodpecker/push/woodpecker Pipeline was canceled
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:
parent
b82770b5c4
commit
2fc92c12f5
9 changed files with 335 additions and 4 deletions
|
|
@ -155,6 +155,7 @@ def simulate(
|
|||
regime: TaxRegime | RegimeFn,
|
||||
horizon_years: int | None = None,
|
||||
annual_savings: npt.NDArray[np.float64] | None = None,
|
||||
cashflow_adjustments: npt.NDArray[np.float64] | None = None,
|
||||
bucket_split: _BucketSplit = default_bucket_split,
|
||||
) -> SimulationResult:
|
||||
"""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,)
|
||||
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
|
||||
callable `(year_idx) -> TaxRegime` to model jurisdiction switches —
|
||||
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:
|
||||
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):
|
||||
alloc = glide(y)
|
||||
|
|
@ -192,8 +201,11 @@ def simulate(
|
|||
real_bond = (1 + nominal_bond) / (1 + cpi) - 1
|
||||
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 + cashflow_adjustments[y]
|
||||
|
||||
# Strategy is per-path Python — 600k iterations at 60y × 10k paths.
|
||||
# Profiled: ~3 seconds for the full Trinity / GK / VPW set.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue