[][src]Struct rv::dist::Mixture

pub struct Mixture<Fx> { /* fields omitted */ }

Mixture distribution Σ wi f(x|θi)

A mixture distribution is a convex combination of distributions.

Example

A bimodal Gaussian mixture model

use rv::prelude::*;

let g1 = Gaussian::new(-2.5, 1.0).unwrap();
let g2 = Gaussian::new(2.0, 2.1).unwrap();

// f(x) = 0.6 * N(-2.5, 1.0) + 0.4 * N(2.0, 2.1)
let mm = Mixture::new(vec![0.6, 0.4], vec![g1, g2]).unwrap();

Implementations

impl<Fx> Mixture<Fx>[src]

pub fn new(weights: Vec<f64>, components: Vec<Fx>) -> Result<Self, MixtureError>[src]

Create a new mixture distribution

Arguments

  • weights: The weights for each component distribution. All entries must be positive and sum to 1.
  • components: The component distributions.

pub fn new_unchecked(weights: Vec<f64>, components: Vec<Fx>) -> Self[src]

Creates a new Mixture without checking whether the parameters are valid.

pub fn uniform(components: Vec<Fx>) -> Result<Self, MixtureError>[src]

Assume uniform component weights

Given a n-length vector of components, automatically sets the component weights to 1/n.

pub fn combine(mixtures: Vec<Mixture<Fx>>) -> Self[src]

Combines many mixtures into one big mixture

Notes

Assumes mixtures are valid.

pub fn k(&self) -> usize[src]

Number of components

pub fn weights(&self) -> &Vec<f64>[src]

Get a reference to the component weights

pub fn components(&self) -> &Vec<Fx>[src]

Get a reference to the components

pub fn set_weights(&mut self, weights: Vec<f64>) -> Result<(), MixtureError>[src]

Set the mixture weights

Example

use rv::dist::Gaussian;

let components = vec![
    Gaussian::new(-2.0, 1.0).unwrap(),
    Gaussian::new(2.0, 1.0).unwrap(),
];

let mut mm = Mixture::uniform(components).unwrap();
assert_eq!(mm.weights(), &vec![0.5, 0.5]);

mm.set_weights(vec![0.2, 0.8]).unwrap();
assert_eq!(mm.weights(), &vec![0.2, 0.8]);

Will error for invalid weights

// This is fine
assert!(mm.set_weights(vec![0.2, 0.8]).is_ok());

// Does not sum to 1
assert!(mm.set_weights(vec![0.1, 0.8]).is_err());

// Wrong number of weights
assert!(mm.set_weights(vec![0.1, 0.1, 0.8]).is_err());

// Negative weight
assert!(mm.set_weights(vec![-0.1, 1.1]).is_err());

// Zero weight are ok
assert!(mm.set_weights(vec![0.0, 1.0]).is_ok());

pub fn set_weights_unchecked(&mut self, weights: Vec<f64>)[src]

pub fn set_components(
    &mut self,
    components: Vec<Fx>
) -> Result<(), MixtureError>
[src]

Set the mixture components

Example

use rv::dist::Gaussian;
use rv::traits::Mean;

let components = vec![
    Gaussian::new(-2.0, 1.0).unwrap(),
    Gaussian::new(2.0, 1.0).unwrap(),
];

let mut mm = Mixture::uniform(components).unwrap();
let mean_1: f64 = mm.mean().unwrap();
assert_eq!(mean_1, 0.0);

let components_2 = vec![
    Gaussian::new(-3.0, 1.0).unwrap(),
    Gaussian::new(2.0, 1.0).unwrap(),
];
mm.set_components(components_2).unwrap();
let mean_2: f64 = mm.mean().unwrap();
assert_eq!(mean_2, -0.5);

pub fn set_components_unchecked(&mut self, components: Vec<Fx>)[src]

Trait Implementations

impl<X, Fx> Cdf<X> for Mixture<Fx> where
    Fx: Rv<X> + Cdf<X>, 
[src]

impl<Fx: Clone> Clone for Mixture<Fx>[src]

impl<X, Fx> ContinuousDistr<X> for Mixture<Fx> where
    Fx: Rv<X> + ContinuousDistr<X>, 
[src]

impl<Fx: Debug> Debug for Mixture<Fx>[src]

impl<X, Fx> DiscreteDistr<X> for Mixture<Fx> where
    Fx: Rv<X> + DiscreteDistr<X>, 
[src]

impl Entropy for Mixture<Categorical>[src]

impl<'_> Entropy for Mixture<&'_ Categorical>[src]

impl Entropy for Mixture<Poisson>[src]

impl<'_> Entropy for Mixture<&'_ Poisson>[src]

impl Entropy for Mixture<Gaussian>[src]

impl<'_> Entropy for Mixture<&'_ Gaussian>[src]

impl<Fx> Mean<f32> for Mixture<Fx> where
    Fx: Mean<f32>, 
[src]

impl<Fx> Mean<f64> for Mixture<Fx> where
    Fx: Mean<f64>, 
[src]

impl<Fx: PartialEq> PartialEq<Mixture<Fx>> for Mixture<Fx>[src]

impl QuadBounds for Mixture<Gaussian>[src]

impl<'_> QuadBounds for Mixture<&'_ Gaussian>[src]

impl QuadBounds for Mixture<Poisson>[src]

impl<'_> QuadBounds for Mixture<&'_ Poisson>[src]

impl<X, Fx> Rv<X> for Mixture<Fx> where
    Fx: Rv<X>, 
[src]

impl<Fx> StructuralPartialEq for Mixture<Fx>[src]

impl<X, Fx> Support<X> for Mixture<Fx> where
    Fx: Rv<X> + Support<X>, 
[src]

impl<Fx> Variance<f32> for Mixture<Fx> where
    Fx: ContinuousDistr<f32> + Mean<f32> + Variance<f32>, 
[src]

impl<Fx> Variance<f64> for Mixture<Fx> where
    Fx: ContinuousDistr<f64> + Mean<f64> + Variance<f64>, 
[src]

Auto Trait Implementations

impl<Fx> RefUnwindSafe for Mixture<Fx> where
    Fx: RefUnwindSafe

impl<Fx> Send for Mixture<Fx> where
    Fx: Send

impl<Fx> Sync for Mixture<Fx> where
    Fx: Sync

impl<Fx> Unpin for Mixture<Fx> where
    Fx: Unpin

impl<Fx> UnwindSafe for Mixture<Fx> where
    Fx: UnwindSafe

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<Fx, X> Cdf<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Cdf<X>, 
[src]

impl<Fx, X> ContinuousDistr<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: ContinuousDistr<X>, 
[src]

impl<Fx, X> DiscreteDistr<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: DiscreteDistr<X>, 
[src]

impl<Fx> Entropy for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Entropy
[src]

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

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

impl<Fx, X> Mean<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Mean<X>, 
[src]

impl<Fx, X> Rv<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Rv<X>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<SS, SP> SupersetOf<SS> for SP where
    SS: SubsetOf<SP>, 

impl<Fx, X> Support<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Support<X>, 
[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>, 

impl<Fx, X> Variance<X> for Fx where
    Fx: Deref,
    <Fx as Deref>::Target: Variance<X>, 
[src]