Skip to content

Commit

Permalink
feat: update heston model
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Sep 27, 2024
1 parent e22d0d7 commit 2e9c96b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 34 deletions.
24 changes: 18 additions & 6 deletions stochastic-rs-core/src/volatility/heston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,29 @@ use crate::{noise::cgns::Cgns, Sampling2D};
#[derive(Default)]

pub struct Heston {
pub mu: f64,
/// Initial stock price
pub s0: Option<f64>,
/// Initial volatility
pub v0: Option<f64>,
/// 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<f64>,
pub v0: Option<f64>,
/// Time to maturity
pub t: Option<f64>,
/// Use the symmetric method for the variance to avoid negative values
pub use_sym: Option<bool>,
/// Number of paths for multithreading
pub m: Option<usize>,
/// Noise generator
pub cgns: Cgns,
}

Expand All @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions stochastic-rs-quant/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::mem::ManuallyDrop;

pub mod pricing;

#[cfg(feature = "mimalloc")]
Expand All @@ -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<T>
where
T: Copy,
{
pub x: T,
pub v: ManuallyDrop<Vec<T>>,
}

/// Implement the `Clone` trait for `ValueOrVec<T>`.
impl Clone for ValueOrVec<f64> {
fn clone(&self) -> Self {
unsafe {
Self {
v: ManuallyDrop::new(self.v.clone().to_vec()),
}
}
}
}

/// Implement the `Clone` trait for `ValueOrVec<T>`.
impl Clone for ValueOrVec<chrono::NaiveDate> {
fn clone(&self) -> Self {
unsafe {
Self {
v: ManuallyDrop::new(self.v.clone().to_vec()),
}
}
}
}
30 changes: 2 additions & 28 deletions stochastic-rs-quant/src/pricing/heston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,34 +35,6 @@ pub struct Heston {
pub expiry: Option<ValueOrVec<chrono::NaiveDate>>,
}

pub union ValueOrVec<T>
where
T: Copy,
{
pub x: T,
pub v: ManuallyDrop<Vec<T>>,
}

impl Clone for ValueOrVec<f64> {
fn clone(&self) -> Self {
unsafe {
Self {
v: ManuallyDrop::new(self.v.clone().to_vec()),
}
}
}
}

impl Clone for ValueOrVec<chrono::NaiveDate> {
fn clone(&self) -> Self {
unsafe {
Self {
v: ManuallyDrop::new(self.v.clone().to_vec()),
}
}
}
}

impl Heston {
/// Create a new Heston model
#[must_use]
Expand Down

0 comments on commit 2e9c96b

Please sign in to comment.