strategies: spending input is honoured + new "Custom" preset with guardrails
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

The user noticed the "Annual spending" field was a no-op for Trinity,
GK, VPW, VPW+floor — the strategies internally hardcoded the year-0
withdrawal as `initial_portfolio × initial_rate` (4% / 5.5%) and
ignored what the user typed. Two fixes:

(1) Trinity + GK now use state.initial_withdrawal (= the user's
    spending_target) as the year-0 draw. GK's guardrail anchor
    becomes the implied initial rate (initial_withdrawal /
    initial_portfolio), so the rule shape adapts to the user's
    chosen rate. Both strategies still fall back to their preset
    rate × initial_portfolio when initial_withdrawal isn't set
    (test paths). VPW and VPW+floor stay algorithmic — they're
    "withdraw-what's-sustainable" by design and don't take a
    spending input.

(2) New "custom" preset (SpendingPlanStrategy) exposing all the
    knobs:
    - initial_spend = "Annual spending" input
    - annual_real_adjust_pct = scale last year's withdrawal by N%
      each year (0 = constant real £, +0.02 = 2%/yr healthcare
      creep, -0.005 = -0.5%/yr slow-down with age)
    - guardrail_threshold_pct = if portfolio falls below X% of
      starting NW, trigger a cut (None = disabled)
    - guardrail_cut_pct = cut last year's withdrawal by Y% each
      triggered year

Adjust applies first, then guardrail cut — so a triggered year in
+2% adjust mode goes 40k → 40.8k → 36.7k.

UI: "custom" added to the strategy dropdown; when selected, three
extra fields appear (annual real adjustment %, guardrail trigger
threshold, guardrail cut size) with hints. The existing inputs
(spending, NW seed) drive year 0 across all strategies that use
them. About-the-model panel updated.

10 new tests on SpendingPlanStrategy + adjusted GK tests for the
new spending_target-aware behaviour. 209 backend tests + 7
frontend tests. mypy + ruff + tsc all pass.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Viktor Barzin 2026-05-10 01:21:55 +00:00
parent 00ec874889
commit f43322e5ce
10 changed files with 300 additions and 21 deletions

View file

