XAD documentation

QuantLibAAD for Python

XAD is open source under AGPL-3.0, to try, for research and for open source. Using it in production? See when a subscription applies.

The Python package QuantLibAAD is available on PyPI. It integrates automatic differentiation capabilities into QuantLib through its dependency on XAD's Python bindings. This integration significantly boosts the efficiency of performing high-performance risk assessments within QuantLib from Python.

The key advantage brought by QuantLibAAD is its ability to expediently ascertain how the pricing of derivatives is influenced by various input variables, notably market quotes.

The user interface mimics the official QuantLib Python package, with the addition of replacing its Real type with xad.adj_1st.Real, which can be tracked on an adjoint automatic differentiation tape and derivatives can be calculated with xad. QuantLib and the xad package use the same XAD library, so they share one active tape per thread (see Version 1.44).

Installation

pip install QuantLibAAD

Wheels are available for Python 3.10 to 3.14 on Linux, macOS and Windows. QuantLibAAD 1.44 requires xad 2.2 (xad>=2.2,<2.3), which pip installs with it.

This package was called QuantLib-Risks up to version 1.33.3. From 1.44, QuantLib-Risks is a deprecated alias that installs QuantLibAAD and keeps import QuantLib_Risks working with a DeprecationWarning. Please switch to pip install QuantLibAAD and import QuantLibAAD. Anyone pinned to QuantLib-Risks==1.33.3 must also pin xad<2.

Version 1.44

QuantLibAAD 1.44 is built on QuantLib 1.44 and its Python bindings.

  • One tape with xad: QuantLib's calculations and your own xad code record on the same xad.adj_1st.Tape, one active tape per Python thread.
  • More accurate sensitivities: a few finite-difference cases now get the right derivatives, with fixes taken ahead of QuantLib 1.45.
  • Same values as without AD: prices computed with AD match a plain-double build of QuantLib with the same compiler settings.
  • API changes: classes QuantLib removed in 1.42 to 1.44 are gone here too; the changelog lists them.

Usage Illustration

import QuantLibAAD as ql
from xad.adj_1st import Tape
with Tape() as tape:
    rate = ql.Real(0.2)
    tape.registerInput(rate)
    tape.newRecording()
    # quantlib pricing code, setting up an option
    quote = ql.SimpleQuote(rate)
    ...
    npv = option.NPV()
    tape.registerOutput(npv)
    npv.derivative = 1.0
    tape.computeAdjoints()
    print(f"price = {npv}")
    print(f"delta = {rate.derivative}")

More examples, such as sensitivities of a swap to its curve quotes, are in the examples folder.

Questions or issues: GitHub Discussions · issues. Would you rather we AAD-enabled your code? Get a free speed-up estimate.