stochastic-rs-quant 2.0.0-rc.1

Quantitative finance: pricing, calibration, vol surfaces, instruments.
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
//! # Snell Envelope (American Options)
//!
//! Discrete-time Snell envelope recursion on a CRR binomial tree:
//! $$
//! Y_N = g(S_N),\qquad
//! Y_i = \max\left(g(S_i), e^{-r\Delta t}\mathbb{E}^{\mathbb{Q}}[Y_{i+1}\mid\mathcal{F}_i]\right).
//! $$
//!
//! With two-state transition each step:
//! $$
//! \mathbb{E}^{\mathbb{Q}}[Y_{i+1}\mid\mathcal{F}_i]
//! = pY_{i+1}^{u} + (1-p)Y_{i+1}^{d},
//! $$
//! where, in the CRR tree,
//! $$
//! u=e^{\sigma\sqrt{\Delta t}},\quad d=u^{-1},\quad
//! p=\frac{e^{(r-q)\Delta t}-d}{u-d}.
//! $$
//!
//! Source:
//! - Snell envelope / optimal stopping foundation
//! - Cox-Ross-Rubinstein binomial tree discretization

use crate::OptionType;
use crate::traits::PricerExt;
use crate::traits::TimeExt;

#[derive(Debug, Clone)]
pub struct SnellEnvelopeResult {
  pub price: f64,
  pub european_price: f64,
  pub early_exercise_premium: f64,
  /// Exercise boundary as `(time_in_years, critical_stock_price)` pairs.
  pub exercise_boundary: Vec<(f64, f64)>,
}

#[derive(Debug, Clone)]
pub struct SnellEnvelopePricer {
  /// Spot level $S_0$.
  pub s: f64,
  /// Volatility $\sigma$.
  pub v: f64,
  /// Strike $K$.
  pub k: f64,
  /// Risk-free rate $r$.
  pub r: f64,
  /// Continuous dividend yield $q$.
  pub q: Option<f64>,
  /// Number of binomial time steps.
  pub steps: usize,
  /// Time-to-maturity in years.
  pub tau: Option<f64>,
  /// Evaluation date (optional if `tau` is set).
  pub eval: Option<chrono::NaiveDate>,
  /// Expiration date (optional if `tau` is set).
  pub expiration: Option<chrono::NaiveDate>,
  /// Option direction.
  pub option_type: OptionType,
}

impl SnellEnvelopePricer {
  #[allow(clippy::too_many_arguments)]
  pub fn new(
    s: f64,
    v: f64,
    k: f64,
    r: f64,
    q: Option<f64>,
    steps: usize,
    tau: Option<f64>,
    eval: Option<chrono::NaiveDate>,
    expiration: Option<chrono::NaiveDate>,
    option_type: OptionType,
  ) -> Self {
    assert!(s.is_finite() && s > 0.0, "s must be finite and positive");
    assert!(v.is_finite() && v > 0.0, "v must be finite and positive");
    assert!(k.is_finite() && k > 0.0, "k must be finite and positive");
    assert!(r.is_finite(), "r must be finite");
    if let Some(q) = q {
      assert!(q.is_finite(), "q must be finite");
    }
    assert!(steps > 0, "steps must be > 0");

    Self {
      s,
      v,
      k,
      r,
      q,
      steps,
      tau,
      eval,
      expiration,
      option_type,
    }
  }

  /// Builder for fluent construction with sensible defaults.
  pub fn builder(s: f64, v: f64, k: f64, r: f64) -> SnellEnvelopePricerBuilder {
    SnellEnvelopePricerBuilder {
      s,
      v,
      k,
      r,
      q: None,
      steps: 100,
      tau: None,
      eval: None,
      expiration: None,
      option_type: OptionType::Call,
    }
  }

