Skip to content

A local-first personal finance app

:chart_with_upwards_trend: Monte Carlo - Fix floor and ceiling/boundaries anchors (#8861)

Michael Clark9d63d6f24f18
diff --git a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.test.ts b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.test.ts
index fe6b658bc8..77238872b9 100644
--- a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.test.ts
+++ b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.test.ts
@@ -622,6 +622,121 @@ describe('runMonteCarloSimulation', () => {
expect(withRule.medianTotalWithdrawn).toBeLessThanOrEqual(100_000);
});
+ it('floor-ceiling anchors its rate in the first spending year', () => {
+ // User-reported bug: with a zero-spend phase before retirement, the
+ // rule anchored to year 1's planned spending of 0, so every
+ // retirement year withdrew the floor (planned x 0.85). The anchor
+ // must instead be set where spending starts.
+ const result = runMonteCarloSimulation(
+ makeParams(
+ {
+ annualWithdrawal: 0,
+ horizonYears: 30,
+ currentAge: 55,
+ spendingPhases: [
+ { id: 'working', name: '', fromAge: null, annualWithdrawal: 0 },
+ {
+ id: 'retired',
+ name: '',
+ fromAge: 65,
+ annualWithdrawal: 60_000,
+ },
+ ],
+ withdrawalRule: {
+ ...WITHDRAWAL_RULE_DEFAULTS,
+ type: 'floor-ceiling',
+ },
+ captureRunDetail: 0,
+ },
+ {
+ startingBalance: 1_000_000,
+ expectedReturnMean: 0.05,
+ returnStdDev: 0,
+ },
+ ),
+ );
+
+ const rows = result.runDetail!;
+ // Accumulation years take nothing
+ for (const row of rows.slice(0, 10)) {
+ expect(row.withdrawal).toBe(0);
+ }
+ // The first retirement year withdraws exactly the planned amount
+ expect(rows[10].withdrawal).toBe(60_000);
+ // With steady growth the rate rule rises above the plan - it must
+ // not sit pinned at the 51,000 floor
+ expect(rows[11].withdrawal).toBeGreaterThan(60_000);
+ for (const row of rows.slice(11)) {
+ expect(row.withdrawal).toBeGreaterThanOrEqual(51_000);
+ expect(row.withdrawal).toBeLessThanOrEqual(72_000);
+ }
+ });
+
+ it('boundaries does not compound raises through zero-spend years', () => {
+ // A zero-spend year has a withdrawal rate of 0 - below any lower
+ // threshold - which must not be read as a "raise spending" signal
+ const result = runMonteCarloSimulation(
+ makeParams(
+ {
+ annualWithdrawal: 0,
+ horizonYears: 12,
+ currentAge: 55,
+ spendingPhases: [
+ { id: 'working', name: '', fromAge: null, annualWithdrawal: 0 },
+ {
+ id: 'retired',
+ name: '',
+ fromAge: 65,
+ annualWithdrawal: 40_000,
+ },
+ ],
+ withdrawalRule: { ...WITHDRAWAL_RULE_DEFAULTS, type: 'boundaries' },
+ captureRunDetail: 0,
+ },
+ {
+ startingBalance: 1_000_000,
+ expectedReturnMean: 0,
+ returnStdDev: 0,
+ },
+ ),
+ );
+
+ expect(result.runDetail![10].withdrawal).toBe(40_000);
+ });
+
+ it('ratcheting does not ratchet before spending starts', () => {
+ // Strong accumulation growth blows past the balance threshold long
+ // before retirement; the streak must not start counting until
+ // spending does
+ const result = runMonteCarloSimulation(
+ makeParams(
+ {
+ annualWithdrawal: 0,
+ horizonYears: 10,
+ currentAge: 60,
+ spendingPhases: [
+ { id: 'working', name: '', fromAge: null, annualWithdrawal: 0 },
+ {
+ id: 'retired',
+ name: '',
+ fromAge: 65,
+ annualWithdrawal: 10_000,
+ },
+ ],
+ withdrawalRule: { ...WITHDRAWAL_RULE_DEFAULTS, type: 'ratcheting' },
+ captureRunDetail: 0,
+ },
+ {
+ startingBalance: 100_000,
+ expectedReturnMean: 0.2,
+ returnStdDev: 0,
+ },
+ ),
+ );
+
+ expect(result.runDetail![5].withdrawal).toBe(10_000);
+ });
+
it('floor-ceiling scales withdrawals with the pot within limits', () => {
const base = makeParams(
{ annualWithdrawal: 10_000, horizonYears: 30 },
diff --git a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.ts b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.ts
index fe30561672..67d224c447 100644
--- a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.ts
+++ b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloSimulation.ts
@@ -897,6 +897,17 @@ export function runMonteCarloSimulation(
plannedTodayByYear[year] = amount;
}
+ // The first year with planned spending - withdrawal rules anchor here
+ // and only adjust from here on, so zero-spend years (accumulation
+ // before retirement) neither trigger nor drift them
+ let firstSpendingYear = Infinity;
+ for (let year = 1; year <= horizonYears; year++) {
+ if (plannedTodayByYear[year] > 0) {
+ firstSpendingYear = year;
+ break;
+ }
+ }
+
// Planned contributions per pot per year, in today's money - split into
// an inflation-adjusted and a flat portion so a year's deposit is
// flat + adjusted × cumulativeInflation. Deterministic and shared by
@@ -1001,10 +1012,6 @@ export function runMonteCarloSimulation(
accessibleStartByYear[year] = accessibleStart;
}
- const initialRate =
- accessibleStartByYear[1] > 0
- ? plannedTodayByYear[1] / accessibleStartByYear[1]
- : 0;
const withdrawnTotals = new Float64Array(simulationCount);
const depletionYearBySimulation = new Int32Array(simulationCount).fill(-1);
@@ -1023,6 +1030,10 @@ export function runMonteCarloSimulation(
// Cuts/raises from the withdrawal rule compound here, applied on top of
// the planned spending path (so they persist across spending phases)
let adjustmentFactor = 1;
+ // Floor & ceiling's withdrawal rate, anchored per run in the first
+ // spending year so the first retirement withdrawal is the planned
+ // amount exactly
+ let floorCeilingAnchorRate = 0;
// This replay's realized inflation path: each year draws its own rate
// when inflation volatility is set
let cumulativeInflation = 1;
@@ -1099,20 +1110,32 @@ export function runMonteCarloSimulation(
}
// Apply the dynamic withdrawal rule before taking this year's
- // withdrawal (from year 2 - year 1 always uses the planned amount).
- // Rules evaluate accessible wealth only: locked pots can't fund
- // spending, so they must not drive cuts or raises until they unlock
- if (year > 1 && rule.type === 'floor-ceiling') {
- // Recompute rule: a fixed share of the accessible balance, kept
- // within limits around the planned spending
- withdrawal = clamp(
- initialRate * accessibleTotal,
- planned * (1 - rule.floorPct),
- planned * (1 + rule.ceilingPct),
- );
+ // withdrawal. Rules only operate in years with planned spending,
+ // measured from the first spending year: the anchor year takes
+ // the planned amount as-is, and zero-spend years (accumulation
+ // before retirement, or a later zero phase) neither adjust nor
+ // drift them. Rules evaluate accessible wealth only: locked pots
+ // can't fund spending, so they must not drive cuts or raises
+ // until they unlock
+ if (rule.type === 'floor-ceiling') {
+ if (year === firstSpendingYear) {
+ floorCeilingAnchorRate =
+ accessibleTotal > 0 ? planned / accessibleTotal : 0;
+ }
+ withdrawal =
+ planned > 0 && year > firstSpendingYear
+ ? // A fixed share of the accessible balance, kept within
+ // limits around the planned spending
+ clamp(
+ floorCeilingAnchorRate * accessibleTotal,
+ planned * (1 - rule.floorPct),
+ planned * (1 + rule.ceilingPct),
+ )
+ : planned;
} else {
if (
- year > 1 &&
+ planned > 0 &&
+ year > firstSpendingYear &&
rule.type !== 'none' &&
accessibleTotal > 0 &&
accessibleStartByYear[year] > 0
diff --git a/packages/docs/docs/experimental/monte-carlo-analysis.md b/packages/docs/docs/experimental/monte-carlo-analysis.md
index a74bbc8fbd..8883356a72 100644
--- a/packages/docs/docs/experimental/monte-carlo-analysis.md
+++ b/packages/docs/docs/experimental/monte-carlo-analysis.md
@@ -128,7 +128,7 @@ By default, the simulation withdraws the same (inflation-adjusted) amount every
All the rules share a few ideas:
-- Your **spending phases** set the planned amounts. From the second year onward, the rule adjusts what's actually taken - independently in every replay, reacting to how that replay is going. A cut or raise carries across phase boundaries: if the rule cut your spending by 10% during a rough patch, the next phase's amount starts 10% lower too.
+- Your **spending phases** set the planned amounts. The rules wake up in your first year of planned spending: that year takes the planned amount (only a **Minimum withdrawal** set higher than it can override that), and from the next year onward the rule adjusts what's actually taken - independently in every replay, reacting to how that replay is going. Zero-spend years (for example, working years before retirement) neither trigger nor move the rules. A cut or raise carries across phase boundaries: if the rule cut your spending by 10% during a rough patch, the next phase's amount starts 10% lower too.
- Rules usually improve your **success rate** by cutting spending in bad times, but that safety isn't free - you get it by living on less. Keep an eye on the **Median total withdrawn** stat to see what a rule costs you in income.
- Rules only see the wealth you can actually spend. If a pension is locked until its access age, it doesn't earn you spending raises while a bridge pot pays the bills - the rules watch the accessible pots, and the pension starts counting the moment it unlocks.
@@ -144,7 +144,7 @@ The optimist's rule: withdrawals only ever go **up**, never down. If your balanc
### Floor & Ceiling (Bengen)
-Instead of a fixed amount, each year you withdraw a fixed **percentage of whatever the pots are currently worth** - so spending naturally falls in bad years and rises in good ones. To stop that swinging too wildly, the withdrawal is kept within a floor and a ceiling around your original (inflation-adjusted) amount.
+Instead of a fixed amount, each year you withdraw a fixed **percentage of whatever the pots are currently worth** - so spending naturally falls in bad years and rises in good ones. The percentage is set in your first year of planned spending (your planned amount divided by your accessible wealth at that point), and the withdrawal is then kept within a floor and a ceiling around your planned (inflation-adjusted) amount so it can't swing too wildly.
### Boundaries
diff --git a/upcoming-release-notes/fix-monte-carlo-rules-zero-spend.md b/upcoming-release-notes/fix-monte-carlo-rules-zero-spend.md
new file mode 100644
index 0000000000..13fff92771
--- /dev/null
+++ b/upcoming-release-notes/fix-monte-carlo-rules-zero-spend.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [MikesGlitch]
+---
+
+Fix Monte Carlo withdrawal rules misbehaving when retirement starts after years of zero planned spending