Michiel Nys

derivkit

A Python package for pricing European options with FFT, calibrating Black-Scholes and Heston to market prices, and Monte Carlo for European options and variance swaps.

Python · NumPy · SciPy · pandas   code on GitHub

Problem

Market prices of European calls are quoted for a grid of strikes and maturities. A pricing model is only useful once its parameters reproduce those prices. Calibration does exactly that: it searches for the parameters that minimise the distance between model and market prices.

Each step of that search prices the whole surface again, so the pricer has to be fast. For models without a closed-form price, such as Heston, the standard answer is to price in Fourier space with the characteristic function of the log price. derivkit is built around that idea. Each model is one class that calibrates (fit), prices with FFT (predict) and simulates paths for Monte Carlo (simulate), all with the same parameters.

Models

Black-Scholes

The stock follows a geometric Brownian motion with constant volatility σ\sigma, interest rate rr and dividend yield qq:

dStSt=(r−q) dt+σ dWt.\frac{dS_t}{S_t} = (r - q)\,dt + \sigma\,dW_t .

The characteristic function of log⁡ST\log S_T is

ϕT(u)=exp⁡ ⁣(iu(log⁡S0+(r−q−12σ2)T)−12σ2u2T).\phi_T(u) = \exp\!\Big( iu\big(\log S_0 + (r - q - \tfrac{1}{2}\sigma^2)T\big) - \tfrac{1}{2}\sigma^2 u^2 T \Big).

Heston

The variance VtV_t is itself random. It reverts to a long-run level η\eta at speed κ\kappa, has volatility of variance θ\theta, and its shocks are correlated with those of the stock:

dStSt=(r−q) dt+Vt dWtS,dVt=κ(η−Vt) dt+θVt dWtV,d⟨WS,WV⟩t=ρ dt.\begin{aligned} \frac{dS_t}{S_t} &= (r - q)\,dt + \sqrt{V_t}\,dW^S_t, \\ dV_t &= \kappa(\eta - V_t)\,dt + \theta\sqrt{V_t}\,dW^V_t, \qquad d\langle W^S, W^V\rangle_t = \rho\,dt . \end{aligned}

There are five parameters, κ,θ,η,ρ\kappa, \theta, \eta, \rho and the starting variance V0V_0. Calibration only accepts parameters that satisfy the Feller condition 2κη>θ22\kappa\eta > \theta^2, which keeps the variance away from zero. With

d=(ρθiu−κ)2+θ2(iu+u2),g=κ−ρθiu−dκ−ρθiu+d,d = \sqrt{(\rho\theta iu - \kappa)^2 + \theta^2(iu + u^2)}, \qquad g = \frac{\kappa - \rho\theta iu - d}{\kappa - \rho\theta iu + d},

the characteristic function of log⁡ST\log S_T is

ϕT(u)=exp⁡(iu(log⁡S0+(r−q)T)+κηθ2[(κ−ρθiu−d)T−2log⁡1−ge−dT1−g]+V0θ2 (κ−ρθiu−d) 1−e−dT1−ge−dT).\begin{aligned} \phi_T(u) = \exp\Big(& iu\big(\log S_0 + (r - q)T\big) + \frac{\kappa\eta}{\theta^2}\Big[(\kappa - \rho\theta iu - d)T - 2\log\frac{1 - g e^{-dT}}{1 - g}\Big] \\ &+ \frac{V_0}{\theta^2}\,(\kappa - \rho\theta iu - d)\,\frac{1 - e^{-dT}}{1 - g e^{-dT}} \Big). \end{aligned}

Pricing with the FFT

Carr and Madan (1999) price a call through the Fourier transform of its price as a function of the log strike k=log⁡Kk = \log K. The call price itself is not integrable in kk, so it is first damped with a factor eαke^{\alpha k}, α>0\alpha > 0. The transform of the damped price has a closed form in terms of ϕT\phi_T:

ψT(v)=e−rT ϕT(v−(α+1)i)α2+α−v2+i(2α+1)v,\psi_T(v) = \frac{e^{-rT}\,\phi_T\big(v - (\alpha + 1)i\big)}{\alpha^2 + \alpha - v^2 + i(2\alpha + 1)v},

and the call price follows by inverting it:

