Skip to content

Commit

Permalink
fix: mallavin derivates
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Oct 8, 2024
1 parent 516e58a commit 6c3d25e
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stochastic-rs"
version = "0.10.3"
version = "0.10.4"
edition = "2021"
license = "MIT"
description = "A Rust library for quant finance and simulating stochastic processes."
Expand Down
6 changes: 2 additions & 4 deletions src/stochastic/diffusion/cev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ impl Sampling<f64> for CEV {
if i > 0 {
stochastic_term[i] = self.sigma * self.gamma * cev[i].powf(self.gamma - 1.0) * gn[i - 1];
}
malliavin[i] = self.sigma
* cev[i].powf(self.gamma)
* (det_term[i] + stochastic_term[i]).exp()
* (i as f64 * dt);
malliavin[i] =
self.sigma * cev[i].powf(self.gamma) * (det_term[i] + stochastic_term[i]).exp()
}

let _ = std::mem::replace(&mut *self.malliavin.lock().unwrap(), Some(malliavin));
Expand Down
2 changes: 1 addition & 1 deletion src/stochastic/diffusion/gbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Sampling<f64> for GBM {

// reverse due the option pricing
for i in 0..self.n {
malliavin[i] = self.sigma * gbm.last().unwrap() * (i as f64 * dt);
malliavin[i] = self.sigma * gbm.last().unwrap();
}

// This equivalent to the following:
Expand Down
6 changes: 2 additions & 4 deletions src/stochastic/volatility/heston.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,15 @@ impl Sampling2D<f64> for Heston {
- self.kappa / 2.0)
* ((self.n - i) as f64 * dt))
.exp();
malliavin[i] =
(self.sigma * v.last().unwrap().sqrt() / 2.0) * det_term[i] * (i as f64 * dt);
malliavin[i] = (self.sigma * v.last().unwrap().sqrt() / 2.0) * det_term[i];
}
HestonPow::ThreeHalves => {
det_term[i] = ((-(self.kappa * self.theta / 2.0 + 3.0 * self.sigma.powi(2) / 8.0)
* v.last().unwrap()
- (self.kappa * self.theta) / 2.0)
* ((self.n - i) as f64 * dt))
.exp();
malliavin[i] =
(self.sigma * v.last().unwrap().powf(1.5) / 2.0) * det_term[i] * (i as f64 * dt);
malliavin[i] = (self.sigma * v.last().unwrap().powf(1.5) / 2.0) * det_term[i];
}
};
}
Expand Down
3 changes: 1 addition & 2 deletions src/stochastic/volatility/sabr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ impl Sampling2D<f64> for SABR {
#[cfg(feature = "malliavin")]
if self.calculate_malliavin.is_some() && self.calculate_malliavin.unwrap() {
// Only volatility Malliavin derivative is supported
let dt = self.t.unwrap_or(1.0) / (self.n - 1) as f64;
let mut malliavin_of_vol = Array1::<f64>::zeros(self.n);

for i in 0..self.n {
malliavin_of_vol[i] = self.alpha * v.last().unwrap() * (i as f64 * dt);
malliavin_of_vol[i] = self.alpha * v.last().unwrap();
}

let _ = std::mem::replace(
Expand Down

0 comments on commit 6c3d25e

Please sign in to comment.