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>
impl<Fx> Mixture<Fx>
sourcepub fn new(weights: Vec<f64>, components: Vec<Fx>) -> Result<Self, MixtureError>
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.
pub fn ln_weights(&self) -> &[f64]
sourcepub fn new_unchecked(weights: Vec<f64>, components: Vec<Fx>) -> Self
pub fn new_unchecked(weights: Vec<f64>, components: Vec<Fx>) -> Self
Creates a new Mixture without checking whether the parameters are valid.
sourcepub fn uniform(components: Vec<Fx>) -> Result<Self, MixtureError>
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.
sourcepub fn components(&self) -> &Vec<Fx>
pub fn components(&self) -> &Vec<Fx>
Get a reference to the components
sourcepub fn set_weights(&mut self, weights: Vec<f64>) -> Result<(), MixtureError>
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());
pub fn set_weights_unchecked(&mut self, weights: Vec<f64>)
sourcepub fn set_components(
&mut self,
components: Vec<Fx>,
) -> Result<(), MixtureError>
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);
pub fn set_components_unchecked(&mut self, components: Vec<Fx>)
Trait Implementations§
source§impl<X, Fx> ContinuousDistr<X> for Mixture<Fx>where
Fx: Rv<X> + ContinuousDistr<X>,
impl<X, Fx> ContinuousDistr<X> for Mixture<Fx>where
Fx: Rv<X> + ContinuousDistr<X>,
source§impl<'de, Fx> Deserialize<'de> for Mixture<Fx>where
Fx: Deserialize<'de>,
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>,
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>,
impl<X, Fx> DiscreteDistr<X> for Mixture<Fx>where
Fx: Rv<X> + DiscreteDistr<X>,
source§impl Entropy for &Mixture<StickBreakingDiscrete>
impl Entropy for &Mixture<StickBreakingDiscrete>
source§impl<X, Fx> HasDensity<X> for Mixture<Fx>where
Fx: Rv<X>,
impl<X, Fx> HasDensity<X> for Mixture<Fx>where
Fx: Rv<X>,
source§impl<Fx: Parameterized> Parameterized for Mixture<Fx>
impl<Fx: Parameterized> Parameterized for Mixture<Fx>
type Parameters = MixtureParameters<Fx>
fn emit_params(&self) -> Self::Parameters
fn from_params(params: Self::Parameters) -> Self
source§impl<Fx: PartialEq> PartialEq for Mixture<Fx>
impl<Fx: PartialEq> PartialEq for Mixture<Fx>
source§impl QuadBounds for Mixture<Gaussian>
impl QuadBounds for Mixture<Gaussian>
fn quad_bounds(&self) -> (f64, f64)
source§impl QuadBounds for Mixture<Poisson>
impl QuadBounds for Mixture<Poisson>
fn quad_bounds(&self) -> (f64, f64)
source§impl<X, Fx> Sampleable<X> for Mixture<Fx>where
Fx: Rv<X>,
impl<X, Fx> Sampleable<X> for Mixture<Fx>where
Fx: Rv<X>,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self
from the equivalent element of its
superset. Read moresource§fn is_in_subset(&self) -> bool
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self
to the equivalent element of its superset.