Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QuantSimLab Logo

QuantSimLab: GPU-Accelerated Stochastic Market Environment

Python 3.12+ License: MIT PyTorch CUDA

Abstract

QuantSimLab is a high-fidelity GPU-accelerated stochastic market simulation platform designed to generate synthetic financial data for training Reinforcement Learning (RL) agents and Graph Neural Networks (GNNs). Unlike traditional backtesting frameworks constrained by a single historical path, QuantSimLab constructs a Digital Twin of financial markets capable of producing massive-scale, mathematically rigorous datasets across counterfactual scenarios.

The platform is grounded in rigorous financial mathematics, implementing analytical solutions to Stochastic Differential Equations (SDEs) including Geometric Brownian Motion (GBM), Ornstein-Uhlenbeck (OU) processes, and multi-factor risk models. Leveraging NVIDIA GPU hardware (optimized for RTX 5090/Blackwell architecture), QuantSimLab generates high-frequency market data at scales previously computationally prohibitive, enabling researchers to stress-test algorithmic strategies under diverse market regimes before deployment.

For Chinese researchers, you may refer to the document 高保真合成市场环境仿真的理论设计与架构规范 in the docs directory.

Theoretical Foundations

QuantSimLab bridges the "Sim-to-Real" gap in quantitative finance by implementing mathematically sound models that capture the essential physics of market dynamics:

1. Geometric Brownian Motion (GBM)

The foundation for directional price movements, implementing the Black-Scholes SDE:

$$dS_t = \mu S_t dt + \sigma S_t dW_t$$

We utilize Itô's Lemma to solve this analytically, preventing discretization errors:

$$S_t = S_0 \exp\left( (\mu - 0.5\sigma^2)t + \sigma W_t \right)$$

Key Insight: In the short-term limit ($t \to 0$), the noise term ($\sigma \sqrt{t}$) dominates drift ($\mu t$), validating that active trading fundamentally manages variance, not just directional prediction.

2. Ornstein-Uhlenbeck Process (Mean Reversion)

For statistical arbitrage and pairs trading, we implement the OU process with a physical "restoring force":

$$dX_t = \theta (\mu - X_t) dt + \sigma dW_t$$

  • $\theta$ (Theta): Mean reversion speed—the governing parameter for trade profitability
  • Dynamic Equilibrium: Optional "wandering equilibrium" ($M_t$) to distinguish noise (opportunity) from regime shift (trap)

The half-life of mean reversion is analytically determined: $T_{1/2} = \frac{\ln(2)}{\theta}$

3. Factor Risk Budgeting

To generate realistic portfolios of 50+ assets, we implement a Block-Diagonal Factor Model:

$$R_i = \beta_{mkt} F_{global} + \beta_{sec} F_{sector} + \epsilon_i$$

This creates natural correlation clusters (sectors) and tail risks (log-normal volatility), essential for training agents on sector rotation and risk parity strategies.

4. Signal-to-Noise Analysis

A critical contribution of this framework is the mathematical proof that standard GBM is unsuitable for high-frequency directional agents. The Signal-to-Noise Ratio (SNR) vanishes as $\Delta t \to 0$:

$$\text{SNR}(\Delta t) = \frac{|\mu|}{\sigma} \sqrt{\Delta t} \xrightarrow{\Delta t \to 0} 0$$

This theoretical result explains why agents trained on naive GBM simulations "hallucinate" patterns that fail in live markets. Our implementation addresses this through regime-switching models and microstructure noise.

Architecture

QuantSim follows a Data-Centric AI architecture optimized for NVIDIA GPUs:

