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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
// Copyright 2026 Regit.io — Nicolas Koenig
// SPDX-License-Identifier: Apache-2.0

//! Typed error enums for parametrisation, conversion, and calibration.
//!
//! All failure paths return a typed `Result` — no `panic!()`, no `unwrap()`,
//! no string errors. Each variant carries enough context for the caller to
//! decide how to recover.
//!
//! Three enums separate the three failure domains:
//!
//! - [`ParamError`] — invalid SVI / SSVI parameters or out-of-domain quotes.
//! - [`ConvertError`] — a parametrisation conversion has no valid pre-image.
//! - [`CalibrationError`] — a calibrator could not produce a usable fit.

use core::fmt;

// ─── Parametrisation errors ──────────────────────────────────────────────────

/// Error returned when SVI / SSVI parameters or market quotes fail validation.
///
/// Every SVI parametrisation has a validity domain (raw SVI: `b >= 0`,
/// `|rho| < 1`, `sigma > 0`, `a + b*sigma*sqrt(1-rho^2) >= 0`). A constructor
/// or `validate` method returns one of these variants when an input lies
/// outside that domain.
///
/// # Examples
///
/// ```
/// use regit_svi::errors::ParamError;
///
/// let err = ParamError::NegativeSlope { b: -0.1 };
/// assert_eq!(format!("{err}"), "raw SVI slope b must be non-negative, got -0.1");
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ParamError {
    /// Raw SVI slope `b` is negative.
    NegativeSlope {
        /// The offending value of `b`.
        b: f64,
    },
    /// Raw SVI / SSVI correlation `rho` is outside `(-1, 1)`.
    CorrelationOutOfRange {
        /// The offending value of `rho`.
        rho: f64,
    },
    /// Raw SVI curvature `sigma` is not strictly positive.
    NonPositiveSigma {
        /// The offending value of `sigma`.
        sigma: f64,
    },
    /// The minimum total variance `a + b*sigma*sqrt(1-rho^2)` is negative,
    /// so the slice produces negative variance somewhere.
    NegativeMinVariance {
        /// The minimum value of `w` over the slice.
        w_min: f64,
    },
    /// A maturity `t` is not strictly positive.
    NonPositiveMaturity {
        /// The offending value of `t`.
        t: f64,
    },
    /// A fitting weight on a market quote is negative.
    NegativeWeight {
        /// The offending weight.
        weight: f64,
    },
    /// An observed total variance on a market quote is negative.
    NegativeTotalVariance {
        /// The offending total variance.
        w: f64,
    },
    /// An SSVI smoothing-function parameter is outside its valid domain
    /// (`lambda > 0`, `eta > 0`, `gamma in (0, 1)`).
    InvalidPhiParameter {
        /// Human-readable name of the offending parameter.
        name: &'static str,
        /// The offending value.
        value: f64,
    },
    /// An SSVI ATM total variance `theta` is not strictly positive.
    NonPositiveTheta {
        /// The offending value of `theta`.
        theta: f64,
    },
    /// A non-finite (`NaN` or infinite) value was supplied where a finite
    /// number is required.
    NonFinite {
        /// Human-readable name of the offending input.
        name: &'static str,
    },
}

impl fmt::Display for ParamError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::NegativeSlope { b } => {
                write!(f, "raw SVI slope b must be non-negative, got {b}")
            }
            Self::CorrelationOutOfRange { rho } => {
                write!(f, "correlation rho must lie in (-1, 1), got {rho}")
            }
            Self::NonPositiveSigma { sigma } => {
                write!(f, "raw SVI curvature sigma must be positive, got {sigma}")
            }
            Self::NegativeMinVariance { w_min } => {
                write!(
                    f,
                    "minimum total variance must be non-negative, got w_min = {w_min}"
                )
            }
            Self::NonPositiveMaturity { t } => {
                write!(f, "maturity t must be positive, got {t}")
            }
            Self::NegativeWeight { weight } => {
                write!(f, "quote weight must be non-negative, got {weight}")
            }
            Self::NegativeTotalVariance { w } => {
                write!(f, "quoted total variance must be non-negative, got {w}")
            }
            Self::InvalidPhiParameter { name, value } => {
                write!(f, "SSVI phi parameter {name} is out of range: {value}")
            }
            Self::NonPositiveTheta { theta } => {
                write!(f, "SSVI ATM variance theta must be positive, got {theta}")
            }
            Self::NonFinite { name } => {
                write!(f, "input {name} must be a finite number")
            }
        }
    }
}

