Skip to content

shivrajld/qflib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

qflib

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.

Features

  • 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

Repository Layout

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

Dependencies

  • 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 numpy

Build

From the repository root:

cmake -S . -B build
cmake --build build

The build creates:

  • lib/libqflib.a, the static C++ library
  • pyqflib/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())"

Python Example

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}")

Design Notes

  • 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.

Example Notebooks

  • examples/Python/qflib-examples.ipynb walks through the main pricing and numerical workflows.
  • examples/Python/DeltaHedging.ipynb studies discrete delta hedging behavior across product types, volatility assumptions, and hedge frequencies.

License

No open source license has been applied to this repository.

About

C++ quantitative finance library for derivatives pricing with Monte Carlo, PDE solvers, and Python bindings

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors