Monte Carlo · VaR / CVaR · Option pricing · CPU optimization
Simulate correlated multi-asset portfolio P&L, compute Value-at-Risk and Conditional VaR, and price options by Monte Carlo against an analytic Black-Scholes check. The hot loop is written four ways so the optimization is measured, not asserted.
↑ Animation paced from the project's real measured CPU times (best of 7 runs).
01 — The engine
Cholesky factor of the correlation matrix drives correlated normal draws; simulated correlation matches the target to atol=0.01.
Empirical Value-at-Risk and Conditional VaR (Expected Shortfall), cross-checked against the closed-form normal quantile and ES.
European call by Monte Carlo with antithetic variates, validated against the analytic Black-Scholes value.
Arithmetic-normal or GBM returns over a horizon, aggregated across long/short holdings into scenario P&L.
15 pytest tests: convergence to Black-Scholes, analytic VaR/CVaR match, backend agreement, correlation recovery.
A CuPy backend slots into the same interface — runs on-device where a GPU exists, falls back to NumPy here, never fakes a number.
02 — Measured speedups
A path-dependent Asian (average-price) call, 50 steps/path, 2,000,000 paths, 16 logical CPUs. All four backends agree on the price (~5.86), so the speedup is genuine. Speedup is relative to the naive pure-Python loop (mean of 7 runs).
All values are real measurements on this machine. Multiprocessing shows run-to-run variance on Windows; its best-case reaches ~40×.
03 — Risk numbers
Single normal asset (holding €1,000,000, 2% daily vol, 1,000,000 paths): Monte Carlo VaR/CVaR vs the closed-form normal values.
| Confidence | VaR (MC) | VaR (analytic) | CVaR (MC) | CVaR (analytic) |
|---|---|---|---|---|
| 95% | 32,879 | 32,897 | 41,268 | 41,254 |
| 99% | 46,547 | 46,527 | 53,310 | 53,304 |
04 — Stack