Skip to main content

CategoricalLikelihood

Struct CategoricalLikelihood 

Source
pub struct CategoricalLikelihood {
    pub number_of_categories: i64,
    pub function_name: String,
    pub description: String,
    pub formula: String,
}
Expand description

Likelihood for categorical outcomes.

Fields§

§number_of_categories: i64

Number of categories.

§function_name: String

Unique name identifying a likelihood function.

§description: String

Free-text description.

§formula: String

Mathematical formula.

Implementations§

Source§

impl CategoricalLikelihood

Source

pub fn log_likelihood( &self, n_categories: usize, params: &[f64], data: &[f64], ) -> f64

Evaluates the categorical log-likelihood Σⱼ countⱼ · ln pⱼ of data under the probability vector params.

The number of categories k is passed explicitly rather than read from the struct (see the module docs); the frozen LogLikelihood trait is instead implemented on CategoricalLikelihoodModel.

§Arguments
  • n_categories — the number of categories k.
  • params — the probability vector [p₀ … p_{k−1}]; length must equal n_categories and the entries must sum to 1 within SIMPLEX_TOLERANCE.
  • data — the observed category indices, each a non-negative integer below k stored as f64.
§Returns

The log-likelihood, or f64::NEG_INFINITY when params has the wrong length, is not normalized, assigns a non-positive probability to a non-empty category, or data contains an invalid index. A category with zero observed count contributes 0 (the 0 · ln 0 ≔ 0 convention).

§Examples
use stats_claw::likelihood::CategoricalLikelihood;

let model = CategoricalLikelihood::default();
// counts = [2, 1, 3] over k = 3; ℓ = 2·ln0.2 + 1·ln0.3 + 3·ln0.5.
let ll = model.log_likelihood(3, &[0.2, 0.3, 0.5], &[0.0, 0.0, 1.0, 2.0, 2.0, 2.0]);
assert!((ll - (-6.502_290_170_873_972)).abs() < 1e-10, "ll was {ll}");
Source

pub fn fit(&self, n_categories: usize, data: &[f64]) -> Result<MleFit>

Closed-form maximum-likelihood fit of the category probabilities, p̂ⱼ = countⱼ / n.

§Arguments
  • n_categories — the number of categories k.
  • data — the observed category indices, each a non-negative integer below k stored as f64.
§Returns

An MleFit whose parameters are the empirical frequencies (length n_categories, summing to 1) and whose log-likelihood is ℓ(p̂; data). Categories with zero observed count are allowed and receive p̂ⱼ = 0, contributing 0 to the log-likelihood.

§Errors
§Examples
use stats_claw::likelihood::CategoricalLikelihood;

let model = CategoricalLikelihood::default();
let fit = model.fit(3, &[0.0, 0.0, 1.0, 2.0, 2.0, 2.0])?;
assert!((fit.params()[2] - 0.5).abs() < 1e-12, "p2 was {}", fit.params()[2]);

Trait Implementations§

Source§

impl Clone for CategoricalLikelihood

Source§

fn clone(&self) -> CategoricalLikelihood

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CategoricalLikelihood

Source§

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

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

impl Default for CategoricalLikelihood

Source§

fn default() -> CategoricalLikelihood

Returns the “default value” for a type. 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> 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.