A local-first personal finance app
:chart_with_upwards_trend: Fix monte carlo withdrawal rule wording (#9252)
Michael Clark50a488481e95
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.tsindex 46e02c091c..9cf6d5edc6 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@@ -1318,6 +1318,7 @@ describe('runMonteCarloSimulation', () => {kind: 'factor',rule: 'boundaries',factor: 1,+ previousFactor: 1,planned: 40_000,adjusted: 40_000,action: 'none',@@ -1362,6 +1363,7 @@ describe('runMonteCarloSimulation', () => {kind: 'factor',rule: 'ratcheting',factor: 1,+ previousFactor: 1,planned: 10_000,adjusted: 10_000,action: 'none',@@ -1371,6 +1373,7 @@ describe('runMonteCarloSimulation', () => {kind: 'factor',rule: 'ratcheting',factor: 1.05,+ previousFactor: 1,planned: 10_000,adjusted: 10_500,action: 'raise',@@ -1531,6 +1534,7 @@ describe('runMonteCarloSimulation', () => {kind: 'factor',rule: 'guardrails',factor: 0.5,+ previousFactor: 1,planned: 10_000,adjusted: 5_000,action: 'cut',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.tsindex ae292bf201..fb2c8e26f4 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@@ -1037,6 +1037,8 @@ export type MonteCarloRuleExplanation =rule: 'guardrails' | 'ratcheting' | 'boundaries';/** The running adjustment factor after this year's decision */factor: number;+ /** The factor before it - what a cut or raise moved spending from */+ previousFactor: number;/** The year's planned spending, in minor units (display-deflated) */planned: number;/** planned x factor - the rule's result before any minimum floor */@@ -2195,6 +2197,7 @@ export function runMonteCarloSimulation(accessibleStartByYear[year] > 0) {const currentRate = (planned * adjustmentFactor) / accessibleTotal;+ const factorBeforeDecision = adjustmentFactor;let ruleAction: 'cut' | 'raise' | 'none' = 'none';if (rule.type === 'guardrails') {// Drift is measured against the planned spending path, not@@ -2249,6 +2252,7 @@ export function runMonteCarloSimulation(kind: 'factor',rule: rule.type,factor: adjustmentFactor,+ previousFactor: factorBeforeDecision,planned: toSafeAmount(Math.round(planned * startDeflator)),adjusted: toSafeAmount(Math.round(planned * adjustmentFactor * startDeflator),diff --git a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.test.ts b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.test.tsindex 043170d87b..676c1fa5ed 100644--- a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.test.ts+++ b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.test.ts@@ -12,7 +12,14 @@ const translate = ((key: string, options?: Record<string, unknown>) =>String(options?.[name]),)) as unknown as TFunction;-const format = (amount: number) => amount.toFixed(2);+// Rejects fractions like the real formatter, which only takes whole+// minor units+const format = (amount: number) => {+ if (!Number.isInteger(amount)) {+ throw new Error(`not an integer: ${amount}`);+ }+ return amount.toFixed(2);+};function makeRow(overrides: Partial<MonteCarloRunDetailRow> = {},@@ -77,6 +84,7 @@ describe('buildMonteCarloYearStory', () => {kind: 'factor',rule: 'guardrails',factor: 0.75,+ previousFactor: 1,planned: 40_000,adjusted: 16_875,action: 'cut',@@ -103,6 +111,7 @@ describe('buildMonteCarloYearStory', () => {kind: 'factor',rule: 'guardrails',factor: 1,+ previousFactor: 1,planned: 4_000,adjusted: 4_000,action: 'none',@@ -122,6 +131,7 @@ describe('buildMonteCarloYearStory', () => {kind: 'factor',rule: 'boundaries',factor: 0.9,+ previousFactor: 0.9,planned: 4_000,adjusted: 3_600,action: 'none',@@ -138,6 +148,7 @@ describe('buildMonteCarloYearStory', () => {kind: 'factor',rule: 'boundaries',factor: 0.9,+ previousFactor: 1,planned: 4_000,adjusted: 3_600,action: 'cut',@@ -153,6 +164,7 @@ describe('buildMonteCarloYearStory', () => {kind: 'factor',rule: 'ratcheting',factor: 1.05,+ previousFactor: 1,planned: 4_000,adjusted: 4_200,action: 'raise',@@ -164,6 +176,67 @@ describe('buildMonteCarloYearStory', () => {);});+ it("measures a change from last year's level when earlier changes still apply", () => {+ // The screenshot year 57: earlier cuts had the factor at 0.249, a+ // 20% raise takes it to 0.299 - so spending rose from 14,946.78 to+ // 17,936.13, nowhere near the 60,000 planned+ const raiseStillBelowPlan = makeRow({+ withdrawal: 17_936,+ plannedSpending: 17_936,+ spent: 17_936,+ ruleExplanation: {+ kind: 'factor',+ rule: 'guardrails',+ factor: 0.298936,+ previousFactor: 0.298936 / 1.2,+ planned: 60_000,+ adjusted: 17_936,+ action: 'raise',+ currentRate: 0.01504,+ referenceRate: 0.053553,+ },+ });+ expect(story(raiseStillBelowPlan)[0]).toBe(+ "Guardrails raised this year's spending from 14947.00 to 17936.00 because the withdrawal rate had fallen well below the plan. With earlier changes included, that is below the 60000.00 planned.",+ );++ const cutStillAbovePlan = makeRow({+ ruleExplanation: {+ kind: 'factor',+ rule: 'boundaries',+ factor: 1.2 * 0.9,+ previousFactor: 1.2,+ planned: 4_000,+ adjusted: 4_320,+ action: 'cut',+ currentRate: 0.07,+ },+ });+ expect(story(cutStillAbovePlan)[0]).toBe(+ "Boundaries cut this year's spending from 4800.00 to 4320.00 because the withdrawal rate rose above 6%. With earlier changes included, that is above the 4000.00 planned.",+ );+ });++ it('survives a 100% cut, which zeroes the factor', () => {+ const row = makeRow({+ withdrawal: 0,+ plannedSpending: 0,+ spent: 0,+ ruleExplanation: {+ kind: 'factor',+ rule: 'guardrails',+ factor: 0,+ previousFactor: 0.8,+ planned: 4_000,+ adjusted: 0,+ action: 'cut',+ },+ });+ expect(story(row)[0]).toBe(+ "Guardrails cut this year's spending from 3200.00 to 0.00 because the withdrawal rate had climbed too far above the plan. With earlier changes included, that is below the 4000.00 planned.",+ );+ });+it('describes floor & ceiling only when a bound applied', () => {const floorApplied = makeRow({ruleExplanation: {@@ -239,18 +312,18 @@ describe('buildMonteCarloYearStory', () => {});it('says where income went when contributions took it', () => {- // The user's year 66: 12,547.60 income, all paid into the cash pot+ // The user's year 66: 12,548 income, all paid into the cash potconst allContributed = makeRow({- income: 12_547.6,- incomeAmounts: [12_547.6],- contributions: 12_547.6,- withdrawal: 24_282.41,- taxPaid: 4_282.41,+ income: 12_548,+ incomeAmounts: [12_548],+ contributions: 12_548,+ withdrawal: 24_282,+ taxPaid: 4_282,plannedSpending: 20_000,spent: 20_000,});expect(story(allContributed, { hasSurplusPot: true })).toEqual([- 'The 12547.60 income all went into contributions, so the 20000.00 spent came from the pots.',+ 'The 12548.00 income all went into contributions, so the 20000.00 spent came from the pots.',]);const partlyContributed = makeRow({diff --git a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.ts b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.tsindex 3974eef115..3827f317e9 100644--- a/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.ts+++ b/packages/desktop-client/src/components/reports/reports/monte-carlo/monteCarloYearStory.ts@@ -99,6 +99,34 @@ function getRuleName(return translate('Boundaries');}+// Where the rule's result sits against the plan once earlier years'+// changes are counted - only worth saying when there were some, since a+// first change moves from the planned amount itself+function getEarlierChangesSentence(+ explanation: Extract<MonteCarloRuleExplanation, { kind: 'factor' }>,+ previousFactor: number,+ format: MonteCarloYearStoryInput['format'],+ translate: TFunction,+): string | null {+ if (Math.abs(previousFactor - 1) < 1e-6) {+ return null;+ }+ const planned = format(explanation.planned, 'financial');+ if (explanation.adjusted < explanation.planned) {+ return translate(+ 'With earlier changes included, that is below the {{planned}} planned.',+ { planned },+ );+ }+ if (explanation.adjusted > explanation.planned) {+ return translate(+ 'With earlier changes included, that is above the {{planned}} planned.',+ { planned },+ );+ }+ return null;+}+// What the rule decided, with the reason in words; null when the rule// did nothing worth tellingfunction getRuleDecisionSentence(@@ -140,50 +168,76 @@ function getRuleDecisionSentence(const rule = getRuleName(explanation.rule, translate);const planned = format(explanation.planned, 'financial');const adjusted = format(explanation.adjusted, 'financial');+ // A change steps the running factor, so spending moves from last+ // year's adjusted level - the planned amount only when nothing had+ // changed before. Rounded because amounts are whole minor units and+ // the formatter rejects fractions+ const from = format(+ Math.round(explanation.planned * explanation.previousFactor),+ 'financial',+ );+ const earlierChanges = getEarlierChangesSentence(+ explanation,+ explanation.previousFactor,+ format,+ translate,+ );+ const withEarlierChanges = (sentence: string) =>+ earlierChanges == null ? sentence : `${sentence} ${earlierChanges}`;if (explanation.action === 'cut') {if (explanation.rule === 'boundaries') {- return translate(- "{{rule}} cut this year's spending from {{planned}} to {{adjusted}} because the withdrawal rate rose above {{upper}}.",- {- rule,- planned,- adjusted,- upper: formatRuleRate(withdrawalRule.upperRateThreshold),- },+ return withEarlierChanges(+ translate(+ "{{rule}} cut this year's spending from {{from}} to {{adjusted}} because the withdrawal rate rose above {{upper}}.",+ {+ rule,+ from,+ adjusted,+ upper: formatRuleRate(withdrawalRule.upperRateThreshold),+ },+ ),);}- return translate(- "{{rule}} cut this year's spending from {{planned}} to {{adjusted}} because the withdrawal rate had climbed too far above the plan.",- { rule, planned, adjusted },+ return withEarlierChanges(+ translate(+ "{{rule}} cut this year's spending from {{from}} to {{adjusted}} because the withdrawal rate had climbed too far above the plan.",+ { rule, from, adjusted },+ ),);}if (explanation.action === 'raise') {if (explanation.rule === 'boundaries') {- return translate(- "{{rule}} raised this year's spending from {{planned}} to {{adjusted}} because the withdrawal rate fell below {{lower}}.",- {- rule,- planned,- adjusted,- lower: formatRuleRate(withdrawalRule.lowerRateThreshold),- },+ return withEarlierChanges(+ translate(+ "{{rule}} raised this year's spending from {{from}} to {{adjusted}} because the withdrawal rate fell below {{lower}}.",+ {+ rule,+ from,+ adjusted,+ lower: formatRuleRate(withdrawalRule.lowerRateThreshold),+ },+ ),);}if (explanation.rule === 'ratcheting') {- return translate(- "{{rule}} raised this year's spending from {{planned}} to {{adjusted}} because the balance had stayed above {{multiple}} its starting level for {{years}} years.",- {- rule,- planned,- adjusted,- multiple: `${Number(withdrawalRule.balanceThresholdMultiple.toFixed(2))}×`,- years: withdrawalRule.consecutiveYears,- },+ return withEarlierChanges(+ translate(+ "{{rule}} raised this year's spending from {{from}} to {{adjusted}} because the balance had stayed above {{multiple}} its starting level for {{years}} years.",+ {+ rule,+ from,+ adjusted,+ multiple: `${Number(withdrawalRule.balanceThresholdMultiple.toFixed(2))}×`,+ years: withdrawalRule.consecutiveYears,+ },+ ),);}- return translate(- "{{rule}} raised this year's spending from {{planned}} to {{adjusted}} because the withdrawal rate had fallen well below the plan.",- { rule, planned, adjusted },+ return withEarlierChanges(+ translate(+ "{{rule}} raised this year's spending from {{from}} to {{adjusted}} because the withdrawal rate had fallen well below the plan.",+ { rule, from, adjusted },+ ),);}if (explanation.factor !== 1) {diff --git a/upcoming-release-notes/fix-monte-carlo-rule-change-wording.md b/upcoming-release-notes/fix-monte-carlo-rule-change-wording.mdnew file mode 100644index 0000000000..7c1a5ee46d--- /dev/null+++ b/upcoming-release-notes/fix-monte-carlo-rule-change-wording.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [MikesGlitch]+---++Monte carlo - Fix the run summary misstating what a withdrawal rule change moved spending from