[][src]Struct rstat::UnitSimplex

pub struct UnitSimplex(_);

Utility for sampling from a unit \(K\)-simplex.

Methods

impl UnitSimplex[src]

pub fn new(n: usize) -> UnitSimplex[src]

Construct a \(K = n + 1\) probability simplex.

pub fn centre(&self) -> SimplexVector[src]

Compute the central point of the simplex \(x_i = 1 / K\).

Examples

use rstat::UnitSimplex;

let s = UnitSimplex::new(2).centre();

for i in 0..3 {
    assert_eq!(s[i], 1.0 / 3.0);
}

pub fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> SimplexVector[src]

Draws a uniformly random point on the simplex.

This algorithm works as follows:

  1. Draw \(K\) independent points, \(x_i \in [0, 1]\), uniformly at random.
  2. Apply the transformation \(z_i = -\ln{x_i}\).
  3. Compute the sum \(s = \sum_i x_i\).
  4. Return the vector of values \(z_i / s\).

Examples

use rand::thread_rng;
use rstat::UnitSimplex;

let s = UnitSimplex::new(2).sample(&mut thread_rng());

assert!((s.iter().sum::<f64>() - 1.0).abs() < 1e-7);

Trait Implementations

impl Clone for UnitSimplex[src]

impl Copy for UnitSimplex[src]

impl Debug for UnitSimplex[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,