RustQuant_instruments 0.3.1

A Rust library for quantitative finance.
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
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2023 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
//      - LICENSE-APACHE.md
//      - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//! The generalised Black-Scholes-Merton European Option pricing model.
//!
//! The differing cost of carry factor allows for the following models:
//! - b = r
//!     - Black-Scholes 1973 stock option model.
//! - b = r - q
//!     - Merton 1973 stock option model with continuous dividend yield.
//! - b = 0
//!     - Black 1976 futures option model.
//! - b = 0, r = 0
//!     - Asay 1982 margined futures option model.
//! - b = r_d - r_f
//!     - Garman and Kohlhagen 1983 currency option model.

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// IMPORTS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

use crate::options::TypeFlag;
use crate::Instrument;
use time::Date;
use RustQuant_math::distributions::{Distribution, Gaussian};
use RustQuant_time::{utilities::today, DayCountConvention};

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS, ENUMS, AND TRAITS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// Generalised Black-Scholes-Merton European Option pricing model.
#[derive(derive_builder::Builder)]
pub struct BlackScholesMerton {
    /// The cost of carry factor.
    /// For the generalised Black-Scholes-Merton model there are five options:
    /// - b = r
    ///     - Black-Scholes 1973 stock option model.
    /// - b = r - q
    ///     - Merton 1973 stock option model with continuous dividend yield.
    /// - b = 0
    ///     - Black 1976 futures option model.
    /// - b = 0, r = 0
    ///     - Asay 1982 margined futures option model.
    /// - b = r_d - r_f
    ///     - Garman and Kohlhagen 1983 currency option model.
    pub cost_of_carry: f64,
    /// S - The underlying asset price.
    pub underlying_price: f64,
    /// K - The options strike price.
    pub strike_price: f64,
    /// sigma - The underlying asset's volatility.
    pub volatility: f64,
    /// r - The risk-free interest rate.
    pub risk_free_rate: f64,

    /// Evaluation date (optional, defaults to today t = 0).
    #[builder(default = "None")]
    pub evaluation_date: Option<Date>,
    /// The options expiration date.
    pub expiration_date: Date,

    /// Call or put flag.
    pub option_type: TypeFlag,
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// IMPLEMENTATIONS, TRAITS, AND FUNCTIONS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

impl Instrument for BlackScholesMerton {
    /// Returns the price (net present value) of the instrument.
    fn price(&self) -> f64 {
        self.price()
    }

    /// Returns the error on the NPV in case the pricing engine can
    /// provide it (e.g. Monte Carlo pricing engine).
    fn error(&self) -> Option<f64> {
        None
    }

    /// Returns the date at which the NPV is calculated.
    fn valuation_date(&self) -> Date {
        self.evaluation_date.unwrap_or(today())
    }

    /// Instrument type.
    fn instrument_type(&self) -> &'static str {
        "Black-Scholes-Merton European Option"
    }
}

impl BlackScholesMerton {
    /// New European Option
    #[allow(clippy::too_many_arguments)]
    #[must_use]
    pub fn new(
        cost_of_carry: f64,
        underlying_price: f64,
        strike_price: f64,
        volatility: f64,
        risk_free_rate: f64,
        evaluation_date: Option<Date>,
        expiration_date: Date,
        option_type: TypeFlag,
    ) -> Self {
        Self {
            cost_of_carry,
            underlying_price,
            strike_price,
            volatility,
            risk_free_rate,
            evaluation_date,
            expiration_date,
            option_type,
        }
    }

