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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
//! Module to handle Gaussian distributions.
//!
//! # Examples
//!
//! If the parameters of a Gaussian distribution are already known a Gaussian
//! distribution can be simply created as follows.
//!
//! ```
//! # extern crate rustml;
//! use rustml::gaussian::*;
//!
//! # fn main() {
//! let g = Gaussian::new(0.5 /* mean */, 0.1 /* variance */);
//! assert_eq!(g.mean(), 0.5);
//! assert_eq!(g.var(), 0.1);
//! // standard deviation
//! assert!(g.std() - 0.31623 <= 0.0001);
//! # }
//! ```
//! 

extern crate num;

use self::num::traits::Float;
use math::{Mean, MeanVec, Var};
use math::{Dimension, Normalization};


/// Trait to estimate the mean and the variance of a set of samples.
pub trait GaussianEstimator<T> {
    fn gaussian(&self, nrm: Normalization) -> Gaussian<T>;
}

/// Contains all parameters to describe a Gaussian distribution, i.e. the
/// mean and the variance.
///
/// Given a set of samples the parameters can be estimated for all types for
/// which the trait [Gaussian](trait.Gaussian.html) is implemented.
pub struct Gaussian<T> {
    mean: T,
    var: T
}

impl <T: Float> Gaussian<T> {

    /// Creates a gaussian distribution from the given mean `mean` and the variance
    /// `var`.
    pub fn new(mean: T, var: T) -> Gaussian<T> {
        Gaussian {
            mean: mean,
            var: var
        }
    }
}

pub trait GaussianFunctions<T> {

    /// Returns the mean of the Gaussian distribution.
    /// 
    /// # Examples
    /// ```
    /// # extern crate rustml;
    /// use rustml::gaussian::*;
    /// use rustml::Normalization;
    ///
    /// # fn main() {
    /// let a = vec![1.0f32, 2.0, 4.0, 3.0, 6.0, 5.0];
    /// let g = a.gaussian(Normalization::N);
    /// assert_eq!(g.mean(), 3.5);
    /// # }
    /// ```
    fn mean(&self) -> T;

    /// Returns the variance of the Gaussian distribution.
    ///
    /// # Examples
    /// ```
    /// # extern crate rustml;
    /// use rustml::gaussian::*;
    /// use rustml::Normalization;
    ///
    /// # fn main() {
    /// let a = vec![1.0f32, 2.0, 4.0, 3.0, 6.0, 5.0];
    /// let g = a.gaussian(Normalization::N);
    /// assert!(g.var() - 2.9167 <= 0.0001);
    /// # }
    /// ```
    fn var(&self) -> T;

    /// Returns the standard deviation (the square root of the variance)
    /// of the Gaussian distribution.
    ///
    /// # Examples
    /// ```
    /// # extern crate rustml;
    /// use rustml::gaussian::*;
    /// use rustml::Normalization;
    ///
    /// # fn main() {
    /// let a = vec![1.0f32, 2.0, 4.0, 3.0, 6.0, 5.0];
    /// let g = a.gaussian(Normalization::N);
    /// assert!(g.std() - 1.7078 <= 0.0001);
    /// # }
    /// ```
    fn std(&self) -> T;

    /// Computes the probability density function for the
    /// given value.
    ///
    /// # Examples
    /// ```
    /// # extern crate rustml;
    /// use rustml::gaussian::*;
    /// use rustml::Normalization;
    ///
    /// # fn main() {
    /// let a = vec![1.0f32, 2.0, 4.0, 3.0, 6.0, 5.0];
    /// let g = a.gaussian(Normalization::N);
    /// assert!(g.pr(2.3) - 0.18250 <= 0.00001);
    /// # }
    /// ```
    fn pr(&self, x: T) -> T;
}

macro_rules! gaussian_impl {
    ($($t:ty)*) => ($(
        impl GaussianFunctions<$t> for Gaussian<$t> {

            fn mean(&self) -> $t { self.mean.clone() }

            fn var(&self) -> $t { self.var.clone() }

            fn std(&self) -> $t { self.var().sqrt() }

            fn pr(&self, x: $t) -> $t {

                let s = self.std();
                let m = self.mean();

                let a = s * ((2.0 as $t) * 3.14159265 as $t).sqrt();
                let b = (-(x-m)*(x-m) / ((2.0 as $t) * s * s)).exp();
                b / a
            }
        }

        impl GaussianEstimator<$t> for Vec<$t> {
            fn gaussian(&self, nrm: Normalization) -> Gaussian<$t> {
                (&self[..]).gaussian(nrm)
            }
        }

        impl GaussianEstimator<$t> for [$t] {
            fn gaussian(&self, nrm: Normalization) -> Gaussian<$t> {
                Gaussian {
                    mean: self.mean(),
                    var: self.var(Dimension::Row, nrm)
                }
            }
        }
    )*)
}

gaussian_impl!{ f32 f64 }

// ----------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use math::Normalization;

    #[test]
    fn test_parameters() {
        let a = vec![1.0f32, 2.0, 3.0, 2.0, 1.0, 2.0];
        let mut p = a.gaussian(Normalization::N);
        assert!(p.mean() - 1.8333 <= 0.0001);
        assert!(p.var() - 0.47222 <= 0.00001);
        assert!(p.std() - 0.68718 <= 0.00001);
        assert!(p.pr(2.0) - 0.56372 <= 0.00001);
        assert!(p.pr(1.5) - 0.51613 <= 0.00001);

        p = a.gaussian(Normalization::MinusOne);
        assert!(p.mean() - 1.8333 <= 0.0001);
        assert!(p.var() - 0.56667 <= 0.00001);
        assert!(p.std() - 0.75277 <= 0.00001);
        assert!(p.pr(2.0) - 0.51713 <= 0.00001);
        assert!(p.pr(1.5) - 0.48048 <= 0.00001);
    }
}