  fn price_american(&self, option_type: OptionType) -> f64 {
    let tau = self.tau_or_from_dates();
    assert!(tau.is_finite() && tau > 0.0, "tau must be positive");

    let dt = tau / self.steps as f64;
    let sqrt_dt = dt.sqrt();
    let u = (self.v * sqrt_dt).exp();
    let d = 1.0 / u;
    let disc = (-self.r * dt).exp();
    let growth = ((self.r - self.q.unwrap_or(0.0)) * dt).exp();
    let p = (growth - d) / (u - d);
    assert!(
      (0.0..=1.0).contains(&p),
      "risk-neutral probability out of range: p={p}. Increase steps or adjust parameters."
    );

    let mut values = vec![0.0_f64; self.steps + 1];
    let mut s_node = self.s * d.powi(self.steps as i32);
    let ud_ratio = u / d;
    for val in values.iter_mut().take(self.steps + 1) {
      *val = payoff(option_type, s_node, self.k);
      s_node *= ud_ratio;
    }

    for i in (0..self.steps).rev() {
      let mut s_i0 = self.s * d.powi(i as i32);
      for j in 0..=i {
        let continuation = disc * (p * values[j + 1] + (1.0 - p) * values[j]);
        let exercise = payoff(option_type, s_i0, self.k);
        values[j] = continuation.max(exercise);
        s_i0 *= ud_ratio;
      }
    }

    values[0]
  }

  pub fn price_detailed(&self, option_type: OptionType) -> SnellEnvelopeResult {
    let tau = self.tau_or_from_dates();
    assert!(tau.is_finite() && tau > 0.0, "tau must be positive");

    let dt = tau / self.steps as f64;
    let sqrt_dt = dt.sqrt();
    let u = (self.v * sqrt_dt).exp();
    let d = 1.0 / u;
    let disc = (-self.r * dt).exp();
    let growth = ((self.r - self.q.unwrap_or(0.0)) * dt).exp();
    let p = (growth - d) / (u - d);
    assert!(
      (0.0..=1.0).contains(&p),
      "risk-neutral probability out of range: p={p}. Increase steps or adjust parameters."
    );
    let ud_ratio = u / d;

    let mut am_values = vec![0.0_f64; self.steps + 1];
    let mut eu_values = vec![0.0_f64; self.steps + 1];
    let mut s_node = self.s * d.powi(self.steps as i32);
    for idx in 0..=self.steps {
      let pv = payoff(option_type, s_node, self.k);
      am_values[idx] = pv;
      eu_values[idx] = pv;
      s_node *= ud_ratio;
    }

    let mut exercise_boundary = Vec::new();

    for i in (0..self.steps).rev() {
      let mut s_i0 = self.s * d.powi(i as i32);
      let mut boundary_s = f64::NAN;

      for j in 0..=i {
        let am_cont = disc * (p * am_values[j + 1] + (1.0 - p) * am_values[j]);
        let eu_cont = disc * (p * eu_values[j + 1] + (1.0 - p) * eu_values[j]);
        let exercise = payoff(option_type, s_i0, self.k);

        am_values[j] = am_cont.max(exercise);
        eu_values[j] = eu_cont;

        if exercise > am_cont + 1e-12 {
          match option_type {
            OptionType::Put => {
              if boundary_s.is_nan() || s_i0 > boundary_s {
                boundary_s = s_i0;
              }
            }
            OptionType::Call => {
              if boundary_s.is_nan() || s_i0 < boundary_s {
                boundary_s = s_i0;
              }
            }
          }
        }

        s_i0 *= ud_ratio;
      }

      if boundary_s.is_finite() {
        exercise_boundary.push(((i as f64) * dt, boundary_s));
      }
    }

    exercise_boundary.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());

    SnellEnvelopeResult {
      price: am_values[0],
      european_price: eu_values[0],
      early_exercise_premium: am_values[0] - eu_values[0],
      exercise_boundary,
    }
  }
}

#[derive(Debug, Clone)]
pub struct SnellEnvelopePricerBuilder {
  s: f64,
  v: f64,
  k: f64,
  r: f64,
  q: Option<f64>,
  steps: usize,
  tau: Option<f64>,
  eval: Option<chrono::NaiveDate>,
  expiration: Option<chrono::NaiveDate>,
  option_type: OptionType,
}

impl SnellEnvelopePricerBuilder {
  pub fn q(mut self, q: f64) -> Self {
    self.q = Some(q);
    self
  }
  pub fn steps(mut self, steps: usize) -> Self {
    self.steps = steps;
    self
  }
  pub fn tau(mut self, tau: f64) -> Self {
    self.tau = Some(tau);
    self
  }
  pub fn eval(mut self, eval: chrono::NaiveDate) -> Self {
    self.eval = Some(eval);
    self
  }
  pub fn expiration(mut self, expiration: chrono::NaiveDate) -> Self {
    self.expiration = Some(expiration);
    self
  }
  pub fn option_type(mut self, option_type: OptionType) -> Self {
    self.option_type = option_type;
    self
  }
  pub fn build(self) -> SnellEnvelopePricer {
    SnellEnvelopePricer::new(
      self.s,
      self.v,
      self.k,
      self.r,
      self.q,
      self.steps,
      self.tau,
      self.eval,
      self.expiration,
      self.option_type,
    )
  }
}

