pub struct GammaPoisson { /* private fields */ }Expand description
Gamma-Poisson conjugate prior for count data.
Models a rate parameter λ > 0 for Poisson-distributed count data. Uses shape-rate parameterization: Gamma(α, β) where β is the rate parameter.
Prior: Gamma(α, β) Likelihood: Poisson(λ) Posterior: Gamma(α + Σxᵢ, β + n)
§Mathematical Foundation
Given n observations x₁, …, xₙ from Poisson(λ):
- Prior: p(λ) = Gamma(α, β) ∝ λ^(α-1) × exp(-βλ)
- Likelihood: p(x|λ) = ∏ Poisson(xᵢ|λ) ∝ λ^(Σxᵢ) × exp(-nλ)
- Posterior: p(λ|x) = Gamma(α + Σxᵢ, β + n)
§Example
use aprender::bayesian::GammaPoisson;
// Start with non-informative prior
let mut model = GammaPoisson::noninformative();
// Observe counts: [3, 5, 4, 6, 2] events per hour
model.update(&[3, 5, 4, 6, 2]);
// Expected event rate
let mean = model.posterior_mean();
assert!((mean - 4.0).abs() < 0.5); // Should be around 4 events/hourImplementations§
Source§impl GammaPoisson
impl GammaPoisson
Sourcepub fn noninformative() -> Self
pub fn noninformative() -> Self
Creates a non-informative prior Gamma(0.001, 0.001).
This weakly informative prior has minimal influence on the posterior, allowing the data to dominate.
§Example
use aprender::bayesian::GammaPoisson;
let prior = GammaPoisson::noninformative();
assert_eq!(prior.alpha(), 0.001);
assert_eq!(prior.beta(), 0.001);Sourcepub fn new(alpha: f32, beta: f32) -> Result<Self>
pub fn new(alpha: f32, beta: f32) -> Result<Self>
Creates an informative prior Gamma(α, β) from prior belief.
§Arguments
alpha- Shape parameter α > 0 (prior total count)beta- Rate parameter β > 0 (prior number of intervals)
§Interpretation
- α/β: Prior mean rate
- α: Pseudo-count of prior observations
- β: Pseudo-count of prior time intervals
- Larger α, β: Stronger prior belief
§Errors
Returns error if α ≤ 0 or β ≤ 0.
§Example
use aprender::bayesian::GammaPoisson;
// Prior belief: 50 events in 10 intervals (rate = 5)
let prior = GammaPoisson::new(50.0, 10.0).expect("valid alpha and beta parameters");
assert!((prior.posterior_mean() - 5.0).abs() < 0.01);Sourcepub fn update(&mut self, counts: &[u32])
pub fn update(&mut self, counts: &[u32])
Updates the posterior with observed count data (Bayesian update).
§Arguments
counts- Slice of observed counts (non-negative integers)
§Panics
Panics if any count is negative.
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::noninformative();
model.update(&[3, 5, 4, 6, 2]);
// Posterior is Gamma(0.001 + 20, 0.001 + 5)
assert!((model.alpha() - 20.001).abs() < 0.01);
assert!((model.beta() - 5.001).abs() < 0.01);Sourcepub fn posterior_mean(&self) -> f32
pub fn posterior_mean(&self) -> f32
Computes the posterior mean E[λ|data] = α/β.
This is the expected value of the rate parameter under the posterior.
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::noninformative();
model.update(&[3, 5, 4, 6, 2]); // Sum = 20, n = 5
let mean = model.posterior_mean();
assert!((mean - 20.001/5.001).abs() < 0.01); // ≈ 4.0Sourcepub fn posterior_mode(&self) -> Option<f32>
pub fn posterior_mode(&self) -> Option<f32>
Computes the posterior mode (MAP estimate) = (α-1)/β.
This is the most probable value of λ under the posterior. Only defined for α > 1.
§Returns
Some(mode)if α > 1Noneif distribution is monotonically decreasing (α ≤ 1)
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::new(2.0, 1.0).expect("valid alpha and beta parameters");
model.update(&[3, 5, 4, 6, 2]);
let mode = model.posterior_mode().expect("mode exists when alpha > 1");
assert!((mode - (22.0 - 1.0)/6.0).abs() < 0.01);Sourcepub fn posterior_variance(&self) -> f32
pub fn posterior_variance(&self) -> f32
Computes the posterior variance Var[λ|data] = α/β².
Measures uncertainty in the rate estimate.
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::noninformative();
model.update(&[3, 5, 4, 6, 2]);
let variance = model.posterior_variance();
assert!(variance < 1.0); // Low uncertainty with 5 observationsSourcepub fn posterior_predictive(&self) -> f32
pub fn posterior_predictive(&self) -> f32
Computes the posterior predictive distribution for next observation.
For Gamma-Poisson, the posterior predictive is Negative Binomial. Returns the mean of the predictive distribution: α/β.
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::noninformative();
model.update(&[3, 5, 4, 6, 2]);
let pred_mean = model.posterior_predictive();
assert!((pred_mean - 4.0).abs() < 0.5);Sourcepub fn credible_interval(&self, confidence: f32) -> Result<(f32, f32)>
pub fn credible_interval(&self, confidence: f32) -> Result<(f32, f32)>
Computes the (1-α) credible interval using normal approximation.
Returns (lower, upper) bounds such that P(lower ≤ λ ≤ upper | data) = 1-α.
§Arguments
confidence- Confidence level (e.g., 0.95 for 95% credible interval)
§Returns
(lower, upper) quantiles, with lower ≥ 0 (rate cannot be negative)
§Errors
Returns error if confidence ∉ (0, 1).
§Example
use aprender::bayesian::GammaPoisson;
let mut model = GammaPoisson::noninformative();
model.update(&[3, 5, 4, 6, 2]);
let (lower, upper) = model.credible_interval(0.95).expect("valid confidence level");
assert!(lower < 4.0 && 4.0 < upper);Trait Implementations§
Source§impl Clone for GammaPoisson
impl Clone for GammaPoisson
Source§fn clone(&self) -> GammaPoisson
fn clone(&self) -> GammaPoisson
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 GammaPoisson
impl RefUnwindSafe for GammaPoisson
impl Send for GammaPoisson
impl Sync for GammaPoisson
impl Unpin for GammaPoisson
impl UnwindSafe for GammaPoisson
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