quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Yield curve and interest rate term structures.

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

/// Trait implemented by interest rate term structures.
pub trait YieldTermStructure: Send + Sync {
    /// Return the reference (as-of) date of the curve.
    fn reference_date(&self) -> Date;

    /// Calculate the discount factor $P(0, t)$ for a time $t$ in years.
    fn discount(&self, t: f64) -> f64;

    /// Calculate the discount factor $P(0, T)$ for a given date.
    fn discount_date(&self, date: Date, day_counter: &dyn DayCounter) -> f64 {
        let t = day_counter.year_fraction(self.reference_date(), date);
        self.discount(t)
    }

    /// Calculate the continuously compounded zero rate for a time $t$ in years.
    fn zero_rate(&self, t: f64) -> f64;
}

/// Flat forward yield curve with a constant continuously compounded zero rate.
///
/// $$P(0, t) = e^{-r t}$$
///
/// # Examples
/// ```
/// use quantlib_rs::termstructures::yield_term_structure::{YieldTermStructure, FlatForward};
/// use quantlib_rs::time::date::{Date, Month};
///
/// let today = Date::from_ymd(2026, Month::September, 3).unwrap();
/// let curve = FlatForward::new(0.05, today);
///
/// assert_eq!(curve.discount(0.0), 1.0);
/// assert!((curve.discount(1.0) - (-0.05f64).exp()).abs() < 1e-12);
/// assert_eq!(curve.zero_rate(2.0), 0.05);
/// ```
#[derive(Debug, Clone, Copy)]
pub struct FlatForward {
    rate: f64,
    reference_date: Date,
}

impl FlatForward {
    /// Create a new FlatForward curve with the given continuously compounded rate.
    pub const fn new(rate: f64, reference_date: Date) -> Self {
        Self {
            rate,
            reference_date,
        }
    }

    /// Return the constant continuously compounded rate.
    pub const fn rate(&self) -> f64 {
        self.rate
    }
}

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

    #[inline]
    fn discount(&self, t: f64) -> f64 {
        if t <= 0.0 {
            1.0
        } else {
            (-self.rate * t).exp()
        }
    }

    #[inline]
    fn zero_rate(&self, _t: f64) -> f64 {
        self.rate
    }
}