1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use super::traits::*;
use rand::prelude::*;
use rand_distr::{Uniform, Poisson, Distribution};

use ndarray::array;
use ndarray::prelude::*;
use ndarray_parallel::prelude::*;

use rayon::prelude::*;


/// Homogeneous, constant intensity Poisson process.
pub struct PoissonProcess {
    /// Process intensity.
    lambda: f64
}

impl PoissonProcess {
    pub fn new(lambda: f64) -> Self {
        PoissonProcess {
            lambda
        }
    }
}

impl DeterministicIntensity for PoissonProcess {
    fn intensity(&self, _t: f64) -> f64 {
        self.lambda
    }
}

/// Poisson process with variable intensity.
pub struct VariablePoissonProcess<F>
where F: Fn(f64) -> f64 + Send + Sync
{
    /// Upper bound on the intensity function of the process.
    max_lambda: f64,
    /// Process intensity function.
    lambda: F
}

impl<F> DeterministicIntensity for VariablePoissonProcess<F>
where F: Fn(f64) -> f64 + Send + Sync
{
    fn intensity(&self, t: f64) -> f64 {
        (self.lambda)(t)
    }
}

impl<F> VariablePoissonProcess<F>
where F: Fn(f64) -> f64 + Send + Sync
{
    pub fn new(lambda: F, max_lambda: f64) -> Self {
        VariablePoissonProcess {
            max_lambda,
            lambda
        }
    }
}


impl TemporalProcess for PoissonProcess {
    fn sample(&self, tmax: f64) -> TimeProcessResult {
        let lambda = self.lambda;
        let mut rng = thread_rng();
        let fish = Poisson::new(tmax * lambda).unwrap();
        let num_events: u64 = fish.sample(&mut rng);
        let num_events = num_events as usize;
        
        let mut events_vec: Vec<_> = (0..num_events).into_par_iter()
            .map(|_| {
                // get reference to local thread rng
                let mut rng = thread_rng();
                let u = Uniform::new(0.0, tmax);
                u.sample(&mut rng)
        }).collect();
        events_vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let timestamps = Array1::<f64>::from_vec(events_vec);
        let mut intensities = Array1::<f64>::zeros(num_events as usize);
        for i in 0..num_events as usize {
            intensities[i] = lambda;
        }
        TimeProcessResult {
            timestamps, intensities
        }
    }
}

impl<F> TemporalProcess for VariablePoissonProcess<F>
where F: Fn(f64) -> f64 + Send + Sync
{
    fn sample(&self, tmax: f64) -> TimeProcessResult {
        // Parallelized, multithreaded algorithm for sampling
        // from the process.

        let mut rng = thread_rng();
        let max_lambda = self.max_lambda;
        let lambda = &self.lambda;
        let fish = Poisson::new(tmax*max_lambda).unwrap();
        let num_events: u64 = fish.sample(&mut rng);
        let num_events = num_events as usize;
        let lambda = std::sync::Arc::from(lambda);
        
        // Get timestamp and intensity values of events distributed
        // according to a homogeneous Poisson process
        // and keep those who are under the intensity curve
        let mut events: Vec<Array1<f64>> = (0..num_events)
                .into_par_iter().filter_map(|_| {
            let mut rng = thread_rng();
            let timestamp = rng.gen::<f64>()*tmax;
            let lambda_val = rng.gen::<f64>()*max_lambda;

            if lambda_val < lambda(timestamp) {
                Some(array![timestamp, lambda(timestamp)])
            } else {
                None
            }
        }).collect();
        events.sort_by(|a, b| a[0].partial_cmp(&b[0]).unwrap());

        let num_events = events.len();

        let mut timestamps = Array1::<f64>::zeros(num_events);
        let mut intensities = Array1::<f64>::zeros(num_events);
        if num_events > 0 {
            for i in 0..num_events {
                timestamps[i] = events[i][0];
                intensities[i] = events[i][1];

            }
        }
        TimeProcessResult { timestamps, intensities }
    }
}