Multinomial Logistic Regression: From Binary to Multiclass Outcomes
Binary logistic regression assigns a probability to each of two outcomes. For an outcome Y with K classes, we need K nonnegative probabilities that sum to one. Softmax supplies this normalization. We derive its likelihood and gradient, then explain why fitting the coefficients requires a reference-class constraint.
Recap: The binary logistic score
In binary logistic regression, observation i contributes
∇βℓi=xi(yi−πi). Scroll to see the full equation →
The predictor xi and observed binary label yi are fixed during optimization; πi is the current model probability. Below, each free class coefficient has the same observed-minus-predicted form, with class membership replacing the binary response.
1. Model Setup
For each observation i=1,…,n, let xi∈R(p+1)×1 denote the predictor vector (including the intercept), defined as
xi=(1,xi1,xi2,…,xip)T. Scroll to see the full equation →
The response Yi∈{1,…,K} is a class label. For likelihood calculations, we encode its observed value as a vector yi∈{0,1}K, with yik=1 when observation i belongs to class k and zero otherwise. Thus exactly one entry equals 1. For example, when K=3 and the observed class is 2, the encoding is yi=(0,1,0)T. The label and the vector carry the same information but have different mathematical roles.
We assign a coefficient vector to each class k=1,…,K, with βk∈R(p+1)×1, and collect them in B=[β1,β2,…,βK]∈R(p+1)×K. The softmax function converts their linear scores into class probabilities:
Pr(Yi=k∣xi)=∑j=1Kexp(xi⊤βj)exp(xi⊤βk). Scroll to see the full equation →
Softmax Probability for Class k
Pr(Yi=k∣xi)=πik=σk(xi;B)=softmaxk(xi;B)=∑j=1Kexp(xi⊤βj)exp(xi⊤βk) Scroll to see the full equation →
Adding the same vector to every class coefficient leaves these probabilities unchanged: the extra exponential factor cancels between numerator and denominator. To identify the coefficients, we use class K as the reference and fix βK=0. We retain all K columns in the notation for convenience, but estimate only the first K−1. This reference-class convention is also described in the Stan User's Guide.
Conditional on the fixed design X and coefficients B, the labels are independent with Yi∼Categorical(πi1,…,πiK). Their random one-hot encodings satisfy Yi∼Multinomial(1,πi); lowercase yi denotes the observed encoding.
The likelihood treats the predictors and coefficients as fixed. In a Bayesian analysis, we instead assign a prior to the free coefficients and update it using the observed data to obtain their posterior distribution.
2. Likelihood and Estimation
For independent observations, the one-hot encoding selects the probability of each observed class: yik=1 for that class and 0 for the others, with ∑k=1Kyik=1. The conditional likelihood is therefore:
L(B)=i=1∏nk=1∏Kπikyik Scroll to see the full equation →
Taking the logarithm, we have the log-likelihood:
ℓ(B)=i=1∑nk=1∑Kyikln(∑j=1Kexp(xi⊤βj)exp(xi⊤βk)) Scroll to see the full equation →
Expanding the logarithm yields:
ℓ(B)=i=1∑nk=1∑Kyik[xi⊤βk−ln(j=1∑Kexp(xi⊤βj))] Scroll to see the full equation →
Distributing the sum over classes gives:
ℓ(B)=i=1∑n[k=1∑Kyikxi⊤βk−k=1∑Kyikln(j=1∑Kexp(xi⊤βj))]. Scroll to see the full equation →
The log-normalizing term ln(∑j=1Kexp(xi⊤βj)) does not depend on k, so we can factor it out of the sum over classes:
k=1∑Kyikln(j=1∑Kexp(xi⊤βj))=ln(j=1∑Kexp(xi⊤βj))k=1∑Kyik. Scroll to see the full equation →
Because ∑k=1Kyik=1, the log-likelihood simplifies to:
ℓ(B)=i=1∑n[k=1∑Kyikxi⊤βk−ln(j=1∑Kexp(xi⊤βj))]. Scroll to see the full equation →
3. Assumptions
The softmax formula defines probabilities, but it does not by itself guarantee identifiable coefficients or a finite maximum likelihood estimate. Those questions require the following conditions:
- Linear Log-Probability Ratios: Relative to the reference class, log(πik/πiK)=xi⊤βk for k<K. These are not the one-versus-all logits log(πik/(1−πik)).
- Independence: Each observation is assumed to be independent.
- Finite Estimates: Complete or quasi-complete separation can prevent a finite unpenalized maximum likelihood estimate from existing.
- Identifiability and Design Rank: We fix the reference coefficient vector and require a full-column-rank design for uniquely identified free coefficients. Strong, but not exact, collinearity can still make estimates unstable.
- Probability Model: The softmax specification must adequately describe the class probabilities for the intended use. It models an unordered categorical response; an ordered outcome may call for a different model.
4. Derivation of the Log-Likelihood Derivative
To derive the fitting direction, return to the simplified log-likelihood:
ℓ(B)=i=1∑n[k=1∑Kyikxi⊤βk−ln(j=1∑Kexp(xi⊤βj))], Scroll to see the full equation →
We differentiate this expression with respect to a free coefficient vector βr, where r<K. Only the class-r contribution in the linear term depends on this vector. For the logarithm of the normalizing sum, we apply the chain rule to the class-r exponential:
∂βr∂ℓ(B)=i=1∑n[∂βr∂(yirxi⊤βr)−∂βr∂ln(j=1∑Kexp(xi⊤βj))]. Scroll to see the full equation →For the first term, we differentiate
yirxi⊤βr, Scroll to see the full equation →
with respect to βr. Applying the basic rule of matrix calculus—that is, for a constant vector a,
∂β∂(a⊤β)=a, Scroll to see the full equation →
we obtain:
∂βr∂(yirxi⊤βr)=yirxi. Scroll to see the full equation →The normalization term also depends on the coefficients. Applying the chain rule gives:
∂βr∂ln(j=1∑Kexp(xi⊤βj))=∑j=1Kexp(xi⊤βj)1⋅∂βr∂exp(xi⊤βr)=∑j=1Kexp(xi⊤βj)exp(xi⊤βr)xi. Scroll to see the full equation →Here, only the j=r term depends on βr; defining πir=∑j=1Kexp(xi⊤βj)exp(xi⊤βr) gives the result πirxi.
∂βr∂ln(j=1∑Kexp(xi⊤βj))=πirxi. Scroll to see the full equation →
Subtracting the log-normalizing derivative from the linear-term derivative gives:
∂βr∂ℓ(B)=i=1∑n[yirxi−πirxi]=i=1∑n(yir−πir)xi. Scroll to see the full equation →
The gradient for βr is a sum of predictor vectors weighted by the difference between observed class membership and predicted class probability. This is the multiclass counterpart of the binary logistic score.
Let X∈Rn×(p+1) have rows xiT, let Y∈Rn×K collect the observed one-hot encodings, and let Π∈Rn×K have entries πik. The formal gradient with respect to all columns is:
∇Bℓ(B)=X⊤(Y−Π). Scroll to see the full equation →
Under the reference-class constraint, we use only its first K−1 columns; the last coefficient vector remains fixed at zero.
5. Gradient Ascent Update
To maximize ℓ(B), we move the free coefficients in the gradient direction. Write B−K for the first K−1 columns. At iteration t, the gradient ascent update is:
B−K(t+1)=B−K(t)+η∇B−Kℓ(B(t)), Scroll to see the full equation →
where η>0 is the learning rate. Using the vectorized gradient expression
∇Bℓ(B)=X⊤(Y−Π), Scroll to see the full equation →
the update becomes:
B−K(t+1)=B−K(t)+ηX⊤(Y−K−Π−K(t)),βK(t+1)=0, Scroll to see the full equation →
with Π(t) denoting the softmax probability matrix at iteration t, where the (i,k)-th entry is given by
πik(t)=∑j=1Kexp(xi⊤βj(t))exp(xi⊤βk(t)). Scroll to see the full equation →
Here, Y−K and Π−K(t) retain the first K−1 columns. We choose a step size that increases the log-likelihood and check convergence, for example through the norm of the gradient for the free coefficients. A small change in ℓ(B) alone does not establish a finite optimum, especially when the classes can be separated. Minimizing the negative log-likelihood gives the equivalent gradient-descent procedure.
6. Applications in Real-World Problems
The same model can be used whenever observations belong to a finite set of known, mutually exclusive categories. Examples include:
- Epidemiology: Predicting a disease subtype from measured risk factors.
- Marketing: Predicting membership in predefined customer groups from purchasing behavior.
- Natural Language Processing: Classifying text documents into multiple categories.
- Customer Choice: Modeling which of several products a customer selects.
- Medical Diagnosis: Differentiating among various diagnoses using multiple clinical indicators.
In these settings, the output is a probability for each class rather than only a predicted label. Those probabilities still require assessment on data relevant to the intended use. A fitted coefficient describes a change in the log probability ratio of a class to the reference class, holding other predictors fixed—not a causal effect or a guarantee of predictive accuracy.
Conclusion
The softmax function extends binary logistic regression to multiple classes while preserving a simple gradient: observed class indicators minus predicted probabilities, multiplied by the predictors. The reference-class constraint removes a redundancy in the coefficient representation. Together, the probability model, gradient, and constraint specify what we fit; model checking then determines how useful that fit is for the application.