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
use crate::{
ad::scalar::Scalar,
indices::marketindex::MarketIndex,
time::{date::Date, enums::TimeUnit, period::Period},
utils::errors::{QSError, Result},
volatility::volatilityindexing::{SmileType, VolatilityType},
};
/// Trait for volatility cubes parameterized by numeric type.
pub trait VolatilityCube<T: Scalar> {
/// Returns the volatility for a given expiry and key (e.g., strike, delta, log-moneyness).
///
/// ## Errors
/// Returns an error if the volatility cannot be computed for the given period and key.
fn volatility_from_date(&self, expiry: Date, maturity: Period, key: f64) -> Result<T> {
let today = self.reference_date();
let days = expiry - today;
let period = Period::new(
i32::try_from(days).map_err(|_| {
QSError::InvalidValueErr("Unable to transform days into i32.".into())
})?,
TimeUnit::Days,
);
self.volatility_from_period(period, maturity, key)
}
/// Returns the volatility for a given time to expiry and key (e.g., strike, delta, log-moneyness).
///
/// ## Errors
/// Returns an error if the volatility cannot be computed for the given period and key.
fn volatility_from_period(&self, expirty: Period, maturity: Period, key: f64) -> Result<T>;
/// Returns the volatility type (e.g., Black, Normal).
#[must_use]
fn volatility_type(&self) -> VolatilityType;
/// Returns the market index associated with the volatility surface.
#[must_use]
fn market_index(&self) -> &MarketIndex;
/// Returns the reference date of the volatility surface.
#[must_use]
fn reference_date(&self) -> Date;
/// Returns the smile axis convention used by the cube.
#[must_use]
fn smile_type(&self) -> SmileType;
}