sanos 0.2.1

SANOS: Smooth strictly Arbitrage-free Non-parametric Option Surfaces (Rust implementation)
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// src/grid/config.rs
use crate::error::{SanosError, SanosResult};
use crate::grid::policy::{LogMoneynessQuantiles, MarketAnchored};

/// How to extend the grid beyond market strikes.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct WingsConfig {
    pub n_left: usize,
    pub n_right: usize,
    pub ratio: f64, // must be > 1
}

impl Default for WingsConfig {
    fn default() -> Self {
        Self { n_left: 2, n_right: 2, ratio: 1.2 }
    }
}

impl WingsConfig {
    pub fn validate(&self) -> SanosResult<()> {
        if !self.ratio.is_finite() {
            return Err(SanosError::NonFinite { field: "grid.wings.ratio", value: self.ratio });
        }
        if self.ratio <= 1.0 {
            return Err(SanosError::InvalidBound {
                field: "grid.wings.ratio",
                value: self.ratio,
                min: 1.0 + f64::EPSILON,
                max: f64::INFINITY,
            });
        }
        Ok(())
    }
}

/// Optional ATM refinement around k=1.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct AtmRefineConfig {
    pub enabled: bool,
    pub steps: usize,
    pub delta_log: f64, // > 0 when enabled
}

impl Default for AtmRefineConfig {
    fn default() -> Self {
        Self { enabled: false, steps: 2, delta_log: 0.05 }
    }
}

impl AtmRefineConfig {
    pub fn validate(&self) -> SanosResult<()> {
        if !self.delta_log.is_finite() {
            return Err(SanosError::NonFinite { field: "grid.atm_refine.delta_log", value: self.delta_log });
        }
        if self.enabled && self.delta_log <= 0.0 {
            return Err(SanosError::InvalidBound {
                field: "grid.atm_refine.delta_log",
                value: self.delta_log,
                min: f64::MIN_POSITIVE,
                max: f64::INFINITY,
            });
        }
        Ok(())
    }
}

/// Controls the maximum number of grid points.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GridSizeConfig {
    pub max_points: usize,
    pub keep_all_market_strikes: bool,
}

impl Default for GridSizeConfig {
    fn default() -> Self {
        Self { max_points: 80, keep_all_market_strikes: true }
    }
}

impl GridSizeConfig {
    pub fn validate(&self) -> SanosResult<()> {
        if self.max_points == 0 {
            return Err(SanosError::InvalidOrdering { msg: "grid.grid_size.max_points must be > 0" });
        }
        Ok(())
    }
}

/// User-facing, serializable strike grid policy config.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub enum StrikeGridPolicyConfig {
    MarketAnchored(MarketAnchoredGridConfig),
    LogMoneynessQuantiles(LogMoneynessQuantilesGridConfig),
}

impl Default for StrikeGridPolicyConfig {
    fn default() -> Self {
        Self::MarketAnchored(MarketAnchoredGridConfig::default())
    }
}

/// Serializable config for the runtime `MarketAnchored` policy.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq)]
pub struct MarketAnchoredGridConfig {
    pub ensure_atm: bool,
    pub wings: WingsConfig,
    pub atm_refine: AtmRefineConfig,
    pub grid_size: GridSizeConfig,
    pub min_strike: f64,
    pub max_strike: f64,
    pub min_spacing_log: f64,
}

impl Default for MarketAnchoredGridConfig {
    fn default() -> Self {
        Self {
            ensure_atm: true,
            wings: WingsConfig::default(),
            atm_refine: AtmRefineConfig::default(),
            grid_size: GridSizeConfig::default(),
            min_strike: 1e-4,
            max_strike: 1e4,
            min_spacing_log: 1e-3,
        }
    }
}

impl MarketAnchoredGridConfig {
    pub fn validate(&self) -> SanosResult<()> {
        self.wings.validate()?;
        self.atm_refine.validate()?;
        self.grid_size.validate()?;

        for (field, v) in [
            ("grid.market_anchored.min_strike", self.min_strike),
            ("grid.market_anchored.max_strike", self.max_strike),
            ("grid.market_anchored.min_spacing_log", self.min_spacing_log),
        ] {
            if !v.is_finite() {
                return Err(SanosError::NonFinite { field, value: v });
            }
        }

        if self.min_strike <= 0.0 {
            return Err(SanosError::InvalidBound {
                field: "grid.market_anchored.min_strike",
                value: self.min_strike,
                min: f64::MIN_POSITIVE,
                max: f64::INFINITY,
            });
        }
        if self.max_strike <= self.min_strike {
            return Err(SanosError::InvalidOrdering { msg: "grid.market_anchored.max_strike must be > min_strike" });
        }
        if self.min_spacing_log < 0.0 {
            return Err(SanosError::InvalidBound {
                field: "grid.market_anchored.min_spacing_log",
                value: self.min_spacing_log,
                min: 0.0,
                max: f64::INFINITY,
            });
        }

        Ok(())
    }

