|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// https://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! The dirichlet distribution. |
| 12 | +
|
| 13 | +use Rng; |
| 14 | +use distributions::Distribution; |
| 15 | +use distributions::gamma::Gamma; |
| 16 | + |
| 17 | +/// The dirichelet distribution `Dirichlet(alpha)`. |
| 18 | +/// |
| 19 | +/// The Dirichlet distribution is a family of continuous multivariate probability distributions parameterized by |
| 20 | +/// a vector alpha of positive reals. https://en.wikipedia.org/wiki/Dirichlet_distribution |
| 21 | +/// It is a multivariate generalization of the beta distribution. |
| 22 | +/// |
| 23 | +/// # Example |
| 24 | +/// |
| 25 | +/// ``` |
| 26 | +/// use rand::prelude::*; |
| 27 | +/// use rand::distributions::Dirichlet; |
| 28 | +/// |
| 29 | +/// let dirichlet = Dirichlet::new(vec![1.0, 2.0, 3.0]); |
| 30 | +/// let samples = dirichlet.sample(&mut rand::thread_rng()); |
| 31 | +/// println!("{:?} is from a Dirichlet([1.0, 2.0, 3.0]) distribution", samples); |
| 32 | +/// ``` |
| 33 | +
|
| 34 | +#[derive(Clone, Debug)] |
| 35 | +pub struct Dirichlet { |
| 36 | + /// Concentration parameters (alpha) |
| 37 | + alpha: Vec<f64>, |
| 38 | +} |
| 39 | + |
| 40 | +impl Dirichlet { |
| 41 | + /// Construct a new `Dirichlet` with the given alpha parameter `alpha`. |
| 42 | + /// |
| 43 | + /// # Panics |
| 44 | + /// - if `alpha.len() < 2` |
| 45 | + /// |
| 46 | + #[inline] |
| 47 | + pub fn new<V: Into<Vec<f64>>>(alpha: V) -> Dirichlet { |
| 48 | + let a = alpha.into(); |
| 49 | + assert!(a.len() > 1); |
| 50 | + for i in 0..a.len() { |
| 51 | + assert!(a[i] > 0.0); |
| 52 | + } |
| 53 | + |
| 54 | + Dirichlet { alpha: a } |
| 55 | + } |
| 56 | + |
| 57 | + /// Construct a new `Dirichlet` with the given shape parameter `alpha` and `size`. |
| 58 | + /// |
| 59 | + /// # Panics |
| 60 | + /// - if `alpha <= 0.0` |
| 61 | + /// - if `size < 2` |
| 62 | + /// |
| 63 | + #[inline] |
| 64 | + pub fn new_with_param(alpha: f64, size: usize) -> Dirichlet { |
| 65 | + assert!(alpha > 0.0); |
| 66 | + assert!(size > 1); |
| 67 | + Dirichlet { |
| 68 | + alpha: vec![alpha; size], |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Distribution<Vec<f64>> for Dirichlet { |
| 74 | + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec<f64> { |
| 75 | + let n = self.alpha.len(); |
| 76 | + let mut samples = vec![0.0f64; n]; |
| 77 | + let mut sum = 0.0f64; |
| 78 | + |
| 79 | + for i in 0..n { |
| 80 | + let g = Gamma::new(self.alpha[i], 1.0); |
| 81 | + samples[i] = g.sample(rng); |
| 82 | + sum += samples[i]; |
| 83 | + } |
| 84 | + let invacc = 1.0 / sum; |
| 85 | + for i in 0..n { |
| 86 | + samples[i] *= invacc; |
| 87 | + } |
| 88 | + samples |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod test { |
| 94 | + use super::Dirichlet; |
| 95 | + use distributions::Distribution; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_dirichlet() { |
| 99 | + let d = Dirichlet::new(vec![1.0, 2.0, 3.0]); |
| 100 | + let mut rng = ::test::rng(221); |
| 101 | + let samples = d.sample(&mut rng); |
| 102 | + let _: Vec<f64> = samples |
| 103 | + .into_iter() |
| 104 | + .map(|x| { |
| 105 | + assert!(x > 0.0); |
| 106 | + x |
| 107 | + }) |
| 108 | + .collect(); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn test_dirichlet_with_param() { |
| 113 | + let alpha = 0.5f64; |
| 114 | + let size = 2; |
| 115 | + let d = Dirichlet::new_with_param(alpha, size); |
| 116 | + let mut rng = ::test::rng(221); |
| 117 | + let samples = d.sample(&mut rng); |
| 118 | + let _: Vec<f64> = samples |
| 119 | + .into_iter() |
| 120 | + .map(|x| { |
| 121 | + assert!(x > 0.0); |
| 122 | + x |
| 123 | + }) |
| 124 | + .collect(); |
| 125 | + } |
| 126 | + |
| 127 | + #[test] |
| 128 | + #[should_panic] |
| 129 | + fn test_dirichlet_invalid_length() { |
| 130 | + Dirichlet::new_with_param(0.5f64, 1); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + #[should_panic] |
| 135 | + fn test_dirichlet_invalid_alpha() { |
| 136 | + Dirichlet::new_with_param(0.0f64, 2); |
| 137 | + } |
| 138 | +} |
0 commit comments