quantsupport 0.1.0

Rust library for fixed-income, derivative pricing and risk analytics.
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::{
    hash::Hash,
    ops::{Add, Sub},
};

use serde::{Deserialize, Serialize};

use crate::utils::errors::{QSError, Result};

/// Enum representing a financial frequency.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord, Hash)]
pub enum Frequency {
    /// No frequency.
    NoFrequency = -1,
    /// Once.
    Once = 0,
    /// Annual frequency.
    Annual = 1,
    /// Semiannual frequency.
    Semiannual = 2,
    /// Every fourth month frequency.
    EveryFourthMonth = 3,
    /// Quarterly frequency.
    Quarterly = 4,
    /// Bimonthly frequency.
    Bimonthly = 6,
    /// Monthly frequency.
    Monthly = 12,
    /// Every fourth week frequency.
    EveryFourthWeek = 13,
    /// Biweekly frequency.
    Biweekly = 26,
    /// Weekly frequency.
    Weekly = 52,
    /// Daily frequency.
    Daily = 365,
    /// Other frequency.
    OtherFrequency = 999,
}

impl TryFrom<String> for Frequency {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "NoFrequency" => Ok(Self::NoFrequency),
            "Once" => Ok(Self::Once),
            "Annual" => Ok(Self::Annual),
            "Semiannual" => Ok(Self::Semiannual),
            "EveryFourthMonth" => Ok(Self::EveryFourthMonth),
            "Quarterly" => Ok(Self::Quarterly),
            "Bimonthly" => Ok(Self::Bimonthly),
            "Monthly" => Ok(Self::Monthly),
            "EveryFourthWeek" => Ok(Self::EveryFourthWeek),
            "Biweekly" => Ok(Self::Biweekly),
            "Weekly" => Ok(Self::Weekly),
            "Daily" => Ok(Self::Daily),
            "OtherFrequency" => Ok(Self::OtherFrequency),
            _ => Err(QSError::InvalidValueErr(format!("Invalid frequency: {s}"))),
        }
    }
}

impl From<Frequency> for String {
    fn from(frequency: Frequency) -> Self {
        match frequency {
            Frequency::NoFrequency => "NoFrequency".to_string(),
            Frequency::Once => "Once".to_string(),
            Frequency::Annual => "Annual".to_string(),
            Frequency::Semiannual => "Semiannual".to_string(),
            Frequency::EveryFourthMonth => "EveryFourthMonth".to_string(),
            Frequency::Quarterly => "Quarterly".to_string(),
            Frequency::Bimonthly => "Bimonthly".to_string(),
            Frequency::Monthly => "Monthly".to_string(),
            Frequency::EveryFourthWeek => "EveryFourthWeek".to_string(),
            Frequency::Biweekly => "Biweekly".to_string(),
            Frequency::Weekly => "Weekly".to_string(),
            Frequency::Daily => "Daily".to_string(),
            Frequency::OtherFrequency => "OtherFrequency".to_string(),
        }
    }
}

/// Enum representing a time unit.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub enum TimeUnit {
    /// Days.
    Days,
    /// Weeks.
    Weeks,
    /// Months.
    Months,
    /// Years.
    Years,
}

impl TryFrom<String> for TimeUnit {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "Days" => Ok(Self::Days),
            "Weeks" => Ok(Self::Weeks),
            "Months" => Ok(Self::Months),
            "Years" => Ok(Self::Years),
            _ => Err(QSError::InvalidValueErr(format!("Invalid time unit: {s}"))),
        }
    }
}

impl From<TimeUnit> for String {
    fn from(time_unit: TimeUnit) -> Self {
        match time_unit {
            TimeUnit::Days => "Days".to_string(),
            TimeUnit::Weeks => "Weeks".to_string(),
            TimeUnit::Months => "Months".to_string(),
            TimeUnit::Years => "Years".to_string(),
        }
    }
}

/// Enum representing a month.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub enum Month {
    /// January.
    January = 1,
    /// February.
    February,
    /// March.
    March,
    /// April.
    April,
    /// May.
    May,
    /// June.
    June,
    /// July.
    July,
    /// August.
    August,
    /// September.
    September,
    /// October.
    October,
    /// November.
    November,
    /// December.
    December,
}

impl TryFrom<String> for Month {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "January" => Ok(Self::January),
            "February" => Ok(Self::February),
            "March" => Ok(Self::March),
            "April" => Ok(Self::April),
            "May" => Ok(Self::May),
            "June" => Ok(Self::June),
            "July" => Ok(Self::July),
            "August" => Ok(Self::August),
            "September" => Ok(Self::September),
            "October" => Ok(Self::October),
            "November" => Ok(Self::November),
            "December" => Ok(Self::December),
            _ => Err(QSError::InvalidValueErr(format!("Invalid month: {s}"))),
        }
    }
}