impl std::error::Error for ParamError {}

// ─── Conversion errors ───────────────────────────────────────────────────────

/// Error returned when a parametrisation conversion has no valid pre-image.
///
/// The Raw <-> Jump-Wings map is bijective only on a subset of JW space: a
/// JW tuple with `|beta| > 1` (where `beta = rho - 2*psi*sqrt(w)/b`) does not
/// correspond to any raw SVI slice — see MATH.md §4.
///
/// # Examples
///
/// ```
/// use regit_svi::errors::ConvertError;
///
/// let err = ConvertError::JwHasNoRawPreimage { beta: 1.4 };
/// let msg = format!("{err}");
/// assert!(msg.contains("1.4"));
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConvertError {
    /// The Jump-Wings tuple yields `|beta| > 1`, so no raw SVI slice exists.
    JwHasNoRawPreimage {
        /// The computed value of `beta`.
        beta: f64,
    },
    /// A wing slope `p_t` or `c_t` is negative, which has no raw pre-image.
    NegativeWingSlope {
        /// Human-readable name of the offending wing slope.
        name: &'static str,
        /// The offending value.
        value: f64,
    },
    /// The Jump-Wings ATM total variance `v_t * t` is not strictly positive.
    NonPositiveAtmVariance {
        /// The computed ATM total variance.
        w: f64,
    },
    /// A degenerate intermediate (`b = 0` or `c_t + p_t = 0`) makes the
    /// inverse map indeterminate.
    DegenerateJw,
    /// A parameter error surfaced while constructing the converted slice.
    Param(ParamError),
}

impl fmt::Display for ConvertError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::JwHasNoRawPreimage { beta } => {
                write!(
                    f,
                    "Jump-Wings tuple has no raw SVI pre-image: |beta| > 1, beta = {beta}"
                )
            }
            Self::NegativeWingSlope { name, value } => {
                write!(
                    f,
                    "Jump-Wings wing slope {name} must be non-negative, got {value}"
                )
            }
            Self::NonPositiveAtmVariance { w } => {
                write!(f, "Jump-Wings ATM total variance must be positive, got {w}")
            }
            Self::DegenerateJw => {
                write!(
                    f,
                    "Jump-Wings tuple is degenerate: inverse map is indeterminate"
                )
            }
            Self::Param(e) => write!(f, "converted slice is invalid: {e}"),
        }
    }
}

impl std::error::Error for ConvertError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Param(e) => Some(e),
            _ => None,
        }
    }
}

impl From<ParamError> for ConvertError {
    fn from(e: ParamError) -> Self {
        Self::Param(e)
    }
}

// ─── Calibration errors ──────────────────────────────────────────────────────

/// Error returned when a calibrator cannot produce a usable fit.
///
/// Covers insufficient data, non-convergence of the outer optimiser, and any
/// parameter error surfaced while assembling the calibrated slice.
///
/// # Examples
///
/// ```
/// use regit_svi::errors::CalibrationError;
///
/// let err = CalibrationError::TooFewQuotes { got: 2, need: 5 };
/// let msg = format!("{err}");
/// assert!(msg.contains("2"));
/// assert!(msg.contains("5"));
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CalibrationError {
    /// Fewer quotes were supplied than the model has free parameters.
    TooFewQuotes {
        /// Number of quotes supplied.
        got: usize,
        /// Minimum number of quotes required.
        need: usize,
    },
    /// The supplied quote set is empty.
    EmptyQuotes,
    /// The outer optimiser reached its iteration cap without converging.
    DidNotConverge {
        /// Number of iterations performed.
        iterations: usize,
        /// The final residual norm.
        residual: f64,
    },
    /// All fitting weights are zero, so the objective is identically zero.
    AllWeightsZero,
    /// A parameter error surfaced while assembling the calibrated slice.
    Param(ParamError),
}

impl fmt::Display for CalibrationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::TooFewQuotes { got, need } => {
                write!(
                    f,
                    "too few quotes for calibration: got {got}, need at least {need}"
                )
            }
            Self::EmptyQuotes => write!(f, "quote set is empty"),
            Self::DidNotConverge {
                iterations,
                residual,
            } => {
                write!(
                    f,
                    "calibration did not converge after {iterations} iterations, residual = {residual}"
                )
            }
            Self::AllWeightsZero => write!(f, "all fitting weights are zero"),
            Self::Param(e) => write!(f, "calibrated slice is invalid: {e}"),
        }
    }
}