@ -0,0 +1,87 @@
"""SpendingPlanStrategy: user-customisable initial spend + annual adjust + guardrail."""
from fire_planner.strategies.base import StrategyState
from fire_planner.strategies.spending_plan import SpendingPlanStrategy
def state(**overrides: float | int) -> StrategyState:
base = dict(
portfolio=1_000_000.0,
initial_portfolio=1_000_000.0,
initial_withdrawal=40_000.0,
year_idx=0,
horizon_years=60,
last_withdrawal=40_000.0,
expected_real_return=0.04,
)
base.update(overrides)
return StrategyState(**base) # type: ignore[arg-type]
def test_year_zero_takes_initial_withdrawal() -> None:
s = SpendingPlanStrategy()
assert s.propose_withdrawal(state()) == 40_000.0
def test_year_zero_explicit_override() -> None:
s = SpendingPlanStrategy(initial_spend=72_000.0)
assert s.propose_withdrawal(state()) == 72_000.0
def test_constant_real_with_zero_adjust() -> None:
s = SpendingPlanStrategy(annual_real_adjust_pct=0.0)
assert s.propose_withdrawal(state(year_idx=10, last_withdrawal=40_000.0)) == 40_000.0
def test_positive_annual_adjust_grows_spending() -> None:
"""+2% real adjust → year 5 spend is last_w × 1.02 (one step from year 4)."""
s = SpendingPlanStrategy(annual_real_adjust_pct=0.02)
out = s.propose_withdrawal(state(year_idx=5, last_withdrawal=40_000.0))
assert abs(out - 40_800.0) < 1e-6
def test_negative_annual_adjust_shrinks_spending() -> None:
s = SpendingPlanStrategy(annual_real_adjust_pct=-0.005)
out = s.propose_withdrawal(state(year_idx=10, last_withdrawal=40_000.0))
assert abs(out - 39_800.0) < 1e-6
def test_guardrail_not_triggered_when_portfolio_above_threshold() -> None:
s = SpendingPlanStrategy(guardrail_threshold_pct=0.80, guardrail_cut_pct=0.10)
# portfolio at 90% of starting → above threshold → no cut
out = s.propose_withdrawal(state(year_idx=3, portfolio=900_000.0,
last_withdrawal=40_000.0))
assert out == 40_000.0
def test_guardrail_triggered_cuts_by_pct() -> None:
s = SpendingPlanStrategy(guardrail_threshold_pct=0.80, guardrail_cut_pct=0.10)
# portfolio at 70% of starting → below threshold → 10% cut
out = s.propose_withdrawal(state(year_idx=3, portfolio=700_000.0,
last_withdrawal=40_000.0))
assert abs(out - 36_000.0) < 1e-6
def test_guardrail_combines_with_annual_adjust() -> None:
"""Adjust applies first, then cut — both fire."""
s = SpendingPlanStrategy(
annual_real_adjust_pct=0.02,
guardrail_threshold_pct=0.80,
guardrail_cut_pct=0.10,
)
out = s.propose_withdrawal(state(year_idx=3, portfolio=700_000.0,
last_withdrawal=40_000.0))
# 40_000 * 1.02 = 40_800; trigger; 40_800 * 0.90 = 36_720
assert abs(out - 36_720.0) < 1e-6
def test_guardrail_disabled_when_threshold_none() -> None:
s = SpendingPlanStrategy(guardrail_threshold_pct=None)
out = s.propose_withdrawal(state(year_idx=3, portfolio=10_000.0,
last_withdrawal=40_000.0))
assert out == 40_000.0 # no cut despite tiny portfolio
def test_returns_zero_when_portfolio_drained() -> None:
s = SpendingPlanStrategy()
out = s.propose_withdrawal(state(year_idx=5, portfolio=0.0, last_withdrawal=40_000.0))
assert out == 0.0

View file

@ -36,17 +36,30 @@ def test_trinity_doesnt_increase_with_portfolio_growth() -> None:
last_withdrawal=40_000.0)) == 40_000.0
def test_gk_year_zero_uses_initial_rate() -> None:
def test_gk_year_zero_uses_initial_withdrawal() -> None:
"""Year-0 honours the user's target spending (state.initial_withdrawal),
not the strategy's preset rate. The preset rate now only matters as a
fallback when initial_withdrawal isn't set."""
s = GuytonKlingerStrategy(initial_rate=0.055)
# 5.5% of 1M = 55,000
assert s.propose_withdrawal(state()) == 55_000.0
# state default has initial_withdrawal=40_000 → year 0 returns 40_000.
assert s.propose_withdrawal(state()) == 40_000.0
def test_gk_year_zero_falls_back_to_preset_when_no_target() -> None:
s = GuytonKlingerStrategy(initial_rate=0.055)
# Override initial_withdrawal=0 → fall back to 5.5% × 1M = 55_000.
assert s.propose_withdrawal(state(initial_withdrawal=0)) == 55_000.0
def test_gk_capital_preservation_cut() -> None:
"""Portfolio crashed: current rate now > 120% of 5.5% = 6.6%; > 15y left → cut 10%."""
"""Portfolio crashed: current rate now > 120% of the implied initial rate (5.5%);
> 15y left cut 10%. Implied rate = initial_withdrawal / initial_portfolio."""
s = GuytonKlingerStrategy(initial_rate=0.055)
# last_w = 55,000; portfolio = 700,000 → rate = 7.86% > 6.6%
out = s.propose_withdrawal(state(year_idx=5, portfolio=700_000.0, last_withdrawal=55_000.0))
# initial_withdrawal=55k, initial_portfolio=1M → implied rate = 5.5%.
# last_w = 55k; portfolio = 700k → current rate = 7.86% > 6.6% guardrail.
out = s.propose_withdrawal(state(year_idx=5, portfolio=700_000.0,
last_withdrawal=55_000.0,
initial_withdrawal=55_000.0))
assert abs(out - 49_500.0) < 0.01