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
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);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 QuantLibsabrVolatility).normalVol(F, K, T, params)— Hagan Bachelier/normal implied vol, in the simplified (FK)^{β/2}, ln(F/K) form (matches QuantLib'sNormalSABR). 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 fitrmse. 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.
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
shiftedSabrVolatilitywith negative forwards and strikes, lognormal and normal, matching to 1e-9. - Obłój: 378 fixtures from a 50-digit
mpmathtranscription 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
mpmathevaluations 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.
Pairs with svi-vol-surface (arbitrage-free surface parametrization). Part of a quant/fixed-income toolkit: compounded-sofr · day-count · instrument-identifiers · newyorkfed.
Built by Moshe Malka — engineering leader in New York City. Studio work at Quentin.Code.
MIT © Moshe Malka