regit-svi 1.0.0

Arbitrage-free SVI volatility surfaces in pure Rust. Raw, Jump-Wings and SSVI parametrisations, calibration, and static-arbitrage checks. Zero dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// Copyright 2026 Regit.io โ€” Nicolas Koenig
// SPDX-License-Identifier: Apache-2.0

//! Static-arbitrage checks: butterfly (`g(k) >= 0`) and calendar-spread.
//!
//! # Butterfly arbitrage and the g function
//!
//! A slice admits butterfly arbitrage when the risk-neutral density it
//! implies is negative somewhere โ€” a butterfly spread with negative cost.
//! Define
//!
//! ```text
//! g(k) = ( 1 - k*w'(k) / (2*w(k)) )^2
//!      - ( w'(k) / 2 )^2 * ( 1/w(k) + 1/4 )
//!      + w''(k) / 2
//! ```
//!
//! A slice is free of butterfly arbitrage iff `g(k) >= 0` for all `k` and
//! call prices vanish at infinite strike. The latter is, for raw SVI,
//! equivalent to the wing bound `b*(1 + |rho|) <= 2` (Lee 2004): implied
//! total variance cannot grow faster than `2|k|`.
//!
//! `g(k)` is evaluated in closed form from `w, w', w''`. The check scans `g`
//! on a dense grid spanning the quoted range plus a wing margin, and refines
//! any sign change with a Brent root-find to report the arbitrage interval.
//!
//! # Calendar-spread arbitrage
//!
//! Two slices at maturities `t_1 < t_2` admit calendar-spread arbitrage when
//! their total-variance curves cross. Absence is the pointwise monotonicity
//! `w(k, t_1) <= w(k, t_2)` for all `k`. The difference
//! `D(k) = w(k, t_2) - w(k, t_1)` is scanned for negativity, with Brent
//! refinement of any crossing.
//!
//! For SSVI, the closed-form Theorem 4.1 / 4.2 conditions of
//! [`crate::ssvi`] are exact and are used instead of the grid scan.
//!
//! # References
//!
//! - Gatheral, J. & Jacquier, A., "Arbitrage-free SVI volatility surfaces",
//!   *Quantitative Finance* 14(1):59-71 (2014), Section 2.
//! - Roper, M., "Arbitrage free implied volatility surfaces", preprint,
//!   University of Sydney (2010).
//! - Lee, R. W., "The moment formula for implied volatility at extreme
//!   strikes", *Mathematical Finance* 14(3):469-480 (2004).

use crate::math::{brent_root, index_to_f64};
use crate::raw::RawSvi;

/// Number of grid points used by the butterfly and calendar scans.
const SCAN_POINTS: usize = 401;
/// Wing margin added on each side of the quoted range when scanning.
const SCAN_MARGIN: f64 = 1.0;
/// Brent tolerance for refining a reported arbitrage boundary.
const REFINE_TOL: f64 = 1e-10;
/// Brent iteration cap for boundary refinement.
const REFINE_MAX_ITER: usize = 200;

/// The butterfly function `g(k)` for a raw SVI slice (MATH.md ยง7).
///
/// `g` is the (normalised) risk-neutral density in log-strike space:
/// `g(k) >= 0` everywhere is exactly the no-butterfly-arbitrage condition.
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::g;
///
/// // A gently curved slice has positive g near the money.
/// let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
/// assert!(g(&svi, 0.0) > 0.0);
/// ```
#[must_use]
pub fn g(svi: &RawSvi, k: f64) -> f64 {
    let w = svi.total_variance(k);
    let wp = svi.w_prime(k);
    let wpp = svi.w_double_prime(k);

    // term1 = (1 - k*w'/(2*w))^2
    let t1 = {
        let inner = 1.0 - k * wp / (2.0 * w);
        inner * inner
    };
    // term2 = (w'/2)^2 * (1/w + 1/4)
    let t2 = {
        let half_wp = wp / 2.0;
        half_wp * half_wp * (1.0 / w + 0.25)
    };
    // term3 = w''/2
    let t3 = wpp / 2.0;

    t1 - t2 + t3
}