    pub fn to_runtime(&self) -> SanosResult<MarketAnchored> {
        self.validate()?;
        Ok(MarketAnchored {
            ensure_atm: self.ensure_atm,
            wings: self.wings,
            atm_refine: self.atm_refine,
            size_control: self.grid_size,
            min_strike: self.min_strike,
            max_strike: self.max_strike,
            min_spacing_log: self.min_spacing_log,
        })
    }
}

/// Serializable config for the runtime `LogMoneynessQuantiles` policy.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LogMoneynessQuantilesGridConfig {
    pub n: usize,
    pub left_sigmas: f64,
    pub right_sigmas: f64,
    pub alpha_left: f64,
    pub alpha_right: f64,
    pub include_market_strikes: bool,
    pub min_spacing: Option<f64>,
    pub k_min: Option<f64>,
    pub k_max: Option<f64>,
}

impl Default for LogMoneynessQuantilesGridConfig {
    fn default() -> Self {
        Self {
            n: 80,
            left_sigmas: 4.5,
            right_sigmas: 3.0,
            alpha_left: 1.8,
            alpha_right: 1.2,
            include_market_strikes: true,
            min_spacing: None,
            k_min: None,
            k_max: None,
        }
    }
}

impl LogMoneynessQuantilesGridConfig {
    pub fn validate(&self) -> SanosResult<()> {
        if self.n < 3 {
            return Err(SanosError::InvalidOrdering { msg: "grid.log_moneyness_quantiles.n must be >= 3" });
        }

        for (field, value) in [
            ("grid.log_moneyness_quantiles.left_sigmas", self.left_sigmas),
            ("grid.log_moneyness_quantiles.right_sigmas", self.right_sigmas),
            ("grid.log_moneyness_quantiles.alpha_left", self.alpha_left),
            ("grid.log_moneyness_quantiles.alpha_right", self.alpha_right),
        ] {
            if !value.is_finite() {
                return Err(SanosError::NonFinite { field, value });
            }
            if value <= 0.0 {
                return Err(SanosError::InvalidBound {
                    field,
                    value,
                    min: f64::MIN_POSITIVE,
                    max: f64::INFINITY,
                });
            }
        }

        if let Some(min_spacing) = self.min_spacing {
            if !min_spacing.is_finite() {
                return Err(SanosError::NonFinite {
                    field: "grid.log_moneyness_quantiles.min_spacing",
                    value: min_spacing,
                });
            }
            if min_spacing < 0.0 {
                return Err(SanosError::InvalidBound {
                    field: "grid.log_moneyness_quantiles.min_spacing",
                    value: min_spacing,
                    min: 0.0,
                    max: f64::INFINITY,
                });
            }
        }

        if let Some(k_min) = self.k_min {
            if !k_min.is_finite() {
                return Err(SanosError::NonFinite {
                    field: "grid.log_moneyness_quantiles.k_min",
                    value: k_min,
                });
            }
            if k_min <= 0.0 {
                return Err(SanosError::InvalidBound {
                    field: "grid.log_moneyness_quantiles.k_min",
                    value: k_min,
                    min: f64::MIN_POSITIVE,
                    max: f64::INFINITY,
                });
            }
        }

        if let Some(k_max) = self.k_max {
            if !k_max.is_finite() {
                return Err(SanosError::NonFinite {
                    field: "grid.log_moneyness_quantiles.k_max",
                    value: k_max,
                });
            }
            if k_max <= 0.0 {
                return Err(SanosError::InvalidBound {
                    field: "grid.log_moneyness_quantiles.k_max",
                    value: k_max,
                    min: f64::MIN_POSITIVE,
                    max: f64::INFINITY,
                });
            }
        }

        if let (Some(k_min), Some(k_max)) = (self.k_min, self.k_max) {
            if k_max <= k_min {
                return Err(SanosError::InvalidOrdering {
                    msg: "grid.log_moneyness_quantiles.k_max must be > k_min",
                });
            }
        }

        Ok(())
    }

