-
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.
Merge pull request #8 from dancixx/feat/rework-api-and-performance
feat: add rbergomi
- Loading branch information
Showing
20 changed files
with
132 additions
and
454 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
[workspace] | ||
members = ["stochastic-rs-core", "stochastic-rs-quant", "stochastic-rs-stats"] | ||
members = [ | ||
"stochastic-rs-core", | ||
"stochastic-rs-ml", | ||
"stochastic-rs-quant", | ||
"stochastic-rs-stats", | ||
] | ||
resolver = "2" |
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 |
---|---|---|
@@ -1,24 +1,31 @@ | ||
use stochastic_rs::process::fbm::Fbm; | ||
use stochastic_rs::{process::fbm::Fbm, Sampling}; | ||
|
||
fn main() { | ||
// let fbm = Fbm::new(0.75, 10000, Some(1.0), None); | ||
// let mut runs = Vec::new(); | ||
let fbm = Fbm::new(&Fbm { | ||
hurst: 0.9, | ||
n: 10000, | ||
t: None, | ||
m: Some(100), | ||
..Default::default() | ||
}); | ||
println!("{:?}", fbm.fgn.hurst); | ||
let mut runs = Vec::new(); | ||
|
||
// for _ in 0..20 { | ||
// let start = std::time::Instant::now(); | ||
// for _ in 0..1000 { | ||
// let _ = fbm.sample(); | ||
// } | ||
for _ in 0..20 { | ||
let start = std::time::Instant::now(); | ||
for _ in 0..1000 { | ||
let _ = fbm.sample_par(); | ||
} | ||
|
||
// let duration = start.elapsed(); | ||
// println!( | ||
// "Time elapsed in expensive_function() is: {:?}", | ||
// duration.as_secs_f32() | ||
// ); | ||
// runs.push(duration.as_secs_f32()); | ||
// } | ||
let duration = start.elapsed(); | ||
println!( | ||
"Time elapsed in expensive_function() is: {:?}", | ||
duration.as_secs_f32() | ||
); | ||
runs.push(duration.as_secs_f32()); | ||
} | ||
|
||
// let sum: f32 = runs.iter().sum(); | ||
// let average = sum / runs.len() as f32; | ||
// println!("Average time: {}", average); | ||
let sum: f32 = runs.iter().sum(); | ||
let average = sum / runs.len() as f32; | ||
println!("Average time: {}", average); | ||
} |
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 |
---|---|---|
@@ -1,33 +1,4 @@ | ||
//! This module contains the implementations of various diffusion processes. | ||
//! | ||
//! The following diffusion processes are implemented: | ||
//! | ||
//! - Duffie-Kan Model | ||
//! - The Duffie-Kan model is a multifactor interest rate model incorporating correlated Brownian motions. | ||
//! - SDE: `dr(t) = (a1 * r(t) + b1 * x(t) + c1) * dt + sigma1 * (alpha * r(t) + beta * x(t) + gamma) * dW_r(t)` | ||
//! - SDE: `dx(t) = (a2 * r(t) + b2 * x(t) + c2) * dt + sigma2 * (alpha * r(t) + beta * x(t) + gamma) * dW_x(t)` | ||
//! - where Corr(W_r(t), W_x(t)) = rho | ||
//! | ||
//! - **Heston Model** | ||
//! - A stochastic volatility model used to describe the evolution of the volatility of an underlying asset. | ||
//! - SDE: `dS(t) = mu * S(t) * dt + S(t) * sqrt(V(t)) * dW_1(t)` | ||
//! - SDE: `dV(t) = kappa * (theta - V(t)) * dt + eta * sqrt(V(t)) * dW_2(t)` | ||
//! | ||
//! - SABR (Stochastic Alpha, Beta, Rho) Model | ||
//! - Widely used in financial mathematics for modeling stochastic volatility. | ||
//! - SDE: `dF(t) = V(t) * F(t)^beta * dW_F(t)` | ||
//! - SDE: `dV(t) = alpha * V(t) * dW_V(t)` | ||
//! - where Corr(W_F(t), W_V(t)) = rho | ||
//! | ||
//! - **Vasicek Model** | ||
//! - An Ornstein-Uhlenbeck process used to model interest rates. | ||
//! - SDE: `dX(t) = theta * (mu - X(t)) * dt + sigma * dW(t)` | ||
//! | ||
//! - **Fractional Vasicek (fVasicek) Model** | ||
//! - Incorporates fractional Brownian motion into the Vasicek model. | ||
//! - SDE: `dX(t) = theta * (mu - X(t)) * dt + sigma * dW^H(t)` | ||
//! | ||
//! Each process has its own module and functions to generate sample paths. | ||
pub mod fheston; | ||
pub mod heston; | ||
pub mod rbergomi; | ||
pub mod sabr; |
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,71 @@ | ||
use ndarray::{s, Array1}; | ||
|
||
use crate::{noise::cgns::Cgns, Sampling2D}; | ||
|
||
#[derive(Default)] | ||
pub struct RoughBergomi { | ||
pub hurst: f64, | ||
pub nu: f64, | ||
pub sigma_0: f64, | ||
pub r: f64, | ||
pub rho: f64, | ||
pub n: usize, | ||
pub t: Option<f64>, | ||
pub m: Option<usize>, | ||
pub cgns: Cgns, | ||
} | ||
|
||
impl RoughBergomi { | ||
#[must_use] | ||
pub fn new(params: &Self) -> Self { | ||
let cgns = Cgns::new(&Cgns { | ||
rho: params.rho, | ||
n: params.n, | ||
t: params.t, | ||
m: params.m, | ||
}); | ||
|
||
Self { | ||
hurst: params.hurst, | ||
nu: params.nu, | ||
sigma_0: params.sigma_0, | ||
r: params.r, | ||
rho: params.rho, | ||
n: params.n, | ||
t: params.t, | ||
m: params.m, | ||
cgns, | ||
} | ||
} | ||
} | ||
|
||
impl Sampling2D<f64> for RoughBergomi { | ||
fn sample(&self) -> [Array1<f64>; 2] { | ||
let dt = self.t.unwrap_or(1.0) / self.n as f64; | ||
let [cgn1, z] = self.cgns.sample(); | ||
|
||
let mut s = Array1::<f64>::zeros(self.n + 1); | ||
let mut sigma2 = Array1::<f64>::zeros(self.n + 1); | ||
|
||
for i in 1..(self.n + 1) { | ||
s[i] = s[i - 1] + self.r * s[i - 1] + sigma2[i - 1] * cgn1[i - 1]; | ||
|
||
let sum_z = z.slice(s![..i]).sum(); | ||
let t = i as f64 * dt; | ||
sigma2[i] = self.sigma_0.powi(2) | ||
* (self.nu * (2.0 * self.hurst).sqrt() * t.powf(self.hurst - 0.5) * sum_z | ||
- 0.5 * self.nu.powi(2) * t.powf(2.0 * self.hurst)) | ||
.exp(); | ||
} | ||
|
||
[s, sigma2] | ||
} | ||
|
||
fn n(&self) -> usize { | ||
self.n | ||
} | ||
|
||
fn m(&self) -> Option<usize> { | ||
self.m | ||
} | ||
} |
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,6 @@ | ||
[package] | ||
name = "stochastic-rs-ml" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] |
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,14 @@ | ||
pub fn add(left: u64, right: u64) -> u64 { | ||
left + right | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.