]>
git.proxmox.com Git - rustc.git/blob - vendor/rand/src/distributions/dirichlet.rs
1 // Copyright 2018 Developers of the Rand project.
2 // Copyright 2013 The Rust Project Developers.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! The dirichlet distribution.
12 #![allow(clippy::all)]
14 use crate::distributions
::gamma
::Gamma
;
15 use crate::distributions
::Distribution
;
18 /// The dirichelet distribution `Dirichlet(alpha)`.
20 /// The Dirichlet distribution is a family of continuous multivariate
21 /// probability distributions parameterized by a vector alpha of positive reals.
22 /// It is a multivariate generalization of the beta distribution.
23 #[deprecated(since = "0.7.0", note = "moved to rand_distr crate")]
24 #[derive(Clone, Debug)]
25 pub struct Dirichlet
{
26 /// Concentration parameters (alpha)
31 /// Construct a new `Dirichlet` with the given alpha parameter `alpha`.
34 /// - if `alpha.len() < 2`
36 pub fn new
<V
: Into
<Vec
<f64>>>(alpha
: V
) -> Dirichlet
{
43 Dirichlet { alpha: a }
46 /// Construct a new `Dirichlet` with the given shape parameter `alpha` and `size`.
49 /// - if `alpha <= 0.0`
52 pub fn new_with_param(alpha
: f64, size
: usize) -> Dirichlet
{
56 alpha
: vec
![alpha
; size
],
61 impl Distribution
<Vec
<f64>> for Dirichlet
{
62 fn sample
<R
: Rng
+ ?Sized
>(&self, rng
: &mut R
) -> Vec
<f64> {
63 let n
= self.alpha
.len();
64 let mut samples
= vec
![0.0f64; n
];
68 let g
= Gamma
::new(self.alpha
[i
], 1.0);
69 samples
[i
] = g
.sample(rng
);
72 let invacc
= 1.0 / sum
;
83 use crate::distributions
::Distribution
;
87 let d
= Dirichlet
::new(vec
![1.0, 2.0, 3.0]);
88 let mut rng
= crate::test
::rng(221);
89 let samples
= d
.sample(&mut rng
);
90 let _
: Vec
<f64> = samples
100 fn test_dirichlet_with_param() {
103 let d
= Dirichlet
::new_with_param(alpha
, size
);
104 let mut rng
= crate::test
::rng(221);
105 let samples
= d
.sample(&mut rng
);
106 let _
: Vec
<f64> = samples
117 fn test_dirichlet_invalid_length() {
118 Dirichlet
::new_with_param(0.5f64, 1);
123 fn test_dirichlet_invalid_alpha() {
124 Dirichlet
::new_with_param(0.0f64, 2);