libitofin 0.5.0

A ground-up Rust port of QuantLib: quantitative-finance primitives for pricing, risk, and numerical methods.
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
//! Constant swaption volatility.
//!
//! Port of `ql/termstructures/volatility/swaption/swaptionconstantvol.{hpp,cpp}`:
//! [`ConstantSwaptionVolatility`] implements
//! [`SwaptionVolatilityStructure`](super::SwaptionVolatilityStructure) with a
//! single volatility, no option-time, swap-length or strike dependence. The
//! volatility is either a fixed value (wrapped in an unobservable
//! [`SimpleQuote`](crate::quotes::SimpleQuote), as in C++) or a quote handle
//! whose changes propagate to the structure's observers. The business-day
//! convention, volatility type and shift are pinned by the constructor; the
//! strike domain spans all of `Real` and the swap-tenor domain spans 100 years.
//!
//! The moving constructors take the shared [`Settings`] handle explicitly, per
//! D5.

use crate::errors::QlResult;
use crate::handle::Handle;
use crate::patterns::observable::{AsObservable, Observable};
use crate::quotes::{Quote, make_quote_handle};
use crate::settings::Settings;
use crate::shared::Shared;
use crate::termstructures::volatility::{VolatilityTermStructure, VolatilityType};
use crate::termstructures::{TermStructure, TermStructureBase};
use crate::time::businessdayconvention::BusinessDayConvention;
use crate::time::calendar::Calendar;
use crate::time::date::Date;
use crate::time::daycounter::DayCounter;
use crate::time::period::Period;
use crate::time::timeunit::TimeUnit;
use crate::types::{Natural, Rate, Real, Time, Volatility};

use super::SwaptionVolatilityStructure;

/// Constant swaption volatility, no time-strike dependence.
pub struct ConstantSwaptionVolatility {
    base: TermStructureBase,
    business_day_convention: BusinessDayConvention,
    volatility: Handle<dyn Quote>,
    max_swap_tenor: Period,
    volatility_type: VolatilityType,
    shift: Real,
}

impl ConstantSwaptionVolatility {
    fn wrap(volatility: Volatility) -> Handle<dyn Quote> {
        make_quote_handle(volatility).handle()
    }

    fn assemble(
        base: TermStructureBase,
        business_day_convention: BusinessDayConvention,
        volatility: Handle<dyn Quote>,
        volatility_type: VolatilityType,
        shift: Real,
        observe: bool,
    ) -> ConstantSwaptionVolatility {
        if observe {
            volatility.register_observer(&base.updater());
        }
        ConstantSwaptionVolatility {
            base,
            business_day_convention,
            volatility,
            max_swap_tenor: Period::new(100, TimeUnit::Years),
            volatility_type,
            shift,
        }
    }

    /// Fixed reference date, fixed market data.
    pub fn new(
        reference_date: Date,
        calendar: Calendar,
        business_day_convention: BusinessDayConvention,
        volatility: Volatility,
        day_counter: DayCounter,
        volatility_type: VolatilityType,
        shift: Real,
    ) -> ConstantSwaptionVolatility {
        Self::assemble(
            TermStructureBase::with_reference_date(
                reference_date,
                Some(calendar),
                Some(day_counter),
            ),
            business_day_convention,
            Self::wrap(volatility),
            volatility_type,
            shift,
            false,
        )
    }

    /// Fixed reference date, quote-backed market data; quote changes notify the
    /// structure's observers.
    pub fn with_quote(
        reference_date: Date,
        calendar: Calendar,
        business_day_convention: BusinessDayConvention,
        volatility: Handle<dyn Quote>,
        day_counter: DayCounter,
        volatility_type: VolatilityType,
        shift: Real,
    ) -> ConstantSwaptionVolatility {
        Self::assemble(
            TermStructureBase::with_reference_date(
                reference_date,
                Some(calendar),
                Some(day_counter),
            ),
            business_day_convention,
            volatility,
            volatility_type,
            shift,
            true,
        )
    }

    /// Reference date moving off the evaluation date, fixed market data.
    #[allow(clippy::too_many_arguments)]
    pub fn moving(
        settlement_days: Natural,
        calendar: Calendar,
        business_day_convention: BusinessDayConvention,
        volatility: Volatility,
        day_counter: DayCounter,
        volatility_type: VolatilityType,
        shift: Real,
        settings: Shared<Settings<Date>>,
    ) -> ConstantSwaptionVolatility {
        Self::assemble(
            TermStructureBase::moving(settlement_days, calendar, Some(day_counter), settings),
            business_day_convention,
            Self::wrap(volatility),
            volatility_type,
            shift,
            false,
        )
    }