impl std::error::Error for CalibrationError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Param(e) => Some(e),
            _ => None,
        }
    }
}

impl From<ParamError> for CalibrationError {
    fn from(e: ParamError) -> Self {
        Self::Param(e)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn param_error_display_negative_slope() {
        let err = ParamError::NegativeSlope { b: -0.1 };
        assert_eq!(
            format!("{err}"),
            "raw SVI slope b must be non-negative, got -0.1"
        );
    }

    #[test]
    fn param_error_display_correlation() {
        let err = ParamError::CorrelationOutOfRange { rho: 1.5 };
        assert!(format!("{err}").contains("1.5"));
    }

    #[test]
    fn param_error_display_non_positive_sigma() {
        let err = ParamError::NonPositiveSigma { sigma: 0.0 };
        assert!(format!("{err}").contains("sigma"));
    }

    #[test]
    fn param_error_display_negative_min_variance() {
        let err = ParamError::NegativeMinVariance { w_min: -0.01 };
        assert!(format!("{err}").contains("w_min"));
    }

    #[test]
    fn param_error_display_remaining_variants() {
        assert!(format!("{}", ParamError::NonPositiveMaturity { t: 0.0 }).contains("maturity"));
        assert!(format!("{}", ParamError::NegativeWeight { weight: -1.0 }).contains("weight"));
        assert!(
            format!("{}", ParamError::NegativeTotalVariance { w: -0.1 }).contains("total variance")
        );
        assert!(
            format!(
                "{}",
                ParamError::InvalidPhiParameter {
                    name: "eta",
                    value: -1.0
                }
            )
            .contains("eta")
        );
        assert!(format!("{}", ParamError::NonPositiveTheta { theta: 0.0 }).contains("theta"));
        assert!(format!("{}", ParamError::NonFinite { name: "k" }).contains("finite"));
    }

    #[test]
    fn param_error_is_error_trait() {
        let err: &dyn std::error::Error = &ParamError::NegativeSlope { b: -1.0 };
        assert!(err.source().is_none());
    }

    #[test]
    fn param_error_copy_eq() {
        let err = ParamError::NonFinite { name: "x" };
        let copy = err;
        assert_eq!(err, copy);
    }

    #[test]
    fn convert_error_display() {
        let err = ConvertError::JwHasNoRawPreimage { beta: 1.4 };
        assert!(format!("{err}").contains("1.4"));
        let err = ConvertError::NegativeWingSlope {
            name: "p_t",
            value: -1.0,
        };
        assert!(format!("{err}").contains("p_t"));
        assert!(format!("{}", ConvertError::DegenerateJw).contains("degenerate"));
        assert!(
            format!("{}", ConvertError::NonPositiveAtmVariance { w: -0.1 }).contains("positive")
        );
    }

    #[test]
    fn convert_error_from_param_and_source() {
        let pe = ParamError::NegativeSlope { b: -1.0 };
        let ce: ConvertError = pe.into();
        assert!(matches!(ce, ConvertError::Param(_)));
        let dyn_err: &dyn std::error::Error = &ce;
        assert!(dyn_err.source().is_some());
    }

    #[test]
    fn calibration_error_display() {
        let err = CalibrationError::TooFewQuotes { got: 2, need: 5 };
        let msg = format!("{err}");
        assert!(msg.contains('2') && msg.contains('5'));
        assert!(format!("{}", CalibrationError::EmptyQuotes).contains("empty"));
        assert!(
            format!(
                "{}",
                CalibrationError::DidNotConverge {
                    iterations: 100,
                    residual: 1e-3
                }
            )
            .contains("converge")
        );
        assert!(format!("{}", CalibrationError::AllWeightsZero).contains("weights"));
    }

    #[test]
    fn calibration_error_from_param_and_source() {
        let pe = ParamError::NonPositiveSigma { sigma: 0.0 };
        let ce: CalibrationError = pe.into();
        assert!(matches!(ce, CalibrationError::Param(_)));
        let dyn_err: &dyn std::error::Error = &ce;
        assert!(dyn_err.source().is_some());
    }

    #[test]
    fn errors_debug() {
        assert!(format!("{:?}", ParamError::NonFinite { name: "k" }).contains("NonFinite"));
        assert!(format!("{:?}", ConvertError::DegenerateJw).contains("Degenerate"));
        assert!(format!("{:?}", CalibrationError::EmptyQuotes).contains("Empty"));
    }
}