Skip to content

Commit

Permalink
Merge pull request #574 from vks/beta
Browse files Browse the repository at this point in the history
Implement Beta distribution
  • Loading branch information
dhardy committed Aug 4, 2018
2 parents 6024971 + 15cb132 commit 3ce3ea7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
56 changes: 55 additions & 1 deletion src/distributions/gamma.rs
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
Expand Up @@ -101,7 +101,8 @@
//! - [`StudentT`] distribution
//! - [`FisherF`] distribution
//! - Triangular distribution:
//! - [`Triangular`] distribution
//! - [`Beta`] distribution
//! - [`Triangular`] distribution
//! - Multivariate probability distributions
//! - [`Dirichlet`] distribution
//! - [`UnitSphereSurface`] distribution
Expand Down Expand Up @@ -155,6 +156,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 @@ -190,7 +192,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

0 comments on commit 3ce3ea7

Please sign in to comment.