Pro Feature. Requires a Pro or Ultra subscription. Get started at api.mathematicalcompany.com
Monte Carlo Simulation
Horizon includes a Monte Carlo simulation engine for stress-testing prediction market portfolios. The entire simulation runs in Rust with a custom PRNG (xoshiro256++), Box-Muller normal generation, and Cholesky decomposition for correlated outcomes. No external dependencies.Monte Carlo simulation answers the question: “Given my current positions and probability estimates, what is the distribution of possible portfolio outcomes?” This helps you understand tail risk (VaR, CVaR), win probability, and the impact of correlation between positions.
Overview
Fast Simulation
50,000 scenarios in milliseconds. All math runs in Rust with zero Python overhead.
Correlated Outcomes
Model dependencies between markets via correlation matrices. Uses Cholesky decomposition.
Full Risk Stats
VaR (95/99), CVaR, win probability, max loss/gain, percentile distribution, and every scenario PnL.
Deterministic Seeds
Reproducible results with seed parameter. Same seed = identical output every time.
Quick Start
SimPosition
Represents a position for simulation input.
The
current_price is used as the true probability for the Bernoulli draw:
- Yes position: wins
size * (1 - entry_price)if outcome = 1, losessize * entry_priceif outcome = 0 - No position: wins
size * entry_priceif outcome = 0, losessize * (1 - entry_price)if outcome = 1
SimulationResult
The result object contains full risk statistics.Core Functions
hz.monte_carlo
Run the Monte Carlo simulation directly (Rust function).hz.simulate
Python wrapper with ergonomic features.engine is passed, positions are extracted from engine.positions() and current prices from engine.all_feed_snapshots().
Correlation dict: Instead of building a full NxN matrix, pass a dict of pairwise correlations:
("a", "b") and ("b", "a") work).
Examples
Portfolio Risk Assessment
Simulate from Live Engine
Comparing Correlated vs Uncorrelated Risk
Seed Determinism
Mathematical Background
Binary Outcome PnL
Binary Outcome PnL
Each position is a binary contract. If the event resolves YES (outcome = 1):
- YES position PnL =
size * (1 - entry_price) - NO position PnL =
size * (-(1 - entry_price))
- YES position PnL =
size * (-entry_price) - NO position PnL =
size * entry_price
current_price is the probability of outcome = 1 in each simulation draw.VaR and CVaR
VaR and CVaR
- VaR 95: The 5th percentile of the PnL distribution. “With 95% confidence, losses will not exceed this amount.”
- VaR 99: The 1st percentile. More conservative.
- CVaR 95 (Expected Shortfall): The average PnL of the worst 5% of scenarios. Always ≤ VaR 95.