Multinomial Logistic Regression › Formula
Machine Learning from ScratchAll chapters Multinomial Logistic Regression › FormulaOne section · Sections in this chapter2 / 8 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
The mathematical foundation of multinomial logistic regression builds upon the principles of binary logistic regression, but extends them to handle multiple categories. Let's walk through the derivation step by step, starting with the most intuitive form and progressing to the matrix notation.
Basic Probability Model
We begin with the fundamental assumption that we have categories (where ) and want to model the probability that observation belongs to category . Let's denote this probability as , where represents the vector of predictor variables for observation .
The key insight is that we can model the log-odds of each category relative to a reference category. Without loss of generality, let's choose category as our reference category. The log-odds of category relative to category is:
where:
- is the probability that observation belongs to category given its predictor values (the conditional probability we're modeling for the non-reference category)
- is the probability that observation belongs to the reference category (the baseline category against which all others are compared)
- is the intercept for category (the baseline log-odds when all predictors are zero)
- is the coefficient for predictor in category (quantifies the effect of predictor on the log-odds of category relative to the reference category)
- is the value of predictor for observation (the observed feature value)
- is the total number of predictor variables (the dimensionality of the feature space)
- is the reference category (chosen arbitrarily, typically the last category, with all its coefficients set to zero for identifiability)
From Log-Odds to Probabilities
To convert these log-odds back to probabilities, we need to solve for . Let's define the linear predictor for category as:
where:
- is the linear predictor for observation and category (a real-valued quantity that can take any value from to , representing the log-odds before normalization)
Then we can write:
Taking the exponential of both sides:
This gives us:
Normalization Constraint
Since all probabilities must sum to one, we have:
Substituting our expression for :
Since is common to all terms, we can factor it out:
This gives us the probability of the reference category:
Final Probability Formula
Now we can express the probability of any category as:
This is the softmax function, which ensures that all probabilities sum to one and are non-negative. Notice that when , this reduces to the standard logistic regression formula.


Matrix Notation
For computational efficiency and clarity, we can express the multinomial logistic regression model in matrix notation.
Let be the design matrix, where is the number of observations and is the number of predictors (the extra column accounts for the intercept). For each non-reference category (where ), let be a vector of coefficients. We stack these coefficient vectors horizontally to form a coefficient matrix:
where:
- is the design matrix (includes a column of 1s for the intercept term, allowing matrix multiplication to compute all linear predictors)
- is the number of observations in the dataset (the sample size)
- is the number of predictor variables (the number of features, excluding the intercept)
- is the coefficient vector for category (includes the intercept as the first element, followed by slope coefficients)
- is the coefficient matrix for all non-reference categories (each column is a coefficient vector for one non-reference category)
- is the total number of categories (including the reference category)
The linear predictors for all observations and all non-reference categories can be computed as:
where:
- is the matrix of linear predictors (each row corresponds to one observation, each column corresponds to one non-reference category, and element is the linear predictor for observation and category )
To obtain the predicted probabilities for all categories, we apply the softmax function row-wise. For each observation and category :
where for the reference category , we set (since its coefficients are all zero by convention).
In matrix notation, the full probability matrix , with elements , is given by:
where:
- is the probability matrix (each row contains the predicted probabilities for one observation across all categories)
- is the probability that observation belongs to category (an element of the probability matrix)
- is the matrix formed by horizontally concatenating (the matrix of linear predictors for non-reference categories) with (a column of zeros for the reference category)
- is the column vector of zeros for the reference category (representing for all observations)
- is the softmax function applied row-wise (maps linear predictors into probabilities that are non-negative and sum to 1 for each observation)
This formulation ensures that all predicted probabilities are non-negative and sum to one for each observation.
This matrix-based approach is highly efficient for computation, especially when working with large datasets or when fitting the model using optimization algorithms that require gradients with respect to all parameters.
Calculating the Coefficients
The coefficients in multinomial logistic regression are estimated using maximum likelihood estimation (MLE). This process involves finding the parameter values that maximize the likelihood of observing our data given the model.
Likelihood Function
For a dataset with observations and classes, the likelihood function is:
where:
- is the likelihood function (the probability of observing the entire dataset given the model parameters )
- represents all coefficient parameters in the model (the collection of all values for all non-reference categories and all predictors)
- is the number of observations (the sample size)
- is the number of categories (the total number of possible outcome classes)
- is the predicted probability that observation belongs to category (computed using the softmax function)
- is the indicator function that equals 1 if observation truly belongs to class , and 0 otherwise (selects the probability of the observed class)
Log-Likelihood Function
Working with the log-likelihood is computationally more convenient:
where:
- is the log-likelihood function (the natural logarithm of the likelihood function, which maps products into sums for computational convenience and numerical stability)
Substituting the softmax probability formula:
Gradient and Hessian
To find the maximum likelihood estimates, we need the gradient (first derivatives) and Hessian (second derivatives) of the log-likelihood function.
Let's break down how to compute the gradient and Hessian of the multinomial logistic regression log-likelihood step by step.
Step 1: Recall the log-likelihood
The log-likelihood for multinomial logistic regression is:
where is 1 if observation belongs to class , and 0 otherwise.
Step 2: Substitute the softmax probability
Recall that
where:
- is the linear predictor for observation and category (computed as the dot product of the feature vector and coefficient vector)
- is the transpose of the feature vector for observation (a row vector including a 1 for the intercept)
- is the coefficient vector for category (a column vector including the intercept and slope coefficients)
Step 3: Compute the gradient (first derivative) with respect to
We want to find the derivative of the log-likelihood with respect to a particular coefficient (the coefficient for predictor in class ):
-
The log-likelihood for a single observation is:
-
The derivative with respect to is:
-
Since depends on all , but only directly on through , we use the chain rule.
-
The derivative of with respect to is:
(This comes from differentiating the log-softmax.)
-
Summing over all observations, we get:
Step 4: Compute the Hessian (second derivative) with respect to and
- The Hessian entry for parameters and is the second derivative:
- Differentiating the gradient again, we get: where is 1 if , and 0 otherwise.
Results:
-
Gradient (First Derivatives):
where:
- is the partial derivative of the log-likelihood with respect to coefficient (the gradient component for this parameter)
- is the coefficient for predictor in category (the parameter we're taking the derivative with respect to)
- is the value of predictor for observation (the feature value that multiplies this coefficient)
- is the indicator function (equals 1 if observation truly belongs to category , and 0 otherwise)
- is the predicted probability for observation in category (computed using the current parameter values)
-
Hessian (Second Derivatives):
where:
- is the second partial derivative of the log-likelihood (measures the curvature of the log-likelihood surface with respect to parameters and )
- is the coefficient for predictor in category (the second parameter in the mixed partial derivative)
- is the value of predictor for observation (the feature value that multiplies coefficient )
- is the indicator function (equals 1 if categories and are the same, and 0 otherwise)
Optimization Methods
The coefficients are found by maximizing the log-likelihood function using numerical optimization methods:
- Newton-Raphson Method: Uses both gradient and Hessian information for faster convergence
- Fisher Scoring: Uses the expected Fisher information matrix instead of the observed Hessian
- Gradient Descent: Iteratively updates parameters in the direction of the gradient
- Limited-memory BFGS (L-BFGS): A quasi-Newton method that approximates the Hessian
Iterative Reweighted Least Squares (IRLS)
Multinomial logistic regression can also be solved using IRLS, which reformulates the problem as a series of weighted least squares problems. This approach:
- Starts with initial coefficient estimates
- Computes fitted probabilities using the current coefficients
- Creates working responses and weights
- Solves a weighted least squares problem
- Updates the coefficients
- Repeats until convergence
Convergence Criteria
The optimization process continues until one of the following convergence criteria is met:
- Parameter convergence:
- Log-likelihood convergence:
- Gradient convergence:
where:
- is the vector of coefficient estimates at iteration (the current parameter values during optimization)
- is the vector of coefficient estimates at iteration (the updated parameter values after one optimization step)
- is the log-likelihood value at iteration (the objective function value at the current parameters)
- is the log-likelihood value at iteration (the objective function value at the updated parameters)
- is the gradient vector at iteration (the vector of partial derivatives of the log-likelihood with respect to all parameters)
- is the tolerance threshold (a small positive number, typically or , that determines when convergence is achieved)
- denotes the absolute value (for scalars) or norm (for vectors), measuring the magnitude of change
Identifiability and Constraints
The multinomial logistic regression model is said to be overparameterized because, without any constraints, there are more parameters in the model than are needed to describe the relationship between the predictors and the outcome categories. Specifically, for outcome categories, the model estimates a separate set of coefficients for each category, but only of these sets are necessary to uniquely determine the predicted probabilities. This redundancy means that different sets of coefficients can produce the same predicted probabilities, making the model unidentifiable unless we impose a constraint.
To resolve this, the standard approach is to set the coefficients for one of the categories.called the reference category.to zero:
where:
- is the coefficient vector for the reference category (a vector set to all zeros to ensure model identifiability)
- is the zero vector (a vector with all elements equal to zero)
By doing this, we remove the redundancy and ensure that the model is identifiable, meaning the optimization problem has a unique solution and the estimated coefficients are interpretable relative to the reference category.
Mathematical Properties
The multinomial logistic regression model has several important mathematical properties. First, the softmax function ensures that all predicted probabilities are between 0 and 1 and sum to 1, which is necessary for a valid probability model. Second, the model is identifiable only up to the choice of reference category - we can add any constant to all coefficients without changing the predicted probabilities, which is why we typically set the coefficients for the reference category to zero.
The model also exhibits the property of proportional odds in the sense that the ratio of probabilities for any two categories depends only on the difference in their linear predictors. This means that if we change a predictor variable, the relative probabilities of all non-reference categories change proportionally, maintaining their relative relationships.