/// The result of a butterfly-arbitrage scan over a raw SVI slice.
///
/// When `is_free` is `false`, [`Self::worst_k`] reports the log-moneyness of
/// the deepest density violation and [`Self::min_g`] its value.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ButterflyReport {
    /// `true` if `g(k) >= 0` over the scanned domain and the wing bound holds.
    pub is_free: bool,
    /// The smallest value of `g` observed on the scan grid.
    pub min_g: f64,
    /// The log-moneyness at which [`Self::min_g`] was observed.
    pub worst_k: f64,
    /// `true` if the wing bound `b*(1 + |rho|) <= 2` is satisfied.
    pub wing_bound_ok: bool,
}

/// The result of a calendar-spread-arbitrage scan over two raw SVI slices.
///
/// When `is_free` is `false`, [`Self::worst_k`] reports the log-moneyness at
/// which the longer-dated total variance falls furthest below the
/// shorter-dated one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CalendarReport {
    /// `true` if `w(k, t_2) >= w(k, t_1)` over the scanned domain.
    pub is_free: bool,
    /// The smallest value of `w(k, t_2) - w(k, t_1)` observed.
    pub min_difference: f64,
    /// The log-moneyness at which [`Self::min_difference`] was observed.
    pub worst_k: f64,
}

/// Tests the wing bound `b*(1 + |rho|) <= 2` for a raw SVI slice (Lee 2004).
///
/// The wing bound guarantees call prices vanish at infinite strike; it is one
/// of the two conditions for absence of butterfly arbitrage (MATH.md ยง7).
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::wing_bound_ok;
///
/// let ok = RawSvi::new(0.04, 0.5, -0.3, 0.0, 0.1).unwrap();
/// assert!(wing_bound_ok(&ok));
/// let steep = RawSvi::new(0.04, 3.0, -0.3, 0.0, 0.1).unwrap();
/// assert!(!wing_bound_ok(&steep));
/// ```
#[must_use]
#[inline]
pub fn wing_bound_ok(svi: &RawSvi) -> bool {
    svi.b * (1.0 + svi.rho.abs()) <= 2.0
}

/// Scans a raw SVI slice for butterfly arbitrage over `[k_lo, k_hi]` plus a
/// wing margin (MATH.md ยง7).
///
/// Evaluates `g` on a dense grid; any negative value flags arbitrage. The
/// reported [`ButterflyReport::worst_k`] is refined by a Brent root-find on
/// `g` around the deepest grid violation, so the boundary of the arbitrage
/// region is located to a tight tolerance.
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::butterfly_scan;
///
/// // A well-behaved slice is free of butterfly arbitrage.
/// let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
/// let report = butterfly_scan(&svi, -0.5, 0.5);
/// assert!(report.is_free);
/// ```
#[must_use]
pub fn butterfly_scan(svi: &RawSvi, k_lo: f64, k_hi: f64) -> ButterflyReport {
    let lo = k_lo.min(k_hi) - SCAN_MARGIN;
    let hi = k_lo.max(k_hi) + SCAN_MARGIN;
    let step = (hi - lo) / index_to_f64(SCAN_POINTS - 1);

    let mut min_g = f64::INFINITY;
    let mut worst_k = lo;
    let mut worst_idx = 0_usize;

    for i in 0..SCAN_POINTS {
        let k = step.mul_add(index_to_f64(i), lo);
        let gi = g(svi, k);
        if gi < min_g {
            min_g = gi;
            worst_k = k;
            worst_idx = i;
        }
    }

    let wing_ok = wing_bound_ok(svi);
    let is_free = min_g >= 0.0 && wing_ok;

    // Refine the worst-violation location with a Brent root-find on g, if a
    // sign change was bracketed near the deepest grid point.
    if !is_free && min_g < 0.0 {
        let lo_k = step.mul_add(index_to_f64(worst_idx.saturating_sub(1)), lo);
        let hi_k = step.mul_add(index_to_f64((worst_idx + 1).min(SCAN_POINTS - 1)), lo);
        if let Some(root) = brent_root(|x| g(svi, x), lo_k, hi_k, REFINE_TOL, REFINE_MAX_ITER) {
            worst_k = root;
        }
    }

    ButterflyReport {
        is_free,
        min_g,
        worst_k,
        wing_bound_ok: wing_ok,
    }
}