impl From<Month> for String {
    fn from(month: Month) -> Self {
        match month {
            Month::January => "January".to_string(),
            Month::February => "February".to_string(),
            Month::March => "March".to_string(),
            Month::April => "April".to_string(),
            Month::May => "May".to_string(),
            Month::June => "June".to_string(),
            Month::July => "July".to_string(),
            Month::August => "August".to_string(),
            Month::September => "September".to_string(),
            Month::October => "October".to_string(),
            Month::November => "November".to_string(),
            Month::December => "December".to_string(),
        }
    }
}

/// # `IMMMonth`
/// Enum representing an IMM month.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub enum IMMMonth {
    /// F.
    F = 1,
    /// G.
    G = 2,
    /// H.
    H = 3,
    /// J.
    J = 4,
    /// K.
    K = 5,
    /// M.
    M = 6,
    /// N.
    N = 7,
    /// Q.
    Q = 8,
    /// U.
    U = 9,
    /// V.
    V = 10,
    /// X.
    X = 11,
    /// Z.
    Z = 12,
}

/// # `DateGenerationRule`
/// Enum representing a date generation rule.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone, Copy, Hash)]
pub enum DateGenerationRule {
    /// Backward generation rule.
    Backward,
    /// Forward generation rule.
    Forward,
    /// Zero generation rule.
    Zero,
    /// Third Wednesday generation rule.
    ThirdWednesday,
    /// Third Wednesday inclusive generation rule.
    ThirdWednesdayInclusive,
    /// Twentieth generation rule.
    Twentieth,
    /// Twentieth IMM generation rule.
    TwentiethIMM,
    /// Old CDS generation rule.
    OldCDS,
    /// CDS generation rule.
    CDS,
    /// CDS 2015 generation rule.
    CDS2015,
}

impl TryFrom<String> for DateGenerationRule {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "Backward" => Ok(Self::Backward),
            "Forward" => Ok(Self::Forward),
            "Zero" => Ok(Self::Zero),
            "ThirdWednesday" => Ok(Self::ThirdWednesday),
            "ThirdWednesdayInclusive" => Ok(Self::ThirdWednesdayInclusive),
            "Twentieth" => Ok(Self::Twentieth),
            "TwentiethIMM" => Ok(Self::TwentiethIMM),
            "OldCDS" => Ok(Self::OldCDS),
            "CDS" => Ok(Self::CDS),
            "CDS2015" => Ok(Self::CDS2015),
            _ => Err(QSError::InvalidValueErr(format!(
                "Invalid date generation rule: {s}"
            ))),
        }
    }
}

impl From<DateGenerationRule> for String {
    fn from(date_generation_rule: DateGenerationRule) -> Self {
        match date_generation_rule {
            DateGenerationRule::Backward => "Backward".to_string(),
            DateGenerationRule::Forward => "Forward".to_string(),
            DateGenerationRule::Zero => "Zero".to_string(),
            DateGenerationRule::ThirdWednesday => "ThirdWednesday".to_string(),
            DateGenerationRule::ThirdWednesdayInclusive => "ThirdWednesdayInclusive".to_string(),
            DateGenerationRule::Twentieth => "Twentieth".to_string(),
            DateGenerationRule::TwentiethIMM => "TwentiethIMM".to_string(),
            DateGenerationRule::OldCDS => "OldCDS".to_string(),
            DateGenerationRule::CDS => "CDS".to_string(),
            DateGenerationRule::CDS2015 => "CDS2015".to_string(),
        }
    }
}

/// Enum representing a business day convention. Business day conventions are used to
/// adjust a date in case it is not a business day.
///
/// The supported business day conventions are:
/// - [`Self::Following`]: Adjusts to the next business day.
/// - [`Self::ModifiedFollowing`]: Adjusts to the next business day unless it falls in the next month, in which case it adjusts to the previous business day.
/// - [`Self::Preceding`]: Adjusts to the previous business day.
/// - [`Self::ModifiedPreceding`]: Adjusts to the previous business day unless it falls in the previous month, in which case it adjusts to the next business day.
/// - [`Self::Unadjusted`]: No adjustment is made.
/// - [`Self::HalfMonthModifiedFollowing`]: Adjusts to the next business day unless the date falls in the second half of the month, in which case it adjusts to the previous business day.
/// - [`Self::Nearest`]: Adjusts to the nearest business day. If both the following and preceding business days are equidistant, the following business day is used.
#[derive(Debug, PartialEq, Eq, Clone, Copy, Deserialize, Serialize, Hash)]
pub enum BusinessDayConvention {
    /// Following convention.
    Following,
    /// Modified following convention.
    ModifiedFollowing,
    /// Preceding convention.
    Preceding,
    /// Modified preceding convention.
    ModifiedPreceding,
    /// Unadjusted convention.
    Unadjusted,
    /// Half month modified following convention.
    HalfMonthModifiedFollowing,
    /// Nearest convention.
    Nearest,
}