    pub fn to_runtime(&self) -> SanosResult<LogMoneynessQuantiles> {
        self.validate()?;
        Ok(LogMoneynessQuantiles {
            n: self.n,
            left_sigmas: self.left_sigmas,
            right_sigmas: self.right_sigmas,
            alpha_left: self.alpha_left,
            alpha_right: self.alpha_right,
            include_market_strikes: self.include_market_strikes,
            min_spacing: self.min_spacing,
            k_min: self.k_min,
            k_max: self.k_max,
        })
    }
}

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

    #[test]
    fn wings_validate_rejects_ratio_not_greater_than_one() {
        let cfg = WingsConfig { n_left: 1, n_right: 1, ratio: 1.0 };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidBound { field, .. } => assert_eq!(field, "grid.wings.ratio"),
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn atm_refine_validate_requires_positive_delta_when_enabled() {
        let cfg = AtmRefineConfig { enabled: true, steps: 2, delta_log: 0.0 };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidBound { field, .. } => assert_eq!(field, "grid.atm_refine.delta_log"),
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn grid_size_validate_rejects_zero_max_points() {
        let cfg = GridSizeConfig { max_points: 0, keep_all_market_strikes: true };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidOrdering { msg } => {
                assert_eq!(msg, "grid.grid_size.max_points must be > 0")
            }
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn market_anchored_validate_rejects_invalid_ordering() {
        let cfg = MarketAnchoredGridConfig {
            min_strike: 2.0,
            max_strike: 2.0,
            ..MarketAnchoredGridConfig::default()
        };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidOrdering { msg } => {
                assert_eq!(msg, "grid.market_anchored.max_strike must be > min_strike")
            }
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn market_anchored_to_runtime_maps_fields() {
        let cfg = MarketAnchoredGridConfig {
            ensure_atm: false,
            wings: WingsConfig { n_left: 3, n_right: 4, ratio: 1.3 },
            atm_refine: AtmRefineConfig { enabled: true, steps: 5, delta_log: 0.02 },
            grid_size: GridSizeConfig { max_points: 17, keep_all_market_strikes: false },
            min_strike: 0.2,
            max_strike: 3.5,
            min_spacing_log: 0.004,
        };

        let runtime = cfg.to_runtime().unwrap();
        assert_eq!(runtime.ensure_atm, cfg.ensure_atm);
        assert_eq!(runtime.wings, cfg.wings);
        assert_eq!(runtime.atm_refine, cfg.atm_refine);
        assert_eq!(runtime.size_control, cfg.grid_size);
        assert_eq!(runtime.min_strike, cfg.min_strike);
        assert_eq!(runtime.max_strike, cfg.max_strike);
        assert_eq!(runtime.min_spacing_log, cfg.min_spacing_log);
    }

    #[test]
    fn log_moneyness_quantiles_validate_rejects_alpha_not_positive() {
        let cfg = LogMoneynessQuantilesGridConfig {
            alpha_left: 0.0,
            ..LogMoneynessQuantilesGridConfig::default()
        };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidBound { field, .. } => {
                assert_eq!(field, "grid.log_moneyness_quantiles.alpha_left")
            }
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn log_moneyness_quantiles_validate_rejects_bad_caps() {
        let cfg = LogMoneynessQuantilesGridConfig {
            k_min: Some(1.2),
            k_max: Some(1.2),
            ..LogMoneynessQuantilesGridConfig::default()
        };
        let err = cfg.validate().unwrap_err();
        match err {
            SanosError::InvalidOrdering { msg } => {
                assert_eq!(msg, "grid.log_moneyness_quantiles.k_max must be > k_min")
            }
            _ => panic!("unexpected error variant: {err:?}"),
        }
    }

    #[test]
    fn log_moneyness_quantiles_to_runtime_maps_fields() {
        let cfg = LogMoneynessQuantilesGridConfig {
            n: 41,
            left_sigmas: 5.0,
            right_sigmas: 2.8,
            alpha_left: 2.0,
            alpha_right: 1.3,
            include_market_strikes: false,
            min_spacing: Some(1e-4),
            k_min: Some(0.2),
            k_max: Some(4.0),
        };

        let runtime = cfg.to_runtime().unwrap();
        assert_eq!(runtime.n, cfg.n);
        assert_eq!(runtime.left_sigmas, cfg.left_sigmas);
        assert_eq!(runtime.right_sigmas, cfg.right_sigmas);
        assert_eq!(runtime.alpha_left, cfg.alpha_left);
        assert_eq!(runtime.alpha_right, cfg.alpha_right);
        assert_eq!(runtime.include_market_strikes, cfg.include_market_strikes);
        assert_eq!(runtime.min_spacing, cfg.min_spacing);
        assert_eq!(runtime.k_min, cfg.k_min);
        assert_eq!(runtime.k_max, cfg.k_max);
    }
}