CT(k)=e−αkπ∫0∞e−ivk ψT(v) dv.C_T(k) = \frac{e^{-\alpha k}}{\pi} \int_0^\infty e^{-ivk}\,\psi_T(v)\,dv .

The integral is discretised on NN points vj=(j−1)Δvv_j = (j - 1)\Delta v with Simpson weights. Choosing the log strike grid ku=−b+λ(u−1)k_u = -b + \lambda(u - 1) with λ Δv=2π/N\lambda\,\Delta v = 2\pi / N and b=Nλ/2b = N\lambda / 2 turns the sum into a discrete Fourier transform:

CT(ku)≈e−αkuπ∑j=1Ne−i2πN(j−1)(u−1) eibvj ψT(vj) Δv3(3+(−1)j−δj1).C_T(k_u) \approx \frac{e^{-\alpha k_u}}{\pi} \sum_{j=1}^{N} e^{-i\frac{2\pi}{N}(j-1)(u-1)}\, e^{ibv_j}\,\psi_T(v_j)\,\frac{\Delta v}{3}\big(3 + (-1)^j - \delta_{j1}\big).

One FFT gives prices for all NN log strikes at once. Market strikes are then read off the grid by linear interpolation. The defaults are N=4096N = 4096, α=1.5\alpha = 1.5 and Δv=0.25\Delta v = 0.25.

The same FFT also gives the delta. The characteristic function depends on S0S_0 only through S0iuS_0^{iu}, so ∂ϕT/∂S0=(iu/S0) ϕT\partial \phi_T / \partial S_0 = (iu / S_0)\,\phi_T. Multiplying ϕT\phi_T by that factor before the transform returns ∂C/∂S0\partial C / \partial S_0 instead of CC. For Black-Scholes this matches N(d1)N(d_1) up to FFT error.

Calibration

The loss is the sum of squared differences between model and market prices over all strikes and maturities. Options are grouped by maturity, so there is one FFT per maturity. The loss has local minima, especially for Heston, so it is minimised with Nelder-Mead from several starting points and the best result is kept. Parameters outside their bounds or that break the Feller condition get a very large loss, which keeps the simplex in the valid region.

Monte Carlo

Every model can also simulate paths. Heston uses an Euler scheme with correlated normal increments, and a variance that turns negative is reflected back above zero. The same seed always gives the same paths.

Contracts work on paths from any model. A contract only needs its payoff. Its value on every path and every day is then estimated as the discounted conditional expectation of the payoff given StS_t, by regressing discounted payoffs on a cubic polynomial plus a spline in StS_t, in the spirit of Longstaff and Schwartz. The delta is computed pathwise with the same regression,

Δt=e−r(T−t) E[payoff′(ST) STSt ∣ St],\Delta_t = e^{-r(T - t)}\, \mathbb{E}\Big[\text{payoff}'(S_T)\,\frac{S_T}{S_t} \,\Big|\, S_t\Big],

with a finite difference as a fallback for payoffs that jump. The variance swap adds the realised variance so far as an extra regression feature, and it can compute its own fair strike from the paths.

Example

import pandas as pd
from derivkit import HestonPricer
from derivkit.contracts import EuropeanCall, VarianceSwap

data = pd.read_csv("Data.csv")
X = data[["Strikes", "Maturities"]].to_numpy()
y = data["Prices"].to_numpy()

pricer = HestonPricer(S0=100, r=0.05)
pricer.fit(X, y)                       # Nelder-Mead from several starts
print(pricer.get_model_params())       # kappa, theta, eta, rho, V0

T = 4 / 12
paths = pricer.simulate(T=T, steps=round(T * 252), n_paths=10_000, seed=42)

call = EuropeanCall(strike=100, T=T, r=0.05)
values = call.price(paths)             # value on every path, every day
deltas = call.delta(paths)

vs = VarianceSwap(T=T, r=0.05)
vs.strike = vs.fair_strike(paths)

References

  • P. Carr and D. Madan (1999). Option valuation using the fast Fourier transform. Journal of Computational Finance 2(4).
  • S. Heston (1993). A closed-form solution for options with stochastic volatility with applications to bond and currency options. Review of Financial Studies 6(2).

The code, tests and an example notebook are on GitHub.