@retiregolden/engine 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (199) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +79 -0
  3. package/dist/allocation/assetClasses.d.ts +94 -0
  4. package/dist/allocation/assetClasses.js +215 -0
  5. package/dist/decisions/annuitization.d.ts +72 -0
  6. package/dist/decisions/annuitization.js +184 -0
  7. package/dist/decisions/decisionFixtures.d.ts +48 -0
  8. package/dist/decisions/decisionFixtures.js +253 -0
  9. package/dist/decisions/evaluateCandidate.d.ts +61 -0
  10. package/dist/decisions/evaluateCandidate.js +193 -0
  11. package/dist/decisions/generators.d.ts +110 -0
  12. package/dist/decisions/generators.js +667 -0
  13. package/dist/decisions/index.d.ts +17 -0
  14. package/dist/decisions/index.js +17 -0
  15. package/dist/decisions/insightsAdapter.d.ts +27 -0
  16. package/dist/decisions/insightsAdapter.js +51 -0
  17. package/dist/decisions/objectives.d.ts +96 -0
  18. package/dist/decisions/objectives.js +319 -0
  19. package/dist/decisions/pensionElection.d.ts +112 -0
  20. package/dist/decisions/pensionElection.js +239 -0
  21. package/dist/decisions/search.d.ts +51 -0
  22. package/dist/decisions/search.js +110 -0
  23. package/dist/decisions/spendingSolver.d.ts +70 -0
  24. package/dist/decisions/spendingSolver.js +180 -0
  25. package/dist/decisions/spiaQuotes.d.ts +21 -0
  26. package/dist/decisions/spiaQuotes.js +43 -0
  27. package/dist/decisions/stochastic.d.ts +11 -0
  28. package/dist/decisions/stochastic.js +66 -0
  29. package/dist/decisions/swrComparator.d.ts +54 -0
  30. package/dist/decisions/swrComparator.js +89 -0
  31. package/dist/decisions/tournament.d.ts +53 -0
  32. package/dist/decisions/tournament.js +104 -0
  33. package/dist/decisions/types.d.ts +125 -0
  34. package/dist/decisions/types.js +12 -0
  35. package/dist/index.d.ts +13 -0
  36. package/dist/index.js +12 -0
  37. package/dist/insights/detectors/annuitizationHeadroom.d.ts +11 -0
  38. package/dist/insights/detectors/annuitizationHeadroom.js +77 -0
  39. package/dist/insights/detectors/assetLocation.d.ts +9 -0
  40. package/dist/insights/detectors/assetLocation.js +86 -0
  41. package/dist/insights/detectors/hecmBufferCandidate.d.ts +12 -0
  42. package/dist/insights/detectors/hecmBufferCandidate.js +76 -0
  43. package/dist/insights/detectors/incomeFloorFunded.d.ts +8 -0
  44. package/dist/insights/detectors/incomeFloorFunded.js +52 -0
  45. package/dist/insights/detectors/irmaaTierEdge.d.ts +2 -0
  46. package/dist/insights/detectors/irmaaTierEdge.js +102 -0
  47. package/dist/insights/detectors/pensionElectionPending.d.ts +11 -0
  48. package/dist/insights/detectors/pensionElectionPending.js +62 -0
  49. package/dist/insights/detectors/qcdEfficiency.d.ts +2 -0
  50. package/dist/insights/detectors/qcdEfficiency.js +62 -0
  51. package/dist/insights/detectors/rothBridgeHeadroom.d.ts +2 -0
  52. package/dist/insights/detectors/rothBridgeHeadroom.js +77 -0
  53. package/dist/insights/detectors/spendingGuardrails.d.ts +2 -0
  54. package/dist/insights/detectors/spendingGuardrails.js +77 -0
  55. package/dist/insights/detectors/spendingHeadroom.d.ts +12 -0
  56. package/dist/insights/detectors/spendingHeadroom.js +92 -0
  57. package/dist/insights/detectors/ssBridgeGap.d.ts +9 -0
  58. package/dist/insights/detectors/ssBridgeGap.js +92 -0
  59. package/dist/insights/detectors/stateRelocation.d.ts +12 -0
  60. package/dist/insights/detectors/stateRelocation.js +99 -0
  61. package/dist/insights/detectors/widowsPenalty.d.ts +12 -0
  62. package/dist/insights/detectors/widowsPenalty.js +145 -0
  63. package/dist/insights/registry.d.ts +4 -0
  64. package/dist/insights/registry.js +62 -0
  65. package/dist/insights/runInsights.d.ts +6 -0
  66. package/dist/insights/runInsights.js +15 -0
  67. package/dist/insights/types.d.ts +70 -0
  68. package/dist/insights/types.js +1 -0
  69. package/dist/ladder/bridge.d.ts +72 -0
  70. package/dist/ladder/bridge.js +59 -0
  71. package/dist/ladder/fedInvest.d.ts +55 -0
  72. package/dist/ladder/fedInvest.js +77 -0
  73. package/dist/ladder/fundedRatio.d.ts +41 -0
  74. package/dist/ladder/fundedRatio.js +46 -0
  75. package/dist/ladder/ladderMath.d.ts +88 -0
  76. package/dist/ladder/ladderMath.js +140 -0
  77. package/dist/longevity/ssaPeriod2022.d.ts +10 -0
  78. package/dist/longevity/ssaPeriod2022.js +51 -0
  79. package/dist/longevity/types.d.ts +39 -0
  80. package/dist/longevity/types.js +1 -0
  81. package/dist/model/migrations.d.ts +20 -0
  82. package/dist/model/migrations.js +67 -0
  83. package/dist/model/plan.d.ts +3108 -0
  84. package/dist/model/plan.js +1514 -0
  85. package/dist/montecarlo/frontiers.d.ts +22 -0
  86. package/dist/montecarlo/frontiers.js +61 -0
  87. package/dist/montecarlo/historicalReturns.d.ts +27 -0
  88. package/dist/montecarlo/historicalReturns.js +124 -0
  89. package/dist/montecarlo/historicalSuites.d.ts +45 -0
  90. package/dist/montecarlo/historicalSuites.js +102 -0
  91. package/dist/montecarlo/ltcShock.d.ts +42 -0
  92. package/dist/montecarlo/ltcShock.js +62 -0
  93. package/dist/montecarlo/marketModels.d.ts +248 -0
  94. package/dist/montecarlo/marketModels.js +672 -0
  95. package/dist/montecarlo/mortality.d.ts +31 -0
  96. package/dist/montecarlo/mortality.js +61 -0
  97. package/dist/montecarlo/riskBasedGuardrails.d.ts +93 -0
  98. package/dist/montecarlo/riskBasedGuardrails.js +200 -0
  99. package/dist/montecarlo/rng.d.ts +18 -0
  100. package/dist/montecarlo/rng.js +44 -0
  101. package/dist/montecarlo/run.d.ts +210 -0
  102. package/dist/montecarlo/run.js +353 -0
  103. package/dist/montecarlo/sharedPaths.d.ts +43 -0
  104. package/dist/montecarlo/sharedPaths.js +28 -0
  105. package/dist/montecarlo/survival.d.ts +49 -0
  106. package/dist/montecarlo/survival.js +126 -0
  107. package/dist/params/data/realYieldCurve2026.d.ts +17 -0
  108. package/dist/params/data/realYieldCurve2026.js +26 -0
  109. package/dist/params/data/year2026.d.ts +14 -0
  110. package/dist/params/data/year2026.js +199 -0
  111. package/dist/params/index.d.ts +63 -0
  112. package/dist/params/index.js +134 -0
  113. package/dist/params/provenance.d.ts +29 -0
  114. package/dist/params/provenance.js +123 -0
  115. package/dist/params/state/data/year2026.d.ts +18 -0
  116. package/dist/params/state/data/year2026.js +581 -0
  117. package/dist/params/state/index.d.ts +13 -0
  118. package/dist/params/state/index.js +26 -0
  119. package/dist/params/state/types.d.ts +73 -0
  120. package/dist/params/state/types.js +10 -0
  121. package/dist/params/types.d.ts +201 -0
  122. package/dist/params/types.js +7 -0
  123. package/dist/projection/annuityForms.d.ts +62 -0
  124. package/dist/projection/annuityForms.js +87 -0
  125. package/dist/projection/compare.d.ts +96 -0
  126. package/dist/projection/compare.js +216 -0
  127. package/dist/projection/flatTax.d.ts +7 -0
  128. package/dist/projection/flatTax.js +14 -0
  129. package/dist/projection/optimizePlan.d.ts +328 -0
  130. package/dist/projection/optimizePlan.js +1039 -0
  131. package/dist/projection/relocation.d.ts +149 -0
  132. package/dist/projection/relocation.js +317 -0
  133. package/dist/projection/simulate.d.ts +61 -0
  134. package/dist/projection/simulate.js +2754 -0
  135. package/dist/projection/types.d.ts +381 -0
  136. package/dist/projection/types.js +9 -0
  137. package/dist/rmd/jointLifeTable.d.ts +8 -0
  138. package/dist/rmd/jointLifeTable.js +663 -0
  139. package/dist/rmd/rmd.d.ts +29 -0
  140. package/dist/rmd/rmd.js +34 -0
  141. package/dist/scenarios/scenarios.d.ts +70 -0
  142. package/dist/scenarios/scenarios.js +130 -0
  143. package/dist/socialSecurity/benefitFactor.d.ts +13 -0
  144. package/dist/socialSecurity/benefitFactor.js +40 -0
  145. package/dist/socialSecurity/claimFactor.d.ts +33 -0
  146. package/dist/socialSecurity/claimFactor.js +58 -0
  147. package/dist/socialSecurity/disability.d.ts +36 -0
  148. package/dist/socialSecurity/disability.js +42 -0
  149. package/dist/socialSecurity/familyMaximum.d.ts +38 -0
  150. package/dist/socialSecurity/familyMaximum.js +48 -0
  151. package/dist/socialSecurity/maritalBenefits.d.ts +57 -0
  152. package/dist/socialSecurity/maritalBenefits.js +87 -0
  153. package/dist/socialSecurity/nra.d.ts +30 -0
  154. package/dist/socialSecurity/nra.js +83 -0
  155. package/dist/socialSecurity/piaFromEarnings.d.ts +100 -0
  156. package/dist/socialSecurity/piaFromEarnings.js +202 -0
  157. package/dist/socialSecurity/ssaWageData.d.ts +68 -0
  158. package/dist/socialSecurity/ssaWageData.js +306 -0
  159. package/dist/socialSecurity/survivorBenefit.d.ts +61 -0
  160. package/dist/socialSecurity/survivorBenefit.js +61 -0
  161. package/dist/spending/abw.d.ts +57 -0
  162. package/dist/spending/abw.js +80 -0
  163. package/dist/spending/flexibleGoals.d.ts +62 -0
  164. package/dist/spending/flexibleGoals.js +136 -0
  165. package/dist/spending/guardrails.d.ts +78 -0
  166. package/dist/spending/guardrails.js +100 -0
  167. package/dist/spending/layers.d.ts +81 -0
  168. package/dist/spending/layers.js +67 -0
  169. package/dist/spending/shapePresets.d.ts +42 -0
  170. package/dist/spending/shapePresets.js +77 -0
  171. package/dist/strategies/accountEligibility.d.ts +81 -0
  172. package/dist/strategies/accountEligibility.js +98 -0
  173. package/dist/strategies/inheritedIra.d.ts +45 -0
  174. package/dist/strategies/inheritedIra.js +43 -0
  175. package/dist/strategies/iraBasis.d.ts +48 -0
  176. package/dist/strategies/iraBasis.js +46 -0
  177. package/dist/strategies/optimizer.d.ts +307 -0
  178. package/dist/strategies/optimizer.js +551 -0
  179. package/dist/strategies/rothBasis.d.ts +67 -0
  180. package/dist/strategies/rothBasis.js +83 -0
  181. package/dist/strategies/rothConversion.d.ts +55 -0
  182. package/dist/strategies/rothConversion.js +83 -0
  183. package/dist/strategies/sepp.d.ts +38 -0
  184. package/dist/strategies/sepp.js +53 -0
  185. package/dist/tax/aca.d.ts +40 -0
  186. package/dist/tax/aca.js +82 -0
  187. package/dist/tax/federalTax.d.ts +110 -0
  188. package/dist/tax/federalTax.js +249 -0
  189. package/dist/tax/medicare.d.ts +20 -0
  190. package/dist/tax/medicare.js +36 -0
  191. package/dist/tax/propertySale.d.ts +46 -0
  192. package/dist/tax/propertySale.js +35 -0
  193. package/dist/tax/stateTax.d.ts +66 -0
  194. package/dist/tax/stateTax.js +198 -0
  195. package/dist/testing/money.d.ts +7 -0
  196. package/dist/testing/money.js +17 -0
  197. package/dist/testing/planFixtures.d.ts +29 -0
  198. package/dist/testing/planFixtures.js +141 -0
  199. package/package.json +75 -0
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Tolerance assertions for money and percentages. Deliberately framework-free
3
+ * (a thrown Error fails a test in any runner), so `@retiregolden/engine/testing/*`
4
+ * carries no runtime dependency on vitest for consumers.
5
+ */
6
+ function expectWithin(kind, actual, expected, tolerance) {
7
+ const delta = Math.abs(actual - expected);
8
+ if (Number.isNaN(delta) || delta > tolerance) {
9
+ throw new Error(`expected ${kind} ${actual} to be within ${tolerance} of ${expected} (off by ${delta})`);
10
+ }
11
+ }
12
+ export function expectMoney(actual, expected, tolerance = 0.01) {
13
+ expectWithin('money', actual, expected, tolerance);
14
+ }
15
+ export function expectPercent(actual, expected, tolerance = 0.0001) {
16
+ expectWithin('percent', actual, expected, tolerance);
17
+ }
@@ -0,0 +1,29 @@
1
+ import { type Account, type IncomeStream, type Plan } from '../model/plan.js';
2
+ import type { ProjectionResult, TaxCalculator } from '../projection/types.js';
3
+ export declare function testId(prefix?: string): string;
4
+ export declare function fixedNow(): Date;
5
+ export interface SinglePlanOptions {
6
+ dob?: string;
7
+ planningAge?: number;
8
+ retirementAge?: number | null;
9
+ state?: string;
10
+ }
11
+ export interface CouplePlanOptions {
12
+ p1Dob?: string;
13
+ p2Dob?: string;
14
+ p1PlanningAge?: number;
15
+ p2PlanningAge?: number;
16
+ p1RetirementAge?: number | null;
17
+ p2RetirementAge?: number | null;
18
+ state?: string;
19
+ }
20
+ export declare function singlePersonPlan(opts?: SinglePlanOptions): Plan;
21
+ export declare function couplePlan(opts?: CouplePlanOptions): Plan;
22
+ export declare function cashAccount(id: string, balance: number): Account;
23
+ export declare function taxableAccount(id: string, balance: number, costBasis: number): Account;
24
+ export declare function traditionalAccount(id: string, balance: number, ownerPersonId?: string, kind?: 'ira' | 'employer'): Account;
25
+ export declare function recurringOrdinaryIncome(id: string, annualAmount: number, startYear?: number | null): IncomeStream;
26
+ export declare function socialSecurityIncome(id: string, piaMonthly: number, claimAgeYears: number, personId?: string): IncomeStream;
27
+ export declare function validatePlan(plan: Plan): Plan;
28
+ export declare function productionTaxCalculator(): TaxCalculator;
29
+ export declare function runPlan(plan: Plan, taxCalculator: TaxCalculator, startYear?: number): ProjectionResult;
@@ -0,0 +1,141 @@
1
+ import { createEmptyPlan, parsePlan } from '../model/plan.js';
2
+ import { combineTaxCalculators, createFederalTaxCalculator } from '../tax/federalTax.js';
3
+ import { createStateTaxCalculator } from '../tax/stateTax.js';
4
+ import { simulatePlan } from '../projection/simulate.js';
5
+ let idCounter = 0;
6
+ export function testId(prefix = 'test') {
7
+ idCounter += 1;
8
+ return `${prefix}-${idCounter}`;
9
+ }
10
+ export function fixedNow() {
11
+ return new Date('2026-01-01T00:00:00.000Z');
12
+ }
13
+ export function singlePersonPlan(opts = {}) {
14
+ const plan = createEmptyPlan({ newId: () => testId('plan'), now: fixedNow, name: 'Test plan' });
15
+ plan.household.filingStatus = 'single';
16
+ plan.household.state = opts.state ?? 'KY';
17
+ plan.household.people = [
18
+ {
19
+ id: 'p1',
20
+ name: 'Pat',
21
+ dob: opts.dob ?? '1966-01-01',
22
+ sex: 'average',
23
+ retirementAge: opts.retirementAge ?? null,
24
+ longevity: { planningAge: opts.planningAge ?? 60, source: 'manual' },
25
+ },
26
+ ];
27
+ plan.assumptions.inflationPct = 0;
28
+ plan.assumptions.healthcareExtraInflationPct = 0;
29
+ plan.assumptions.defaultReturnPct = 0;
30
+ plan.assumptions.ssCola = { mode: 'fixed', annualPct: 0 };
31
+ plan.assumptions.ssHaircut = null;
32
+ plan.assumptions.stateEffectiveTaxPct = 0;
33
+ plan.assumptions.recentAnnualMagi = 0;
34
+ plan.accounts = [];
35
+ plan.incomes = [];
36
+ plan.expenses.baseAnnual = 0;
37
+ plan.expenses.phases = [];
38
+ plan.expenses.oneTimeGoals = [];
39
+ plan.expenses.healthcare = {
40
+ pre65MonthlyPremiumPerPerson: 0,
41
+ applyAcaCredit: false,
42
+ medicareExtrasMonthlyPerPerson: 0,
43
+ };
44
+ plan.strategies.withdrawalOrder = { mode: 'sequential' };
45
+ plan.strategies.rothConversion = { mode: 'none' };
46
+ plan.strategies.qcdAnnual = 0;
47
+ return plan;
48
+ }
49
+ export function couplePlan(opts = {}) {
50
+ const plan = singlePersonPlan({
51
+ dob: opts.p1Dob,
52
+ planningAge: opts.p1PlanningAge,
53
+ retirementAge: opts.p1RetirementAge,
54
+ state: opts.state,
55
+ });
56
+ plan.household.filingStatus = 'marriedFilingJointly';
57
+ plan.household.people = [
58
+ {
59
+ id: 'p1',
60
+ name: 'Pat',
61
+ dob: opts.p1Dob ?? '1966-01-01',
62
+ sex: 'average',
63
+ retirementAge: opts.p1RetirementAge ?? null,
64
+ longevity: { planningAge: opts.p1PlanningAge ?? 60, source: 'manual' },
65
+ },
66
+ {
67
+ id: 'p2',
68
+ name: 'Robin',
69
+ dob: opts.p2Dob ?? '1966-01-01',
70
+ sex: 'average',
71
+ retirementAge: opts.p2RetirementAge ?? null,
72
+ longevity: { planningAge: opts.p2PlanningAge ?? 60, source: 'manual' },
73
+ },
74
+ ];
75
+ return plan;
76
+ }
77
+ export function cashAccount(id, balance) {
78
+ return { type: 'cash', id, name: id, ownerPersonId: null, annualReturnPct: 0, balance, annualContribution: 0 };
79
+ }
80
+ export function taxableAccount(id, balance, costBasis) {
81
+ return {
82
+ type: 'taxable',
83
+ id,
84
+ name: id,
85
+ ownerPersonId: null,
86
+ annualReturnPct: 0,
87
+ balance,
88
+ costBasis,
89
+ interestYieldPct: 0,
90
+ dividendYieldPct: 0,
91
+ qualifiedRatio: 0.85,
92
+ reinvestDividends: true,
93
+ annualContribution: 0,
94
+ };
95
+ }
96
+ export function traditionalAccount(id, balance, ownerPersonId = 'p1', kind = 'ira') {
97
+ return {
98
+ type: 'traditional',
99
+ id,
100
+ name: id,
101
+ ownerPersonId,
102
+ annualReturnPct: 0,
103
+ kind,
104
+ balance,
105
+ annualContribution: 0,
106
+ };
107
+ }
108
+ export function recurringOrdinaryIncome(id, annualAmount, startYear = null) {
109
+ return {
110
+ type: 'recurring',
111
+ id,
112
+ label: id,
113
+ annualAmount,
114
+ startYear,
115
+ endYear: null,
116
+ inflationAdjusted: false,
117
+ taxTreatment: 'ordinary',
118
+ };
119
+ }
120
+ export function socialSecurityIncome(id, piaMonthly, claimAgeYears, personId = 'p1') {
121
+ return {
122
+ type: 'socialSecurity',
123
+ id,
124
+ personId,
125
+ piaMonthly,
126
+ earnings: null,
127
+ claimAge: { years: claimAgeYears, months: 0 },
128
+ };
129
+ }
130
+ export function validatePlan(plan) {
131
+ const parsed = parsePlan(plan);
132
+ if (!parsed.ok)
133
+ throw new Error(parsed.issues.join('; '));
134
+ return parsed.plan;
135
+ }
136
+ export function productionTaxCalculator() {
137
+ return combineTaxCalculators(createFederalTaxCalculator(), createStateTaxCalculator());
138
+ }
139
+ export function runPlan(plan, taxCalculator, startYear = 2026) {
140
+ return simulatePlan(validatePlan(plan), { startYear, taxCalculator });
141
+ }
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@retiregolden/engine",
3
+ "version": "0.1.0",
4
+ "description": "Pure-TypeScript retirement-planning engine: year-by-year household projection with federal + state taxes, Social Security, RMDs, Roth conversions, withdrawal strategies, Monte Carlo, and an LP optimizer. Deterministic, no DOM, no network.",
5
+ "license": "AGPL-3.0-only",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "git+https://github.com/RetireGolden/RetireGolden.git",
13
+ "directory": "packages/engine"
14
+ },
15
+ "homepage": "https://github.com/RetireGolden/RetireGolden/tree/main/packages/engine#readme",
16
+ "bugs": "https://github.com/RetireGolden/RetireGolden/issues",
17
+ "keywords": [
18
+ "retirement",
19
+ "financial-planning",
20
+ "social-security",
21
+ "roth-conversion",
22
+ "monte-carlo",
23
+ "tax"
24
+ ],
25
+ "files": [
26
+ "dist",
27
+ "LICENSE",
28
+ "README.md"
29
+ ],
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "default": "./dist/index.js"
36
+ },
37
+ "./decisions": {
38
+ "types": "./dist/decisions/index.d.ts",
39
+ "default": "./dist/decisions/index.js"
40
+ },
41
+ "./params": {
42
+ "types": "./dist/params/index.d.ts",
43
+ "default": "./dist/params/index.js"
44
+ },
45
+ "./params/state": {
46
+ "types": "./dist/params/state/index.d.ts",
47
+ "default": "./dist/params/state/index.js"
48
+ },
49
+ "./*": {
50
+ "types": "./dist/*.d.ts",
51
+ "default": "./dist/*.js"
52
+ },
53
+ "./package.json": "./package.json"
54
+ },
55
+ "scripts": {
56
+ "build": "tsc -b",
57
+ "lint": "eslint .",
58
+ "test": "tsc -p tsconfig.test.json && vitest run",
59
+ "test:coverage": "tsc -p tsconfig.test.json && vitest run --coverage",
60
+ "prepack": "npm run build"
61
+ },
62
+ "dependencies": {
63
+ "highs": "^1.14.2",
64
+ "zod": "^4.4.3"
65
+ },
66
+ "devDependencies": {
67
+ "@eslint/js": "^10.0.1",
68
+ "@vitest/coverage-v8": "^4.1.6",
69
+ "eslint": "^10.3.0",
70
+ "globals": "^17.6.0",
71
+ "typescript": "~6.0.2",
72
+ "typescript-eslint": "^8.59.2",
73
+ "vitest": "^4.0.16"
74
+ }
75
+ }