quantrs 0.1.8

A tiny 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Module for handling the underlying asset of an option and its dividend properties.
//!
//! An `Instrument` represents an underlying asset with dividend properties. It is used in option pricing models to calculate the price of an option.
//!
//! ## References
//! - [Wikipedia - Dividend yield](https://en.wikipedia.org/wiki/Dividend_yield)
//!
//! ## Example
//!
//! ```
//! use quantrs::options::Instrument;
//!
//! let asset1 = Instrument::new().with_spot(100.0);
//! let asset2 = Instrument::new().with_spot(110.0);
//!
//! let instrument = Instrument::new()
//!     .with_spot(100.0)
//!     .with_continuous_dividend_yield(0.2)
//!     .with_discrete_dividend_yield(0.0)
//!     .with_dividend_times(vec![])
//!     .with_weighted_assets(vec![(asset1, 0.5), (asset2, 0.5)]);
//! ```

use core::f64;
use rand::rngs::ThreadRng;
use rand_distr::{Distribution, Normal};

/// A struct representing an instrument with dividend properties.
#[derive(Debug, Default, Clone)]
pub struct Instrument {
    /// Current price of the underlying asset or future price at time 0.
    pub spot: Vec<f64>,
    /// Continuous dividend yield where the dividend amount is proportional to the level of the underlying asset (e.g., 0.02 for 2%).
    pub continuous_dividend_yield: f64,
    /// Discrete proportional dividend yield (e.g., 0.02 for 2%).
    pub discrete_dividend_yield: f64,
    /// Times at which discrete dividends are paid.
    pub dividend_times: Vec<f64>,
    /// Assets and their weights.
    pub assets: Vec<(Instrument, f64)>,
    /// Whether the assets are sorted by performance.
    pub sorted: bool,
}

impl Instrument {
    /// Create a new simple `Instrument` with default values.
    ///
    /// # Returns
    ///
    /// A new `Instrument`.
    pub fn new() -> Self {
        Self {
            spot: vec![0.0],
            continuous_dividend_yield: 0.0,
            discrete_dividend_yield: 0.0,
            dividend_times: Vec::new(),
            assets: Vec::new(),
            sorted: false,
        }
    }

    /// Set the spot price of the instrument.
    ///
    /// # Arguments
    ///
    /// * `spot` - The spot price of the instrument (i.e., the current price of the underlying asset or future price at time 0).
    ///
    /// # Returns
    ///
    /// The instrument with the spot price set.
    pub fn with_spot(mut self, spot: f64) -> Self {
        self.spot = vec![spot];
        self
    }

    /// Set the spot price of the instrument.
    ///
    /// # Arguments
    ///
    /// * `spot` - The spot price of the instrument (i.e., the current price of the underlying asset or future price at time 0).
    ///
    /// # Returns
    ///
    /// The instrument with the spot price set.
    pub fn with_spots(mut self, spot: Vec<f64>) -> Self {
        self.spot = spot;
        self
    }

    /// Get the maximum spot price of the instrument.
    ///     
    /// # Returns
    ///
    /// The maximum spot price of the instrument.
    pub fn max_spot(&self) -> f64 {
        *self.spot.iter().max_by(|x, y| x.total_cmp(y)).unwrap()
    }

    /// Get the minimum spot price of the instrument.
    ///
    /// # Returns
    ///
    /// The minimum spot price of the instrument.
    pub fn min_spot(&self) -> f64 {
        *self.spot.iter().min_by(|x, y| x.total_cmp(y)).unwrap()
    }

    /// Set the continuous dividend yield of the instrument.
    ///
    /// # Arguments
    ///
    /// * `yield_` - The continuous dividend yield of the instrument.
    ///
    /// # Returns
    ///
    /// The instrument with the continuous dividend yield set.
    pub fn with_continuous_dividend_yield(mut self, yield_: f64) -> Self {
        self.continuous_dividend_yield = yield_;
        self.assets.iter_mut().for_each(|(a, _)| {
            a.continuous_dividend_yield = yield_;
        });
        self
    }

    /// Alias for `with_continuous_dividend_yield`.
    pub fn with_cont_yield(self, yield_: f64) -> Self {
        self.with_continuous_dividend_yield(yield_)
    }

    /// Set the discrete dividend yield of the instrument.
    ///
    /// # Arguments
    ///
    /// * `yield_` - The discrete dividend yield of the instrument.
    ///
    /// # Returns
    ///
    /// The instrument with the discrete dividend yield set.
    pub fn with_discrete_dividend_yield(mut self, yield_: f64) -> Self {
        self.discrete_dividend_yield = yield_;
        self
    }

