Skip to main content

GammaPoisson

Struct GammaPoisson 

Source
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/hour

Implementations§

Source§

impl GammaPoisson

Source

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);
Source

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);
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, 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);
Source

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.0
Source

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 α > 1
  • None if 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);
Source

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 observations
Source

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);
Source

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

Source§

fn clone(&self) -> GammaPoisson

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 GammaPoisson

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