quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Generalized Black-Scholes stochastic process.

use crate::termstructures::vol_term_structure::{BlackConstantVol, BlackVolTermStructure};
use crate::termstructures::yield_term_structure::{FlatForward, YieldTermStructure};
use crate::time::date::Date;
use std::sync::Arc;

/// Generalized Black-Scholes 1D stochastic process:
/// $$dS_t = (r - q) S_t dt + \sigma S_t dW_t$$
///
/// Bundles spot price, risk-free interest rate curve, dividend yield curve, and volatility structure.
///
/// # Examples
/// ```
/// use quantlib_rs::termstructures::process::GeneralizedBlackScholesProcess;
/// use quantlib_rs::time::date::{Date, Month};
///
/// let today = Date::from_ymd(2026, Month::September, 3).unwrap();
/// let process = GeneralizedBlackScholesProcess::new(100.0, 0.05, 0.02, 0.20, today);
///
/// assert_eq!(process.spot(), 100.0);
/// assert_eq!(process.risk_free_rate().zero_rate(1.0), 0.05);
/// assert_eq!(process.dividend_yield().zero_rate(1.0), 0.02);
/// assert_eq!(process.black_vol().black_vol(1.0, 100.0), 0.20);
/// ```
#[derive(Clone)]
pub struct GeneralizedBlackScholesProcess {
    spot: f64,
    risk_free: Arc<dyn YieldTermStructure>,
    dividend: Arc<dyn YieldTermStructure>,
    volatility: Arc<dyn BlackVolTermStructure>,
    reference_date: Date,
}

impl GeneralizedBlackScholesProcess {
    /// Create a new Black-Scholes process from constant parameters.
    pub fn new(
        spot: f64,
        risk_free_rate: f64,
        dividend_rate: f64,
        volatility: f64,
        reference_date: Date,
    ) -> Self {
        Self {
            spot,
            risk_free: Arc::new(FlatForward::new(risk_free_rate, reference_date)),
            dividend: Arc::new(FlatForward::new(dividend_rate, reference_date)),
            volatility: Arc::new(BlackConstantVol::new(volatility, reference_date)),
            reference_date,
        }
    }

    /// Create a new Black-Scholes process from term structures.
    pub fn with_curves(
        spot: f64,
        risk_free: Arc<dyn YieldTermStructure>,
        dividend: Arc<dyn YieldTermStructure>,
        volatility: Arc<dyn BlackVolTermStructure>,
        reference_date: Date,
    ) -> Self {
        Self {
            spot,
            risk_free,
            dividend,
            volatility,
            reference_date,
        }
    }

    /// Return the spot price $S_0$.
    #[inline]
    pub fn spot(&self) -> f64 {
        self.spot
    }

    /// Return a reference to the risk-free yield term structure.
    #[inline]
    pub fn risk_free_rate(&self) -> &dyn YieldTermStructure {
        self.risk_free.as_ref()
    }

    /// Return a reference to the dividend yield term structure.
    #[inline]
    pub fn dividend_yield(&self) -> &dyn YieldTermStructure {
        self.dividend.as_ref()
    }

    /// Return a reference to the Black volatility term structure.
    #[inline]
    pub fn black_vol(&self) -> &dyn BlackVolTermStructure {
        self.volatility.as_ref()
    }

    /// Return the reference (as-of) date.
    #[inline]
    pub fn reference_date(&self) -> Date {
        self.reference_date
    }
}