pub struct DirichletRegression {
pub coefficients: Vec<f64>,
pub precision: f64,
pub n_parts: usize,
pub n_covariates: usize,
pub log_likelihood: f64,
}Expand description
A Dirichlet GLM fitted by Iteratively Reweighted Least Squares (IRLS).
The model is: ln(E[yⱼ]) = Xβⱼ + offset, with y ~ Dir(φ · μ) where φ (precision) is estimated jointly.
For simplicity this implementation uses a reduced model:
- Intercept-only mean model for each part
- Single precision parameter φ estimated from variance
For a full covariate model, use DirichletRegression which supports design matrices.
Fields§
§coefficients: Vec<f64>Intercept coefficients for each part (on log-ratio / softmax scale).
precision: f64Precision parameter φ = Σ αⱼ > 0.
n_parts: usizeNumber of parts D.
n_covariates: usizeNumber of covariates (including intercept).
log_likelihood: f64Log-likelihood at fitted parameters.
Implementations§
Source§impl DirichletRegression
impl DirichletRegression
Sourcepub fn fit(responses: &[Vec<f64>], covariates: &[Vec<f64>]) -> StatsResult<Self>
pub fn fit(responses: &[Vec<f64>], covariates: &[Vec<f64>]) -> StatsResult<Self>
Fit a Dirichlet regression model to compositional response data.
responses: N × D matrix (as Vec<Vec<f64>>) of compositional observations.
covariates: N × P matrix of covariates (each row is one observation’s features).
Pass an empty inner vec or &[] to fit an intercept-only model.
Uses IRLS to maximise the Dirichlet log-likelihood.
§Errors
Returns an error if:
responsesis empty or has inconsistent dimensions.- Any response row contains non-positive components.
- The algorithm encounters a numerical degeneracy.
§Examples
use scirs2_stats::compositional::DirichletRegression;
let responses = vec![
vec![0.3, 0.4, 0.3],
vec![0.2, 0.5, 0.3],
vec![0.4, 0.3, 0.3],
vec![0.25, 0.35, 0.4],
];
let covariates: Vec<Vec<f64>> = vec![vec![]; 4]; // intercept-only
let model = DirichletRegression::fit(&responses, &covariates).unwrap();
assert_eq!(model.n_parts, 3);
assert!(model.precision > 0.0);Sourcepub fn predict(&self, x: &[f64]) -> StatsResult<Vec<f64>>
pub fn predict(&self, x: &[f64]) -> StatsResult<Vec<f64>>
Predict the expected composition for a new covariate vector x.
Returns a closed composition summing to 1.
§Errors
Returns an error if x has the wrong length.
§Examples
use scirs2_stats::compositional::DirichletRegression;
let responses = vec![
vec![0.3, 0.4, 0.3],
vec![0.2, 0.5, 0.3],
vec![0.4, 0.3, 0.3],
vec![0.25, 0.35, 0.4],
];
let covariates: Vec<Vec<f64>> = vec![vec![]; 4];
let model = DirichletRegression::fit(&responses, &covariates).unwrap();
let pred = model.predict(&[]).unwrap();
let sum: f64 = pred.iter().sum();
assert!((sum - 1.0).abs() < 1e-12);Trait Implementations§
Source§impl Clone for DirichletRegression
impl Clone for DirichletRegression
Source§fn clone(&self) -> DirichletRegression
fn clone(&self) -> DirichletRegression
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DirichletRegression
impl Debug for DirichletRegression
Auto Trait Implementations§
impl Freeze for DirichletRegression
impl RefUnwindSafe for DirichletRegression
impl Send for DirichletRegression
impl Sync for DirichletRegression
impl Unpin for DirichletRegression
impl UnsafeUnpin for DirichletRegression
impl UnwindSafe for DirichletRegression
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.