/// Tests whether a raw SVI slice is free of butterfly arbitrage over a
/// default symmetric domain `[-1, 1]` in log-moneyness.
///
/// A convenience wrapper over [`butterfly_scan`] for callers without a
/// quoted strike range.
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::is_butterfly_free;
///
/// let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
/// assert!(is_butterfly_free(&svi));
/// ```
#[must_use]
pub fn is_butterfly_free(svi: &RawSvi) -> bool {
    butterfly_scan(svi, -1.0, 1.0).is_free
}

/// Scans two raw SVI slices for calendar-spread arbitrage (MATH.md ยง8).
///
/// `early` is the shorter-dated slice and `late` the longer-dated one. The
/// difference `D(k) = w_late(k) - w_early(k)` is evaluated on a dense grid;
/// any negative value flags arbitrage. The reported worst location is refined
/// by a Brent root-find on `D` when a sign change is bracketed.
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::calendar_scan;
///
/// // A higher-variance late slice dominates the early one everywhere.
/// let early = RawSvi::new(0.04, 0.3, -0.2, 0.0, 0.1).unwrap();
/// let late = RawSvi::new(0.08, 0.3, -0.2, 0.0, 0.1).unwrap();
/// let report = calendar_scan(&early, &late, -0.5, 0.5);
/// assert!(report.is_free);
/// ```
#[must_use]
pub fn calendar_scan(early: &RawSvi, late: &RawSvi, k_lo: f64, k_hi: f64) -> CalendarReport {
    let lo = k_lo.min(k_hi) - SCAN_MARGIN;
    let hi = k_lo.max(k_hi) + SCAN_MARGIN;
    let step = (hi - lo) / index_to_f64(SCAN_POINTS - 1);

    let diff = |k: f64| late.total_variance(k) - early.total_variance(k);

    let mut min_diff = f64::INFINITY;
    let mut worst_k = lo;
    let mut worst_idx = 0_usize;

    for i in 0..SCAN_POINTS {
        let k = step.mul_add(index_to_f64(i), lo);
        let d = diff(k);
        if d < min_diff {
            min_diff = d;
            worst_k = k;
            worst_idx = i;
        }
    }

    let is_free = min_diff >= 0.0;

    if !is_free {
        let lo_k = step.mul_add(index_to_f64(worst_idx.saturating_sub(1)), lo);
        let hi_k = step.mul_add(index_to_f64((worst_idx + 1).min(SCAN_POINTS - 1)), lo);
        if let Some(root) = brent_root(diff, lo_k, hi_k, REFINE_TOL, REFINE_MAX_ITER) {
            worst_k = root;
        }
    }

    CalendarReport {
        is_free,
        min_difference: min_diff,
        worst_k,
    }
}

