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: i64Number of categories.
function_name: StringUnique name identifying a likelihood function.
description: StringFree-text description.
formula: StringMathematical formula.
Implementations§
Source§impl CategoricalLikelihood
impl CategoricalLikelihood
Sourcepub fn log_likelihood(
&self,
n_categories: usize,
params: &[f64],
data: &[f64],
) -> f64
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 categoriesk.params— the probability vector[p₀ … p_{k−1}]; length must equaln_categoriesand the entries must sum to1withinSIMPLEX_TOLERANCE.data— the observed category indices, each a non-negative integer belowkstored asf64.
§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}");Sourcepub fn fit(&self, n_categories: usize, data: &[f64]) -> Result<MleFit>
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 categoriesk.data— the observed category indices, each a non-negative integer belowkstored asf64.
§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
Error::InsufficientDataifdatais empty.Error::InvalidInputif any datum is not a non-negative integer belown_categories.
§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
impl Clone for CategoricalLikelihood
Source§fn clone(&self) -> CategoricalLikelihood
fn clone(&self) -> CategoricalLikelihood
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more