From ca51afe92b7f228af0068009a2056d304e2235a0 Mon Sep 17 00:00:00 2001 From: Daniel Boros Date: Thu, 3 Oct 2024 23:23:29 +0200 Subject: [PATCH] feat: improve malliavin derivate --- src/stochastic/malliavin.rs | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/stochastic/malliavin.rs b/src/stochastic/malliavin.rs index f07f2ef..ca7d857 100644 --- a/src/stochastic/malliavin.rs +++ b/src/stochastic/malliavin.rs @@ -4,43 +4,49 @@ use super::Sampling; pub trait Malliavin { /// Compute the Malliavin derivative of the stochastic process using perturbation. + /// + /// - `f`: the function that maps the path to a scalar value. + /// - `epsilon`: the perturbation value. + /// + /// The Malliavin derivative is defined as the derivative of the function `f` with respect to the path. fn malliavin_derivate(&self, f: F, epsilon: f64) -> Array1 where F: Fn(&Array1) -> f64, { - let path = self.path(); + let mut path = self.path(); let mut derivates = Array1::zeros(path.len() + 1); - let f_original = f(&path); for i in 1..self.path().len() { - let mut perturbed_path = path.clone(); - perturbed_path[i] += epsilon; - - let f_perturbed = f(&perturbed_path); - + let original_value = path[i]; + path[i] += epsilon; + let f_perturbed = f(&path); derivates[i] = (f_perturbed - f_original) / epsilon; + path[i] = original_value; } derivates } /// Compute the Malliavin derivative of the stochastic process using perturbation for latest value. + /// + /// - `epsilon`: the perturbation value. + /// + /// The Malliavin derivative is defined as the derivative of the function `f` with respect to the path. /// For example we want to know how the option price changes if the stock price changes. fn malliavin_derivate_latest(&self, epsilon: f64) -> Array1 { - let path = self.path(); + let mut path = self.path(); let mut derivates = Array1::zeros(path.len() + 1); let final_value = |path: &Array1| -> f64 { *path.last().unwrap() }; let f_original = final_value(&path); for i in 1..path.len() { - let mut perturbed_path = path.clone(); - perturbed_path[i] += epsilon; // Kis perturbáció a folyamat adott pontján - - let f_perturbed = final_value(&perturbed_path); - + let original_value = path[i]; + path[i] += epsilon; + let f_perturbed = final_value(&path); derivates[i] = (f_perturbed - f_original) / epsilon; + path[i] = original_value; } derivates