qflib is a C++ quantitative finance library for pricing derivatives with analytic methods, Monte Carlo simulation, and finite difference PDE solvers. The library also includes Python bindings, NumPy interop, and notebook examples for testing pricing workflows from Python.
The project focuses on Black Scholes style models, market data objects, payoff definitions, numerical methods, and pricing routines that can be used from either C++ or Python.
- Analytic pricing for European options, digital options, forwards, caplets, floorlets, and CDS present value
- Monte Carlo pricing with Euler path generation, antithetic variance reduction, multi asset correlation, and configurable statistics collectors
- One dimensional PDE pricing with Crank Nicolson stepping, log spot transforms, tridiagonal operators, and grid interpolation
- Delta hedging simulation with separate realized and marking volatility inputs
- Market data objects for yield curves, volatility term structures, and named market storage
- Numerical tools for linear algebra, random number generation, interpolation, root finding, normal CDFs, and inverse CDFs
- Python interface through a CPython extension module and a higher level wrapper package
- Jupyter examples covering pricing, curves, Monte Carlo convergence, PDE surfaces, and hedging behavior
qflib/
qflib/ Core C++ library
math/ Linear algebra, stats, RNG, interpolation, roots
market/ Yield curves, volatility curves, market container
products/ Payoff definitions
methods/ Monte Carlo and PDE engines
pricers/ Analytic, Monte Carlo, PDE, and hedge pricers
pyqflib/ Python extension and wrapper package
pymodule.cpp CPython module registration
pyfunctions0-4.hpp Native Python callable implementations
pycpp.hpp Python and NumPy conversion helpers
pyutils.hpp qflib conversion helpers
qflib/__init__.py Public Python API
examples/Python/ Python scripts and notebooks
CMakeLists.txt Build configuration
ReleaseNotes.md Version history
- C++17 compiler
- CMake 3.20 or newer
- Armadillo
- BLAS and LAPACK
- Python 3.8 or newer
- NumPy
On macOS with Homebrew:
brew install cmake armadillo
python3 -m pip install numpyFrom the repository root:
cmake -S . -B build
cmake --build buildThe build creates:
lib/libqflib.a, the static C++ librarypyqflib/qflib/pyqflib.so, the Python extension module
To use the Python wrapper from the repository root:
PYTHONPATH=pyqflib python3 -c "import qflib as qf; print(qf.version())"import numpy as np
import qflib as qf
qf.ycCreate("USD", np.array([1.0]), np.array([0.04]), 0)
greeks = qf.euroBS(
payofftype=1,
spot=100.0,
strike=100.0,
timetoexp=1.0,
intrate=0.04,
divyield=0.02,
volatility=0.30,
)
print(f"price={greeks[0]:.4f}, delta={greeks[1]:.4f}")- The C++ library keeps products, market data, numerical methods, and pricers separate so pricing workflows can be composed without duplicating model setup.
- Monte Carlo statistics are collected through a common calculator interface, which allows mean variance and histogram calculations to run through the same path processing code.
- The delta hedger separates the simulated volatility from the volatility used for marking, which makes volatility mismatch visible in the hedge PnL distribution.
- The interpolation and root finding utilities are used directly by the curve and pricing layers instead of being isolated demo code.
- The Python API is intentionally thin. Most numerical work stays in C++, while Python is used for setup, notebooks, and analysis.
examples/Python/qflib-examples.ipynbwalks through the main pricing and numerical workflows.examples/Python/DeltaHedging.ipynbstudies discrete delta hedging behavior across product types, volatility assumptions, and hedge frequencies.
No open source license has been applied to this repository.