pub struct MultinomialFit { /* private fields */ }Expand description
A fitted baseline-category (multinomial) logistic model for an unordered
categorical response with K ≥ 2 classes.
Class 0 is the reference. For each non-reference class k = 1 … K−1 there
is a coefficient vector βₖ and linear predictor ηᵢₖ = xᵢᵀβₖ (with
ηᵢ₀ ≡ 0), giving the softmax probabilities
P(yᵢ = k) = exp(ηᵢₖ) / (1 + Σⱼ exp(ηᵢⱼ)).
Fit by Newton–Raphson on the full (K−1)·p parameter vector: at
convergence the per-class score equations Xᵀ(yₖ − pₖ) = 0 hold, and the
inverse of the block information matrix gives the coefficient covariance the
Wald statistics use.
With K = 2 this reduces exactly to binary
LogisticFit (the βₖ for the single
non-reference class equal the logistic coefficients).
Implementations§
Source§impl MultinomialFit
impl MultinomialFit
Sourcepub fn new(x: Array2<f64>, y: Array1<f64>) -> Result<Self>
pub fn new(x: Array2<f64>, y: Array1<f64>) -> Result<Self>
Fit multinomial logistic regression of class-labelled y on X
(default: up to 100 Newton iterations, tolerance 1e-10 on the step).
The response must hold integer class labels 0 … K−1 with every class
present; K is inferred as max(y) + 1. The caller owns the design
matrix, intercept column included.
§Errors
RegressionError::EmptyInput/RegressionError::ShapeMismatch.RegressionError::InvalidResponseif labels are not consecutive integers from0, some class is empty, or there are fewer than two classes.RegressionError::RankDeficientif the information matrix is singular.RegressionError::NotConvergedif Newton’s method fails to converge.
Sourcepub fn with_options(
x: Array2<f64>,
y: Array1<f64>,
max_iter: usize,
tol: f64,
) -> Result<Self>
pub fn with_options( x: Array2<f64>, y: Array1<f64>, max_iter: usize, tol: f64, ) -> Result<Self>
Like MultinomialFit::new with an explicit iteration cap and tolerance.
Sourcepub fn n_observations(&self) -> usize
pub fn n_observations(&self) -> usize
Number of observations.
Sourcepub fn n_features(&self) -> usize
pub fn n_features(&self) -> usize
Number of design columns p (per class).
Sourcepub fn n_parameters(&self) -> usize
pub fn n_parameters(&self) -> usize
Total number of free coefficients, (K − 1)·p.
Sourcepub fn has_intercept(&self) -> bool
pub fn has_intercept(&self) -> bool
Whether a constant (intercept) column was detected.
Sourcepub fn iterations(&self) -> usize
pub fn iterations(&self) -> usize
Newton iterations taken to converge.
Sourcepub fn design_matrix(&self) -> ArrayView2<'_, f64>
pub fn design_matrix(&self) -> ArrayView2<'_, f64>
The design matrix as fitted.
Sourcepub fn response(&self) -> ArrayView1<'_, f64>
pub fn response(&self) -> ArrayView1<'_, f64>
The integer class labels.
Sourcepub fn coefficients(&self) -> ArrayView2<'_, f64>
pub fn coefficients(&self) -> ArrayView2<'_, f64>
Coefficients, shape (K−1) × p; row k−1 is βₖ for class k relative
to the reference class 0.
Sourcepub fn fitted_probabilities(&self) -> ArrayView2<'_, f64>
pub fn fitted_probabilities(&self) -> ArrayView2<'_, f64>
Fitted class probabilities, shape n × K.
Sourcepub fn covariance(&self) -> ArrayView2<'_, f64>
pub fn covariance(&self) -> ArrayView2<'_, f64>
Covariance of the stacked (K−1)·p coefficient vector (class-major).
Sourcepub fn log_likelihood(&self) -> f64
pub fn log_likelihood(&self) -> f64
Maximized log-likelihood.
Sourcepub fn coefficient_standard_errors(&self) -> Array2<f64>
pub fn coefficient_standard_errors(&self) -> Array2<f64>
Coefficient standard errors, shape (K−1) × p, aligned with
coefficients.
Sourcepub fn p_values(&self) -> Array2<f64>
pub fn p_values(&self) -> Array2<f64>
Two-sided Wald p-values from the standard normal, shape (K−1) × p.
Sourcepub fn residual_deviance(&self) -> f64
pub fn residual_deviance(&self) -> f64
Residual deviance −2ℓ.
Sourcepub fn null_deviance(&self) -> f64
pub fn null_deviance(&self) -> f64
Deviance of the intercept-only model (class marginals nₖ/n).
Sourcepub fn mcfadden_r2(&self) -> f64
pub fn mcfadden_r2(&self) -> f64
McFadden’s pseudo-R², 1 − ℓ/ℓ₀.
Sourcepub fn predict_proba(&self, x: ArrayView2<'_, f64>) -> Array2<f64>
pub fn predict_proba(&self, x: ArrayView2<'_, f64>) -> Array2<f64>
Predicted class probabilities for a new design matrix x (same column
layout as training), shape rows × K.
Trait Implementations§
Source§impl Clone for MultinomialFit
impl Clone for MultinomialFit
Source§fn clone(&self) -> MultinomialFit
fn clone(&self) -> MultinomialFit
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for MultinomialFit
impl RefUnwindSafe for MultinomialFit
impl Send for MultinomialFit
impl Sync for MultinomialFit
impl Unpin for MultinomialFit
impl UnsafeUnpin for MultinomialFit
impl UnwindSafe for MultinomialFit
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,
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.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.