Skip to content

About

The SABR stochastic-vol model — Hagan 2002 lognormal & normal implied vol, Obloj correction, smile calibration. Matches QuantLib. Zero deps.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

hagan-sabr

The SABR stochastic-volatility model — the Hagan et al. (2002) implied-volatility expansions (lognormal and normal), shifted SABR for negative rates, the Obłój correction, and (α, ρ, ν) calibration to a market smile. Zero dependencies.

npm install hagan-sabr

Why

SABR is the market-standard model for interest-rate swaption and FX volatility smiles — but there was no focused JavaScript/TypeScript implementation. @quantlib/ql has it buried in a 28 MB WASM bundle behind a C++-style API. This is the Hagan formulas as a handful of typed functions, and its lognormal, normal and shifted outputs match QuantLib (sabrVolatility / shiftedSabrVolatility) to 1e-9 relative across 1,260 fixtures.

import { lognormalVol, calibrate } from "hagan-sabr";

const p = { alpha: 0.2, beta: 0.5, rho: -0.3, nu: 0.4 };
lognormalVol(100, 100, 1, p);   // ATM Black vol ≈ 0.0202
lognormalVol(100, 80, 1, p);    // the smile at a lower strike

// Fit (α, ρ, ν) to a market smile (β fixed):
const fit = calibrate(100, 2, [
  { strike: 80, vol: 0.245 },
  { strike: 100, vol: 0.202 },
  { strike: 120, vol: 0.199 },
  // …
], { beta: 0.5 });
fit.alpha; fit.rho; fit.nu; fit.rmse;

// Negative rates: shifted SABR, here fitted to normal (bp) vol quotes
const eur = calibrate(-0.002, 5, normalQuotes, { beta: 0.3, shift: 0.02, volType: "normal" });
normalVol(-0.002, -0.005, 5, eur);

API

Parameters: forward F, strike K, expiry T (years), and { alpha, beta, rho, nu, shift? } — α > 0, 0 ≤ β ≤ 1, −1 < ρ < 1, ν ≥ 0, optional shift ≥ 0. Vols are decimals.

  • lognormalVol(F, K, T, params) — Hagan Black/lognormal implied vol (matches QuantLib sabrVolatility).
  • normalVol(F, K, T, params) — Hagan Bachelier/normal implied vol, in the simplified (FK)^{β/2}, ln(F/K) form (matches QuantLib's Normal SABR). Other published normal forms, e.g. Hagan's (F−K)/(F^{1−β}−K^{1−β}) variant used by pysabr, agree to leading order but differ at higher order.
  • oblojVol(F, K, T, params) — the Obłój (2008) refinement that fixes the small-strike / long-maturity leading term.
  • calibrate(F, T, quotes, opts?) — least-squares fit of (α, ρ, ν) with β fixed (default 0.5), via multi-start Nelder–Mead; returns the params plus fit rmse. Options: beta, shift, volType: "lognormal" | "normal" (what the quote vols are), initial, maxIter.

Shifted SABR. Set shift: s in the params (or calibration options) and the formulas are applied to F + s and K + s, so forwards and strikes only need to exceed −s. lognormalVol/oblojVol then return shifted-Black vols (to be used with a shifted Black formula with the same s); normalVol returns an ordinary Bachelier vol.

Invalid inputs (including NaN, non-finite values and F + shift ≤ 0) throw RangeError.

Limits of the expansion. These are Hagan's asymptotic formulas, not exact SABR prices. For large ν²T, or strongly negative ρ with long expiries, the time-correction factor can turn negative and the formulas return meaningless (even negative) vols, as QuantLib's do. Low strikes can also imply negative densities. Consider the Obłój variant, or a different method, in those regimes.

Correctness

Fixtures are generated by test/generate-fixtures.py:

  • Lognormal: 378 fixtures from QuantLib's sabrVolatility (β ∈ {0, 0.3, 0.5, 0.7, 1}, ν including 0, forwards from 0.02 to 100, expiries to 5y, strikes 0.6–1.5× forward), matching to 1e-9 relative.
  • Normal: 378 fixtures from QuantLib's shiftedSabrVolatility(…, Normal), matching to 1e-9.
  • Shifted: 504 fixtures from QuantLib's shiftedSabrVolatility with negative forwards and strikes, lognormal and normal, matching to 1e-9.
  • Obłój: 378 fixtures from a 50-digit mpmath transcription of Obłój (2008), including the ν = 0 limit, matching to 1e-12.
  • Near the money: 240 strikes within 1e-13 to 1e-5 of the forward, checked against 50-digit mpmath evaluations of all three formulas to 1e-13. The textbook x(z) formula cancels catastrophically there, so it is evaluated in a cancellation-free form. (QuantLib itself is off by up to ~3e-8 in this region.)

An 85,680-point grid audit against the same references (β from 0 to 1, ρ from −0.95 to 0.9, ν from 0 to 1.5, T from 0.02 to 10y, strikes from 0.3× to 3× forward and down to 1e-13 from it) showed a maximum relative error of 2.3e-13 against the 50-digit reference. Calibration is verified to recover known parameters from synthetic smiles, including a case that had trapped a single-start fit in a local minimum.

Related

Pairs with svi-vol-surface (arbitrage-free surface parametrization). Part of a quant/fixed-income toolkit: compounded-sofr · day-count · instrument-identifiers · newyorkfed.

Author

Built by Moshe Malka — engineering leader in New York City. Studio work at Quentin.Code.

MIT © Moshe Malka

About

The SABR stochastic-vol model — Hagan 2002 lognormal & normal implied vol, Obloj correction, smile calibration. Matches QuantLib. Zero deps.

Topics

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages