Skip to content

Commit

Permalink
fix: index errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dancixx committed Sep 12, 2024
1 parent 9149059 commit eeb9f97
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 30 deletions.
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/diffusion/fcir.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::Array1;
use ndarray::{s, Array1};

use crate::{noise::fgn::Fgn, Sampling};

Expand Down Expand Up @@ -57,7 +57,7 @@ impl Sampling<f64> 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 {
Expand Down
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/diffusion/fgbm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::Array1;
use ndarray::{s, Array1};

use crate::{noise::fgn::Fgn, Sampling};

Expand Down Expand Up @@ -49,7 +49,7 @@ impl Sampling<f64> 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 {
Expand Down
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/diffusion/fjacobi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::Array1;
use ndarray::{s, Array1};

use crate::{noise::fgn::Fgn, Sampling};

Expand Down Expand Up @@ -63,7 +63,7 @@ impl Sampling<f64> for Fjacobi {
}
}

fjacobi
fjacobi.slice(s![..self.n()]).to_owned()
}

fn n(&self) -> usize {
Expand Down
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/diffusion/fou.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::Array1;
use ndarray::{s, Array1};

use crate::{noise::fgn::Fgn, Sampling};

Expand Down Expand Up @@ -51,7 +51,7 @@ impl Sampling<f64> 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 {
Expand Down
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/jump/jump_fou.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::Array1;
use ndarray::{s, Array1};

use crate::{
noise::fgn::Fgn, process::cpoisson::CompoundPoisson, ProcessDistribution, Sampling, Sampling3D,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl<D: ProcessDistribution> Sampling<f64> for JumpFou<D> {
+ jumps.sum();
}

jump_fou
jump_fou.slice(s![..self.n()]).to_owned()
}

fn n(&self) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion stochastic-rs-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub trait Sampling<T: Clone + Send + Sync + Zero>: 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());
Expand Down
39 changes: 29 additions & 10 deletions stochastic-rs-core/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
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,
t: None,
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);
}
7 changes: 5 additions & 2 deletions stochastic-rs-core/src/noise/cfgns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ndarray::{Array1, Array2};
use ndarray::{s, Array1, Array2};

use crate::{Sampling, Sampling2D};

Expand Down Expand Up @@ -50,7 +50,10 @@ impl Sampling2D<f64> 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 {
Expand Down
10 changes: 5 additions & 5 deletions stochastic-rs-core/src/noise/fgn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,7 +96,7 @@ impl Sampling<f64> for Fgn {
}

fn n(&self) -> usize {
self.n
self.n - self.offset
}

fn m(&self) -> Option<usize> {
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions stochastic-rs-core/src/process/fbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ impl Fbm {
impl Sampling<f64> for Fbm {
fn sample(&self) -> Array1<f64> {
let fgn = self.fgn.sample();

let mut fbm = Array1::<f64>::zeros(self.n + 1);
fbm.slice_mut(s![1..]).assign(&fgn);

for i in 1..(self.n + 1) {
fbm[i] += fbm[i - 1];
}

fbm
fbm.slice(s![..self.n()]).to_owned()
}

fn n(&self) -> usize {
Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit eeb9f97

Please sign in to comment.