regimeswitching
Pricing libraries for models whose parameters switch with a hidden Markov regime.
A regime is a finite-state chain the market does not observe: a volatility level, a mean rate, a default intensity that is sometimes high and sometimes low. Any parameter of a standard model may take a different value in each regime. The libraries here price the usual instruments under such models, in QuantLib's mold, with a referee behind every number.
Quick start
pip install regimelib
import regimelib as rl
chain = rl.RegimeChain.twoState(3.0, 5.0) # switching rates out of each regime
model = rl.SwitchingVasicek(chain, r0=0.03, a=0.5, b=[0.06, 0.02], sigma=[0.015, 0.008])
bond = rl.ZeroCouponBond(5.0)
bond.setPricingEngine(rl.FastSwitchingEngine(model, order=4, regime=0))
bond.NPV(), bond.delta(), bond.gamma()
Three tiers of engine
The fast-switching expansion expands the price in the mean holding time of the chain, to any order, with an initial layer that remembers the starting regime. Order zero is the averaged model; order one adds the Green–Kubo integral of the parameter fluctuations, the extra convexity the switching creates. The engine stops at the best truncation and warns when even that is rough.
The numerical solution of the same reduced system, without expansion, is the referee for the expansion and is exact up to quadrature for every model whose reduction is exact: Vasicek, CIR, Hull–White, G2, Black–Scholes, Heston, Merton, Bates, variance gamma, jumps, intensities and their baskets, equity with stochastic rates.
Grids and Monte Carlo take the rest: American and barrier options, Bermudan swaptions, and the models whose reduction is only first order, such as a switched vol-of-vol.
Every model has a frozen limit
With every regime equal, each model is QuantLib's model, and each certificate in the test suite checks that limit against QuantLib's own engine before checking the switching case against an independent referee. The certificates page tabulates what is asserted.
Python API →
regimelib, the reference implementation: instruments, engines, models, calibration, closed forms.
Languages →
The reference and the ports, and the parity suite that keeps them identical.
Mathematics →
The expansion, its initial layers and the Green–Kubo term, derived step by step with worked examples.
Examples →
A smile from a regime, which order to trust, default correlation from a common regime, a greek as a formula.
Origin
The expansion goes back to Cotton's 2001 thesis on fast-switching bond prices and survival probabilities; the libraries are its engineering, and homogenization.microprediction.org its mathematics. Comments are welcome through the GitHub issues.