gevlib/dist/
distutils.rs

1//! Functions used for the distributions.
2
3// To verify that specific domains of parameters are respected.
4// In fact this macro comes form: https://docs.rs/probability/latest/src/probability/lib.rs.html#1-35
5
6/// macro used to ensure that the given domain is valid.
7#[macro_use]
8pub mod macros {
9    macro_rules! domain( // should you consider an error enum instead? What is faster? More understandable? One variant for param and one for quantile domain
10        ($requirement:expr) => (debug_assert!($requirement));
11        ($requirement:expr, $code:expr) => (debug_assert!($code, stringify!($requirement)));
12    );
13}
14
15/// Distributional Quantity trait (i.e. each distribution will provide each of the following
16/// quantities: the CDF, PDF, Quantile and random generation)
17pub trait DistQuant {
18    /// Cumulative Distribution Function (CDF)
19    fn cdf(&self, x: f64) -> f64;
20    /// Probability Density Function (PDF)
21    fn pdf(&self, x: f64) -> f64;
22    /// Quantile Function
23    fn quantile(&self, x: f64) -> f64;
24    /// Generate a random value from the distribution
25    fn random(&self, seed: RandomSeed) -> f64;
26}
27
28/// Seeding for the random generation of the distributions.
29/// Can either be Empty (i.e. use random seed) or with a given u64 seed.
30pub enum RandomSeed {
31    Empty,
32    Seed(u64),
33}
34
35/// Default Trait
36impl Default for RandomSeed {
37    /// Default seed is Empty (i.e. random seed)
38    fn default() -> Self {
39        RandomSeed::Empty
40    }
41}
42
43impl RandomSeed {
44    /// To be able to retrieve the given seed, if ever needed.
45    pub fn get_seed(&self) -> Option<u64> {
46        match self {
47            RandomSeed::Empty => None,
48            RandomSeed::Seed(val) => Some(*val),
49        }
50    }
51}