/// Tests whether two raw SVI slices are free of calendar-spread arbitrage
/// over a default domain `[-1, 1]`.
///
/// `early` is the shorter-dated slice, `late` the longer-dated one.
///
/// # Examples
///
/// ```
/// use regit_svi::raw::RawSvi;
/// use regit_svi::arbitrage::is_calendar_free;
///
/// let early = RawSvi::new(0.04, 0.3, -0.2, 0.0, 0.1).unwrap();
/// let late = RawSvi::new(0.08, 0.3, -0.2, 0.0, 0.1).unwrap();
/// assert!(is_calendar_free(&early, &late));
/// ```
#[must_use]
pub fn is_calendar_free(early: &RawSvi, late: &RawSvi) -> bool {
    calendar_scan(early, late, -1.0, 1.0).is_free
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ssvi::{Phi, Ssvi};

    #[test]
    fn g_is_positive_for_benign_slice() {
        let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
        for &k in &[-1.0, -0.3, 0.0, 0.3, 1.0] {
            assert!(g(&svi, k) > 0.0, "g({k}) should be positive");
        }
    }

    #[test]
    fn g_equals_density_factor_at_atm() {
        // For a flat slice (b = 0), w' = w'' = 0, so g(0) = 1.
        let flat = RawSvi::new(0.04, 0.0, 0.0, 0.0, 0.1).unwrap();
        assert!((g(&flat, 0.0) - 1.0).abs() < 1e-12);
    }

    #[test]
    fn wing_bound_accepts_gentle_rejects_steep() {
        assert!(wing_bound_ok(
            &RawSvi::new(0.04, 0.5, -0.3, 0.0, 0.1).unwrap()
        ));
        assert!(!wing_bound_ok(
            &RawSvi::new(0.04, 3.0, -0.3, 0.0, 0.1).unwrap()
        ));
    }

    #[test]
    fn butterfly_scan_passes_benign_slice() {
        let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
        let report = butterfly_scan(&svi, -0.5, 0.5);
        assert!(report.is_free);
        assert!(report.min_g > 0.0);
        assert!(report.wing_bound_ok);
    }

    #[test]
    fn butterfly_scan_flags_vogt_slice() {
        // The Axel Vogt slice from Gatheral & Jacquier (2014), Section 2.2 โ€”
        // a raw SVI slice with documented butterfly arbitrage.
        // a = -0.0410, b = 0.1331, rho = 0.3060, m = 0.3586, sigma = 0.4153.
        let vogt = RawSvi::new(-0.0410, 0.1331, 0.3060, 0.3586, 0.4153).unwrap();
        let report = butterfly_scan(&vogt, -1.5, 1.5);
        assert!(
            !report.is_free,
            "Vogt slice must be flagged as arbitrageable"
        );
        assert!(report.min_g < 0.0, "min_g = {}", report.min_g);
    }

    #[test]
    fn is_butterfly_free_convenience() {
        let svi = RawSvi::new(0.04, 0.1, -0.2, 0.0, 0.3).unwrap();
        assert!(is_butterfly_free(&svi));
        let vogt = RawSvi::new(-0.0410, 0.1331, 0.3060, 0.3586, 0.4153).unwrap();
        assert!(!is_butterfly_free(&vogt));
    }

    #[test]
    fn calendar_scan_passes_ordered_slices() {
        let early = RawSvi::new(0.04, 0.3, -0.2, 0.0, 0.1).unwrap();
        let late = RawSvi::new(0.08, 0.3, -0.2, 0.0, 0.1).unwrap();
        let report = calendar_scan(&early, &late, -0.5, 0.5);
        assert!(report.is_free);
        assert!(report.min_difference > 0.0);
    }

    #[test]
    fn calendar_scan_flags_crossing_slices() {
        // The late slice has lower ATM variance -> the curves cross.
        let early = RawSvi::new(0.08, 0.3, -0.2, 0.0, 0.1).unwrap();
        let late = RawSvi::new(0.04, 0.3, -0.2, 0.0, 0.1).unwrap();
        let report = calendar_scan(&early, &late, -0.5, 0.5);
        assert!(!report.is_free);
        assert!(report.min_difference < 0.0);
    }

    #[test]
    fn is_calendar_free_convenience() {
        let early = RawSvi::new(0.04, 0.3, -0.2, 0.0, 0.1).unwrap();
        let late = RawSvi::new(0.08, 0.3, -0.2, 0.0, 0.1).unwrap();
        assert!(is_calendar_free(&early, &late));
    }

    #[test]
    fn ssvi_slice_passing_theorem_42_is_butterfly_free() {
        // An SSVI slice satisfying Theorem 4.2 should also pass the g-scan.
        let ssvi = Ssvi::new(-0.3, Phi::power_law(0.5, 0.5).unwrap()).unwrap();
        assert!(ssvi.is_butterfly_free_at(0.04));
        let raw = ssvi.slice_at(0.04).unwrap();
        let report = butterfly_scan(&raw, -1.0, 1.0);
        assert!(report.is_free, "min_g = {}", report.min_g);
    }
}