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
use crate::statistics::distrib::Continuous;
use crate::special::gamma;
use crate::special;
use std::f64;
use super::Normal;


/// Chi-Squared distribution
///
/// Fore more information:
/// <a href="https://en.wikipedia.org/wiki/Chi-squared_distribution">https://en.wikipedia.org/wiki/Chi-squared_distribution</a>
///
pub struct ChiSquared
{
    k: f64 //degree of freedom
}

impl ChiSquared
{
    /// Creates a probability distribution
    ///
    /// # Arguments
    ///
    /// * `df`: Degree of freedom, df >= 1
    ///
    /// # Panics
    ///
    /// if df < 1
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::statistics::distrib::ChiSquared;
    ///
    /// let distrib: ChiSquared = ChiSquared::new(&3);
    /// ```
    pub fn new(df: &u32) -> ChiSquared
    {
        if *df < 1
        {
            panic!()
        }
        ChiSquared
        {
            k: *df as f64
        }
    }
}


impl Continuous<f64, f64> for ChiSquared
{

    /// Probability density function
    ///
    /// # Arguments
    ///
    /// * `x` Random variable x &isin; &#x2115
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::statistics::distrib::{Continuous, ChiSquared};
    ///
    /// let distrib: ChiSquared = ChiSquared::new(&2);
    /// let x: f64 = 5.0;
    /// let p: f64 = distrib.pdf(x);
    /// ```
    fn pdf<'a>(self: &'a Self, x: f64) -> f64
    {
        if x < 0.0
        {
            return 0.0
        }
        let t1: f64 = 1.0 / ((2.0_f64).powf(self.k  / 2.0) * gamma::gamma(self.k / 2.0));
        let t2: f64 = x.powf(self.k / 2.0 - 1.0) * (-x / 2.0).exp();
        let chisquared: f64 = t1 * t2;

        chisquared
    }

    /// Cumulative distribution function
    ///
    /// # Arguments
    ///
    /// * `x` Random variable
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::statistics::distrib::{Continuous, ChiSquared};
    ///
    /// let distrib: ChiSquared = ChiSquared::new(&3);
    /// let x: f64 = 0.4;
    /// let p: f64 = distrib.cdf(x);
    /// ```
    fn cdf<'a>(self: &'a Self, x: f64) -> f64
    {
        let t1: f64 = (-x / 2.0).exp();

        let k_natural: u32 = self.k as u32;
        let p: f64;

        if k_natural % 2 == 0
        {
            let mut sum: f64 = 0.0;
            for i in 0..(self.k / 2.0) as u32
            {
                sum += (x / 2.0).powf(i as f64) / gamma::gamma((i + 1) as f64)
            }

            p = 1.0 - t1 * sum;
        }
        else
        {
            let mut sum: f64 = 0.0;
            for i in 0..(self.k / 2.0) as u32
            {
                sum += (x / 2.0).powf((i as f64) + 0.5) / gamma::gamma((i as f64) + 1.5)
            }

            p = special::erf((x / 2.0).sqrt()) - t1 * sum;
        }

        p
    }

    /// Quantile function of inverse cdf
    ///
    fn quantile<'a, 'b>(self: &'a Self, p: f64) -> f64
    {
        let std_distrib: Normal = Normal::new(0.0, 1.0);
        let q: f64 = 0.5 * (std_distrib.quantile(p) + (2.0 * self.k as f64 - 1.0).sqrt()).powi(2);
        q
    }

    /// Expected value
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::statistics::distrib::{Discrete, Bernoulli};
    ///
    /// let distrib: Bernoulli = Bernoulli::new(0.2);
    /// let mean: f64 = distrib.mean();
    /// ```
	fn mean<'a>(self: &'a Self) -> f64
    {
        return self.k as f64
    }

    /// Variance
    ///
    /// # Example
    ///
    /// ```
    /// use mathru::statistics::distrib::{Discrete, Bernoulli};
    ///
    /// let distrib: Bernoulli = Bernoulli::new(0.2);
    /// let var: f64 = distrib.variance();
    /// ```
	fn variance<'a>(self: &'a Self) -> f64
    {
        return 2.0 * (self.k as f64)
    }
}