<commit_before>use rand::distributions::{Gamma, IndependentSample};

use {Distribution, Generator};

/// A beta distribution.
#[derive(Clone, Copy)]
pub struct Beta {
    /// The first shape parameter.
    pub alpha: f64,
    /// The second shape parameter.
    pub beta: f64,
    /// The left endpoint of the support.
    pub a: f64,
    /// The right endpoint of the support.
    pub b: f64,

    ln_beta: f64,
    gamma_alpha: Gamma,
    gamma_beta: Gamma,
}

impl Beta {
    /// Create a beta distribution with the shape parameters `alpha` and `beta`
    /// on the interval `[a, b]`.
    ///
    /// # Panics
    ///
    /// Panics if `a >= b`.
    #[inline]
    pub fn new(alpha: f64, beta: f64, a: f64, b: f64) -> Beta {
        use special::ln_beta;
        debug_assert!(a < b, "Beta::new() is called with a >= b");
        Beta {
            alpha: alpha,
            beta: beta,
            a: a,
            b: b,
            ln_beta: ln_beta(alpha, beta),
            gamma_alpha: Gamma::new(alpha, 1.0),
            gamma_beta: Gamma::new(beta, 1.0),
        }
    }
}

impl Distribution for Beta {
    type Item = f64;

    fn mean(&self) -> f64 {
        let (loc, scale) = (self.a, self.b - self.a);
        loc + scale * self.alpha / (self.alpha + self.beta)
    }

    fn var(&self) -> f64 {
        let scale_sq = (self.b - self.a).powi(2);
        let s = self.alpha + self.beta;
        scale_sq * (self.alpha * self.beta) / (s.powi(2) * (s + 1.0))
    }

    fn sd(&self) -> f64 {
        self.var().sqrt()
    }

    fn median(&self) -> f64 {
        match (self.alpha, self.beta) {
            (alpha, beta) if alpha == beta => 0.5 * (self.b - self.a),
            (alpha, beta) if alpha > 1.0 && beta > 1.0 => {
                let (loc, scale) = (self.a, self.b - self.a);
                loc + scale * (alpha - 1.0 / 3.0) / (alpha + beta - 2.0 / 3.0)
            },
            _ => self.inv_cdf(0.5),
        }
    }

    fn modes(&self) -> Vec<f64> {
        match (self.alpha, self.beta) {
            (1.0, 1.0) => vec![],
            (1.0, beta) if beta > 1.0 => vec![self.a],
            (alpha, 1.0) if alpha > 1.0 => vec![self.b],
            (alpha, beta) if alpha < 1.0 && beta < 1.0 => vec![self.a, self.b],
            (alpha, beta) if alpha < 1.0 && beta >= 1.0 => vec![self.a],
            (alpha, beta) if alpha >= 1.0 && beta < 1.0 => vec![self.b],
            (alpha, beta) => {
                let (loc, scale) = (self.a, self.b - self.a);
                vec![loc + scale * (alpha - 1.0) / (alpha + beta - 2.0)]
            },
        }
    }

    #[inline]
    fn skewness(&self) -> f64 {
        let (a, b) = (self.alpha, self.beta);
        (2.0 * (b - a) * (a + b + 1.0).sqrt()) /
            ((a + b + 2.0) * (a * b).sqrt())
    }

    #[inline]
    fn kurtosis(&self) -> f64 {
        let (a, b) = (self.alpha, self.beta);
        let s = a + b;
        let p = a * b;
        (6.0 * ((a - b).powi(2) * (s + 1.0) - p * (s + 2.0))) /
            (p * (s + 2.0) * (s + 3.0))
    }

    #[inline]
    fn entropy(&self) -> f64 {
        use special::{digamma, ln_beta};

        let (a, b) = (self.alpha, self.beta);
        let scale = self.b - self.a;
        let s = a + b;
        scale.ln() + ln_beta(a, b) -
            (a - 1.0) * digamma(a) -
            (b - 1.0) * digamma(b) +
            (s - 2.0) * digamma(s)
    }

    #[inline]
    fn cdf(&self, x: f64) -> f64 {
        use special::inc_beta;
        inc_beta((x - self.a) / (self.b - self.a), self.alpha, self.beta, self.ln_beta)
    }

    #[inline]
    fn inv_cdf(&self, p: f64) -> f64 {
        use special::inv_inc_beta;
        debug_assert!(0.0 <= p && p <= 1.0, "inv_cdf is called with p outside of [0, 1]");
        self.a + (self.b - self.a) * inv_inc_beta(p, self.alpha, self.beta, self.ln_beta)
    }

