-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: stabilze jumps and fix compound poisson
- Loading branch information
Showing
9 changed files
with
201 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::noises::gn::gn; | ||
|
||
use ndarray::Array1; | ||
use ndarray_rand::{rand_distr::InverseGaussian, RandomExt}; | ||
|
||
pub fn ig(gamma: f64, n: usize, x0: Option<f64>, t: Option<f64>) -> Vec<f64> { | ||
let dt = t.unwrap_or(1.0) / n as f64; | ||
let gn = gn(n - 1, t); | ||
let mut ig = Array1::zeros(n); | ||
ig[0] = x0.unwrap_or(0.0); | ||
|
||
for i in 1..n { | ||
ig[i] = ig[i - 1] + gamma * dt + gn[i - 1] | ||
} | ||
|
||
ig.to_vec() | ||
} | ||
|
||
pub fn nig( | ||
theta: f64, | ||
sigma: f64, | ||
kappa: f64, | ||
n: usize, | ||
x0: Option<f64>, | ||
t: Option<f64>, | ||
) -> Vec<f64> { | ||
let dt = t.unwrap_or(1.0) / n as f64; | ||
let scale = dt.powf(2.0) / kappa; | ||
let mean = dt / scale; | ||
let ig = Array1::random(n - 1, InverseGaussian::new(mean, scale).unwrap()); | ||
let gn = gn(n - 1, t); | ||
let mut nig = Array1::zeros(n); | ||
nig[0] = x0.unwrap_or(0.0); | ||
|
||
for i in 1..n { | ||
nig[i] = nig[i - 1] + theta * ig[i - 1] + sigma * ig[i - 1].sqrt() * gn[i - 1] | ||
} | ||
|
||
nig.to_vec() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::{noises::gn::gn, processes::poisson::compound_poisson}; | ||
use ndarray::Array1; | ||
|
||
pub fn levy_diffusion( | ||
gamma: f64, | ||
sigma: f64, | ||
lambda: f64, | ||
n: usize, | ||
x0: Option<f64>, | ||
t: Option<f64>, | ||
) -> Vec<f64> { | ||
let dt = t.unwrap_or(1.0) / n as f64; | ||
let mut levy = Array1::<f64>::zeros(n); | ||
levy[0] = x0.unwrap_or(0.0); | ||
let gn = gn(n - 1, t); | ||
let z = compound_poisson(n, lambda, None, t, None, None); | ||
|
||
for i in 1..n { | ||
levy[i] = levy[i - 1] + gamma * dt + sigma * gn[i - 1] * z[i - 1]; | ||
} | ||
|
||
levy.to_vec() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_levy_diffusion() { | ||
let gamma = 0.0; | ||
let sigma = 1.0; | ||
let lambda = 10.0; | ||
let n = 1000; | ||
let t = 10.0; | ||
let l = levy_diffusion(gamma, sigma, lambda, n, None, Some(t)); | ||
assert_eq!(l.len(), n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use ndarray::Array1; | ||
|
||
use crate::{noises::gn::gn, processes::poisson::compound_poisson}; | ||
|
||
pub fn merton( | ||
alpha: f64, | ||
sigma: f64, | ||
lambda: f64, | ||
theta: f64, | ||
n: usize, | ||
x0: Option<f64>, | ||
t: Option<f64>, | ||
) -> Vec<f64> { | ||
let dt = t.unwrap_or(1.0) / n as f64; | ||
let mut merton = Array1::<f64>::zeros(n); | ||
merton[0] = x0.unwrap_or(0.0); | ||
let gn = gn(n - 1, t); | ||
let z = compound_poisson(n, lambda, None, t, None, None); | ||
|
||
for i in 1..n { | ||
merton[i] = merton[i - 1] | ||
+ (alpha * sigma.powf(2.0) / 2.0 - lambda * theta) * dt | ||
+ sigma * gn[i - 1] | ||
+ z[i - 1]; | ||
} | ||
|
||
merton.to_vec() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_merton() { | ||
let alpha = 0.0; | ||
let sigma = 1.0; | ||
let lambda = 10.0; | ||
let theta = 0.0; | ||
let n = 1000; | ||
let t = 10.0; | ||
let m = merton(alpha, sigma, lambda, theta, n, None, Some(t)); | ||
assert_eq!(m.len(), n); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
pub mod bates; | ||
pub mod ig; | ||
pub mod levy_diffusion; | ||
pub mod merton; | ||
pub mod vg; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters