diff --git a/stochastic-rs-core/src/diffusion/fcir.rs b/stochastic-rs-core/src/diffusion/fcir.rs index d9e72f8..5a72efb 100644 --- a/stochastic-rs-core/src/diffusion/fcir.rs +++ b/stochastic-rs-core/src/diffusion/fcir.rs @@ -1,4 +1,4 @@ -use ndarray::Array1; +use ndarray::{s, Array1}; use crate::{noise::fgn::Fgn, Sampling}; @@ -57,7 +57,7 @@ impl Sampling for Fcir { fcir[i] = fcir[i - 1] + self.theta * (self.mu - fcir[i - 1]) * dt + random } - fcir + fcir.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/diffusion/fgbm.rs b/stochastic-rs-core/src/diffusion/fgbm.rs index 368e494..f4753dc 100644 --- a/stochastic-rs-core/src/diffusion/fgbm.rs +++ b/stochastic-rs-core/src/diffusion/fgbm.rs @@ -1,4 +1,4 @@ -use ndarray::Array1; +use ndarray::{s, Array1}; use crate::{noise::fgn::Fgn, Sampling}; @@ -49,7 +49,7 @@ impl Sampling for Fgbm { fgbm[i] = fgbm[i - 1] + self.mu * fgbm[i - 1] * dt + self.sigma * fgbm[i - 1] * fgn[i - 1] } - fgbm + fgbm.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/diffusion/fjacobi.rs b/stochastic-rs-core/src/diffusion/fjacobi.rs index f26e558..dc0303c 100644 --- a/stochastic-rs-core/src/diffusion/fjacobi.rs +++ b/stochastic-rs-core/src/diffusion/fjacobi.rs @@ -1,4 +1,4 @@ -use ndarray::Array1; +use ndarray::{s, Array1}; use crate::{noise::fgn::Fgn, Sampling}; @@ -63,7 +63,7 @@ impl Sampling for Fjacobi { } } - fjacobi + fjacobi.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/diffusion/fou.rs b/stochastic-rs-core/src/diffusion/fou.rs index e31b9dc..5b3a4bb 100644 --- a/stochastic-rs-core/src/diffusion/fou.rs +++ b/stochastic-rs-core/src/diffusion/fou.rs @@ -1,4 +1,4 @@ -use ndarray::Array1; +use ndarray::{s, Array1}; use crate::{noise::fgn::Fgn, Sampling}; @@ -51,7 +51,7 @@ impl Sampling for Fou { fou[i] = fou[i - 1] + self.theta * (self.mu - fou[i - 1]) * dt + self.sigma * fgn[i - 1] } - fou + fou.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/jump/jump_fou.rs b/stochastic-rs-core/src/jump/jump_fou.rs index 913a283..c060455 100644 --- a/stochastic-rs-core/src/jump/jump_fou.rs +++ b/stochastic-rs-core/src/jump/jump_fou.rs @@ -1,4 +1,4 @@ -use ndarray::Array1; +use ndarray::{s, Array1}; use crate::{ noise::fgn::Fgn, process::cpoisson::CompoundPoisson, ProcessDistribution, Sampling, Sampling3D, @@ -75,7 +75,7 @@ impl Sampling for JumpFou { + jumps.sum(); } - jump_fou + jump_fou.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/lib.rs b/stochastic-rs-core/src/lib.rs index c4f9b96..2920945 100644 --- a/stochastic-rs-core/src/lib.rs +++ b/stochastic-rs-core/src/lib.rs @@ -49,7 +49,7 @@ pub trait Sampling: Send + Sync { panic!("m must be specified for parallel sampling"); } - let mut xs = Array2::zeros((self.m().unwrap(), self.n() + 1)); + let mut xs = Array2::zeros((self.m().unwrap(), self.n())); xs.axis_iter_mut(Axis(0)).into_par_iter().for_each(|mut x| { x.assign(&self.sample()); diff --git a/stochastic-rs-core/src/main.rs b/stochastic-rs-core/src/main.rs index 547baf6..c58f0e4 100644 --- a/stochastic-rs-core/src/main.rs +++ b/stochastic-rs-core/src/main.rs @@ -1,6 +1,33 @@ use stochastic_rs::{process::fbm::Fbm, Sampling}; fn main() { + // let fbm = Fbm::new(&Fbm { + // hurst: 0.9, + // n: 10000, + // t: None, + // m: Some(100), + // ..Default::default() + // }); + + // let mut runs = Vec::new(); + + // 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 sum: f32 = runs.iter().sum(); + // let average = sum / runs.len() as f32; + // println!("Average time: {}", average); let fbm = Fbm::new(&Fbm { hurst: 0.9, n: 10000, @@ -8,24 +35,16 @@ fn main() { m: Some(100), ..Default::default() }); - println!("{:?}", fbm.fgn.hurst); - let mut runs = Vec::new(); - for _ in 0..20 { + for _ in 0..10 { let start = std::time::Instant::now(); for _ in 0..1000 { - let _ = fbm.sample_par(); + let _ = fbm.sample(); } - 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); } diff --git a/stochastic-rs-core/src/noise/cfgns.rs b/stochastic-rs-core/src/noise/cfgns.rs index 8f106ef..3831fca 100644 --- a/stochastic-rs-core/src/noise/cfgns.rs +++ b/stochastic-rs-core/src/noise/cfgns.rs @@ -1,4 +1,4 @@ -use ndarray::{Array1, Array2}; +use ndarray::{s, Array1, Array2}; use crate::{Sampling, Sampling2D}; @@ -50,7 +50,10 @@ impl Sampling2D for Cfgns { cfgns[[1, i]] = self.rho * fgn1[i - 1] + (1.0 - self.rho.powi(2)).sqrt() * fgn2[i - 1]; } - [cfgns.row(0).into_owned(), cfgns.row(1).into_owned()] + [ + cfgns.row(0).slice(s![..self.n()]).into_owned(), + cfgns.row(1).slice(s![..self.n()]).into_owned(), + ] } fn n(&self) -> usize { diff --git a/stochastic-rs-core/src/noise/fgn.rs b/stochastic-rs-core/src/noise/fgn.rs index d89824a..fa819e2 100644 --- a/stochastic-rs-core/src/noise/fgn.rs +++ b/stochastic-rs-core/src/noise/fgn.rs @@ -31,9 +31,9 @@ impl Fgn { if !(0.0..=1.0).contains(&hurst) { panic!("Hurst parameter must be between 0 and 1"); } - let n_ = n.next_power_of_two(); - let offset = n_ - n; - let n = n_; + + let offset = n.next_power_of_two() - n; + let n = n.next_power_of_two(); let mut r = Array1::linspace(0.0, n as f64, n + 1); r.mapv_inplace(|x| { if x == 0.0 { @@ -96,7 +96,7 @@ impl Sampling for Fgn { } fn n(&self) -> usize { - self.n + self.n - self.offset } fn m(&self) -> Option { @@ -112,7 +112,7 @@ mod tests { #[test] fn plot() { - let fgn = Fgn::new(0.7, 1024, Some(1.0), Some(1)); + let fgn = Fgn::new(0.7, 1000, Some(1.0), Some(1)); let mut plot = Plot::new(); let d = fgn.sample_par(); for data in d.axis_iter(Axis(0)) { diff --git a/stochastic-rs-core/src/process/fbm.rs b/stochastic-rs-core/src/process/fbm.rs index 6ce0118..3444031 100644 --- a/stochastic-rs-core/src/process/fbm.rs +++ b/stochastic-rs-core/src/process/fbm.rs @@ -32,7 +32,6 @@ impl Fbm { impl Sampling for Fbm { fn sample(&self) -> Array1 { let fgn = self.fgn.sample(); - let mut fbm = Array1::::zeros(self.n + 1); fbm.slice_mut(s![1..]).assign(&fgn); @@ -40,7 +39,7 @@ impl Sampling for Fbm { fbm[i] += fbm[i - 1]; } - fbm + fbm.slice(s![..self.n()]).to_owned() } fn n(&self) -> usize { @@ -68,6 +67,7 @@ mod tests { m: Some(1), ..Default::default() }); + let mut plot = Plot::new(); let d = fbm.sample_par(); for data in d.axis_iter(Axis(0)) {