Skip to main content

BetaNP

Struct BetaNP 

Source
pub struct BetaNP {
    pub alpha: f64,
    pub beta: f64,
}
Expand description

Beta distribution stored in (α, β) moment parameterisation.

Natural parameters are η = (α − 1, β − 1). Both α and β must be strictly positive and finite for the distribution to be well-defined; the constructor and ExponentialFamily::set_natural reject values outside that open positive quadrant.

Fields§

§alpha: f64

Shape parameter α > 0.

§beta: f64

Shape parameter β > 0.

Implementations§

Source§

impl BetaNP

Source

pub fn new(alpha: f64, beta: f64) -> Result<Self>

Construct from moment parameters (α, β). Both must be strictly positive and finite.

Source

pub fn from_natural(natural: &[f64]) -> Result<Self>

Reconstruct a Beta from natural parameters η = (α − 1, β − 1).

Source

pub fn expected_x(&self) -> f64

Expected value E[x] = α / (α + β).

Source

pub fn expected_log_x(&self) -> f64

Expected log value E[log x] = ψ(α) − ψ(α + β).

Source

pub fn expected_log_1mx(&self) -> f64

Expected log of the complement E[log(1 − x)] = ψ(β) − ψ(α + β).

Source

pub fn variance(&self) -> f64

Variance Var[x] = α β / ((α + β)² (α + β + 1)).

Source

pub fn multiply_naturals(&self, other: &BetaNP) -> Result<BetaNP>

Sum the natural parameters of self and other. Corresponds to the pointwise product of densities: if both priors are Beta on the same variable, their product is another Beta whose natural parameter is the sum of the two input natural parameters.

Concretely: α_new = α₁ + α₂ − 1 and β_new = β₁ + β₂ − 1.

Source

pub fn kl_to(&self, other: &BetaNP) -> f64

Closed-form KL divergence KL(Beta(α_p, β_p) || Beta(α_q, β_q)).

Standard result:

  KL = ln B(α_q, β_q) − ln B(α_p, β_p)
       + (α_p − α_q) ψ(α_p)
       + (β_p − β_q) ψ(β_p)
       + (α_q − α_p + β_q − β_p) ψ(α_p + β_p)

where ln B(a, b) = ln Γ(a) + ln Γ(b) − ln Γ(a + b).

Trait Implementations§

Source§

impl Clone for BetaNP

Source§

fn clone(&self) -> BetaNP

Returns a duplicate 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 Debug for BetaNP

Source§

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

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

impl ExponentialFamily for BetaNP

Source§

fn family_name(&self) -> &'static str

Name of the family (used for error messages, e.g. “Gaussian”).
Source§

fn natural_dim(&self) -> usize

Dimensionality of the natural-parameter vector.
Source§

fn natural_params(&self) -> Vec<f64>

Return a copy of the current natural parameters.
Source§

fn set_natural(&mut self, new_eta: &[f64]) -> Result<()>

Replace the natural parameters with new_eta. Read more
Source§

fn sufficient_statistics(&self, value: f64) -> Vec<f64>

Sufficient statistics u(x) evaluated at the scalar or categorical value value. Read more
Source§

fn log_partition(&self, natural_params: &[f64]) -> Result<f64>

Log partition function A(η).
Source§

fn expected_sufficient_statistics(&self) -> Vec<f64>

Expected sufficient statistics E_q[u(x)] = ∇_η A(η). Read more
Source§

fn to_natural(&self) -> Vec<f64>

Read-only view of the natural parameter vector. Read more
Source§

fn update_natural(&mut self, delta: &[f64]) -> Result<()>

Additively update the natural parameters: η ← η + δ. Read more
Source§

fn entropy(&self) -> Result<f64>

Differential entropy H(q) = A(η) − ηᵀ E_q[u(x)] − E_q[log h(x)]. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

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,

Source§

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>,

Source§

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>,

Source§

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