    #[inline]
    fn pdf(&self, x: f64) -> f64 {
        let norm = self.b - self.a;
        let x = (x - self.a) / (self.b - self.a);
        ((self.alpha - 1.0) * x.ln() + (self.beta - 1.0) * (1.0 - x).ln() -<commit_msg>Removed linebrakes for lines < 99<commit_after>use rand::distributions::{Gamma, IndependentSample};

use {Distribution, Generator};

/// A beta distribution.
#[derive(Clone, Copy)]
pub struct Beta {
    /// The first shape parameter.
    pub alpha: f64,
    /// The second shape parameter.
    pub beta: f64,
    /// The left endpoint of the support.
    pub a: f64,
    /// The right endpoint of the support.
    pub b: f64,

    ln_beta: f64,
    gamma_alpha: Gamma,
    gamma_beta: Gamma,
}

impl Beta {
    /// Create a beta distribution with the shape parameters `alpha` and `beta`
    /// on the interval `[a, b]`.
    ///
    /// # Panics
    ///
    /// Panics if `a >= b`.
    #[inline]
    pub fn new(alpha: f64, beta: f64, a: f64, b: f64) -> Beta {
        use special::ln_beta;
        debug_assert!(a < b, "Beta::new() is called with a >= b");
        Beta {
            alpha: alpha,
            beta: beta,
            a: a,
            b: b,
            ln_beta: ln_beta(alpha, beta),
            gamma_alpha: Gamma::new(alpha, 1.0),
            gamma_beta: Gamma::new(beta, 1.0),
        }
    }
}

impl Distribution for Beta {
    type Item = f64;

    fn mean(&self) -> f64 {
        let (loc, scale) = (self.a, self.b - self.a);
        loc + scale * self.alpha / (self.alpha + self.beta)
    }

    fn var(&self) -> f64 {
        let scale_sq = (self.b - self.a).powi(2);
        let s = self.alpha + self.beta;
        scale_sq * (self.alpha * self.beta) / (s.powi(2) * (s + 1.0))
    }

    fn sd(&self) -> f64 {
        self.var().sqrt()
    }

    fn median(&self) -> f64 {
        match (self.alpha, self.beta) {
            (alpha, beta) if alpha == beta => 0.5 * (self.b - self.a),
            (alpha, beta) if alpha > 1.0 && beta > 1.0 => {
                let (loc, scale) = (self.a, self.b - self.a);
                loc + scale * (alpha - 1.0 / 3.0) / (alpha + beta - 2.0 / 3.0)
            },
            _ => self.inv_cdf(0.5),
        }
    }

    fn modes(&self) -> Vec<f64> {
        match (self.alpha, self.beta) {
            (1.0, 1.0) => vec![],
            (1.0, beta) if beta > 1.0 => vec![self.a],
            (alpha, 1.0) if alpha > 1.0 => vec![self.b],
            (alpha, beta) if alpha < 1.0 && beta < 1.0 => vec![self.a, self.b],
            (alpha, beta) if alpha < 1.0 && beta >= 1.0 => vec![self.a],
            (alpha, beta) if alpha >= 1.0 && beta < 1.0 => vec![self.b],
            (alpha, beta) => {
                let (loc, scale) = (self.a, self.b - self.a);
                vec![loc + scale * (alpha - 1.0) / (alpha + beta - 2.0)]
            },
        }
    }

    #[inline]
    fn skewness(&self) -> f64 {
        let (a, b) = (self.alpha, self.beta);
        (2.0 * (b - a) * (a + b + 1.0).sqrt()) /  ((a + b + 2.0) * (a * b).sqrt())
    }

    #[inline]
    fn kurtosis(&self) -> f64 {
        let (a, b) = (self.alpha, self.beta);
        let s = a + b;
        let p = a * b;
        (6.0 * ((a - b).powi(2) * (s + 1.0) - p * (s + 2.0))) / (p * (s + 2.0) * (s + 3.0))
    }

    #[inline]
    fn entropy(&self) -> f64 {
        use special::{digamma, ln_beta};

        let (a, b) = (self.alpha, self.beta);
        let scale = self.b - self.a;
        let s = a + b;
        scale.ln() + ln_beta(a, b) - (a - 1.0) * digamma(a) -
            (b - 1.0) * digamma(b) + (s - 2.0) * digamma(s)
    }

    #[inline]
    fn cdf(&self, x: f64) -> f64 {
        use special::inc_beta;
        inc_beta((x - self.a) / (self.b - self.a), self.alpha, self.beta, self.ln_beta)
    }

    #[inline]
    fn inv_cdf(&self, p: f64) -> f64 {
        use special::inv_inc_beta;
        debug_assert!(0.0 <= p && p <= 1.0, "inv_cdf is called with p outside of [0, 1]");
        self.a + (self.b - self.a) * inv_inc_beta(p, self.alpha, self.beta, self.ln_beta)
    }

    #[inline]
    fn pdf(&self, x: f64) -> f64 {
        let norm = self.b - self.a;
        let x = (x - self.a) / (self.b - self.a);
        ((self.alpha - 1.0) * x.ln() + (self.beta - 1.0) * (1.0 - x).ln() -
{'fraction_non_alphanumeric': 0.19103627584499194, 'fraction_numerical': 0.03367586975362139, 'mean_word_length': 1.5283255086071987, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4195555', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3039, 'n_words': 810}