Metadata-Version: 2.4
Name: volsurface-lab
Version: 0.2.1
Summary: Implied volatility surface construction: SVI/SSVI fitting, arbitrage checks, Dupire local vol
License-Expression: MIT
Project-URL: Repository, https://github.com/hossainyousuf751/volsurface
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: pandas>=2.0
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == "plot"
Provides-Extra: data
Requires-Dist: yfinance>=0.2; extra == "data"
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: matplotlib>=3.7; extra == "dev"
Requires-Dist: ruff>=0.6; extra == "dev"
Dynamic: license-file

# volsurface

![tests](https://github.com/hossainyousuf751/volsurface/actions/workflows/ci.yml/badge.svg)

Build an arbitrage-free implied volatility surface from raw option quotes, then
extract Dupire local volatility for pricing path-dependent products.

**Pipeline:** raw bid/ask quotes → cleaning → implied forwards and discount factors from
put-call parity → OTM implied vols → per-expiry SVI smiles → global SSVI and eSSVI surfaces →
static-arbitrage checks → Dupire local vol.

![Fitted smiles](https://raw.githubusercontent.com/hossainyousuf751/volsurface/main/docs/smiles.png)

## Quickstart

```bash
pip install volsurface-lab                 # from PyPI; the import is still `import volsurface`
pip install -e ".[dev]"                    # or, from a clone, for development
pytest
python examples/synthetic_demo.py        # offline, against a known true surface
python examples/snapshot.py              # save today's CBOE ^SPX / ^NDX close to data/ (any time)
python examples/real_data_study.py       # fit every saved snapshot (M3)
```

```python
from volsurface import fit_surface, fit_errors
from volsurface.data import load_cboe_chain

quotes, spot, as_of = load_cboe_chain("^SPX")   # or load_yfinance_chain (pip install -e ".[data]")
fit = fit_surface(quotes, spot, max_rel_spread=0.3)
print(fit.ssvi_params, fit.ssvi_arbitrage.summary())
print(fit_errors(fit))
fit.ssvi_surface.implied_vol(K=0.9 * spot, T=1.0)
fit.ssvi_surface.local_vol(K=0.9 * spot, T=1.0)
```

## Method

**Forwards from the market, not from assumptions.** For each expiry, put-call parity
C − P = DF·(F − K) is linear in strike, so a regression on near-the-money strikes gives
the discount factor (−slope) and forward (intercept/DF). Dividend and funding assumptions
are therefore implied by the options themselves.

**OTM quotes only.** Out-of-the-money options are the most liquid, and for American-style
options they carry the least early-exercise premium. Implied vols are computed for bid,
mid, and ask so fit quality can be judged against the spread.

**SVI per expiry** (Gatheral 2004): w(k) = a + b(ρ(k − m) + √((k − m)² + σ²)), where w is total
implied variance and k = ln(K/F). The fit is vega-weighted, multi-start, and penalises
negative variance and violations of Roger Lee's wing bound b(1 + |ρ|) ≤ 2.

**SSVI surface** (Gatheral & Jacquier 2014) with power-law curvature
φ(θ) = η / (θ^γ (1 + θ)^(1−γ)). The ATM total-variance term structure θ(T) is taken from the
SVI slices, forced to be non-decreasing, and interpolated in T with a monotone cubic (PCHIP),
so it is continuously differentiable and the local vol has no jumps at the expiries.
η is reparameterised as η = s·2/(1 + |ρ|), s ∈ (0, 1], so every candidate satisfies the
no-arbitrage bound η(1 + |ρ|) ≤ 2, γ ≤ ½.
The fitted surface is free of static arbitrage by construction.

**eSSVI** (Hendriks & Martini 2019) lets the skew vary with maturity:
ρ(θ) = ρ₀ + (ρ₁ − ρ₀)·√min(θ/θ_ref, 1). Every slice stays butterfly-free through
η = s·2/(1 + max(|ρ₀|, |ρ₁|)). Calendar arbitrage is no longer excluded by construction, so a
decrease of w in θ is penalised during the fit and reported by the diagnostics. The fit starts
from the SSVI solution, so its total weighted error is never worse than SSVI's.

**Arbitrage-free SVI** (after Gatheral & Jacquier 2014, §5) for the steep SPX put wing that
no single SSVI can reach. Each expiry gets its own 5-parameter SVI slice, started from the
eSSVI slice (an SSVI slice is exactly an SVI slice). Slices are fitted from the longest
expiry down, so each shorter slice only has to stay below the next longer one, and with
residuals in implied-vol units so low-vega wing quotes count. Butterfly (g ≥ margin) and
calendar (w ≤ next slice − margin) violations are penalised on k = z·√θ, z ∈ [−6, 4]
standard deviations, the region prices and paths reach; any slice still infeasible on a
fine grid is refitted with a 10× penalty. Between expiries w(k, ·) is a monotone cubic
(PCHIP) at each k. For local vol, `fit.local_vol_surface()` keeps pillar expiries at least
1.5× apart: interpolating exactly through weeklies a few days apart turns tiny noise in w
into swings in ∂_T w.

**Arbitrage diagnostics.** Butterfly arbitrage via Gatheral's density function g(k) ≥ 0 on
each slice; calendar arbitrage via w(k, T) non-decreasing in T at fixed k. The pipeline checks
every model on the same z ∈ [−6, 4] standard-deviation domain.

**Dupire local volatility** from total variance:

σ²_loc = ∂_T w / [1 − (k/w)∂_k w + ¼(−¼ − 1/w + k²/w²)(∂_k w)² + ½ ∂²_k w]

computed by finite differences on the fitted surface, returning NaN wherever the surface
admits arbitrage (negative numerator or denominator).

## Validation

The synthetic chain generator prices options off a known SSVI surface, adds noise and a
bid-ask spread, and rounds to the tick, so every stage can be checked against the truth:

| Check | Result (noisy synthetic chain) |
|---|---|
| SSVI ρ / η / γ (true −0.70 / 1.00 / 0.40) | −0.699 / 1.002 / 0.399 |
| SSVI RMSE | 0.02–0.12 vol points per expiry |
| Model vols inside bid-ask band | 100% |
| Static arbitrage | none (SVI slices, SSVI, eSSVI) |

On a chain whose skew flattens with maturity (true ρ₀ / ρ₁ = −0.85 / −0.45), constant-ρ SSVI
can no longer fit:

| Check | SSVI | eSSVI |
|---|---|---|
| ρ₀ / ρ₁ | −0.484 (single ρ) | −0.837 / −0.447 |
| Mean RMSE | 0.54 vol points | 0.06 vol points |
| Model vols inside bid-ask band | 54% | 100% |
| Static arbitrage | none | none |

Unit tests also cover implied-vol round trips, parity recovery of forwards and discount
factors, SVI and SSVI parameter recovery, detection of Axel Vogt's known butterfly-arbitrage
SVI counterexample, detection of crossed slices (calendar arbitrage), and local vol
equalling implied vol on a flat surface.

![Local volatility](https://raw.githubusercontent.com/hossainyousuf751/volsurface/main/docs/local_vol.png)

## Real data (M3): SPX and NDX, CBOE close of 2026-10-30

| | SPX | NDX |
|---|---|---|
| Quotes → OTM implied vols | 24,550 → 10,132 (47 expiries) | 14,186 → 3,380 (36 expiries) |
| ATM vol 1m / 1y | 14.9% / 17.1% | 24.2% / 23.1% |
| SSVI ρ / η / γ | −0.55 / 1.24 / 0.50 | −0.46 / 1.26 / 0.46 |
| eSSVI ρ₀ → ρ₁ | −0.68 → −0.53 | −0.53 → −0.44 |
| Median RMSE, SVI / SSVI / eSSVI / arb-free SVI (vol pts) | 0.20 / 2.25 / 2.27 / 0.20 | 0.14 / 0.84 / 0.73 / 0.18 |
| RMSE beyond −2 sd, eSSVI → arb-free SVI | 7.43 → 0.27 | 3.95 → 6.13 (3 quotes) |
| Static arbitrage, free SVI slices | 485 calendar violations, butterfly | 1,213 calendar violations, butterfly |
| Static arbitrage, SSVI, eSSVI, arb-free SVI | none | none |

Standard-deviation bands use z = k/√w, each quote's distance from the money in its own
standard deviations; `examples/real_data_study.py` prints the RMSE per band for every model.

![SPX smiles](https://raw.githubusercontent.com/hossainyousuf751/volsurface/main/docs/spx_smiles.png)

Findings:

- **Skew flattens with maturity**, as eSSVI was built for: SPX ρ goes from −0.68 at the short
  end to −0.53 at the long end. eSSVI improves NDX by 0.1 vol points, but barely moves SPX.
- **SSVI's binding constraint is the SPX put wing.** Within ±1 standard deviation of the money
  SSVI is within 0.5–0.7 vol points, but beyond −2 standard deviations it is about 7 vol points
  too low at every maturity. The fit sits on the no-arbitrage boundary (η(1+|ρ|) = 1.92 ≈ 2,
  γ = ½): SPX's put skew is steeper than a single power-law SSVI can be while staying
  arbitrage-free. Free SVI slices follow the wing but pay for it with calendar and butterfly
  arbitrage.
- **Arbitrage-free SVI closes the gap (M6):** 0.27 vol points beyond −2 sd on SPX, 0.16–0.42
  in every other band, with no static arbitrage. The market quotes themselves have no
  calendar crossings at any moneyness, so the earlier gap was the model, not the data.
- NDX has only 3 quotes beyond −2 sd (half-spread ~1 vol point), so its wing number is noise.
- Tracking ρ, η and the ATM term structure over time needs daily snapshots:
  run `examples/snapshot.py` each trading day; `examples/real_data_study.py` then writes
  `data/study.csv` and `docs/study_params.png`.

## Local-vol Monte Carlo (M5, M6)

`volsurface.montecarlo.simulate_local_vol` simulates correlated paths under Dupire local vol
(daily log-Euler, forwards matched by construction) in the `(n_paths, n_times, n_assets)` layout
that pricing libraries take. It reprices the surface's own vanillas within 4 standard errors, and
with `flat_vols` it is GBM on the same forward curves and random numbers.

The worst-of autocallable study that used it has moved to
[quant-stack](https://github.com/hossainyousuf751/quant-stack) as study S1, so volsurface no
longer depends on sppricer, not even in an example. S1 prices sppricer's README note on SPX and NDX
(ρ = 0.9) with sppricer's own `LocalVolModel`, an independent implementation, and reproduces this
repo's earlier table on the 2026-10-30 close within Monte Carlo error:

| 60% / 70% barrier, per 100 | Flat ATM vol | eSSVI LV | Arb-free SVI LV |
|---|---|---|---|
| quant-stack S1 (sppricer `LocalVolModel`, 100,000 paths) | 100.44 | 96.15 | 96.44 |
| earlier `examples/autocallable_skew.py` (`simulate_local_vol`, 200,000 paths) | 100.41 | 96.14 | 96.42 |

Skew takes about 4 points off the fair value, and fixing the SPX put wing (M6) moves it back by
only about 0.3. NDX's long-dated quotes are wide; S1 keeps them (spread filter 0.5), because
dropping them ends NDX at 1.13 years and holding its smile fixed beyond that overstates the skew
effect.

## Using a fitted surface elsewhere (v0.2)

Three additions let a fitted surface drive a pricer or a risk engine without refitting:

```python
lv = fit.ssvi_surface.local_vol(K, T, floor=0.05, cap=2.0, extrapolate="flat")
fit.ssvi_surface.n_clamped, fit.ssvi_surface.n_local_vol   # how often the guard rails acted

d = fit.ssvi_surface.to_dict()                  # JSON-safe: save a snapshot's surface
surface = SSVISurface.from_dict(d)              # identical surface, no refit

stressed = fit.ssvi_surface.shifted(parallel=0.05, skew=-0.02)   # +5 vol pts, steeper puts
```

- **Safe local vol.** Dupire is unreliable where there are no quotes: in the far wings the
  finite-difference denominator can approach zero. `extrapolate="flat"` holds local vol flat in
  k beyond the fitted range (`surface.k_range`, set by `fit_surface`); NaN (an arbitrage
  point) falls back to implied vol; `floor`/`cap` clip the rest. Every evaluation the guard
  rails changed is counted, so a simulation can report how much of it ran on them. Without
  these keywords `local_vol` is unchanged and still returns NaN at arbitrage.
- **Serialisation.** `ForwardCurve` and `SSVISurface` (SSVI or eSSVI) round-trip exactly
  through `to_dict` / `from_dict`.
- **Scenario surfaces.** `surface.shifted(parallel, skew)` adds `parallel + skew·k` to implied
  vol, for stress tests. The result is checked for static arbitrage at the surface's expiries
  on k ∈ [−1, 1], and the method raises `ValueError` instead of returning a surface with
  arbitrage.

## Part of quant-stack

volsurface is one of five independent libraries linked in
[quant-stack](https://github.com/hossainyousuf751/quant-stack), where a fitted surface drives
sppricer's local-vol Monte Carlo (structural typing, no import between the libraries) and
`shifted` surfaces feed risk-lab stress tests. volsurface installs and tests on its own.

## Roadmap

- [x] **M1 Pipeline:** cleaning, parity forwards, implied vols, SVI, SSVI, arbitrage checks, Dupire local vol
- [x] **M2 Smooth term structure:** replace piecewise-linear θ(T) with a monotone spline, removing
      the horizontal bands in the local vol at each expiry
- [x] **M3 Real-data study:** CBOE snapshots, study script, SVI vs SSVI vs eSSVI on real
      spreads (above). Ongoing: the time series of ρ, η and ATM term structure fills in as
      daily snapshots accumulate
- [x] **M4 eSSVI:** maturity-dependent ρ(θ) for better fits across the term structure
- [x] **M5 Link to the pricer:** local-vol Monte Carlo in the paths layout pricers take; the
      autocallable study now lives in quant-stack (S1): skew lowers the worst-of note by about 4 points
- [x] **M6 Put-wing fit:** arbitrage-free SVI slices; SPX wing error 7.4 → 0.3 vol points,
      worst-of autocallable price moves by only about +0.3 (quant-stack S1)

## Limitations

Yahoo Finance quotes are delayed and sometimes stale; the snapshot is not synchronised
across strikes. Single-stock and ETF options are American, so implied vols from Black-76
carry a small early-exercise bias even on OTM quotes. A single flat discount per expiry
is used rather than a separate OIS curve. CBOE delayed quotes are end-of-day and for
personal use; snapshots stay in the git-ignored `data/` folder.

Arbitrage-free SVI is free of static arbitrage on the checked domain (−6 to +4 standard
deviations at each expiry, verified on a fine grid), not analytically everywhere; between
pillar expiries butterfly freedom is not checked, and local vol falls back to implied vol at
the rare NaN points (`local_vol(..., floor=, cap=)` counts them).

## References

- Gatheral, J. (2006). *The Volatility Surface: A Practitioner's Guide.* Wiley.
- Gatheral, J. & Jacquier, A. (2014). Arbitrage-free SVI volatility surfaces. *Quantitative Finance* 14(1).
- Hendriks, S. & Martini, C. (2019). The extended SSVI volatility surface. *Journal of Computational Finance* 22(5).
- Dupire, B. (1994). Pricing with a smile. *Risk* 7(1).
- Lee, R. (2004). The moment formula for implied volatility at extreme strikes. *Mathematical Finance* 14(3).

## License

[MIT](https://github.com/hossainyousuf751/volsurface/blob/main/LICENSE). See [CONTRIBUTING.md](https://github.com/hossainyousuf751/volsurface/blob/main/CONTRIBUTING.md) to contribute.