    /// Generalised Black-Scholes European Option Price.
    #[must_use]
    pub fn price(&self) -> f64 {
        let (S, K, _, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();
        let n = Gaussian::default();

        match self.option_type {
            TypeFlag::Call => S * ((b - r) * T).exp() * n.cdf(d1) - K * (-r * T).exp() * n.cdf(d2),
            TypeFlag::Put => {
                -S * ((b - r) * T).exp() * n.cdf(-d1) + K * (-r * T).exp() * n.cdf(-d2)
            }
        }
    }

    /// Implied volatility.
    pub fn implied_volatility(&self, price: f64) -> f64 {
        crate::options::implied_volatility(
            price,
            self.underlying_price,
            self.strike_price,
            self.year_fraction(),
            self.risk_free_rate,
            self.option_type,
        )
    }

    /// Compute the year fraction between two dates.
    #[must_use]
    pub fn year_fraction(&self) -> f64 {
        DayCountConvention::default().day_count_factor(
            self.evaluation_date.unwrap_or(today()),
            self.expiration_date,
        )
    }

    // Compute d1 and d2.
    #[must_use]
    fn d1_d2(&self) -> (f64, f64) {
        let (S, K, v, _, b) = self.unpack();

        // Compute time to maturity.
        let T = self.year_fraction();

        let d1 = (1.0 / (v * T.sqrt())) * ((S / K).ln() + (b + 0.5 * v.powi(2)) * T);
        let d2 = d1 - v * T.sqrt();

        (d1, d2)
    }

    // Unpack struct to get option parameters.
    #[must_use]
    fn unpack(&self) -> (f64, f64, f64, f64, f64) {
        (
            self.underlying_price,
            self.strike_price,
            self.volatility,
            self.risk_free_rate,
            self.cost_of_carry,
        )
    }

    /// Delta of generalised Black-Scholes European Option.
    #[must_use]
    pub fn delta(&self) -> f64 {
        let (_, _, _, r, b) = self.unpack();
        let T = self.year_fraction();
        let d1 = self.d1_d2().0;
        let n = Gaussian::default();

        match self.option_type {
            TypeFlag::Call => ((b - r) * T).exp() * n.cdf(d1),
            TypeFlag::Put => ((b - r) * T).exp() * (n.cdf(d1) - 1.0),
        }
    }

    /// Vanna of generalised Black-Scholes European Option.
    /// Also known as DdeltaDvol.
    #[must_use]
    pub fn vanna(&self) -> f64 {
        let (_, _, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();
        let n = Gaussian::default();

        -((b - r) * T).exp() * n.pdf(d1) * d2 / v
    }

    /// Charm of generalised Black-Scholes European Option.
    /// Also known as DdeltaDtime, delta decay or delta bleed.
    #[must_use]
    pub fn charm(&self) -> f64 {
        let (_, _, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();
        let n = Gaussian::default();

        match self.option_type {
            TypeFlag::Call => {
                ((b - r) * T).exp()
                    * (n.pdf(d1) * ((b / (v * T.sqrt())) - (d2 / (2.0 * T))) + (b - r) * n.cdf(d1))
            }
            TypeFlag::Put => {
                ((b - r) * T).exp()
                    * (n.pdf(d1) * ((b / (v * T.sqrt())) - (d2 / (2.0 * T))) - (b - r) * n.cdf(-d1))
            }
        }
    }

    /// Lambda of generalised Black-Scholes European Option.
    /// Also known as elasticity or leverage.
    #[must_use]
    pub fn lambda(&self) -> f64 {
        self.delta() * self.underlying_price / self.price()
    }

    /// Gamma of generalised Black-Scholes European Option.
    /// Also known as convexity.
    #[must_use]
    pub fn gamma(&self) -> f64 {
        let n = Gaussian::default();
        let (S, _, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, _) = self.d1_d2();

        ((b - r) * T).exp() * n.pdf(d1) / (S * v * T.sqrt())
    }

    /// Gamma percent of generalised Black-Scholes European Option.
    #[must_use]
    pub fn gamma_percent(&self) -> f64 {
        self.gamma() * self.underlying_price / 100.0
    }

    /// Zomma of generalised Black-Scholes European Option.
    /// Also known as DgammaDvol.
    #[must_use]
    pub fn zomma(&self) -> f64 {
        let (d1, d2) = self.d1_d2();
        self.gamma() * ((d1 * d2 - 1.0) / self.volatility)
    }

    /// Zomma percent of generalised Black-Scholes European Option.
    #[must_use]
    pub fn zomma_percent(&self) -> f64 {
        self.zomma() * self.underlying_price / 100.0
    }

    /// Speed of generalised Black-Scholes European Option.
    /// Also known as DgammaDspot.
    #[must_use]
    pub fn speed(&self) -> f64 {
        let (S, _, v, _, _) = self.unpack();
        let T = self.year_fraction();
        let (d1, _) = self.d1_d2();

        let gamma = self.gamma();

        -gamma * (1.0 + d1 / (v * T.sqrt())) / S
    }

    /// Colour of generalised Black-Scholes European Option.
    /// Also known as DgammaDtime.
    #[must_use]
    pub fn colour(&self) -> f64 {
        let (_, _, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();

        let gamma = self.gamma();

        gamma * (r - b + b * d1 / (v * T.sqrt()) + (1.0 - d1 * d2) / (2.0 * T))
    }

    /// Vega of generalised Black-Scholes European Option.
    /// Also known as zeta.
    #[must_use]
    pub fn vega(&self) -> f64 {
        let (S, _, _, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, _) = self.d1_d2();

        let n = Gaussian::default();

        S * ((b - r) * T).exp() * n.pdf(d1) * T.sqrt()
    }

    /// Vomma of generalised Black-Scholes European Option.
    /// Also known as DvegaDvol.
    #[must_use]
    pub fn vomma(&self) -> f64 {
        let (d1, d2) = self.d1_d2();

        self.vega() * d1 * d2 / self.volatility
    }

    /// Ultima of generalised Black-Scholes European Option.
    /// Also known as DvommaDvol.
    #[must_use]
    pub fn ultima(&self) -> f64 {
        let (d1, d2) = self.d1_d2();

        (self.vomma() / self.volatility) * (d1 * d2 - d1 / d2 + d2 / d1 - 1.0)
    }

    /// Vega Bleed of the generalised Black-Scholes European option.
    /// Also known as DvegaDtime.
    #[must_use]
    pub fn vega_bleed(&self) -> f64 {
        let (_, _, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();

        self.vega() * (r - b + b * d1 / (v * T.sqrt()) - (d1 * d2 + 1.0) / (2.0 * T))
    }

    /// Theta of the generalised Black-Scholes European option.
    /// Also known as Expected Bleed.
    #[must_use]
    pub fn theta(&self) -> f64 {
        let (S, K, v, r, b) = self.unpack();
        let T = self.year_fraction();
        let (d1, d2) = self.d1_d2();

        let n = Gaussian::default();

        match self.option_type {
            TypeFlag::Call => {
                -S * ((b - r) * T).exp() * n.pdf(d1) * v / (2.0 * T.sqrt())
                    - (b - r) * S * ((b - r) * T).exp() * n.cdf(d1)
                    - r * K * (-r * T).exp() * n.cdf(d2)
            }
            TypeFlag::Put => {
                -S * ((b - r) * T).exp() * n.pdf(d1) * v / (2.0 * T.sqrt())
                    + (b - r) * S * ((b - r) * T).exp() * n.cdf(-d1)
                    + r * K * (-r * T).exp() * n.cdf(-d2)
            }
        }
    }

    /// Rho of the generalised Black-Scholes European option.
    #[must_use]
    pub fn rho(&self) -> f64 {
        let T = self.year_fraction();

        match self.option_type {
            TypeFlag::Call => {
                self.strike_price
                    * T
                    * (-self.risk_free_rate * T).exp()
                    * Gaussian::default().cdf(self.d1_d2().1)
            }
            TypeFlag::Put => {
                -self.strike_price
                    * T
                    * (-self.risk_free_rate * T).exp()
                    * Gaussian::default().cdf(-self.d1_d2().1)
            }
        }
    }

    /// Phi of the generalised Black-Scholes European option.
    /// Also known as Rho-2.
    #[must_use]
    pub fn phi(&self) -> f64 {
        let (S, _, _, r, b) = self.unpack();
        let T = self.year_fraction();

        let (d1, _) = self.d1_d2();

        match self.option_type {
            TypeFlag::Call => -T * S * ((b - r) * T).exp() * Gaussian::default().cdf(d1),
            TypeFlag::Put => T * S * ((b - r) * T).exp() * Gaussian::default().cdf(-d1),
        }
    }

    /// Zeta of the generalised Black-Scholes European option.
    /// Also known as the in-the-money probability.
    #[must_use]
    pub fn zeta(&self) -> f64 {
        let n = Gaussian::default();

        match self.option_type {
            TypeFlag::Call => n.cdf(self.d1_d2().1),
            TypeFlag::Put => n.cdf(-self.d1_d2().1),
        }
    }

    /// Strike Delta of the generalised Black-Scholes European option.
    /// Also known as Dual Delta or Discounted Probability.
    #[must_use]
    pub fn strike_delta(&self) -> f64 {
        let n = Gaussian::default();

        let T = self.year_fraction();

        match self.option_type {
            TypeFlag::Call => -(-self.risk_free_rate * T).exp() * n.cdf(self.d1_d2().1),
            TypeFlag::Put => (-self.risk_free_rate * T).exp() * n.cdf(-self.d1_d2().1),
        }
    }

    /// Strike Gamma of the generalised Black-Scholes European option.
    #[must_use]
    pub fn strike_gamma(&self) -> f64 {
        let n = Gaussian::default();
        let T = self.year_fraction();

        n.pdf(self.d1_d2().1) * (-self.risk_free_rate * T).exp()
            / (self.strike_price * self.volatility * T.sqrt())
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// UNIT TESTS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#[cfg(test)]
mod tests_black_scholes_merton {
    use super::*;
    use time::Duration;
    use RustQuant_utils::assert_approx_equal;

    #[test]
    fn black_scholes_1973() {
        // Values from Haug
        let bsm = BlackScholesMerton::new(
            0.08,
            60.0,
            65.0,
            0.3,
            0.08,
            None,
            today() + Duration::days(91),
            TypeFlag::Call,
        );
        assert_approx_equal!(bsm.price(), 2.121846776001, 1e-2);
    }

    #[test]
    fn merton_1973() {
        // Values from Haug
        let bsm = BlackScholesMerton::new(
            0.1 - 0.05,
            100.0,
            95.0,
            0.2,
            0.1,
            None,
            today() + Duration::days(182),
            TypeFlag::Put,
        );
        assert_approx_equal!(bsm.price(), 2.456571166461579, 1e-2);
    }
}