    /// Set the dividend times of the instrument.
    ///
    /// # Arguments
    ///
    /// * `times` - The dividend times of the instrument.
    ///
    /// # Returns
    ///
    /// The instrument with the dividend times set.
    pub fn with_dividend_times(mut self, times: Vec<f64>) -> Self {
        self.dividend_times = times;
        self
    }

    /// Set the assets of the instrument.
    ///
    /// # Arguments
    ///
    /// * `assets` - The assets of the instrument.
    ///
    /// # Returns
    ///
    /// The instrument with the assets set.
    pub fn with_assets(mut self, assets: Vec<Instrument>) -> Self {
        if assets.is_empty() {
            return self;
        }

        let weight = 1.0 / assets.len() as f64;
        self.assets = assets.iter().map(|asset| (asset.clone(), weight)).collect();
        let new_spot = self.assets.iter().map(|(a, w)| a.spot() * w).sum::<f64>();
        self.spot = vec![new_spot];

        self.sort_assets_by_performance();
        self
    }

    /// Set the assets and their weights of the instrument.
    ///
    /// # Arguments
    ///
    /// * `assets` - The assets and their weights of the instrument.
    ///
    /// # Returns
    ///
    /// The instrument with the assets and their weights set.
    pub fn with_weighted_assets(mut self, assets: Vec<(Instrument, f64)>) -> Self {
        if assets.is_empty() {
            return self;
        }

        self.assets = assets;
        self.sort_assets_by_performance();
        self
    }

    /// Sort the assets by their performance at the payment date.
    pub fn sort_assets_by_performance(&mut self) {
        self.assets
            .sort_by(|a, b| b.0.spot.partial_cmp(&a.0.spot).unwrap());
        self.spot = vec![(self.assets.iter().map(|(a, w)| a.spot() * w).sum::<f64>())];
        self.sorted = true;
    }

    /// Get best performing asset.
    ///
    /// # Returns
    ///
    /// The best performing asset.
    pub fn best_performer(&self) -> &Instrument {
        if self.assets.is_empty() {
            return self;
        }
        if !self.sorted {
            panic!("Assets are not sorted");
        }
        &self.assets.first().unwrap().0
    }

    /// Get worst performing asset.
    ///
    /// # Returns
    ///
    /// The worst performing asset.
    pub fn worst_performer(&self) -> &Instrument {
        if self.assets.is_empty() {
            return self;
        }

        if !self.sorted {
            panic!("Assets are not sorted");
        }
        &self.assets.last().unwrap().0
    }

    /// Get the best-performing asset (mutable).
    ///
    /// # Returns
    ///
    /// The best-performing asset.
    pub fn best_performer_mut(&mut self) -> &mut Instrument {
        if self.assets.is_empty() {
            return self;
        }
        if !self.sorted {
            panic!("Assets are not sorted");
        }
        &mut self.assets.first_mut().unwrap().0
    }

    /// Get the worst-performing asset (mutable).
    ///
    /// # Returns
    ///
    /// The worst-performing asset.
    pub fn worst_performer_mut(&mut self) -> &mut Instrument {
        if self.assets.is_empty() {
            return self;
        }
        if !self.sorted {
            panic!("Assets are not sorted");
        }
        &mut self.assets.last_mut().unwrap().0
    }

    /// Calculate the adjusted spot price.
    ///
    /// # Arguments
    ///
    /// * `instrument` - The instrument to calculate the adjusted spot price for.
    /// * `ttm` - Time to maturity of the option.
    ///
    /// # Returns
    ///
    /// The adjusted spot price.
    pub fn calculate_adjusted_spot(&self, ttm: f64) -> f64 {
        let n_dividends = self.dividend_times.iter().filter(|&&t| t <= ttm).count() as f64;
        self.spot() * (1.0 - self.discrete_dividend_yield).powf(n_dividends)
    }

    /// Return current spot price.
    ///
    /// # Returns
    ///
    /// The current spot price.
    pub fn spot(&self) -> f64 {
        *self.spot.first().unwrap()
    }

    /// Return terminal spot price.
    ///
    /// # Returns
    ///
    /// The terminal spot price.
    pub fn terminal_spot(&self) -> f64 {
        *self.spot.last().unwrap()
    }

    /// Simulate random asset prices (Euler method)
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `volatility` - Volatility.
    ///
    /// # Returns
    ///
    /// A vector of simulated asset prices.
    pub fn euler_simulation(
        &self,
        rng: &mut ThreadRng,
        risk_free_rate: f64,
        volatility: f64,
        steps: usize,
    ) -> Vec<f64> {
        let normal = Normal::new(0.0, 1.0).unwrap();
        let dt: f64 = 1.0 / steps as f64; // Daily time step
        let mut prices = vec![self.spot(); steps];
        for i in 1..steps {
            let z = normal.sample(rng);
            prices[i] = prices[i - 1]
                * (1.0
                    + (risk_free_rate - self.continuous_dividend_yield) * dt
                    + volatility * z * dt.sqrt());
        }
        prices
    }

