use crate::error::IntegrateResult as Result;
pub trait InterestRateModel: Send + Sync {
fn short_rate(&self, t: f64, state: &[f64]) -> f64;
fn drift(&self, t: f64, state: &[f64]) -> Vec<f64>;
fn diffusion(&self, t: f64, state: &[f64]) -> Vec<f64>;
fn state_dimension(&self) -> usize;
}
#[derive(Debug, Clone)]
pub struct HullWhiteModel {
pub a: f64,
pub b: f64,
pub sigma: f64,
}
impl InterestRateModel for HullWhiteModel {
fn short_rate(&self, t: f64, state: &[f64]) -> f64 {
state[0]
}
fn drift(&self, t: f64, state: &[f64]) -> Vec<f64> {
vec![self.a * (self.b - state[0])]
}
fn diffusion(&self, _t: f64, state: &[f64]) -> Vec<f64> {
vec![self.sigma]
}
fn state_dimension(&self) -> usize {
1
}
}
#[derive(Debug, Clone)]
pub struct CIRModel {
pub kappa: f64,
pub theta: f64,
pub sigma: f64,
}
impl InterestRateModel for CIRModel {
fn short_rate(&self, t: f64, state: &[f64]) -> f64 {
state[0].max(0.0)
}
fn drift(&self, t: f64, state: &[f64]) -> Vec<f64> {
vec![self.kappa * (self.theta - state[0].max(0.0))]
}
fn diffusion(&self, t: f64, state: &[f64]) -> Vec<f64> {
vec![self.sigma * state[0].max(0.0).sqrt()]
}
fn state_dimension(&self) -> usize {
1
}
}