SimLab is a Python-based quantitative finance simulation platform built with Flask, NumPy, and Matplotlib. It lets you explore uncertainty, model investment risk, and visualize stochastic processes through Monte Carlo methods โ the same techniques used by banks and hedge funds.
- Geometric Brownian Motion (GBM) random walk engine
- Configurable drift (ฮผ), volatility (ฯ), steps, and paths
- Renders up to 500 simultaneous stochastic paths
- Highlights median path against all simulated trajectories
- Live statistics: Mean, Std, Min, Max, Median, VaR 95%
- Dynamic multi-asset portfolio builder (add/remove assets in the UI)
- Per-asset configuration:
- Name
- Expected annual return (ฮผ)
- Annual volatility (ฯ)
- Portfolio weight
- Real-time weight validation (weights must sum to 1.0)
- Runs up to 10,000 simulations over up to 1,260 trading days
- Two output charts: trajectory paths + return distribution histogram
- Dark terminal-style charts (GitHub-dark palette)
- Portfolio trajectory plot โ all paths + median + mean overlaid
- Histogram of final portfolio values with VaR zone highlighted
- All charts rendered server-side with Matplotlib and returned as base64 PNG
| Metric | Description |
|---|---|
| Mean | Average final portfolio value across all simulations |
| Std Deviation | Spread / dispersion of outcomes |
| VaR 95% | Value at Risk โ only 5% of outcomes are worse than this |
| VaR 99% | Stricter threshold โ only 1% of outcomes fall below |
| CVaR 95% / 99% | Conditional VaR โ average of the worst outcomes beyond VaR |
| Prob. Profit | % of simulations that ended in profit |
| Skewness | Whether returns skew positive or negative |
| Kurtosis | Fat-tail risk (how extreme the outliers are) |
| Min / Max | Worst and best single simulation outcome |
| Layer | Technology |
|---|---|
| Web framework | Flask 3.x |
| Forms & validation | Flask-WTF, WTForms |
| Numerical engine | NumPy |
| Statistical analytics | SciPy |
| Data manipulation | Pandas |
| Charting | Matplotlib |
| Language | Python 3.11+ |
simlab/
โโโ main.py # App entry point
โโโ requirements.txt
โโโ app/
โโโ __init__.py # App factory (create_app)
โโโ config.py # Environment & simulation defaults
โโโ models.py # Asset, Portfolio, SimulationResult dataclasses
โโโ forms.py # WTForms: MonteCarloForm, PortfolioForm
โโโ views.py # Routes: /, /monte-carlo, /portfolio
โโโ auth.py # Auth blueprint (stubs, ready for Flask-Login)
โโโ services/
โ โโโ __init__.py
โ โโโ monte_carlo.py # GBM simulation engine + path chart renderer
โ โโโ portfolio.py # Multi-asset simulator + trajectory & histogram charts
โ โโโ analytics.py # VaR, CVaR, Sharpe, Sortino, max drawdown
โโโ templates/
โโโ layout.html # Base template (dark terminal UI, Space Mono font)
โโโ index.html # Landing page
โโโ monte_carlo.html # Monte Carlo simulation page
โโโ portfolio.html # Portfolio simulator with dynamic asset builder
git clone <your-repo> cd simlab pip install -r requirements.txt python main.py
Open http://localhost:5000
- Navigate to
/monte-carlo - Configure:
- ฮผ (drift) โ expected annual return (e.g.
0.05= 5%) - ฯ (volatility) โ annual volatility (e.g.
0.20= 20%) - Steps โ number of time steps per path (e.g.
252= 1 trading year) - Paths โ number of independent simulations (e.g.
50) - Initial Value โ starting price
- ฮผ (drift) โ expected annual return (e.g.
- Click Run Simulation
- Read the path chart and statistics panel
- Navigate to
/portfolio - Set your initial capital (e.g. $100,000)
- Add assets using the dynamic asset builder:
- Enter name, ฮผ, ฯ, and weight for each asset
- The weight bar turns green when weights sum to 1.0
- Set simulation count and trading days
- Click Run Simulation
- Analyze:
- Portfolio trajectory paths (all simulations + median + mean)
- Return distribution histogram with VaR cutoffs highlighted
- Full risk metrics table (VaR, CVaR, prob. profit, skewness, kurtosis)
Every price path is generated using Geometric Brownian Motion (GBM):
S(t+1) = S(t) ร exp((ฮผ - ยฝฯยฒ)ยทdt + ฯยทโdtยทZ)
Where:
ฮผ= expected annual return (drift)ฯ= annual volatilitydt= time increment (1/252 for daily)Z= random shock drawn from a standard normal distribution
Running this equation 1,000 times in parallel gives you 1,000 different futures โ the full distribution of possible outcomes.
- Real stock price data integration (NSE / Yahoo Finance API)
- Correlated asset modeling (Cholesky decomposition on covariance matrix)
- Portfolio optimization โ efficient frontier (Markowitz)
- Streamlit version for fully interactive UI without Flask
- REST API endpoints for external access
- User authentication with Flask-Login
- Export simulation results to CSV / PDF report
- Backtesting against historical data
- Monte Carlo Simulation
- Geometric Brownian Motion (GBM)
- Stochastic Processes
- Value at Risk (VaR) & Conditional VaR (CVaR)
- Quantitative Risk Modeling
- Portfolio Theory
- Probability Distributions
- Financial Statistics (Skewness, Kurtosis, Sharpe Ratio)
Maswili โ Building quantitative systems with Python โก
"Banks pay quants six figures to run this math. We built it in an afternoon."