Skip to main content

sample_dirichlet

Function sample_dirichlet 

Source
pub fn sample_dirichlet(n: usize, alpha: &[f64]) -> Result<Vec<Vec<f64>>>
Expand description

Samples n points from the Dirichlet distribution with concentration parameters alpha, using the thread’s default random number generator.

Each returned point has alpha.len() non-negative coordinates summing to one (a random probability vector); its expected value is alpha / Σ alpha, and larger concentrations give less spread-out samples. Extremely small concentrations (e.g. 1e-300) make igraph’s gamma samples underflow to zero, and then the coordinates are NaN.

Binds igraph_rng_sample_dirichlet. Time complexity: O(n · alpha.len()).

§Errors

ErrorKind::InvalidValue if alpha has fewer than two entries or an entry that is not positive and finite.

§Examples

use igraph::{misc, prelude::*};

rng::seed(42)?;
for p in misc::sample_dirichlet(100, &[1.0, 2.0, 3.0])? {
    assert_eq!(p.len(), 3);
    assert!((p.iter().sum::<f64>() - 1.0).abs() < 1e-9);
}