@robotixai/calculator-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.
- package/LICENSE +21 -0
- package/README.md +127 -0
- package/dist/advanced.d.ts +24 -0
- package/dist/advanced.d.ts.map +1 -0
- package/dist/advanced.js +747 -0
- package/dist/backtest.d.ts +28 -0
- package/dist/backtest.d.ts.map +1 -0
- package/dist/backtest.js +235 -0
- package/dist/defaults.d.ts +4 -0
- package/dist/defaults.d.ts.map +1 -0
- package/dist/defaults.js +84 -0
- package/dist/heatmap.d.ts +38 -0
- package/dist/heatmap.d.ts.map +1 -0
- package/dist/heatmap.js +63 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/monte-carlo.d.ts +43 -0
- package/dist/monte-carlo.d.ts.map +1 -0
- package/dist/monte-carlo.js +178 -0
- package/dist/optimizer.d.ts +40 -0
- package/dist/optimizer.d.ts.map +1 -0
- package/dist/optimizer.js +134 -0
- package/dist/portfolio.d.ts +43 -0
- package/dist/portfolio.d.ts.map +1 -0
- package/dist/portfolio.js +86 -0
- package/dist/projection.d.ts +16 -0
- package/dist/projection.d.ts.map +1 -0
- package/dist/projection.js +382 -0
- package/dist/sensitivity.d.ts +30 -0
- package/dist/sensitivity.d.ts.map +1 -0
- package/dist/sensitivity.js +92 -0
- package/dist/tax.d.ts +49 -0
- package/dist/tax.d.ts.map +1 -0
- package/dist/tax.js +210 -0
- package/dist/types.d.ts +250 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +7 -0
- package/dist/withdrawal.d.ts +136 -0
- package/dist/withdrawal.d.ts.map +1 -0
- package/dist/withdrawal.js +241 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Shore Crest Education
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# @robotixai/calculator-engine
|
|
2
|
+
|
|
3
|
+
Financial retirement projection engine with Monte Carlo simulation.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Deterministic year-by-year projection (basic + advanced mode)
|
|
8
|
+
- Monte Carlo simulation (1K-10K runs, seeded PRNG)
|
|
9
|
+
- Multi-jurisdiction tax (US, UK, Cayman, Custom)
|
|
10
|
+
- Withdrawal strategies (Standard, Guyton-Klinger, Age-Banded)
|
|
11
|
+
- Sensitivity analysis (tornado chart)
|
|
12
|
+
- Historical backtest (154 years of Shiller data)
|
|
13
|
+
- Retirement age optimizer (binary search)
|
|
14
|
+
- Retirement age x spending heatmap
|
|
15
|
+
- Advanced mode with cash waterfall, loans, properties
|
|
16
|
+
- Portfolio blending and estate value calculation
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install @robotixai/calculator-engine
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
No peer dependencies required. All types and defaults are included in the package.
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import { runProjection, runMonteCarloSimulation, DEFAULT_SCENARIO } from '@robotixai/calculator-engine';
|
|
30
|
+
|
|
31
|
+
// Deterministic projection
|
|
32
|
+
const { timeline, metrics } = runProjection(DEFAULT_SCENARIO);
|
|
33
|
+
console.log(`Terminal balance: $${metrics.terminal_real.toFixed(2)}`);
|
|
34
|
+
|
|
35
|
+
// Monte Carlo simulation
|
|
36
|
+
const mcResult = runMonteCarloSimulation(DEFAULT_SCENARIO, runProjection, { runs: 1000 });
|
|
37
|
+
console.log(`Success probability: ${mcResult.probability_no_shortfall}%`);
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## API Reference
|
|
41
|
+
|
|
42
|
+
### Projection
|
|
43
|
+
|
|
44
|
+
| Function | Description |
|
|
45
|
+
|---|---|
|
|
46
|
+
| `runProjection(scenario, overrideReturns?)` | Deterministic year-by-year projection (basic mode). Returns `{ timeline, metrics }`. |
|
|
47
|
+
| `runAdvancedProjection(scenario, overrideReturns?)` | Advanced mode projection with cash waterfall, individual financial items, loans, and properties. |
|
|
48
|
+
|
|
49
|
+
### Monte Carlo
|
|
50
|
+
|
|
51
|
+
| Function / Type | Description |
|
|
52
|
+
|---|---|
|
|
53
|
+
| `runMonteCarloSimulation(scenario, projectionFn, options?)` | Seeded PRNG Monte Carlo runner. Returns `MCResult` with percentiles, fan chart, and success probability. |
|
|
54
|
+
| `MCOptions` | `{ runs?, seed?, budgetMs? }` |
|
|
55
|
+
| `MCResult` | `{ probability_no_shortfall, median_terminal, p10_terminal, p90_terminal, fan_chart, ... }` |
|
|
56
|
+
| `ProjectionFn` | Type alias for the projection function signature. |
|
|
57
|
+
|
|
58
|
+
### Sensitivity Analysis
|
|
59
|
+
|
|
60
|
+
| Function / Type | Description |
|
|
61
|
+
|---|---|
|
|
62
|
+
| `runSensitivityAnalysis(scenario, projectionFn)` | Tornado chart analysis across 7 key parameters. Returns `SensitivityFactor[]` sorted by impact. |
|
|
63
|
+
| `SensitivityFactor` | `{ name, label, lowValue, highValue, lowTerminal, highTerminal, spread }` |
|
|
64
|
+
|
|
65
|
+
### Historical Backtest
|
|
66
|
+
|
|
67
|
+
| Function / Type | Description |
|
|
68
|
+
|---|---|
|
|
69
|
+
| `runHistoricalBacktest(scenario, projectionFn)` | Rolling-window backtest using embedded Shiller data (1871-2024). Returns `BacktestResult`. |
|
|
70
|
+
| `BacktestPeriod` | `{ startYear, endYear, terminalReal, survived }` |
|
|
71
|
+
| `BacktestResult` | `{ periods, successRate }` |
|
|
72
|
+
|
|
73
|
+
### Retirement Age Optimizer
|
|
74
|
+
|
|
75
|
+
| Function / Type | Description |
|
|
76
|
+
|---|---|
|
|
77
|
+
| `findEarliestRetirementAge(scenario, projectionFn, mcFn?, options?)` | Linear scan + binary search for earliest viable retirement age and minimum contribution. |
|
|
78
|
+
| `OptimizerResult` | `{ retirementAge, terminalReal, survived, mcSuccessPct }` |
|
|
79
|
+
| `OptimizerOutput` | `{ results, earliestViableAge, minContribution }` |
|
|
80
|
+
| `OptimizerOptions` | `{ mcThreshold? }` |
|
|
81
|
+
|
|
82
|
+
### Heatmap
|
|
83
|
+
|
|
84
|
+
| Function / Type | Description |
|
|
85
|
+
|---|---|
|
|
86
|
+
| `generateHeatmap(scenario, projectionFn, options?)` | Generates a 2D grid of retirement age x annual spending viability cells. |
|
|
87
|
+
| `HeatmapCell` | `{ retirementAge, annualSpending, viable, terminalReal }` |
|
|
88
|
+
| `HeatmapOptions` | `{ retirementAgeRange?, spendingRange?, steps? }` |
|
|
89
|
+
|
|
90
|
+
### Portfolio
|
|
91
|
+
|
|
92
|
+
| Function / Type | Description |
|
|
93
|
+
|---|---|
|
|
94
|
+
| `blendPortfolio(assets)` | Computes weighted-average return, fee, and liquidity across assets. |
|
|
95
|
+
| `calculateEstateValue(endBalance, endDebt, items, years)` | Computes estate value with per-item earmarking. |
|
|
96
|
+
| `BlendedPortfolio` | `{ totalValue, blendedReturn, blendedFee, blendedPerfFee, liquidPct }` |
|
|
97
|
+
|
|
98
|
+
## Engine Modules
|
|
99
|
+
|
|
100
|
+
| Module | Purpose |
|
|
101
|
+
|---|---|
|
|
102
|
+
| `projection.ts` | Basic-mode deterministic projection (contributions, income, withdrawals, fees, taxes, growth) |
|
|
103
|
+
| `advanced.ts` | Advanced-mode 9-step cash waterfall with individual financial items |
|
|
104
|
+
| `monte-carlo.ts` | Seeded PRNG, Box-Muller normal/log-normal return generation, fan chart |
|
|
105
|
+
| `tax.ts` | US progressive brackets, UK progressive with personal allowance taper, Cayman zero, Custom flat |
|
|
106
|
+
| `withdrawal.ts` | Standard, Guyton-Klinger guardrails, Age-Banded spending phases |
|
|
107
|
+
| `sensitivity.ts` | Tornado chart across 7 parameters with clamping guards |
|
|
108
|
+
| `backtest.ts` | 154 years of embedded Shiller real total stock returns |
|
|
109
|
+
| `optimizer.ts` | Linear scan + binary search for earliest viable retirement age |
|
|
110
|
+
| `heatmap.ts` | 2D retirement age x spending grid |
|
|
111
|
+
| `portfolio.ts` | Portfolio blending and estate value |
|
|
112
|
+
|
|
113
|
+
## Edge Cases & Guards
|
|
114
|
+
|
|
115
|
+
- **Near-zero threshold ($100)**: Prevents asymptotic depletion with high withdrawal rates
|
|
116
|
+
- **Box-Muller ln(0) guard**: Clamps u1 to `[1e-10, 1)` to prevent `-Infinity` returns
|
|
117
|
+
- **Return clamp**: Normal distribution returns clamped to `>= -1.0` (can't lose more than 100%)
|
|
118
|
+
- **MC run validation**: Runs must be 0 (disabled) or 100-10,000
|
|
119
|
+
- **Wall-clock budget**: MC and optimizer abort after 50 seconds
|
|
120
|
+
- **RMD divisor guard**: Handles missing/zero divisors gracefully
|
|
121
|
+
- **Age-Banded gaps**: Returns $0 withdrawal with console warning
|
|
122
|
+
- **Sensitivity clamping**: retirement_age stays in `(current_age, end_age)`, percentages >= 0
|
|
123
|
+
- **Amortization zero-rate**: Falls back to `principal / term` to avoid division by zero
|
|
124
|
+
|
|
125
|
+
## License
|
|
126
|
+
|
|
127
|
+
MIT
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advanced Mode Cash Waterfall Engine
|
|
3
|
+
*
|
|
4
|
+
* Tracks individual financial items (cash, investments, properties, loans,
|
|
5
|
+
* salary, pensions, etc.) separately with a cash account as the central
|
|
6
|
+
* reservoir. Implements the 9-step cash waterfall per spec section 14.
|
|
7
|
+
*
|
|
8
|
+
* Resolution order per year:
|
|
9
|
+
* 1. Income phase (salary, pension, SS, rental, cash yield)
|
|
10
|
+
* 2. Mandatory debits (mortgages, loan interest/principal)
|
|
11
|
+
* 3. Investment sales (single/staggered profit-taking)
|
|
12
|
+
* 4. Liquidity events
|
|
13
|
+
* 5. Contributions (pre-retirement)
|
|
14
|
+
* 6. Withdrawal demand (post-retirement)
|
|
15
|
+
* 7. Tax settlement
|
|
16
|
+
* 8. Insolvency check
|
|
17
|
+
* 9. Investment growth (per-item rates, fees, performance fees)
|
|
18
|
+
*/
|
|
19
|
+
import type { Scenario, TimelineRow, Metrics } from './types';
|
|
20
|
+
export declare function runAdvancedProjection(scenario: Scenario, overrideReturns?: number[]): {
|
|
21
|
+
timeline: TimelineRow[];
|
|
22
|
+
metrics: Metrics;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=advanced.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"advanced.d.ts","sourceRoot":"","sources":["../src/advanced.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EACV,QAAQ,EAER,WAAW,EACX,OAAO,EACR,MAAM,SAAS,CAAC;AAwIjB,wBAAgB,qBAAqB,CACnC,QAAQ,EAAE,QAAQ,EAClB,eAAe,CAAC,EAAE,MAAM,EAAE,GACzB;IAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAqrB/C"}
|