Skip to content

Commit

Permalink
poisson
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJepsen committed Jul 28, 2023
1 parent 71be366 commit bf19032
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 76 deletions.
1 change: 0 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod environment;
pub mod manager;
pub mod math;
pub mod middleware;
pub mod time;
#[cfg(test)]
pub mod tests;
pub mod utils;
45 changes: 20 additions & 25 deletions core/src/math/stochastic_new.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use RustQuant::{stochastics::{BrownianMotion, OrnsteinUhlenbeck, StochasticProcess, Trajectories}, statistics::distributions::Poisson };
use RustQuant::{stochastics::{BrownianMotion, OrnsteinUhlenbeck, StochasticProcess, Trajectories}, statistics::distributions::{Poisson, Distribution} };
use anyhow::Result;


Expand All @@ -11,12 +11,18 @@ pub enum StochasticProcessType {
}
/// Struct for all processes init parameters.
pub struct EulerMaruyamaInput {
pub x_0: f64, // initial value at t_0
pub t_0: f64, // initial time point
pub t_n: f64, // terminal time point
pub n_steps: usize, // number of time steps between t_0 and t_n
pub m_paths: usize, // how many process trajectories to simulate
pub parallel: bool, // run in parallel or not (recommended for > 1000 paths)
/// initial value at t_0
pub x_0: f64,
/// initial time point
pub t_0: f64,
/// terminal time point
pub t_n: f64,
/// number of time steps between t_0 and t_n
pub n_steps: usize,
/// how many process trajectories to simulate
pub m_paths: usize,
/// run in parallel or not (recommended for > 1000 paths)
pub parallel: bool,
}

/// Create new process and run euler maruyama.
Expand All @@ -34,24 +40,13 @@ pub fn new_procces(proccess_type: StochasticProcessType, config: EulerMaruyamaIn
Ok(trajectories)
}

// impl StochasticProcess {
// pub fn new_brownian_motion() -> Self {
// Self::BrownianMotion(BrownianMotion::new())
// }
// pub fn new_ornstein_uhlenbeck() -> Self {
// Self::OrnsteinUhlenbeck(OrnsteinUhlenbeck::new())
// }
// pub fn new_poisson() -> Self {
// Self::Poisson(Poisson::new())
// }
// pub fn generate(&self, length: usize, seed: usize) -> Vec<f64> {
// match self {
// Self::BrownianMotion(brownian_motion) => brownian_motion.generate(length),
// Self::OrnsteinUhlenbeck(ornstein_uhlenbeck) => ornstein_uhlenbeck.generate(length),
// Self::Poisson(poisson) => poisson.generate(length),
// }
// }
// }
/// Sample Poisson process.
pub fn poisson_process(lambda: f64) {
let poisson = Poisson::new(lambda);
let sample = poisson.sample(10);
println!("{:#?} Poisson samples with lambda = {}", sample, lambda);

}

#[cfg(test)]
mod tests {
Expand Down
39 changes: 36 additions & 3 deletions core/src/math/stochastic_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,50 @@ impl OU {
}
}

use statrs::distribution::Poisson as PoissonDist;

#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Poisson_dist {
pub lambda: i64,
}

impl Poisson_dist {
fn new(lambda: i64) -> Self {
Poisson_dist { lambda }
}
fn default() -> Self {
Poisson_dist { lambda: 1 }
}
fn generate(&self, length: usize) -> Result<Vec<(i64, i64)>> {

let dist = PoissonDist::new(self.lambda as f64)?;
let values = dist.sample_iter(&mut rand::thread_rng()).take(length).collect::<Vec<f64>>();
let index = (0..values.len()).collect::<Vec<usize>>();

// convert usize to i32 and round f64 to i32
let index_i32: Vec<i64> = index.into_iter().map(|x| x as i64).collect();
let values_i32: Vec<i64> = values.into_iter().map(|x| x.round() as i64).collect();

// combine into a vector of tuples
let result: Vec<(i64, i64)> = index_i32.into_iter().zip(values_i32.into_iter()).collect();
Ok(result)
}

}



#[cfg(test)]
mod tests {

use super::*;

#[test]
pub fn add_test() {
assert_eq!(2 + 2, 4);
pub fn small_distribution() {
let poisson = Poisson_dist::new(1);
let result = poisson.generate(100).unwrap();
println!("{:?}", result);
}

#[test]
fn seeded_randomness_test() -> Result<()> {
let gbm = GBM::new(0.05, 0.3);
Expand Down
47 changes: 0 additions & 47 deletions core/src/time.rs

This file was deleted.

0 comments on commit bf19032

Please sign in to comment.