Skip to main content
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, loses size * entry_price if outcome = 0
  • No position: wins size * entry_price if outcome = 0, loses size * (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.
The wrapper provides two key conveniences: Auto-extraction from Engine: If 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:
The wrapper builds the full correlation matrix automatically, including handling reverse key order (both ("a", "b") and ("b", "a") work).

Examples

Portfolio Risk Assessment

Simulate from Live Engine

Comparing Correlated vs Uncorrelated Risk

Seed Determinism


Mathematical Background

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))
If the event resolves NO (outcome = 0):
  • YES position PnL = size * (-entry_price)
  • NO position PnL = size * entry_price
The current_price is the probability of outcome = 1 in each simulation draw.
To model correlated binary outcomes:
  1. Draw N standard normal variables (Box-Muller transform)
  2. Apply Cholesky decomposition of the correlation matrix to induce correlations
  3. Convert correlated normals to uniform via the normal CDF
  4. Threshold each uniform against the position’s current_price to produce a Bernoulli outcome
This produces correlated binary outcomes that respect the specified correlation structure while maintaining the correct marginal probabilities.
  • 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.

Bayesian Optimization

Find optimal strategy parameters using a Gaussian Process surrogate with Expected Improvement acquisition.
Zero external dependencies. Uses RBF kernel GP with Cholesky decomposition.
Monte Carlo simulation assumes current_price represents the true probability. If your price estimates are poor, the simulation results will be misleading. Consider running simulations across a range of probability assumptions to understand sensitivity.