[−][src]Struct rv::dist::Mixture
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]
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]
&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>)
[src]
Trait Implementations
impl<X, Fx> Cdf<X> for Mixture<Fx> where
Fx: Rv<X> + Cdf<X>,
[src]
Fx: Rv<X> + Cdf<X>,
impl<Fx: Clone> Clone for Mixture<Fx>
[src]
impl<X, Fx> ContinuousDistr<X> for Mixture<Fx> where
Fx: Rv<X> + ContinuousDistr<X>,
[src]
Fx: Rv<X> + ContinuousDistr<X>,
impl<Fx: Debug> Debug for Mixture<Fx>
[src]
impl<X, Fx> DiscreteDistr<X> for Mixture<Fx> where
Fx: Rv<X> + DiscreteDistr<X>,
[src]
Fx: Rv<X> + DiscreteDistr<X>,
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]
Fx: Mean<f32>,
impl<Fx> Mean<f64> for Mixture<Fx> where
Fx: Mean<f64>,
[src]
Fx: Mean<f64>,
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]
Fx: Rv<X>,
fn ln_f(&self, x: &X) -> f64
[src]
fn f(&self, x: &X) -> f64
[src]
fn draw<R: Rng>(&self, rng: &mut R) -> X
[src]
fn sample<R: Rng>(&self, n: usize, rng: &mut R) -> Vec<X>
[src]
fn sample_stream<'r, R: Rng>(
&'r self,
rng: &'r mut R
) -> Box<dyn Iterator<Item = X> + 'r>
[src]
&'r self,
rng: &'r mut R
) -> Box<dyn Iterator<Item = X> + 'r>
impl<Fx> StructuralPartialEq for Mixture<Fx>
[src]
impl<X, Fx> Support<X> for Mixture<Fx> where
Fx: Rv<X> + Support<X>,
[src]
Fx: Rv<X> + Support<X>,
impl<Fx> Variance<f32> for Mixture<Fx> where
Fx: ContinuousDistr<f32> + Mean<f32> + Variance<f32>,
[src]
Fx: ContinuousDistr<f32> + Mean<f32> + Variance<f32>,
impl<Fx> Variance<f64> for Mixture<Fx> where
Fx: ContinuousDistr<f64> + Mean<f64> + Variance<f64>,
[src]
Fx: ContinuousDistr<f64> + Mean<f64> + Variance<f64>,
Auto Trait Implementations
impl<Fx> RefUnwindSafe for Mixture<Fx> where
Fx: RefUnwindSafe,
Fx: RefUnwindSafe,
impl<Fx> Send for Mixture<Fx> where
Fx: Send,
Fx: Send,
impl<Fx> Sync for Mixture<Fx> where
Fx: Sync,
Fx: Sync,
impl<Fx> Unpin for Mixture<Fx> where
Fx: Unpin,
Fx: Unpin,
impl<Fx> UnwindSafe for Mixture<Fx> where
Fx: UnwindSafe,
Fx: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<Fx, X> Cdf<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Cdf<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Cdf<X>,
impl<Fx, X> ContinuousDistr<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: ContinuousDistr<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: ContinuousDistr<X>,
impl<Fx, X> DiscreteDistr<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: DiscreteDistr<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: DiscreteDistr<X>,
impl<Fx> Entropy for Fx where
Fx: Deref,
<Fx as Deref>::Target: Entropy,
[src]
Fx: Deref,
<Fx as Deref>::Target: Entropy,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<Fx, X> Mean<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Mean<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Mean<X>,
impl<Fx, X> Rv<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Rv<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Rv<X>,
fn ln_f(&Self, &X) -> f64
[src]
fn f(&Self, &X) -> f64
[src]
fn draw<R>(&Self, &mut R) -> X where
R: Rng,
[src]
R: Rng,
fn sample<R>(&Self, usize, &mut R) -> Vec<X> where
R: Rng,
[src]
R: Rng,
fn sample_stream<'r, R: Rng>(
&'r self,
rng: &'r mut R
) -> Box<dyn Iterator<Item = X> + 'r>
[src]
&'r self,
rng: &'r mut R
) -> Box<dyn Iterator<Item = X> + 'r>
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
fn is_in_subset(&self) -> bool
unsafe fn to_subset_unchecked(&self) -> SS
fn from_subset(element: &SS) -> SP
impl<Fx, X> Support<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Support<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Support<X>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,
fn vzip(self) -> V
impl<Fx, X> Variance<X> for Fx where
Fx: Deref,
<Fx as Deref>::Target: Variance<X>,
[src]
Fx: Deref,
<Fx as Deref>::Target: Variance<X>,