impl TryFrom<String> for BusinessDayConvention {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "Following" => Ok(Self::Following),
            "ModifiedFollowing" => Ok(Self::ModifiedFollowing),
            "Preceding" => Ok(Self::Preceding),
            "ModifiedPreceding" => Ok(Self::ModifiedPreceding),
            "Unadjusted" => Ok(Self::Unadjusted),
            "HalfMonthModifiedFollowing" => Ok(Self::HalfMonthModifiedFollowing),
            "Nearest" => Ok(Self::Nearest),
            _ => Err(QSError::InvalidValueErr(format!(
                "Invalid business day convention: {s}"
            ))),
        }
    }
}

impl From<BusinessDayConvention> for String {
    fn from(business_day_convention: BusinessDayConvention) -> Self {
        match business_day_convention {
            BusinessDayConvention::Following => "Following".to_string(),
            BusinessDayConvention::ModifiedFollowing => "ModifiedFollowing".to_string(),
            BusinessDayConvention::Preceding => "Preceding".to_string(),
            BusinessDayConvention::ModifiedPreceding => "ModifiedPreceding".to_string(),
            BusinessDayConvention::Unadjusted => "Unadjusted".to_string(),
            BusinessDayConvention::HalfMonthModifiedFollowing => {
                "HalfMonthModifiedFollowing".to_string()
            }
            BusinessDayConvention::Nearest => "Nearest".to_string(),
        }
    }
}

/// Enum representing a weekday.
#[derive(Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub enum Weekday {
    /// Sunday.
    Sunday = 1,
    /// Monday.
    Monday,
    /// Tuesday.
    Tuesday,
    /// Wednesday.
    Wednesday,
    /// Thursday.
    Thursday,
    /// Friday.
    Friday,
    /// Saturday.
    Saturday,
}

impl TryFrom<String> for Weekday {
    type Error = QSError;

    fn try_from(s: String) -> Result<Self> {
        match s.as_str() {
            "Sunday" => Ok(Self::Sunday),
            "Monday" => Ok(Self::Monday),
            "Tuesday" => Ok(Self::Tuesday),
            "Wednesday" => Ok(Self::Wednesday),
            "Thursday" => Ok(Self::Thursday),
            "Friday" => Ok(Self::Friday),
            "Saturday" => Ok(Self::Saturday),
            _ => Err(QSError::InvalidValueErr(format!("Invalid weekday: {s}"))),
        }
    }
}

impl From<Weekday> for String {
    fn from(weekday: Weekday) -> Self {
        match weekday {
            Weekday::Sunday => "Sunday".to_string(),
            Weekday::Monday => "Monday".to_string(),
            Weekday::Tuesday => "Tuesday".to_string(),
            Weekday::Wednesday => "Wednesday".to_string(),
            Weekday::Thursday => "Thursday".to_string(),
            Weekday::Friday => "Friday".to_string(),
            Weekday::Saturday => "Saturday".to_string(),
        }
    }
}

impl Add<i32> for Weekday {
    type Output = i32;

    fn add(self, rhs: i32) -> Self::Output {
        self as i32 + rhs
    }
}

impl Sub<i32> for Weekday {
    type Output = i32;

    fn sub(self, rhs: i32) -> Self::Output {
        self + -rhs
    }
}

impl Add<Self> for Weekday {
    type Output = i32;

    fn add(self, rhs: Self) -> Self::Output {
        rhs as i32 + self as i32
    }
}

impl Sub<Self> for Weekday {
    type Output = i32;

    fn sub(self, rhs: Self) -> Self::Output {
        self as i32 + -(rhs as i32)
    }
}

impl Add<Weekday> for i32 {
    type Output = Self;

    fn add(self, rhs: Weekday) -> Self::Output {
        rhs + self
    }
}

impl Sub<Weekday> for i32 {
    type Output = Self;

    fn sub(self, rhs: Weekday) -> Self::Output {
        self + -(rhs as Self)
    }
}

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

    #[test]
    fn test_add() {
        assert_eq!(Weekday::Monday + 1, 3);
    }

    #[test]
    fn test_sub() {
        assert_eq!(Weekday::Monday - 1, 1);
    }

    #[test]
    fn test_add_weekday() {
        assert_eq!(Weekday::Monday + Weekday::Tuesday, 5);
    }

    #[test]
    fn test_sub_weekday() {
        assert_eq!(Weekday::Monday - Weekday::Tuesday, -1);
    }

    #[test]
    fn test_add_i32() {
        assert_eq!(1 + Weekday::Monday, 3);
    }

    #[test]
    fn test_sub_i32() {
        assert_eq!(1 - Weekday::Monday, -1);
    }
}