feat: Monte Carlo Simulation & Goal Attainment Probability Module - #385
Merged
robertocarlous merged 1 commit intoAug 28, 2026
Merged
Conversation
…Neurowealth#319) Implement a pure, zero-I/O Monte Carlo simulation engine that turns goal feasibility and backtest results into probability distributions and confidence intervals. Two documented sampling modes: - Historical bootstrap: resamples from observed daily rate changes with no distributional assumptions imposed - Parametric lognormal: fits mean/σ to returns and draws from a lognormal model (assumption documented) Adds POST /api/v1/goals/:id/simulate endpoint (owner-scoped) that returns: - Attainment probability (fraction of paths crossing the target) - Terminal value / drawdown / APY percentile bands (p5/p50/p95) - Required-rate sensitivity table ("at X% APY you have Y% chance") - Convergence diagnostics (converged flag, recommended iterations) - isSimulation disclaimer on every response Key design decisions: - Pure core (src/analytics/montecarlo.ts) with no stellar/db imports, enforced by structural guarantee tests - I/O service layer (src/goals/simulation.ts) handles strategy resolution, rate loading, and Redis caching - Reuses the same daily accrual + rebalance decision loop as the backtest engine (same BacktestRequest/StrategyParams shapes) - Seeded PRNG (mulberry32) for reproducible results - Redis cache keyed on canonical input hash (5-min TTL) - Concurrency limiter protects against CPU abuse Closes Neurowealth#319 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a Monte Carlo simulation module that turns goal feasibility into probability distributions and confidence intervals, replacing the single deterministic "required APY" line with honest uncertainty quantification.
Closes #319
What's New
Pure Monte Carlo Core (
src/analytics/montecarlo.ts)src/stellarimports — pure computation, unit-testable in isolationconvergedflag,recommendedIterations,effectiveSampleSize— never presents a noisy 1,000-path answer as factGoal Simulation Endpoint (
POST /api/v1/goals/:id/simulate)attainmentProbability— fraction of paths that crossed the target by the target datesensitivityTable— "at X% APY you have Y% chance" across the feasible rate rangeconvergencediagnosticsisSimulation: true+ model disclaimer on every responseinsufficient_historyreturned explicitly (not a guessed probability)Simulation Service (
src/goals/simulation.ts)GoalNotFoundError,GoalValidationError,InsufficientHistoryErrorFiles Changed
src/analytics/montecarlo.tssrc/goals/simulation.tssrc/controllers/goal-simulation-controller.tssrc/validators/simulation-validators.tssrc/routes/goals.tsPOST /:id/simulateroutetests/unit/analytics/montecarlo.test.tsAcceptance Criteria Checklist
converged: false)POST /api/v1/goals/:id/simulatereturns probability + sensitivity table +isSimulationdisclaimerinsufficient_historyhandled withearliestAvailableDatesrc/stellarimports in core)Design Decisions
montecarlo.ts) is import-graph-safe — it can never touch real funds. All I/O (DB, Redis, strategy resolution) lives insimulation.ts.BacktestRequest/StrategyParamsshapes reused). A Monte Carlo run is a distribution of backtests, not a parallel implementation.converged: false,effectiveSampleSize, andinsufficient_historyare explicit output states — never hidden or faked. Follows the same "stated assumption beats hidden default" pattern asriskFreeRate.src/routes/backtest.tsnote: This file contains a duplicate of the engine code (not mounted as an Express router). The backtest?simulate=trueextension from Monte Carlo Simulation & Goal Attainment Probability Module #319 would require first converting it to a proper router — tracked as a follow-up.Testing
🤖 Generated with Codebuff
Co-Authored-By: Codebuff noreply@codebuff.com