quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Volatility term structures.

use crate::time::date::Date;
use crate::time::day_counter::DayCounter;

/// Trait implemented by Black volatility term structures.
pub trait BlackVolTermStructure: Send + Sync {
    /// Return the reference (as-of) date of the volatility surface.
    fn reference_date(&self) -> Date;

    /// Return the Black volatility $\sigma(t, K)$ for maturity $t$ and strike $K$.
    fn black_vol(&self, t: f64, strike: f64) -> f64;

    /// Return the total variance $\sigma^2(t, K) \cdot t$.
    fn black_variance(&self, t: f64, strike: f64) -> f64 {
        let v = self.black_vol(t, strike);
        v * v * t
    }

    /// Return the Black volatility for a specific date and strike.
    fn black_vol_date(&self, date: Date, strike: f64, day_counter: &dyn DayCounter) -> f64 {
        let t = day_counter.year_fraction(self.reference_date(), date);
        self.black_vol(t, strike)
    }
}

/// Constant Black volatility term structure.
///
/// # Examples
/// ```
/// use quantlib_rs::termstructures::vol_term_structure::{BlackVolTermStructure, BlackConstantVol};
/// use quantlib_rs::time::date::{Date, Month};
///
/// let today = Date::from_ymd(2026, Month::September, 3).unwrap();
/// let vol = BlackConstantVol::new(0.20, today);
///
/// assert_eq!(vol.black_vol(1.0, 100.0), 0.20);
/// assert!((vol.black_variance(2.0, 100.0) - 0.08).abs() < 1e-12);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct BlackConstantVol {
    volatility: f64,
    reference_date: Date,
}

impl BlackConstantVol {
    /// Create a new constant Black volatility structure.
    pub const fn new(volatility: f64, reference_date: Date) -> Self {
        Self {
            volatility,
            reference_date,
        }
    }

    /// Return the constant volatility value.
    pub const fn volatility(&self) -> f64 {
        self.volatility
    }
}

impl BlackVolTermStructure for BlackConstantVol {
    #[inline]
    fn reference_date(&self) -> Date {
        self.reference_date
    }

    #[inline]
    fn black_vol(&self, _t: f64, _strike: f64) -> f64 {
        self.volatility
    }
}