strategies: spending input is honoured + new "Custom" preset with guardrails
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
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:
parent
00ec874889
commit
f43322e5ce
10 changed files with 300 additions and 21 deletions
|
|
@ -238,6 +238,16 @@ class SimulateRequest(BaseModel):
|
|||
# recent regime only (~6 years). Glide path is moot.
|
||||
returns_mode: str = Field(default="shiller", pattern="^(shiller|manual|wealthfolio)$")
|
||||
manual_real_return_pct: Decimal | None = None
|
||||
# Custom spending-plan parameters — only consulted when strategy="custom".
|
||||
# All real-£ / real-fraction. annual_real_adjust_pct = 0 means constant
|
||||
# real spending (Trinity-shape). Non-zero scales last year's withdrawal
|
||||
# multiplicatively each year (e.g. -0.005 for slow-down with age,
|
||||
# +0.02 for healthcare creep). Guardrail cuts spending by
|
||||
# `guardrail_cut_pct` whenever the portfolio falls below
|
||||
# `guardrail_threshold_pct` of its starting value; null disables.
|
||||
annual_real_adjust_pct: Decimal = Decimal("0")
|
||||
guardrail_threshold_pct: Decimal | None = None
|
||||
guardrail_cut_pct: Decimal = Decimal("0.10")
|
||||
|
||||
|
||||
class SimulateResult(BaseModel):
|
||||
|
|
|
|||
|
|
@ -105,13 +105,22 @@ def _project(req: SimulateRequest, paths: np.ndarray) -> tuple[SimulationResult,
|
|||
]
|
||||
cashflow_adjustments = events_to_cashflow_array(engine_events, req.horizon_years)
|
||||
|
||||
strategy = build_strategy(
|
||||
req.strategy,
|
||||
floor=floor,
|
||||
annual_real_adjust_pct=float(req.annual_real_adjust_pct),
|
||||
guardrail_threshold_pct=(float(req.guardrail_threshold_pct)
|
||||
if req.guardrail_threshold_pct is not None else None),
|
||||
guardrail_cut_pct=float(req.guardrail_cut_pct),
|
||||
)
|
||||
|
||||
started = time.perf_counter()
|
||||
result = simulate(
|
||||
paths=paths,
|
||||
initial_portfolio=float(req.nw_seed_gbp),
|
||||
spending_target=float(req.spending_gbp),
|
||||
glide=get_glide(req.glide_path),
|
||||
strategy=build_strategy(req.strategy, floor=floor),
|
||||
strategy=strategy,
|
||||
regime=build_regime_schedule(req.jurisdiction, req.leave_uk_year),
|
||||
horizon_years=req.horizon_years,
|
||||
annual_savings=annual_savings,
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ from fire_planner.glide_path import GLIDE_PATHS
|
|||
from fire_planner.simulator import RegimeFn, constant_regime, jurisdiction_schedule
|
||||
from fire_planner.strategies.base import WithdrawalStrategy
|
||||
from fire_planner.strategies.guyton_klinger import GuytonKlingerStrategy
|
||||
from fire_planner.strategies.spending_plan import SpendingPlanStrategy
|
||||
from fire_planner.strategies.trinity import TrinityStrategy
|
||||
from fire_planner.strategies.vpw import VpwStrategy, VpwWithFloorStrategy
|
||||
from fire_planner.tax.base import TaxRegime
|
||||
|
|
@ -58,7 +59,13 @@ class ScenarioSpec:
|
|||
f"glide-{self.glide_path}")
|
||||
|
||||
|
||||
def build_strategy(name: str, floor: float | None = None) -> WithdrawalStrategy:
|
||||
def build_strategy(
|
||||
name: str,
|
||||
floor: float | None = None,
|
||||
annual_real_adjust_pct: float = 0.0,
|
||||
guardrail_threshold_pct: float | None = None,
|
||||
guardrail_cut_pct: float = 0.10,
|
||||
) -> WithdrawalStrategy:
|
||||
if name == "trinity":
|
||||
return TrinityStrategy()
|
||||
if name == "guyton_klinger":
|
||||
|
|
@ -69,6 +76,12 @@ def build_strategy(name: str, floor: float | None = None) -> WithdrawalStrategy:
|
|||
if floor is None:
|
||||
raise ValueError("vpw_floor strategy requires a `floor` value (real GBP)")
|
||||
return VpwWithFloorStrategy(floor=floor)
|
||||
if name == "custom":
|
||||
return SpendingPlanStrategy(
|
||||
annual_real_adjust_pct=annual_real_adjust_pct,
|
||||
guardrail_threshold_pct=guardrail_threshold_pct,
|
||||
guardrail_cut_pct=guardrail_cut_pct,
|
||||
)
|
||||
raise KeyError(f"Unknown strategy: {name!r}")
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -41,17 +41,26 @@ class GuytonKlingerStrategy(WithdrawalStrategy):
|
|||
self.initial_rate = initial_rate
|
||||
|
||||
def propose_withdrawal(self, state: StrategyState) -> float:
|
||||
# Year 0 = the user's target spending; the implied initial rate
|
||||
# (initial_withdrawal / initial_portfolio) becomes the anchor
|
||||
# the guardrails compare against. Falls back to the preset rate
|
||||
# × initial_portfolio when no target was given.
|
||||
target_initial = (state.initial_withdrawal
|
||||
if state.initial_withdrawal > 0 else
|
||||
state.initial_portfolio * self.initial_rate)
|
||||
if state.year_idx == 0:
|
||||
return state.initial_portfolio * self.initial_rate
|
||||
return target_initial
|
||||
if state.portfolio <= 0:
|
||||
return 0.0
|
||||
implied_initial_rate = (target_initial / state.initial_portfolio
|
||||
if state.initial_portfolio > 0 else self.initial_rate)
|
||||
last_w = state.last_withdrawal
|
||||
current_rate = last_w / state.portfolio
|
||||
years_left = state.horizon_years - state.year_idx
|
||||
# Capital-preservation cut: only if more than 15 years remain.
|
||||
if (current_rate > self.initial_rate * CAPITAL_PRESERVATION_RATIO
|
||||
if (current_rate > implied_initial_rate * CAPITAL_PRESERVATION_RATIO
|
||||
and years_left > MIN_HORIZON_FOR_CUT):
|
||||
return last_w * (1 - ADJUSTMENT)
|
||||
if current_rate < self.initial_rate * PROSPERITY_RATIO:
|
||||
if current_rate < implied_initial_rate * PROSPERITY_RATIO:
|
||||
return last_w * (1 + ADJUSTMENT)
|
||||
return last_w
|
||||
|
|
|
|||
62
fire_planner/strategies/spending_plan.py
Normal file
62
fire_planner/strategies/spending_plan.py
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
"""Custom user-defined spending plan.
|
||||
|
||||
A flexible strategy where the user chooses every knob:
|
||||
|
||||
- `initial_spend` — year 0 withdrawal in real GBP (taken from
|
||||
`state.initial_withdrawal` if not overridden).
|
||||
- `annual_real_adjust_pct` — fraction by which last year's withdrawal
|
||||
scales each subsequent year, on top of inflation. 0.0 = constant
|
||||
real GBP (Trinity-shape). +0.02 = 2%/yr above-inflation creep
|
||||
(e.g. healthcare). -0.005 = -0.5%/yr decreasing spend (slowing
|
||||
down with age).
|
||||
- `guardrail_threshold_pct` — if portfolio drops below this fraction
|
||||
of the starting NW, apply a cut. None = no guardrail.
|
||||
- `guardrail_cut_pct` — fraction by which to cut last year's
|
||||
withdrawal when triggered. Applied multiplicatively each triggered
|
||||
year — not "snap to threshold-implied rate", just a soft cut.
|
||||
|
||||
The cut is checked AFTER the annual adjustment, so a cut + an
|
||||
increase don't double-apply: cut wins.
|
||||
|
||||
Compared to Guyton-Klinger this is simpler — one threshold, one
|
||||
cut size, no prosperity rule. If the user wants the prosperity rule
|
||||
behaviour they can pick the GK preset.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from fire_planner.strategies.base import StrategyState, WithdrawalStrategy
|
||||
|
||||
|
||||
class SpendingPlanStrategy(WithdrawalStrategy):
|
||||
name = "custom"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
initial_spend: float | None = None,
|
||||
annual_real_adjust_pct: float = 0.0,
|
||||
guardrail_threshold_pct: float | None = None,
|
||||
guardrail_cut_pct: float = 0.10,
|
||||
) -> None:
|
||||
self.initial_spend = initial_spend
|
||||
self.annual_real_adjust_pct = annual_real_adjust_pct
|
||||
self.guardrail_threshold_pct = guardrail_threshold_pct
|
||||
self.guardrail_cut_pct = guardrail_cut_pct
|
||||
|
||||
def propose_withdrawal(self, state: StrategyState) -> float:
|
||||
if state.year_idx == 0:
|
||||
# Explicit override wins; otherwise take the user's target.
|
||||
return (self.initial_spend
|
||||
if self.initial_spend is not None and self.initial_spend > 0 else
|
||||
state.initial_withdrawal)
|
||||
if state.portfolio <= 0:
|
||||
return 0.0
|
||||
|
||||
proposed = state.last_withdrawal * (1.0 + self.annual_real_adjust_pct)
|
||||
|
||||
if (self.guardrail_threshold_pct is not None
|
||||
and state.initial_portfolio > 0):
|
||||
trigger_at = state.initial_portfolio * self.guardrail_threshold_pct
|
||||
if state.portfolio < trigger_at:
|
||||
proposed = proposed * (1.0 - self.guardrail_cut_pct)
|
||||
|
||||
return max(0.0, proposed)
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
"""Trinity 4% Safe Withdrawal Rate.
|
||||
"""Constant-real-£ withdrawal (the classic 4% rule shape).
|
||||
|
||||
Bengen's seminal 1994 paper + the Trinity Study (Cooley/Hubbard/Walz,
|
||||
1998) — withdraw 4% of the starting balance in year 1, then keep the
|
||||
real withdrawal constant for the rest of retirement. In our real-GBP
|
||||
internal frame this is just "the same number every year".
|
||||
Withdraw `state.initial_withdrawal` in year 0, then keep that real-£
|
||||
amount fixed for the rest of retirement. In a 4% / £1M setup the year-0
|
||||
draw is £40k, then £40k real every year after. The strategy's
|
||||
`initial_rate` is kept only as a fallback for callers that don't feed
|
||||
`state.initial_withdrawal`.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
|
|
@ -20,5 +21,10 @@ class TrinityStrategy(WithdrawalStrategy):
|
|||
|
||||
def propose_withdrawal(self, state: StrategyState) -> float:
|
||||
if state.year_idx == 0:
|
||||
# Year 0 = the user's target spending. Falls back to
|
||||
# initial_rate × initial_portfolio if no target was provided
|
||||
# (zero or missing) for backwards compatibility.
|
||||
if state.initial_withdrawal > 0:
|
||||
return state.initial_withdrawal
|
||||
return state.initial_portfolio * self.initial_rate
|
||||
return state.last_withdrawal
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue