Skip to main content

NormalInverseGamma

Struct NormalInverseGamma 

Source
pub struct NormalInverseGamma { /* private fields */ }
Expand description

Normal-InverseGamma conjugate prior for normal data with unknown mean and variance.

Models both the mean μ and variance σ² of normally distributed data. Uses the Normal-InverseGamma hierarchical structure:

  • σ² ~ InverseGamma(α, β)
  • μ | σ² ~ Normal(μ₀, σ²/κ)

Prior: N-IG(μ₀, κ₀, α₀, β₀) Likelihood: N(μ, σ²) Posterior: N-IG(μₙ, κₙ, αₙ, βₙ)

§Mathematical Foundation

Given n observations x₁, …, xₙ from N(μ, σ²):

  • μₙ = (κ₀μ₀ + nμ̄) / (κ₀ + n)
  • κₙ = κ₀ + n
  • αₙ = α₀ + n/2
  • βₙ = β₀ + ½s² + κ₀n(μ̄ - μ₀)²/(2(κ₀ + n))

where μ̄ = sample mean, s² = sum of squared deviations

§Example

use aprender::bayesian::NormalInverseGamma;

// Start with non-informative prior
let mut model = NormalInverseGamma::noninformative();

// Observe data from N(5, 4): [4.2, 5.8, 6.1, 4.5, 5.0]
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

// Posterior mean of μ
let mean = model.posterior_mean_mu();
assert!((mean - 5.1).abs() < 0.5);

Implementations§

Source§

impl NormalInverseGamma

Source

pub fn noninformative() -> Self

Creates a non-informative prior N-IG(0, 0.001, 0.001, 0.001).

This weakly informative prior allows the data to dominate.

§Example
use aprender::bayesian::NormalInverseGamma;

let prior = NormalInverseGamma::noninformative();
assert_eq!(prior.mu(), 0.0);
assert_eq!(prior.kappa(), 0.001);
Source

pub fn new(mu: f32, kappa: f32, alpha: f32, beta: f32) -> Result<Self>

Creates an informative prior N-IG(μ₀, κ₀, α₀, β₀) from prior belief.

§Arguments
  • mu - Prior mean μ₀
  • kappa - Prior precision κ₀ > 0 (pseudo sample size for mean)
  • alpha - Prior shape α₀ > 0 (pseudo sample size for variance)
  • beta - Prior scale β₀ > 0 (pseudo sum of squared deviations)
§Interpretation
  • μ₀: Prior belief about the mean
  • κ₀: Confidence in prior mean (larger = stronger belief)
  • α₀: Prior degrees of freedom for variance
  • β₀: Prior scale for variance
§Errors

Returns error if κ ≤ 0, α ≤ 0, or β ≤ 0.

§Example
use aprender::bayesian::NormalInverseGamma;

// Prior belief: mean ≈ 5.0, variance ≈ 1.0, moderate confidence
let prior = NormalInverseGamma::new(5.0, 10.0, 5.0, 5.0).expect("valid prior parameters");
assert_eq!(prior.mu(), 5.0);
Source

pub fn mu(&self) -> f32

Returns the current μ₀ parameter.

Source

pub fn kappa(&self) -> f32

Returns the current κ₀ parameter.

Source

pub fn alpha(&self) -> f32

Returns the current α₀ parameter.

Source

pub fn beta(&self) -> f32

Returns the current β₀ parameter.

Source

pub fn update(&mut self, data: &[f32])

Updates the posterior with observed data (Bayesian update).

§Arguments
  • data - Slice of observed values
§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

// Parameters have been updated via Bayesian inference
assert!(model.kappa() > 0.001); // Precision increased
Source

pub fn posterior_mean_mu(&self) -> f32

Computes the posterior mean of μ: E[μ|data] = μₙ.

This is the expected value of the mean parameter.

§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

let mean_mu = model.posterior_mean_mu();
assert!((mean_mu - 5.1).abs() < 0.5); // Close to sample mean
Source

pub fn posterior_mean_variance(&self) -> Option<f32>

Computes the posterior mean of σ²: E[σ²|data] = β/(α-1) for α > 1.

This is the expected value of the variance parameter.

§Returns
  • Some(variance) if α > 1
  • None if α ≤ 1 (mean undefined)
§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

let mean_var = model.posterior_mean_variance().expect("mean variance exists when alpha > 1");
assert!(mean_var > 0.0);
Source

pub fn posterior_variance_mu(&self) -> Option<f32>

Computes the posterior variance of μ: Var[μ|data] = β/(κ(α-1)) for α > 1.

Measures uncertainty in the mean estimate.

§Returns
  • Some(variance) if α > 1
  • None if α ≤ 1 (variance undefined)
§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

let var_mu = model.posterior_variance_mu().expect("variance of mu exists when alpha > 1");
assert!(var_mu > 0.0); // Positive uncertainty
Source

pub fn posterior_predictive(&self) -> f32

Computes the posterior predictive distribution mean: μₙ.

For Normal-InverseGamma, the posterior predictive is Student’s t-distribution. Returns the mean of the predictive distribution.

§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

let pred_mean = model.posterior_predictive();
assert!((pred_mean - 5.1).abs() < 0.5);
Source

pub fn credible_interval_mu(&self, confidence: f32) -> Result<(f32, f32)>

Computes a (1-α) credible interval for μ using Student’s t-distribution.

Returns (lower, upper) bounds such that P(lower ≤ μ ≤ upper | data) = 1-α.

§Arguments
  • confidence - Confidence level (e.g., 0.95 for 95% credible interval)
§Errors

Returns error if confidence ∉ (0, 1) or if α ≤ 1 (variance undefined).

§Example
use aprender::bayesian::NormalInverseGamma;

let mut model = NormalInverseGamma::noninformative();
model.update(&[4.2, 5.8, 6.1, 4.5, 5.0]);

let (lower, upper) = model.credible_interval_mu(0.95).expect("valid confidence level with alpha > 1");
assert!(lower < 5.1 && 5.1 < upper);

Trait Implementations§

Source§

impl Clone for NormalInverseGamma

Source§

fn clone(&self) -> NormalInverseGamma

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 NormalInverseGamma

Source§

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

Formats the value using the given formatter. 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> 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