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
impl NormalInverseGamma
Sourcepub fn noninformative() -> Self
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);Sourcepub fn new(mu: f32, kappa: f32, alpha: f32, beta: f32) -> Result<Self>
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);Sourcepub fn update(&mut self, data: &[f32])
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 increasedSourcepub fn posterior_mean_mu(&self) -> f32
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 meanSourcepub fn posterior_mean_variance(&self) -> Option<f32>
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 α > 1Noneif α ≤ 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);Sourcepub fn posterior_variance_mu(&self) -> Option<f32>
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 α > 1Noneif α ≤ 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 uncertaintySourcepub fn posterior_predictive(&self) -> f32
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);Sourcepub fn credible_interval_mu(&self, confidence: f32) -> Result<(f32, f32)>
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
impl Clone for NormalInverseGamma
Source§fn clone(&self) -> NormalInverseGamma
fn clone(&self) -> NormalInverseGamma
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for NormalInverseGamma
impl RefUnwindSafe for NormalInverseGamma
impl Send for NormalInverseGamma
impl Sync for NormalInverseGamma
impl Unpin for NormalInverseGamma
impl UnsafeUnpin for NormalInverseGamma
impl UnwindSafe for NormalInverseGamma
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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>
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