PyStochasticVolatility is a collection of numerical tools that support the book Malliavin Calculus and Stochastic Volatility. The library focuses on implementing stochastic-volatility (SV) models and pricing routines that are useful for both research and production prototyping. It bundles analytical pricers, Monte Carlo engines, utilities for building volatility surfaces, and worked examples illustrating end-to-end workflows.
- Key features
- Installation
- Quick start
- Project structure
- Module overview
- Development
- Support & contributions
- Fourier-based analytic pricers (COS and Fourier inversion) for models with known characteristic functions such as Heston and Merton.
- Monte Carlo pricing engines, variance reduction techniques, and reusable instrument definitions that decouple payoff logic from model dynamics.
- Dedicated rough Bergomi workflows that combine fractional Brownian motion sampling, forward variance curve handling.
- Tools for sampling fractional Brownian motion and building volatility surfaces with SVI and SABR parameterisations.
- Utilities written with
numbato accelerate critical numerical kernels. - Reproducible numerical experiments that accompany each chapter of the book.
PyStochasticVolatility targets Python 3.8 and newer. The recommended workflow uses uv, but any PEP 517 compliant build tool is supported.
# Create and activate a virtual environment (optional but recommended)
uv venv
source .venv/bin/activate
# Install the project in editable mode together with its runtime dependencies
uv pip install -e .If you prefer pip, install the package directly from the repository root:
python -m pip install -e .The main runtime dependencies include:
- numpy
- numba
- scipy
- matplotlib
- pandas
- statsmodels
- quantlib
- tabulate / prettytable
- sympy
- ncephes
Refer to pyproject.toml for the authoritative list of
constraints.
- Install the project following the instructions above.
- Explore the notebooks and scripts in
Examples/to reproduce the numerical experiments from the book. - Use the Monte Carlo engines from
MC_Engines/together with the instruments inInstruments/to price custom payoffs. - Build or calibrate a volatility surface with the tools under
VolatilitySurface/. - Experiment with the rough Bergomi engines in
MC_Engines/MC_RBergomi/by simulating forward variance curves, Malliavin weights, and joint spot/variance paths.
A minimal script that prices a European option under the Heston model using the COS method could look like:
from AnalyticEngines.COS import HestonCOSPricer
from Instruments.options import EuropeanCall
instrument = EuropeanCall(strike=100.0, maturity=1.0)
model_params = {
"kappa": 1.5,
"theta": 0.04,
"sigma": 0.3,
"rho": -0.6,
"v0": 0.04,
}
price = HestonCOSPricer().price(spot=100.0, rate=0.01, div=0.0,
instrument=instrument,
params=model_params)
print(f"Option price: {price:.4f}")PyStochasticVolatility/
├── AnalyticEngines/ # Fourier-based pricing routines and characteristic functions
├── Examples/ # Reproducible experiments and tutorials
├── FractionalBrownian/ # Fractional Brownian motion sampling utilities
├── Instruments/ # Payoff definitions reusable across models
├── MCPricers/ # Monte Carlo pricing logic decoupled from engines
├── MC_Engines/ # Simulation engines for stochastic processes
├── Solvers/ # PDE solver utilities for local volatility models
├── Tools/ # Shared helpers (random numbers, numba kernels, ...)
└── VolatilitySurface/ # Construction and calibration of volatility surfaces
Fourier-inversion and COS methods that operate on characteristic functions. The module provides implementations for classical models such as Heston and Merton alongside Malliavin-based approximations for implied volatility and variance products.
Notebooks and scripts that replicate the experiments presented in each chapter of the book, serving as practical guides for using the library.
Sampling algorithms for both full and truncated fractional Brownian motion, including helper routines for parameter calibration.
A catalogue of model-agnostic instruments (European options, variance swaps, volatility swaps, etc.) designed to interface seamlessly with either analytic or Monte Carlo pricing engines.
Reusable pricing logic for Monte Carlo simulations. These pricers consume an instrument and a simulated path bundle, separating payoff evaluation from the choice of engine.
Simulation back-ends for a variety of stochastic processes under stochastic volatility dynamics. Engines can be combined with the pricers above to perform scenario analysis or pricing.
The rough Bergomi implementation exposes several building blocks:
- Discretisation of the Volterra kernel and hybrid scheme to simulate the rough variance process driven by fractional Brownian motion.
- Utilities for constructing forward variance curves and calibrating the volatility-of-volatility parameter from market quotes.
- Monte Carlo pricers that couple spot and variance simulations, enabling the valuation of variance swaps, options on realised variance, and equity derivatives with rough volatility dynamics.
- Optional Malliavin weight computation to perform pathwise sensitivity analysis with respect to model parameters.
A robust one-dimensional PDE solver tailored for local volatility models. The solver supports explicit, implicit, and theta schemes with configurable boundary conditions.
Shared utilities such as random number generators, numba-accelerated helper
functions, and general-purpose numerical routines reused throughout the
repository.
Components for constructing and calibrating volatility term structures. Current implementations include SVI and SABR parameterisations.
The entire development workflow is designed to work smoothly with
uv. The tool handles virtual environment
management, dependency resolution, and editable installs without requiring
separate pip or virtualenv steps.
- Install development dependencies:
or use the
uv pip install -e .[dev]
requirements.txtfile if you prefer classicpipworkflows. - Run your preferred test or lint suite. (A comprehensive test harness is not yet bundled with the project.)
- Format contributions using the conventions already present in the codebase.
Issues and pull requests are welcome on the GitHub repository. If you use the library in academic work, please consider citing the book Malliavin Calculus and Stochastic Volatility and referencing this repository.
The project is distributed under the Apache 2.0 License. Individual source files contain the relevant license headers.