┌─────────────────────────────────────────────────────────┐
│                  Streamlit Dashboard                    │
│               (Interactive Control Panel)               │
└─────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────┐
│              Simulation Configuration                   │
│   (Frequency, Horizon, Paths, Device Selection)         │
└─────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────┐
│               Physics Engine (GPU)                      │
│  ┌─────────────┬──────────────┬──────────────────────┐ │
│  │ Standard GBM│  OU Process  │  Portfolio Factor    │ │
│  │   Module    │    Module    │  Model Module        │ │
│  └─────────────┴──────────────┴──────────────────────┘ │
└─────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────┐
│         PyTorch GPU Tensors (In-Memory)                 │
│    [n_sims × n_steps × n_assets] ∈ ℝ^(P×T×N)           │
└─────────────────────────────────────────────────────────┘
                          │
                          ▼
┌─────────────────────────────────────────────────────────┐
│        Visualization & Export                           │
│  (Plotly WebGL, Polars DataFrames, PyArrow)             │
└─────────────────────────────────────────────────────────┘

Engineering Challenges Solved

Challenge Solution
VRAM OOM torch.no_grad() + in-place operations + VRAM guard (pre-calculates memory cost)
Hardware Selection Auto-detect optimal GPU with max VRAM (_get_optimal_device())
I/O Latency WSL2 (Ubuntu) with Ext4 filesystem + uv package manager
Browser Crash Plotly WebGL (Scattergl) + CPU-side downsampling (5k points/line)
Unrealistic Correlations Block-diagonal covariance matrices via factor models

Installation

Prerequisites

  • Python: 3.12 or higher
  • GPU (Optional but recommended): NVIDIA GPU with CUDA 12.8+ and ≥8GB VRAM
  • OS: Linux (native or WSL2), macOS, or Windows

Using uv (Recommended)

# Install uv package manager
curl -LsSf https://astral.sh/uv/install.sh | sh

# Clone the repository
git clone https://github.com/richwu/quantsim_project.git
cd quantsim

# Create virtual environment and install dependencies
uv sync

# Activate the environment
source .venv/bin/activate  # Linux/macOS
# or
.venv\Scripts\activate     # Windows

Using pip

# Clone the repository
git clone https://github.com/richwu/quantsim_project.git
cd quantsim

# Create and activate virtual environment
python -m venv .venv
source .venv/bin/activate  # Linux/macOS
# or
.venv\Scripts\activate     # Windows

# Install package
pip install -e .

Quick Start

Basic Usage (Python API)

import torch
from quantsim.config import SimulationConfig
from quantsim.physics.standard import MultivariateGBM

# Configure simulation: 1-minute bars, 1 year, 5000 Monte Carlo paths
cfg = SimulationConfig(
    t_horizon=1.0,      # Years
    freq='1min',        # Frequency: '1min', '5min', '1h', '1d'
    n_sims=5000,        # Monte Carlo paths
    device='cuda'       # or 'cpu'
)

# Audit VRAM requirement
mem_gb = cfg.audit_memory(n_assets=2)
print(f"Required VRAM: {mem_gb:.2f} GB")

# Define market parameters
mu = torch.tensor([0.05, 0.05], device=cfg.device)      # Annual drift
sigma = torch.tensor([0.20, 0.20], device=cfg.device)   # Volatility
corr = torch.tensor([[1.0, 0.8], [0.8, 1.0]], device=cfg.device)

# Initialize model
model = MultivariateGBM(cfg, mu, sigma, corr)

# Generate synthetic data
s0 = torch.tensor([100.0, 100.0], device=cfg.device)
with torch.no_grad():
    results = model(s0)

print(f"Output shape: {results['price'].shape}")  # [5000, T, 2]

Interactive Dashboard

Launch the Streamlit dashboard for visual exploration:

streamlit run quantsim/vis/dashboard.py

Navigate to http://localhost:8501 to access:

  • Hardware Monitor: Real-time VRAM usage
  • Simulation Modes: GBM, OU Pairs, Portfolio Basket
  • Visualization: Interactive charts with WebGL acceleration
  • Parameter Controls: Adjust $\mu$, $\sigma$, $\theta$, correlation structures

Simulation Modes

Mode A: Standard Market (GBM)