impl PricerExt for SnellEnvelopePricer {
  fn calculate_call_put(&self) -> (f64, f64) {
    (
      self.price_american(OptionType::Call),
      self.price_american(OptionType::Put),
    )
  }

  fn calculate_price(&self) -> f64 {
    self.price_american(self.option_type)
  }
}

impl TimeExt for SnellEnvelopePricer {
  fn tau(&self) -> Option<f64> {
    self.tau
  }

  fn eval(&self) -> Option<chrono::NaiveDate> {
    self.eval
  }

  fn expiration(&self) -> Option<chrono::NaiveDate> {
    self.expiration
  }
}

fn payoff(option_type: OptionType, s: f64, k: f64) -> f64 {
  match option_type {
    OptionType::Call => (s - k).max(0.0),
    OptionType::Put => (k - s).max(0.0),
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::pricing::bsm::BSMCoc;
  use crate::pricing::bsm::BSMPricer;

  #[test]
  fn american_put_is_at_least_european_put() {
    let amer = SnellEnvelopePricer::new(
      100.0,
      0.2,
      100.0,
      0.03,
      Some(0.01),
      800,
      Some(1.0),
      None,
      None,
      OptionType::Put,
    )
    .calculate_price();

    let euro = BSMPricer::new(
      100.0,
      0.2,
      100.0,
      0.03,
      None,
      None,
      Some(0.01),
      Some(1.0),
      None,
      None,
      OptionType::Put,
      BSMCoc::Merton1973,
    )
    .calculate_price();

    assert!(amer + 1e-10 >= euro);
  }

  #[test]
  fn american_call_matches_european_without_dividend() {
    let amer = SnellEnvelopePricer::new(
      100.0,
      0.2,
      100.0,
      0.05,
      Some(0.0),
      1200,
      Some(1.0),
      None,
      None,
      OptionType::Call,
    )
    .calculate_price();

    let euro = BSMPricer::new(
      100.0,
      0.2,
      100.0,
      0.05,
      None,
      None,
      Some(0.0),
      Some(1.0),
      None,
      None,
      OptionType::Call,
      BSMCoc::Merton1973,
    )
    .calculate_price();

    assert!((amer - euro).abs() < 5e-2);
  }

  #[test]
  fn price_detailed_returns_exercise_boundary_and_premium() {
    let pricer = SnellEnvelopePricer::new(
      100.0,
      0.2,
      100.0,
      0.03,
      Some(0.01),
      800,
      Some(1.0),
      None,
      None,
      OptionType::Put,
    );
    let result = pricer.price_detailed(OptionType::Put);

    assert!(result.price > 0.0);
    assert!(result.european_price > 0.0);
    assert!(result.early_exercise_premium >= -1e-10);
    assert!(result.price >= result.european_price - 1e-10);
    assert!(!result.exercise_boundary.is_empty());

    for &(t, s_star) in &result.exercise_boundary {
      assert!((0.0..1.0).contains(&t));
      assert!(s_star > 0.0 && s_star <= 100.0);
    }

    let times: Vec<f64> = result.exercise_boundary.iter().map(|p| p.0).collect();
    for w in times.windows(2) {
      assert!(w[0] <= w[1]);
    }
  }

  #[test]
  fn american_call_can_exceed_european_with_dividend() {
    let amer = SnellEnvelopePricer::new(
      100.0,
      0.25,
      90.0,
      0.03,
      Some(0.08),
      1000,
      Some(1.0),
      None,
      None,
      OptionType::Call,
    )
    .calculate_price();

    let euro = BSMPricer::new(
      100.0,
      0.25,
      90.0,
      0.03,
      None,
      None,
      Some(0.08),
      Some(1.0),
      None,
      None,
      OptionType::Call,
      BSMCoc::Merton1973,
    )
    .calculate_price();

    assert!(amer + 1e-10 >= euro);
  }
}