#![deny(missing_docs)]
use crate::stochastics::*;
#[derive(Debug)]
pub struct CoxIngersollRoss {
pub mu: f64,
pub sigma: f64,
pub theta: f64,
}
impl CoxIngersollRoss {
pub fn new(mu: f64, sigma: f64, theta: f64) -> Self {
assert!(sigma >= 0.0);
Self { mu, sigma, theta }
}
}
impl StochasticProcess for CoxIngersollRoss {
fn drift(&self, x: f64) -> f64 {
self.theta * (self.mu - x)
}
fn diffusion(&self, x: f64) -> f64 {
self.sigma * x.sqrt()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::helpers::*;
#[test]
fn test_cox_ingersoll_ross() -> Result<(), Box<dyn std::error::Error>> {
let cir = CoxIngersollRoss::new(0.15, 0.45, 0.01);
let output = cir.euler_maruyama(10.0, 0.0, 0.5, 1000, 2, false);
let file1 = "./Images/CIR1.png";
plot_vector((&output.trajectories[0]).clone(), file1).unwrap();
let file2 = "./Images/CIR2.png";
plot_vector((&output.trajectories[1]).clone(), file2)
}
}