Use Case: Trend following, momentum strategies, vanilla options pricing

  • Drift ($\mu$): Annualized expected return (e.g., 0.05 = 5% bull market, -0.20 = crash)
  • Volatility ($\sigma$): Annual volatility (0.15 = stable blue-chip, 0.80 = meme stock)
  • Correlation ($\rho$): Asset correlation (0.95 = lockstep, 0.0 = independent, -0.8 = hedge)

Mode B: Pairs Spread (Ornstein-Uhlenbeck)

Use Case: Statistical arbitrage, convergence trading

  • Theta ($\theta$): Reversion speed (20+ = HFT arbitrage, <2 = weak convergence)
  • Spread Volatility: Noise around mean (affects Bollinger band width)
  • Dynamic Equilibrium: Wandering mean to test regime detection ($\sigma_\mu &gt; 0.1$ = "real world mode")

Mode C: Portfolio Basket (Multi-Factor)

Use Case: Portfolio optimization, risk parity, sector rotation

  • Number of Assets: Universe size (50 for sector ETF, 500+ for index)
  • Volatility Range: Log-normal distribution (0.10–0.40 typical)
  • Market Correlation Factor: Systemic risk (0.9 = crisis, 0.2 = stock picker's market)
  • Correlation Heatmap: Visual validation of sector clustering

Hardware Requirements

Minimum

  • CPU: Modern multi-core processor
  • RAM: 8GB
  • GPU: Optional (falls back to CPU)

Recommended

  • CPU: Intel i7/AMD Ryzen 7 or higher
  • RAM: 16GB+
  • GPU: NVIDIA RTX 3080 (10GB VRAM) or higher
  • Storage: SSD recommended for data I/O

Optimal (Research-Grade)

  • GPU: NVIDIA RTX 5090 (24GB VRAM) or A100
  • RAM: 32GB+
  • OS: Linux (Ubuntu 22.04+) or WSL2 for maximum performance

VRAM Estimation

Configuration Assets Paths Horizon Frequency VRAM
Low 2 1,000 0.1 yr 1h ~0.5 GB
Medium 10 5,000 1.0 yr 5min ~8 GB
High 50 10,000 5.0 yr 1min ~20 GB

Documentation

Technical Reports

Workflow for Strategy Development

  1. Hypothesis: "Train an agent to trade Tech vs Energy divergence"
  2. Setup: Mode C, 50 assets, correlation factor 0.4 (distinct sectors)
  3. Generate: Execute simulation
  4. Validate: Inspect correlation heatmap for sector clustering
  5. Export: Save tensor to disk (PyArrow/Parquet)
  6. Train: Feed synthetic history to RL/GNN model
  7. Stress Test: Increase correlation to 0.9 (crash mode) and validate robustness

Citation

If you use QuantSimLab in your research, please cite:

@software{quantsimlab2025,
  author = {Richard Wu},
  title = {QuantSimLab: GPU-Accelerated Stochastic Market Environment for Algorithmic Trading Research},
  year = {2025},
  publisher = {GitHub},
  url = {https://github.com/richwu/quantsimlab},
  version = {0.1.0},
  email = {gwu188@gmail.com}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

We welcome contributions from the quantitative finance and machine learning research community! Please see CONTRIBUTING.md for guidelines on:

  • Code style (Ruff, Black, MyPy)
  • Testing standards (pytest)
  • Pull request process
  • Issue reporting

Acknowledgments

  • Built on PyTorch for GPU acceleration
  • Visualization powered by Plotly and Streamlit
  • Data structures optimized with Polars and PyArrow
  • Mathematical foundations derived from Black-Scholes, Itô calculus, and modern stochastic control theory


Disclaimer: This software is for research and educational purposes only. It does not constitute financial advice. Trading in financial markets involves substantial risk of loss.

About

quantsim_project is a high-fidelity GPU-accelerated stochastic market simulation platform designed to generate synthetic financial data for training Reinforcement Learning (RL) agents and Graph Neural Networks (GNNs).

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages