pub struct Gaussian { /* private fields */ }
Expand description
Gaussian / Normal distribution, N(μ, σ) over real values.
§Examples
Compute the KL Divergence between two Gaussians.
use rv::prelude::*;
let gauss_1 = Gaussian::new(0.1, 2.3).unwrap();
let gauss_2 = Gaussian::standard();
// KL is not symmetric
let kl_12 = gauss_1.kl(&gauss_2);
let kl_21 = gauss_2.kl(&gauss_1);
// ... but kl_sym is because it's the sum of KL(P|Q) and KL(Q|P)
let kl_sym = gauss_1.kl_sym(&gauss_2);
assert!((kl_sym - (kl_12 + kl_21)).abs() < 1E-12);
Implementations§
source§impl Gaussian
impl Gaussian
sourcepub fn new_unchecked(mu: f64, sigma: f64) -> Self
pub fn new_unchecked(mu: f64, sigma: f64) -> Self
Creates a new Gaussian without checking whether the parameters are valid.
sourcepub fn standard() -> Self
pub fn standard() -> Self
Standard normal
§Example
let gauss = Gaussian::standard();
assert_eq!(gauss, Gaussian::new(0.0, 1.0).unwrap());
sourcepub fn mu(&self) -> f64
pub fn mu(&self) -> f64
Get mu parameter
§Example
let gauss = Gaussian::new(2.0, 1.5).unwrap();
assert_eq!(gauss.mu(), 2.0);
sourcepub fn set_mu(&mut self, mu: f64) -> Result<(), GaussianError>
pub fn set_mu(&mut self, mu: f64) -> Result<(), GaussianError>
Set the value of mu
§Example
let mut gauss = Gaussian::new(2.0, 1.5).unwrap();
assert_eq!(gauss.mu(), 2.0);
gauss.set_mu(1.3).unwrap();
assert_eq!(gauss.mu(), 1.3);
Will error for invalid values
assert!(gauss.set_mu(1.3).is_ok());
assert!(gauss.set_mu(f64::NEG_INFINITY).is_err());
assert!(gauss.set_mu(f64::INFINITY).is_err());
assert!(gauss.set_mu(f64::NAN).is_err());
sourcepub fn set_mu_unchecked(&mut self, mu: f64)
pub fn set_mu_unchecked(&mut self, mu: f64)
Set the value of mu without input validation
sourcepub fn sigma(&self) -> f64
pub fn sigma(&self) -> f64
Get sigma parameter
§Example
let gauss = Gaussian::new(2.0, 1.5).unwrap();
assert_eq!(gauss.sigma(), 1.5);
sourcepub fn set_sigma(&mut self, sigma: f64) -> Result<(), GaussianError>
pub fn set_sigma(&mut self, sigma: f64) -> Result<(), GaussianError>
Set the value of sigma
§Example
let mut gauss = Gaussian::standard();
assert_eq!(gauss.sigma(), 1.0);
gauss.set_sigma(2.3).unwrap();
assert_eq!(gauss.sigma(), 2.3);
Will error for invalid values
assert!(gauss.set_sigma(2.3).is_ok());
assert!(gauss.set_sigma(0.0).is_err());
assert!(gauss.set_sigma(-1.0).is_err());
assert!(gauss.set_sigma(f64::INFINITY).is_err());
assert!(gauss.set_sigma(f64::NEG_INFINITY).is_err());
assert!(gauss.set_sigma(f64::NAN).is_err());
sourcepub fn set_sigma_unchecked(&mut self, sigma: f64)
pub fn set_sigma_unchecked(&mut self, sigma: f64)
Set the value of sigma
Trait Implementations§
source§impl ConjugatePrior<f64, Gaussian> for NormalGamma
impl ConjugatePrior<f64, Gaussian> for NormalGamma
§type Posterior = NormalGamma
type Posterior = NormalGamma
Type of the posterior distribution
§type PpCache = (GaussianSuffStat, f64)
type PpCache = (GaussianSuffStat, f64)
Type of the cache for the posterior predictive
fn posterior(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self
source§fn ln_m_cache(&self) -> Self::MCache
fn ln_m_cache(&self) -> Self::MCache
Compute the cache for the log marginal likelihood.
source§fn ln_m_with_cache(
&self,
cache: &Self::MCache,
x: &DataOrSuffStat<'_, f64, Gaussian>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, f64, Gaussian>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
Compute the cache for the Log posterior predictive of y given x. Read more
source§fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
Log posterior predictive of y given x with supplied ln(norm)
source§fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
Computes the posterior distribution from the data
source§fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
The log marginal likelihood
source§fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Log posterior predictive of y given x
source§fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Marginal likelihood of x
fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
source§impl ConjugatePrior<f64, Gaussian> for NormalInvChiSquared
impl ConjugatePrior<f64, Gaussian> for NormalInvChiSquared
§type Posterior = NormalInvChiSquared
type Posterior = NormalInvChiSquared
Type of the posterior distribution
§type PpCache = (GaussianSuffStat, f64)
type PpCache = (GaussianSuffStat, f64)
Type of the cache for the posterior predictive
fn posterior(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self
source§fn ln_m_cache(&self) -> Self::MCache
fn ln_m_cache(&self) -> Self::MCache
Compute the cache for the log marginal likelihood.
source§fn ln_m_with_cache(
&self,
cache: &Self::MCache,
x: &DataOrSuffStat<'_, f64, Gaussian>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, f64, Gaussian>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
Compute the cache for the Log posterior predictive of y given x. Read more
source§fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
Log posterior predictive of y given x with supplied ln(norm)
source§fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
Computes the posterior distribution from the data
source§fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
The log marginal likelihood
source§fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Log posterior predictive of y given x
source§fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Marginal likelihood of x
fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
source§impl ConjugatePrior<f64, Gaussian> for NormalInvGamma
impl ConjugatePrior<f64, Gaussian> for NormalInvGamma
§type Posterior = NormalInvGamma
type Posterior = NormalInvGamma
Type of the posterior distribution
§type PpCache = (GaussianSuffStat, f64)
type PpCache = (GaussianSuffStat, f64)
Type of the cache for the posterior predictive
fn posterior(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self
source§fn ln_m_cache(&self) -> Self::MCache
fn ln_m_cache(&self) -> Self::MCache
Compute the cache for the log marginal likelihood.
source§fn ln_m_with_cache(
&self,
cache: &Self::MCache,
x: &DataOrSuffStat<'_, f64, Gaussian>,
) -> f64
fn ln_m_with_cache( &self, cache: &Self::MCache, x: &DataOrSuffStat<'_, f64, Gaussian>, ) -> f64
Log marginal likelihood with supplied cache.
source§fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
fn ln_pp_cache(&self, x: &DataOrSuffStat<'_, f64, Gaussian>) -> Self::PpCache
Compute the cache for the Log posterior predictive of y given x. Read more
source§fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
fn ln_pp_with_cache(&self, cache: &Self::PpCache, y: &f64) -> f64
Log posterior predictive of y given x with supplied ln(norm)
source§fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
fn posterior_from_suffstat(&self, stat: &Fx::Stat) -> Self::Posterior
Computes the posterior distribution from the data
source§fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
The log marginal likelihood
source§fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn ln_pp(&self, y: &X, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Log posterior predictive of y given x
source§fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
fn m(&self, x: &DataOrSuffStat<'_, X, Fx>) -> f64
Marginal likelihood of x
fn pp_with_cache(&self, cache: &Self::PpCache, y: &X) -> f64
source§impl ContinuousDistr<Gaussian> for NormalGamma
impl ContinuousDistr<Gaussian> for NormalGamma
source§impl ContinuousDistr<f32> for Gaussian
impl ContinuousDistr<f32> for Gaussian
source§impl ContinuousDistr<f64> for Gaussian
impl ContinuousDistr<f64> for Gaussian
source§impl<'de> Deserialize<'de> for Gaussian
impl<'de> Deserialize<'de> for Gaussian
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 GewekeTestable<Gaussian, f64> for NormalGamma
impl GewekeTestable<Gaussian, f64> for NormalGamma
source§impl GewekeTestable<Gaussian, f64> for NormalInvChiSquared
impl GewekeTestable<Gaussian, f64> for NormalInvChiSquared
source§impl GewekeTestable<Gaussian, f64> for NormalInvGamma
impl GewekeTestable<Gaussian, f64> for NormalInvGamma
source§impl HasDensity<Gaussian> for NormalGamma
impl HasDensity<Gaussian> for NormalGamma
source§impl HasDensity<Gaussian> for NormalInvChiSquared
impl HasDensity<Gaussian> for NormalInvChiSquared
source§impl HasDensity<Gaussian> for NormalInvGamma
impl HasDensity<Gaussian> for NormalInvGamma
source§impl HasDensity<f32> for Gaussian
impl HasDensity<f32> for Gaussian
source§impl HasDensity<f64> for Gaussian
impl HasDensity<f64> for Gaussian
source§impl HasSuffStat<f32> for Gaussian
impl HasSuffStat<f32> for Gaussian
source§impl HasSuffStat<f64> for Gaussian
impl HasSuffStat<f64> for Gaussian
source§impl InverseCdf<f32> for Gaussian
impl InverseCdf<f32> for Gaussian
source§impl InverseCdf<f64> for Gaussian
impl InverseCdf<f64> for Gaussian
source§impl KlDivergence for Gaussian
impl KlDivergence for Gaussian
source§impl Parameterized for Gaussian
impl Parameterized for Gaussian
type Parameters = GaussianParameters
fn emit_params(&self) -> Self::Parameters
fn from_params(params: Self::Parameters) -> Self
source§impl PartialEq for Gaussian
impl PartialEq for Gaussian
source§impl QuadBounds for Gaussian
impl QuadBounds for Gaussian
fn quad_bounds(&self) -> (f64, f64)
source§impl Sampleable<Gaussian> for NormalGamma
impl Sampleable<Gaussian> for NormalGamma
source§impl Sampleable<Gaussian> for NormalInvChiSquared
impl Sampleable<Gaussian> for NormalInvChiSquared
source§impl Sampleable<Gaussian> for NormalInvGamma
impl Sampleable<Gaussian> for NormalInvGamma
source§impl Sampleable<f32> for Gaussian
impl Sampleable<f32> for Gaussian
source§impl Sampleable<f64> for Gaussian
impl Sampleable<f64> for Gaussian
source§impl Support<Gaussian> for NormalGamma
impl Support<Gaussian> for NormalGamma
Auto Trait Implementations§
impl !Freeze for Gaussian
impl RefUnwindSafe for Gaussian
impl Send for Gaussian
impl Sync for Gaussian
impl Unpin for Gaussian
impl UnwindSafe for Gaussian
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.