use crate::curves::Curve;
use crate::error::CurveError;
use crate::error::SurfaceError;
use crate::surfaces::Surface;
use positive::Positive;
pub trait ThetaCurve {
fn theta_curve(&self) -> Result<Curve, CurveError>;
}
pub trait ThetaSurface {
fn theta_surface(
&self,
price_range: (Positive, Positive),
days_to_expiry: Vec<Positive>,
price_steps: usize,
) -> Result<Surface, SurfaceError>;
}
#[cfg(test)]
mod tests_theta {
use super::*;
use crate::curves::Point2D;
use crate::surfaces::Point3D;
use positive::pos_or_panic;
use rust_decimal::Decimal;
use rust_decimal::MathematicalOps;
use rust_decimal_macros::dec;
use std::collections::BTreeSet;
struct TestTheta {
underlying_price: Positive,
}
impl ThetaCurve for TestTheta {
fn theta_curve(&self) -> Result<Curve, CurveError> {
let mut points = BTreeSet::new();
let spot = self.underlying_price.to_dec();
let strikes = [
dec!(380.0),
dec!(400.0),
dec!(420.0),
dec!(440.0),
dec!(450.0),
dec!(460.0),
dec!(480.0),
dec!(500.0),
dec!(520.0),
];
for strike in strikes {
let moneyness = ((strike - spot) / spot).abs();
let theta = dec!(-0.15) * (-moneyness * dec!(10.0)).exp();
points.insert(Point2D::new(strike, theta));
}
Ok(Curve::new(points))
}
}
impl ThetaSurface for TestTheta {
fn theta_surface(
&self,
price_range: (Positive, Positive),
days_to_expiry: Vec<Positive>,
price_steps: usize,
) -> Result<Surface, SurfaceError> {
let mut points = BTreeSet::new();
let price_step = if price_steps > 0 {
(price_range.1 - price_range.0).to_dec() / Decimal::from(price_steps)
} else {
Decimal::ZERO
};
let strike = self.underlying_price.to_dec();
for days in &days_to_expiry {
let time_factor = (dec!(30.0) / days.to_dec()).sqrt().unwrap_or(Decimal::ONE);
for p in 0..=price_steps {
let price = price_range.0.to_dec() + price_step * Decimal::from(p);
let moneyness = ((price - strike) / strike).abs();
let theta = dec!(-0.10) * (-moneyness * dec!(5.0)).exp() * time_factor;
points.insert(Point3D::new(price, days.to_dec(), theta));
}
}
Ok(Surface::new(points))
}
}
#[test]
fn test_theta_curve_creation() {
let theta = TestTheta {
underlying_price: pos_or_panic!(450.0),
};
let curve = theta.theta_curve();
assert!(curve.is_ok());
let curve = curve.unwrap();
assert_eq!(curve.points.len(), 9);
}
#[test]
fn test_theta_curve_atm_most_negative() {
let theta = TestTheta {
underlying_price: pos_or_panic!(450.0),
};
let curve = theta.theta_curve().unwrap();
let points: Vec<&Point2D> = curve.points.iter().collect();
let min_theta = points
.iter()
.min_by(|a, b| a.y.partial_cmp(&b.y).unwrap_or(std::cmp::Ordering::Equal));
if let Some(min) = min_theta {
assert_eq!(min.x, dec!(450.0));
}
}
#[test]
fn test_theta_curve_negative_values() {
let theta = TestTheta {
underlying_price: pos_or_panic!(450.0),
};
let curve = theta.theta_curve().unwrap();
for point in curve.points.iter() {
assert!(point.y <= Decimal::ZERO);
}
}
#[test]
fn test_theta_surface_creation() {
let theta = TestTheta {
underlying_price: pos_or_panic!(450.0),
};
let price_range = (pos_or_panic!(400.0), pos_or_panic!(500.0));
let days = vec![pos_or_panic!(7.0), pos_or_panic!(14.0), pos_or_panic!(30.0)];
let surface = theta.theta_surface(price_range, days, 10);
assert!(surface.is_ok());
let surface = surface.unwrap();
assert_eq!(surface.points.len(), 33);
}
#[test]
fn test_theta_surface_time_acceleration() {
let theta = TestTheta {
underlying_price: pos_or_panic!(450.0),
};
let price_range = (pos_or_panic!(450.0), pos_or_panic!(450.0));
let days = vec![pos_or_panic!(7.0), pos_or_panic!(30.0)];
let surface = theta.theta_surface(price_range, days, 0).unwrap();
let points: Vec<&Point3D> = surface.points.iter().collect();
let theta_7d = points.iter().find(|p| p.y == dec!(7.0)).map(|p| p.z);
let theta_30d = points.iter().find(|p| p.y == dec!(30.0)).map(|p| p.z);
if let (Some(t7), Some(t30)) = (theta_7d, theta_30d) {
assert!(t7 < t30); }
}
}