    /// Reference date moving off the evaluation date, quote-backed market data;
    /// quote changes notify the structure's observers.
    #[allow(clippy::too_many_arguments)]
    pub fn moving_with_quote(
        settlement_days: Natural,
        calendar: Calendar,
        business_day_convention: BusinessDayConvention,
        volatility: Handle<dyn Quote>,
        day_counter: DayCounter,
        volatility_type: VolatilityType,
        shift: Real,
        settings: Shared<Settings<Date>>,
    ) -> ConstantSwaptionVolatility {
        Self::assemble(
            TermStructureBase::moving(settlement_days, calendar, Some(day_counter), settings),
            business_day_convention,
            volatility,
            volatility_type,
            shift,
            true,
        )
    }
}

impl AsObservable for ConstantSwaptionVolatility {
    fn observable(&self) -> &Observable {
        self.base.observable()
    }
}

impl TermStructure for ConstantSwaptionVolatility {
    fn base(&self) -> &TermStructureBase {
        &self.base
    }

    fn max_date(&self) -> Date {
        Date::max_date()
    }
}

impl VolatilityTermStructure for ConstantSwaptionVolatility {
    fn business_day_convention(&self) -> BusinessDayConvention {
        self.business_day_convention
    }

    fn min_strike(&self) -> Rate {
        Rate::MIN
    }

    fn max_strike(&self) -> Rate {
        Rate::MAX
    }
}

impl SwaptionVolatilityStructure for ConstantSwaptionVolatility {
    fn volatility_impl(
        &self,
        _option_time: Time,
        _swap_length: Time,
        _strike: Rate,
    ) -> QlResult<Volatility> {
        self.volatility.current_link()?.value()
    }

    fn max_swap_tenor(&self) -> Period {
        self.max_swap_tenor
    }

    fn volatility_type(&self) -> VolatilityType {
        self.volatility_type
    }

