diff --git a/stochastic-rs-core/src/volatility/heston.rs b/stochastic-rs-core/src/volatility/heston.rs index c877af8..b359a33 100644 --- a/stochastic-rs-core/src/volatility/heston.rs +++ b/stochastic-rs-core/src/volatility/heston.rs @@ -5,17 +5,29 @@ use crate::{noise::cgns::Cgns, Sampling2D}; #[derive(Default)] pub struct Heston { - pub mu: f64, + /// Initial stock price + pub s0: Option, + /// Initial volatility + pub v0: Option, + /// Mean reversion rate pub kappa: f64, + /// Long-run average volatility pub theta: f64, + /// Volatility of volatility pub sigma: f64, + /// Correlation between the stock price and its volatility pub rho: f64, + /// Drift of the stock price + pub mu: f64, + /// Number of time steps pub n: usize, - pub s0: Option, - pub v0: Option, + /// Time to maturity pub t: Option, + /// Use the symmetric method for the variance to avoid negative values pub use_sym: Option, + /// Number of paths for multithreading pub m: Option, + /// Noise generator pub cgns: Cgns, } @@ -30,14 +42,14 @@ impl Heston { }); Self { - mu: params.mu, + s0: params.s0, + v0: params.v0, kappa: params.kappa, theta: params.theta, sigma: params.sigma, rho: params.rho, + mu: params.mu, n: params.n, - s0: params.s0, - v0: params.v0, t: params.t, use_sym: params.use_sym, m: params.m, diff --git a/stochastic-rs-quant/src/lib.rs b/stochastic-rs-quant/src/lib.rs index fbf62e7..880583d 100644 --- a/stochastic-rs-quant/src/lib.rs +++ b/stochastic-rs-quant/src/lib.rs @@ -1,3 +1,5 @@ +use std::mem::ManuallyDrop; + pub mod pricing; #[cfg(feature = "mimalloc")] @@ -7,3 +9,34 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc; #[cfg(feature = "jemalloc")] #[global_allocator] static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; + +/// A value or a vector of values. +pub union ValueOrVec +where + T: Copy, +{ + pub x: T, + pub v: ManuallyDrop>, +} + +/// Implement the `Clone` trait for `ValueOrVec`. +impl Clone for ValueOrVec { + fn clone(&self) -> Self { + unsafe { + Self { + v: ManuallyDrop::new(self.v.clone().to_vec()), + } + } + } +} + +/// Implement the `Clone` trait for `ValueOrVec`. +impl Clone for ValueOrVec { + fn clone(&self) -> Self { + unsafe { + Self { + v: ManuallyDrop::new(self.v.clone().to_vec()), + } + } + } +} diff --git a/stochastic-rs-quant/src/pricing/heston.rs b/stochastic-rs-quant/src/pricing/heston.rs index 272dd29..ed1c872 100644 --- a/stochastic-rs-quant/src/pricing/heston.rs +++ b/stochastic-rs-quant/src/pricing/heston.rs @@ -3,6 +3,8 @@ use std::{f64::consts::FRAC_1_PI, mem::ManuallyDrop}; use num_complex::Complex64; use quadrature::double_exponential; +use crate::ValueOrVec; + #[derive(Default)] pub struct Heston { /// Initial stock price @@ -33,34 +35,6 @@ pub struct Heston { pub expiry: Option>, } -pub union ValueOrVec -where - T: Copy, -{ - pub x: T, - pub v: ManuallyDrop>, -} - -impl Clone for ValueOrVec { - fn clone(&self) -> Self { - unsafe { - Self { - v: ManuallyDrop::new(self.v.clone().to_vec()), - } - } - } -} - -impl Clone for ValueOrVec { - fn clone(&self) -> Self { - unsafe { - Self { - v: ManuallyDrop::new(self.v.clone().to_vec()), - } - } - } -} - impl Heston { /// Create a new Heston model #[must_use]