fire-planner/tests/test_simulator_fixed_rates.py
Viktor Barzin 9cc781a8d6
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
fire-planner: ProjectionLab parity Wave 1 — tabbed shell, year stats, goals,
income streams, Sankey cashflow, progress overlay, settings sub-pages

Wave 1 (9 features across 4 streams):

Stream A — dashboard skeleton
  1.A.1 ScenarioShell with top tabs (Plan/Cash Flow/Tax Analytics/Compare/
        Reports/Estate/Settings) + left Sidebar with Plans switcher.
  1.A.2 GET /scenarios/{id}/year-stats?year=N returning per-year metrics
        (NW, Δ NW, taxable income, taxes, eff. rate, spending, contribs,
        investment growth). YearScrubber + YearStatsPanel render the
        right-hand sidebar; URL ?year= preserves selection.
  1.A.3 FanChart gains optional `milestones` prop (lib/milestone.ts maps
        life_event.kind → emoji) + selectedYear marker line.

Stream B — goals + progress
  1.B.1 New goals_eval module: target_nw_by_year / never_run_out /
        target_real_income probability evaluation. Wired into POST
        /simulate (exact, per-path) and GET /scenarios/{id}/projection
        (approximated from persisted fan via percentile interpolation).
        GoalsSection renders pass/fail badges.
  1.B.2 GET /scenarios/{id}/progress overlays AccountSnapshot totals on
        the projection fan; ProgressPage shows variance side-panel.

Stream C — income + cashflow
  1.C.1 New IncomeStream model + alembic 0003 + CRUD endpoints. Engine
        aggregates streams into per-year inflows + taxable arrays;
        income tax routes through the jurisdiction tax engine.
        IncomeStreamsSection on Plan tab.
  1.C.2 GET /scenarios/{id}/cashflow?year=N returns sources/sinks for
        an ECharts Sankey (sums conserve). CashflowTab body.

Stream D — settings
  1.D.1 SettingsTab + sub-nav (Milestones/Rates/Dividends/Bonds/Tax/
        Metrics/Other/Notes); placeholder cards for unbuilt sub-pages.
  1.D.2 LifeEventsSection relocated to /scenarios/:id/settings.
  1.D.3 RatesSettings (Fixed/Historical/Advanced segmented + per-asset
        cards). SimulateRequest gains rates_mode, inflation_pct,
        stocks/bonds growth + dividend, stocks_allocation. New
        build_fixed_paths() in simulator. Real-return arithmetic
        verified against (1+g+d)/(1+i)−1 ≈ 5.4%.
  1.D.4 NotesSettings — markdown textarea, save-on-blur, stored in
        scenario.config_json.notes.

Backend: 238 pytest pass (+19 new), mypy + ruff clean.
Frontend: typecheck + 7 unit tests + production build clean.

Roadmap for Wave 2-N is documented in the implementation plan.
2026-05-10 12:49:44 +00:00

56 lines
2.1 KiB
Python

"""Verify the Settings → Rates fixed-mode arithmetic.
For 100% stocks with growth=6% and dividend=2.5%, inflation=3%, the
expected real return per year is ``(1 + 0.06 + 0.025) / 1.03 - 1``
≈ 0.0534. We assert the simulator's portfolio compounds at this rate
in the absence of withdrawals (spending=0, no strategy draw).
"""
from __future__ import annotations
from decimal import Decimal
import numpy as np
import pytest
from fire_planner.api.schemas import SimulateRequest
from fire_planner.api.simulate import _build_paths, _project
@pytest.mark.asyncio
async def test_fixed_rates_real_return_arithmetic() -> None:
req = SimulateRequest(
jurisdiction="uae", # 0% tax to isolate compounding
strategy="trinity",
leave_uk_year=0,
spending_gbp=Decimal("1"),
nw_seed_gbp=Decimal("100000"),
horizon_years=30,
n_paths=100,
rates_mode="fixed",
inflation_pct=Decimal("0.03"),
stocks_growth_pct=Decimal("0.06"),
stocks_dividend_pct=Decimal("0.025"),
bonds_growth_pct=Decimal("0.015"),
bonds_dividend_pct=Decimal("0.035"),
stocks_allocation=Decimal("1"),
)
paths = await _build_paths(req)
assert paths.shape == (100, 30, 3)
# nominal stock return embedded should be growth + dividend = 0.085
assert paths[0, 0, 0] == pytest.approx(0.085)
assert paths[0, 0, 1] == pytest.approx(0.05)
assert paths[0, 0, 2] == pytest.approx(0.03)
expected_real = (1 + 0.06 + 0.025) / (1 + 0.03) - 1
assert expected_real == pytest.approx(0.0534, abs=1e-3)
result, _ = _project(req, paths)
# All paths identical → median == any single path. After 30 years of
# compounding 0.0534 with the trinity 4% draw, ending NW lies in a
# well-defined window.
end_real = float(np.median(result.portfolio_real[:, -1]))
assert end_real > 100_000 # grew despite the £1/y withdrawal
growth_factor = end_real / 100_000.0
expected_factor = (1 + expected_real)**30
# Loose because trinity strategy still draws something each year.
assert growth_factor == pytest.approx(expected_factor, rel=0.05)