Struct rv::dist::Mixture

source ·
pub struct Mixture<Fx> { /* private fields */ }
Expand description

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§

source§

impl<Fx> Mixture<Fx>

source

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

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.
source

pub fn ln_weights(&self) -> &[f64]

source

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

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

source

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

Assume uniform component weights

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

source

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

Combines many mixtures into one big mixture

§Notes

Assumes mixtures are valid.

source

pub fn k(&self) -> usize

Number of components

source

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

Get a reference to the component weights

source

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

Get a reference to the components

source

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

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());
source

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

source

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

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);
source

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

Trait Implementations§

source§

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

source§

fn cdf(&self, x: &X) -> f64

The value of the Cumulative Density Function at x Read more
source§

fn sf(&self, x: &X) -> f64

Survival function, 1 - CDF(x)
source§

impl<Fx: Clone> Clone for Mixture<Fx>

source§

fn clone(&self) -> Mixture<Fx>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

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

source§

fn pdf(&self, x: &X) -> f64

The value of the Probability Density Function (PDF) at x Read more
source§

fn ln_pdf(&self, x: &X) -> f64

The value of the log Probability Density Function (PDF) at x Read more
source§

impl<Fx: Debug> Debug for Mixture<Fx>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de, Fx> Deserialize<'de> for Mixture<Fx>
where Fx: Deserialize<'de>,

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

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

source§

fn pmf(&self, x: &X) -> f64

Probability mass function (PMF) at x Read more
source§

fn ln_pmf(&self, x: &X) -> f64

Natural logarithm of the probability mass function (PMF) Read more
source§

impl Entropy for Mixture<Bernoulli>

source§

fn entropy(&self) -> f64

The entropy, H(X)
source§

impl Entropy for Mixture<Categorical>

source§

fn entropy(&self) -> f64

The entropy, H(X)
source§

impl Entropy for Mixture<Gaussian>

source§

fn entropy(&self) -> f64

The entropy, H(X)
source§

impl Entropy for Mixture<Poisson>

source§

fn entropy(&self) -> f64

The entropy, H(X)
source§

impl<Fx> From<Mixture<Fx>> for Vec<(f64, Fx)>

source§

fn from(mm: Mixture<Fx>) -> Self

Converts to this type from the input type.
source§

impl<Fx> Mean<f32> for Mixture<Fx>
where Fx: Mean<f32>,

source§

fn mean(&self) -> Option<f32>

Returns None if the mean is undefined
source§

impl<Fx> Mean<f64> for Mixture<Fx>
where Fx: Mean<f64>,

source§

fn mean(&self) -> Option<f64>

Returns None if the mean is undefined
source§

impl<Fx: PartialEq> PartialEq for Mixture<Fx>

source§

fn eq(&self, other: &Mixture<Fx>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl QuadBounds for Mixture<Gaussian>

source§

fn quad_bounds(&self) -> (f64, f64)

source§

impl QuadBounds for Mixture<Poisson>

source§

fn quad_bounds(&self) -> (f64, f64)

source§

impl Rv<Datum> for Mixture<Vec<Distribution>>

source§

fn ln_f(&self, x: &Datum) -> f64

Probability function Read more
source§

fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<Datum>

Multiple draws of the Rv Read more
source§

fn draw<R: Rng>(&self, rng: &mut R) -> Datum

Single draw from the Rv Read more
source§

fn f(&self, x: &X) -> f64

Probability function Read more
source§

fn sample_stream<'r, R: Rng>( &'r self, rng: &'r mut R ) -> Box<dyn Iterator<Item = X> + 'r>

Create a never-ending iterator of samples Read more
source§

impl<X, Fx> Rv<X> for Mixture<Fx>
where Fx: Rv<X>,

source§

fn ln_f(&self, x: &X) -> f64

Probability function Read more
source§

fn f(&self, x: &X) -> f64

Probability function Read more
source§

fn draw<R: Rng>(&self, rng: &mut R) -> X

Single draw from the Rv Read more
source§

fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<X>

Multiple draws of the Rv Read more
source§

fn sample_stream<'r, R: Rng>( &'r self, rng: &'r mut R ) -> Box<dyn Iterator<Item = X> + 'r>

Create a never-ending iterator of samples Read more
source§

impl<Fx> Serialize for Mixture<Fx>
where Fx: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

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

source§

fn supports(&self, x: &X) -> bool

Returns true if x is in the support of the Rv Read more
source§

impl<Fx> TryFrom<Vec<(f64, Fx)>> for Mixture<Fx>

§

type Error = MixtureError

The type returned in the event of a conversion error.
source§

fn try_from(weighted_components: Vec<(f64, Fx)>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<Fx> Variance<f32> for Mixture<Fx>
where Fx: Mean<f32> + Variance<f32>,

source§

fn variance(&self) -> Option<f32>

Returns None if the variance is undefined
source§

impl<Fx> Variance<f64> for Mixture<Fx>
where Fx: Mean<f64> + Variance<f64>,

source§

fn variance(&self) -> Option<f64>

Returns None if the variance is undefined
source§

impl<Fx> StructuralPartialEq for Mixture<Fx>

Auto Trait Implementations§

§

impl<Fx> !Freeze for Mixture<Fx>

§

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§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

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

source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

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

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> DeserializeOwnedAlias for T

source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,

source§

impl<T> SendAlias for T

source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

source§

impl<T> SerializeAlias for T
where T: Serialize,

source§

impl<T> SyncAlias for T