    fn shift_impl(&self, _option_time: Time, _swap_length: Time) -> QlResult<Real> {
        super::require_lognormal_for_shift(self.volatility_type)?;
        Ok(self.shift)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::quotes::SimpleQuote;
    use crate::shared::{Shared, shared};
    use crate::test_support::{Flag, as_observer};
    use crate::time::calendars::target::Target;
    use crate::time::date::Month;
    use crate::time::daycounters::actual360::Actual360;

    fn flat_surface(vol: Volatility) -> (Date, ConstantSwaptionVolatility) {
        let reference = Date::new(15, Month::June, 2026);
        let surface = ConstantSwaptionVolatility::new(
            reference,
            Target::new(),
            BusinessDayConvention::Following,
            vol,
            Actual360::new(),
            VolatilityType::ShiftedLognormal,
            0.0,
        );
        (reference, surface)
    }

    #[test]
    fn volatility_is_constant_across_option_and_swap_axes() {
        let (reference, surface) = flat_surface(0.2);
        for option_time in [0.0, 0.25, 1.0, 10.0] {
            for swap_length in [0.5, 1.0, 30.0] {
                for strike in [-0.01, 0.0, 0.03, 1.0e6] {
                    assert_eq!(
                        surface
                            .volatility_time(option_time, swap_length, strike, false)
                            .unwrap(),
                        0.2
                    );
                }
            }
        }
        assert_eq!(
            surface
                .volatility(reference + 180, 5.0, 0.03, false)
                .unwrap(),
            0.2
        );
    }

    #[test]
    fn black_variance_is_vol_squared_times_option_time() {
        let (reference, surface) = flat_surface(0.25);
        let var = surface.black_variance_time(2.0, 5.0, 0.03, false).unwrap();
        assert!((var - 0.125).abs() < 1e-15);

        let date = reference + 180;
        let t = surface.time_from_reference(date).unwrap();
        assert_eq!(t, 0.5);
        let by_date = surface.black_variance(date, 5.0, 0.03, false).unwrap();
        let by_time = surface.black_variance_time(t, 5.0, 0.03, false).unwrap();
        assert_eq!(by_date, by_time);
        assert!((by_date - 0.25 * 0.25 * 0.5).abs() < 1e-15);
    }

    #[test]
    fn max_swap_tenor_and_length_span_a_century() {
        let (_, surface) = flat_surface(0.2);
        assert_eq!(surface.max_swap_tenor(), Period::new(100, TimeUnit::Years));
        assert_eq!(surface.max_swap_length().unwrap(), 100.0);
    }

    #[test]
    fn shifted_lognormal_reports_its_shift() {
        let reference = Date::new(15, Month::June, 2026);
        let surface = ConstantSwaptionVolatility::new(
            reference,
            Target::new(),
            BusinessDayConvention::Following,
            0.2,
            Actual360::new(),
            VolatilityType::ShiftedLognormal,
            0.01,
        );
        assert_eq!(surface.volatility_type(), VolatilityType::ShiftedLognormal);
        assert_eq!(surface.shift(reference + 90, 5.0, false).unwrap(), 0.01);
    }

    #[test]
    fn normal_surface_rejects_a_shift_query() {
        let reference = Date::new(15, Month::June, 2026);
        let surface = ConstantSwaptionVolatility::new(
            reference,
            Target::new(),
            BusinessDayConvention::Following,
            0.2,
            Actual360::new(),
            VolatilityType::Normal,
            0.0,
        );
        assert_eq!(surface.volatility_type(), VolatilityType::Normal);
        assert!(surface.shift(reference + 90, 5.0, false).is_err());
    }

    #[test]
    fn defaults_report_shifted_lognormal_without_shift() {
        let (reference, surface) = flat_surface(0.2);
        assert_eq!(surface.volatility_type(), VolatilityType::ShiftedLognormal);
        assert_eq!(surface.shift(reference + 90, 5.0, false).unwrap(), 0.0);
    }

    #[test]
    fn engine_facing_constructor_uses_null_calendar_settlement_zero() {
        use crate::time::calendars::nullcalendar::NullCalendar;
        let settings = shared(Settings::new());
        settings.set_evaluation_date(Date::new(15, Month::January, 2026));
        let surface = ConstantSwaptionVolatility::moving(
            0,
            NullCalendar::new(),
            BusinessDayConvention::Following,
            0.2,
            Actual360::new(),
            VolatilityType::ShiftedLognormal,
            0.0,
            settings.clone(),
        );
        assert_eq!(
            surface.reference_date().unwrap(),
            Date::new(15, Month::January, 2026)
        );
        let variance = surface
            .black_variance(Date::new(15, Month::January, 2027), 5.0, 0.03, false)
            .unwrap();
        assert!(variance > 0.0);
    }

    #[test]
    fn quote_changes_propagate_and_notify() {
        let reference = Date::new(15, Month::June, 2026);
        let handle = make_quote_handle(0.18);
        let surface = ConstantSwaptionVolatility::with_quote(
            reference,
            Target::new(),
            BusinessDayConvention::Following,
            handle.handle(),
            Actual360::new(),
            VolatilityType::ShiftedLognormal,
            0.0,
        );
        assert_eq!(
            surface.volatility_time(1.0, 5.0, 0.03, false).unwrap(),
            0.18
        );

        let flag = Flag::new();
        surface.observable().register_observer(&as_observer(&flag));

        let quote = shared(SimpleQuote::new(0.23));
        handle.link_to(quote.clone() as Shared<dyn Quote>);
        assert!(Flag::is_up(&flag));
        assert_eq!(
            surface.volatility_time(1.0, 5.0, 0.03, false).unwrap(),
            0.23
        );
    }

    #[test]
    fn moving_reference_date_follows_the_evaluation_date() {
        let settings = shared(Settings::new());
        settings.set_evaluation_date(Date::new(15, Month::January, 2026));
        let surface = ConstantSwaptionVolatility::moving(
            2,
            Target::new(),
            BusinessDayConvention::Following,
            0.2,
            Actual360::new(),
            VolatilityType::ShiftedLognormal,
            0.0,
            settings.clone(),
        );
        assert_eq!(
            surface.reference_date().unwrap(),
            Date::new(19, Month::January, 2026)
        );
        assert_eq!(surface.volatility_time(1.0, 5.0, 0.03, false).unwrap(), 0.2);

        settings.set_evaluation_date(Date::new(16, Month::January, 2026));
        assert_eq!(
            surface.reference_date().unwrap(),
            Date::new(20, Month::January, 2026)
        );
    }
}