quant_opts/models/black_scholes/mod.rs
1//! Black–Scholes–Merton model implementation for vanilla options.
2//!
3//! This module provides pricing, Greeks, and implied-volatility solvers
4//! expressed in terms of the core domain types:
5//! - `VanillaOption` (contract),
6//! - `MarketData` (spot, rate, dividend yield).
7//!
8//! The implementation is split across several submodules to keep files small:
9//! - `pricing` – price and rational price,
10//! - `greeks_impl` – Greeks calculations,
11//! - `iv` – implied volatility solvers,
12//! - `math` – internal helpers (d1/d2 and related quantities).
13
14mod greeks_higher;
15mod greeks_impl;
16mod iv;
17mod math;
18mod pricing;
19
20use crate::{
21 core::{Greeks, MarketData, VanillaOption},
22 models::VanillaModel,
23};
24
25/// Black–Scholes–Merton model for vanilla options.
26///
27/// The struct form is used with the [`VanillaModel`] trait, where the
28/// volatility is stored in `vol`. For convenience, the existing
29/// associated functions such as [`BlackScholes::price`] continue to
30/// take volatility explicitly and can be used in a “namespace” style.
31#[derive(Debug, Clone, Copy, Default)]
32pub struct BlackScholes {
33 /// Volatility parameter used by the [`VanillaModel`] interface.
34 pub vol: f64,
35}
36
37impl BlackScholes {
38 /// Construct a Black–Scholes model with the given volatility.
39 pub fn new(vol: f64) -> Self {
40 Self { vol }
41 }
42}
43
44impl VanillaModel for BlackScholes {
45 fn price(&self, opt: &VanillaOption, mkt: &MarketData) -> Result<f64, String> {
46 // Delegate to the existing associated function which already
47 // encodes the error semantics for invalid inputs.
48 BlackScholes::price(opt, mkt, self.vol)
49 }
50
51 fn greeks(&self, opt: &VanillaOption, mkt: &MarketData) -> Result<Greeks, String> {
52 BlackScholes::greeks(opt, mkt, self.vol)
53 }
54
55 fn implied_vol(
56 &self,
57 target_price: f64,
58 opt: &VanillaOption,
59 mkt: &MarketData,
60 ) -> Result<f64, String> {
61 // Use the rational implied-volatility solver as the default
62 // implementation for the trait.
63 BlackScholes::rational_implied_vol(target_price, opt, mkt)
64 }
65}