    /// Simulate random asset prices' logarithms
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// A vector of simulated asset prices' logarithms.
    pub fn log_simulation(
        &self,
        rng: &mut ThreadRng,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> Vec<f64> {
        let dt = time_to_maturity / steps as f64; // Time step
        let normal: Normal<f64> = Normal::new(0.0, dt.sqrt()).unwrap(); // Adjusted standard deviation
        let mut logs = vec![self.spot().ln(); steps];
        for i in 1..steps {
            let z = normal.sample(rng);
            logs[i] = logs[i - 1]
                + (risk_free_rate - self.continuous_dividend_yield - 0.5 * volatility.powi(2)) * dt
                + volatility * z;
        }
        logs.iter().map(|log| log.exp()).collect()
    }

    /// Average asset prices
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `method` - Simulation method.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// The average asset price.
    pub fn simulate_arithmetic_average(
        &self,
        rng: &mut ThreadRng,
        method: SimMethod,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> f64 {
        let prices = match method {
            SimMethod::Milstein => unimplemented!("Milstein method not implemented"),
            SimMethod::Euler => self.euler_simulation(rng, risk_free_rate, volatility, steps),
            SimMethod::Log => {
                self.log_simulation(rng, volatility, time_to_maturity, risk_free_rate, steps)
            }
        };

        prices.iter().sum::<f64>() / (prices.len()) as f64
    }

    /// Geometric average asset prices
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `method` - Simulation method.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// The geometric average asset price.
    pub fn simulate_geometric_average(
        &self,
        rng: &mut ThreadRng,
        method: SimMethod,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> f64 {
        let prices = match method {
            SimMethod::Milstein => unimplemented!("Milstein method not implemented"),
            SimMethod::Euler => self.euler_simulation(rng, risk_free_rate, volatility, steps),
            SimMethod::Log => {
                self.log_simulation(rng, volatility, time_to_maturity, risk_free_rate, steps)
            }
        };

        (self.spot.iter().map(|price| price.ln()).sum::<f64>() / self.spot.len() as f64).exp()
    }

    /// Average asset prices (mutates the underlying instrument)
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `method` - Simulation method.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// The average asset price.
    pub fn simulate_arithmetic_average_mut(
        &mut self,
        rng: &mut ThreadRng,
        method: SimMethod,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> f64 {
        self.spot = match method {
            SimMethod::Milstein => unimplemented!("Milstein method not implemented"),
            SimMethod::Euler => self.euler_simulation(rng, risk_free_rate, volatility, steps),
            SimMethod::Log => {
                self.log_simulation(rng, volatility, time_to_maturity, risk_free_rate, steps)
            }
        };

        self.spot.iter().sum::<f64>() / (self.spot.len()) as f64
    }

    /// Geometric asset prices (mutates the underlying instrument)
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `method` - Simulation method.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// The geometric average asset price.
    pub fn simulate_geometric_average_mut(
        &mut self,
        rng: &mut ThreadRng,
        method: SimMethod,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> f64 {
        self.spot = match method {
            SimMethod::Milstein => unimplemented!("Milstein method not implemented"),
            SimMethod::Euler => self.euler_simulation(rng, risk_free_rate, volatility, steps),
            SimMethod::Log => {
                self.log_simulation(rng, volatility, time_to_maturity, risk_free_rate, steps)
            }
        };

        (self.spot.iter().map(|price| price.ln()).sum::<f64>() / self.spot.len() as f64).exp()
    }

    /// Directly simulate the asset price using the geometric Brownian motion formula
    ///
    /// # Arguments
    ///
    /// * `rng` - Random number generator.
    /// * `volatility` - Volatility.
    /// * `time_to_maturity` - Time to maturity.
    /// * `risk_free_rate` - Risk-free rate.
    /// * `steps` - Number of steps.
    ///
    /// # Returns
    ///
    /// The simulated asset price.
    pub fn simulate_geometric_brownian_motion(
        &self,
        rng: &mut ThreadRng,
        volatility: f64,
        time_to_maturity: f64,
        risk_free_rate: f64,
        steps: usize,
    ) -> f64 {
        let normal = Normal::new(0.0, 1.0).unwrap();
        let dt = time_to_maturity / steps as f64;
        let mut price = self.spot();
        for _ in 0..steps {
            let z = normal.sample(rng);
            price *= ((risk_free_rate - self.continuous_dividend_yield - 0.5 * volatility.powi(2))
                * dt
                + volatility * z * dt.sqrt())
            .exp();
        }
        price
    }
}

/// Enum for different simulation methods.
pub enum SimMethod {
    Milstein,
    Euler,
    Log,
}