Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Beta distribution #574

Merged
merged 1 commit into from
Aug 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,49 @@ impl Distribution<f64> for StudentT {
}
}

/// The Beta distribution with shape parameters `alpha` and `beta`.
///
/// # Example
///
/// ```
/// use rand::distributions::{Distribution, Beta};
///
/// let beta = Beta::new(2.0, 5.0);
/// let v = beta.sample(&mut rand::thread_rng());
/// println!("{} is from a Beta(2, 5) distribution", v);
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Beta {
gamma_a: Gamma,
gamma_b: Gamma,
}

impl Beta {
/// Construct an object representing the `Beta(alpha, beta)`
/// distribution.
///
/// Panics if `shape <= 0` or `scale <= 0`.
pub fn new(alpha: f64, beta: f64) -> Beta {
assert!((alpha > 0.) & (beta > 0.));
Beta {
gamma_a: Gamma::new(alpha, 1.),
gamma_b: Gamma::new(beta, 1.),
}
}
}

impl Distribution<f64> for Beta {
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let x = self.gamma_a.sample(rng);
let y = self.gamma_b.sample(rng);
x / (x + y)
}
}

#[cfg(test)]
mod test {
use distributions::Distribution;
use super::{ChiSquared, StudentT, FisherF};
use super::{Beta, ChiSquared, StudentT, FisherF};

#[test]
fn test_chi_squared_one() {
Expand Down Expand Up @@ -357,4 +396,19 @@ mod test {
t.sample(&mut rng);
}
}

#[test]
fn test_beta() {
let beta = Beta::new(1.0, 2.0);
let mut rng = ::test::rng(201);
for _ in 0..1000 {
beta.sample(&mut rng);
}
}

#[test]
#[should_panic]
fn test_beta_invalid_dof() {
Beta::new(0., 0.);
}
}
7 changes: 5 additions & 2 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
//! - [`StudentT`] distribution
//! - [`FisherF`] distribution
//! - Triangular distribution:
//! - [`Triangular`] distribution
//! - [`Beta`] distribution
//! - [`Triangular`] distribution
//! - Multivariate probability distributions
//! - [`Dirichlet`] distribution
//! - [`UnitSphereSurface`] distribution
Expand Down Expand Up @@ -153,6 +154,7 @@
// distributions
//! [`Alphanumeric`]: struct.Alphanumeric.html
//! [`Bernoulli`]: struct.Bernoulli.html
//! [`Beta`]: struct.Beta.html
//! [`Binomial`]: struct.Binomial.html
//! [`Cauchy`]: struct.Cauchy.html
//! [`ChiSquared`]: struct.ChiSquared.html
Expand Down Expand Up @@ -187,7 +189,8 @@ pub use self::bernoulli::Bernoulli;
#[cfg(feature="alloc")] pub use self::weighted::{WeightedIndex, WeightedError};
#[cfg(feature="std")] pub use self::unit_sphere::UnitSphereSurface;
#[cfg(feature="std")] pub use self::unit_circle::UnitCircle;
#[cfg(feature="std")] pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
#[cfg(feature="std")] pub use self::gamma::{Gamma, ChiSquared, FisherF,
StudentT, Beta};
#[cfg(feature="std")] pub use self::normal::{Normal, LogNormal, StandardNormal};
#[cfg(feature="std")] pub use self::exponential::{Exp, Exp1};
#[cfg(feature="std")] pub use self::pareto::Pareto;
Expand Down