Linear and Quadratic Discriminant Analysis › Formula

Michael BrenndoerferOctober 27, 202620 min read
Machine Learning from ScratchAll chapters
Part 7 of 15Supervised Learning Beyond Linear Models
Chapter 55Linear and Quadratic Discriminant Analysis › Formula
Reading section 4 of 10 · Formula · 20 minSection 4 of 10 · 20 min

Section 4 of 10 · 20 min read

Linear and Quadratic Discriminant Analysis › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes

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

Notes & comments

We first combine Gaussian class densities with Bayes' rule, then examine which score terms cancel under a shared covariance. This separates the population decision rule from the covariance estimates and numerical calculations used to fit it.

Gaussian class models and Bayes decision rules

A discriminant classifier begins with a generative thought experiment. Imagine that nature first chooses a class according to its prior probability, and then draws a feature vector from a probability density that belongs to that class. To classify a new observation, we reverse the order of that story: instead of asking "what does class kk tend to look like?", we ask "how plausible is this particular x\mathbf{x} under each class, after accounting for how common each class is?" That reversal is Bayes' rule, and the entire geometry of LDA and QDA follows from the density we choose.

The first modeling decision is therefore to describe each class by a full density rather than by a single prototype. An ordinary Euclidean nearest-centroid rule scores a point by its Euclidean distance from a class mean, so equal-length displacements have equal distance scores regardless of direction. A class density can instead describe one direction as tightly concentrated and another as broad or tilted. The multivariate Gaussian is the canonical choice for this job. It is specified by a center and a covariance, its level sets are ellipsoids, and its log-density is quadratic, which is exactly what makes the later decision rules tractable. We assume

p(x∣y=k)=N(x∣μk,Σk),p(\mathbf{x} \mid y = k) = \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_k, \boldsymbol{\Sigma}_k),

where N\mathcal{N} denotes the multivariate normal density

N(x∣μ,Σ)=(2π)−d/2∣Σ∣−1/2exp⁡ ⁣(−12(x−μ)⊤Σ−1(x−μ)).\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}, \boldsymbol{\Sigma}) = (2\pi)^{-d/2} |\boldsymbol{\Sigma}|^{-1/2} \exp\!\left(-\tfrac{1}{2} (\mathbf{x} - \boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu})\right).

where:

  • μ\boldsymbol{\mu}: the mean vector of the Gaussian, which locates the center of the ellipsoidal cloud
  • Σ\boldsymbol{\Sigma}: the covariance matrix, which sets the shape and orientation of the cloud
  • ∣Σ∣|\boldsymbol{\Sigma}|: the covariance determinant; its square root is proportional to the volume of an ellipsoid with fixed Mahalanobis radius
  • (x−μ)⊤Σ−1(x−μ)(\mathbf{x} - \boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}): the squared Mahalanobis distance from x\mathbf{x} to μ\boldsymbol{\mu}
  • dd: the number of feature dimensions

The mean μk\boldsymbol{\mu}_k locates the center of a class cloud, and the covariance Σk\boldsymbol{\Sigma}_k determines its shape and orientation. For ellipsoids with the same Mahalanobis radius, volume is proportional to ∣Σ∣\sqrt{|\boldsymbol{\Sigma}|}. A larger determinant therefore means a larger such ellipsoid and a lower Gaussian peak density. The quadratic form (x−μ)⊤Σ−1(x−μ)(\mathbf{x} - \boldsymbol{\mu})^\top \boldsymbol{\Sigma}^{-1}(\mathbf{x} - \boldsymbol{\mu}) is the squared Mahalanobis distance. It measures distance relative to the class covariance, so a large Euclidean displacement along a broad direction can still have a small standardized distance. The density decreases continuously with this distance. There is no single hard neighborhood boundary built into the Gaussian model.

A density alone is not yet a classifier, because classes may occur with different frequencies. Bayes' rule combines the class-conditional density with the prior:

p(y=k∣x)=p(x∣y=k) πkp(x),p(y = k \mid \mathbf{x}) = \frac{p(\mathbf{x} \mid y = k)\, \pi_k}{p(\mathbf{x})},

where:

  • p(y=k∣x)p(y = k \mid \mathbf{x}): the posterior probability that the observation belongs to class kk
  • p(x∣y=k)p(\mathbf{x} \mid y = k): the class-conditional density of x\mathbf{x} given class kk
  • πk\pi_k: the prior probability of class kk
  • p(x)=∑j=1Kp(x∣y=j) πjp(\mathbf{x}) = \sum_{j=1}^K p(\mathbf{x} \mid y = j)\, \pi_j: the marginal density of x\mathbf{x} under the generative model, shared by all classes and normalizing the posteriors to sum to one

The denominator is the same for every class, so it cannot change which class has the largest posterior probability. The rule "predict the class with the largest posterior probability" is therefore equivalent to "predict the class with the largest p(x∣y=k) πkp(\mathbf{x} \mid y = k)\, \pi_k." This is a small algebraic observation with a large practical consequence: we never need to compute the marginal density in order to classify.

Comparing products of densities is awkward, and in high dimensions the products can underflow. Taking logarithms preserves the argmax because the logarithm is monotonic. So

log⁡ ⁣[p(x∣y=k) πk]=−d2log⁡(2π)−12log⁡∣Σk∣−12(x−μk)⊤Σk−1(x−μk)+log⁡πk.\log\!\left[p(\mathbf{x} \mid y = k)\, \pi_k\right] = -\tfrac{d}{2} \log(2\pi) - \tfrac{1}{2} \log |\boldsymbol{\Sigma}_k| - \tfrac{1}{2} (\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}_k^{-1} (\mathbf{x} - \boldsymbol{\mu}_k) + \log \pi_k.

where:

  • x\mathbf{x}: the feature vector being classified
  • μk\boldsymbol{\mu}_k: the mean vector of class kk
  • Σk\boldsymbol{\Sigma}_k: the covariance matrix of class kk
  • πk\pi_k: the prior probability of class kk
  • dd: the number of features
  • ∣Σk∣|\boldsymbol{\Sigma}_k|: the determinant of the class covariance

The first term −d2log⁡(2π)-\tfrac{d}{2} \log(2\pi) is identical for every kk and drops out of any comparison. Dropping it, we obtain the discriminant function

δk(x)=−12(x−μk)⊤Σk−1(x−μk)−12log⁡∣Σk∣+log⁡πk,\delta_k(\mathbf{x}) = -\tfrac{1}{2} (\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}_k^{-1} (\mathbf{x} - \boldsymbol{\mu}_k) - \tfrac{1}{2} \log |\boldsymbol{\Sigma}_k| + \log \pi_k,

where:

  • δk(x)\delta_k(\mathbf{x}): the discriminant score for class kk at point x\mathbf{x}
  • x\mathbf{x}: the feature vector
  • μk\boldsymbol{\mu}_k: the mean vector of class kk
  • Σk\boldsymbol{\Sigma}_k: the covariance matrix of class kk
  • πk\pi_k: the prior probability of class kk
  • (x−μk)⊤Σk−1(x−μk)(\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}_k^{-1} (\mathbf{x} - \boldsymbol{\mu}_k): the squared Mahalanobis distance from x\mathbf{x} to class kk's mean, in units of class kk's own covariance
  • ∣Σk∣|\boldsymbol{\Sigma}_k|: the determinant of the class covariance, penalizing diffuse class clouds

The Bayes decision rule is then y^=arg⁡max⁡kδk(x)\hat{y} = \arg\max_k \delta_k(\mathbf{x}).

Each term has a clear interpretation, and it is worth reading them as a sequence of questions. The first term rewards closeness to the class mean, measured in the class's own Mahalanobis metric: does x\mathbf{x} fall inside the class's ellipsoidal neighborhood? The second term is a normalization penalty: at a fixed feature dimension, a larger covariance determinant lowers the Gaussian peak. The density at a particular point also depends on its Mahalanobis distance from the class mean. The third term rewards frequently observed classes, pulling the decision toward a prior that may be strong. Classification is thus a balance between geometric fit, concentration, and prior belief.

Two facts about this derivation are useful. First, adding the same quantity to every class score, even a quantity depending on x\mathbf{x}, does not change the predicted class. Second, the difference between two class scores is exactly the corresponding log-posterior ratio, because all common terms cancel:

log⁡p(y=k∣x)p(y=k′∣x)=δk(x)−δk′(x),\log \frac{p(y = k \mid \mathbf{x})}{p(y = k' \mid \mathbf{x})} = \delta_k(\mathbf{x}) - \delta_{k'}(\mathbf{x}),

where:

  • k,k′k, k': the two classes being compared
  • δk(x),δk′(x)\delta_k(\mathbf{x}), \delta_{k'}(\mathbf{x}): the discriminant scores for classes kk and k′k' at point x\mathbf{x}, each defined up to the same additive constant
  • p(y=k∣x),p(y=k′∣x)p(y = k \mid \mathbf{x}), p(y = k' \mid \mathbf{x}): the posterior probabilities of classes kk and k′k' given x\mathbf{x}

The right-hand side is a score difference, and its zero set is the pairwise tie locus. With two classes, a change in the winning label can occur only at a tie; a degenerate tie need not separate two open decision regions. With more than two classes, a tie between kk and k′k' contributes to their shared decision boundary only where their common score is at least as large as every other class score. We now examine how the covariance assumptions determine these score differences.

Shared covariance and linear discriminants

The LDA assumption is that all classes share the same covariance matrix: Σk=Σ\boldsymbol{\Sigma}_k = \boldsymbol{\Sigma} for all kk. The shared-covariance condition is a model assumption about the data-generating process. It says that all class clouds have the same shape, orientation, and volume, and differ only in where they are centered and how common they are. Under that assumption, the Gaussian ellipsoids are translated copies of one another. The discriminant simplifies because the −12log⁡∣Σ∣-\tfrac{1}{2} \log |\boldsymbol{\Sigma}| term is the same for all classes and drops out:

δk(x)=−12(x−μk)⊤Σ−1(x−μk)+log⁡πk.\delta_k(\mathbf{x}) = -\tfrac{1}{2} (\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}_k) + \log \pi_k.

where:

  • δk(x)\delta_k(\mathbf{x}): the discriminant score for class kk at point x\mathbf{x}
  • x\mathbf{x}: the feature vector being classified
  • μk\boldsymbol{\mu}_k: the mean vector of class kk
  • Σ\boldsymbol{\Sigma}: the shared covariance matrix common to all classes
  • πk\pi_k: the prior probability of class kk
  • (x−μk)⊤Σ−1(x−μk)(\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}_k): the squared Mahalanobis distance from x\mathbf{x} to class kk's mean in the shared metric

We now expand the quadratic form. In matrix notation,

(x−μk)⊤Σ−1(x−μk)=x⊤Σ−1x−2 μk⊤Σ−1x+μk⊤Σ−1μk.(\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}^{-1} (\mathbf{x} - \boldsymbol{\mu}_k) = \mathbf{x}^\top \boldsymbol{\Sigma}^{-1} \mathbf{x} - 2\, \boldsymbol{\mu}_k^\top \boldsymbol{\Sigma}^{-1} \mathbf{x} + \boldsymbol{\mu}_k^\top \boldsymbol{\Sigma}^{-1} \boldsymbol{\mu}_k.

After multiplying the expansion by −12-\tfrac{1}{2}, the term −12x⊤Σ−1x-\tfrac{1}{2}\mathbf{x}^\top\boldsymbol{\Sigma}^{-1}\mathbf{x} is identical for every class and can be dropped. The other terms retain their signs and coefficients; multiplying an entire score by a negative number would reverse its ranking. The shared quadratic term is the cancellation that removes curvature from the score differences. What remains is

δk(x)=μk⊤Σ−1x−12 μk⊤Σ−1μk+log⁡πk.\delta_k(\mathbf{x}) = \boldsymbol{\mu}_k^\top \boldsymbol{\Sigma}^{-1} \mathbf{x} - \tfrac{1}{2}\, \boldsymbol{\mu}_k^\top \boldsymbol{\Sigma}^{-1} \boldsymbol{\mu}_k + \log \pi_k.

This is a linear function of x\mathbf{x}. Define

wk=Σ−1μk,bk=−12 μk⊤Σ−1μk+log⁡πk,\mathbf{w}_k = \boldsymbol{\Sigma}^{-1} \boldsymbol{\mu}_k, \qquad b_k = -\tfrac{1}{2}\, \boldsymbol{\mu}_k^\top \boldsymbol{\Sigma}^{-1} \boldsymbol{\mu}_k + \log \pi_k,

where:

  • wk\mathbf{w}_k: the covariance-adjusted weight vector for class kk
  • bkb_k: the class-kk bias (intercept), combining the mean-quadratic term and the log prior
  • Σ,μk,πk\boldsymbol{\Sigma}, \boldsymbol{\mu}_k, \pi_k: the shared covariance, class-kk mean, and class-kk prior, as defined earlier

so that δk(x)=wk⊤x+bk\delta_k(\mathbf{x}) = \mathbf{w}_k^\top \mathbf{x} + b_k is a linear score. For two classes, the decision boundary between them is δ0(x)=δ1(x)\delta_0(\mathbf{x}) = \delta_1(\mathbf{x}), or

w⊤x+b=0,\mathbf{w}^\top \mathbf{x} + b = 0,

with

w=Σ−1(μ0−μ1),b=−12(μ0+μ1)⊤Σ−1(μ0−μ1)+log⁡π0π1.\mathbf{w} = \boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_0 - \boldsymbol{\mu}_1), \qquad b = -\tfrac{1}{2}(\boldsymbol{\mu}_0 + \boldsymbol{\mu}_1)^\top \boldsymbol{\Sigma}^{-1} (\boldsymbol{\mu}_0 - \boldsymbol{\mu}_1) + \log \frac{\pi_0}{\pi_1}.

where:

  • w\mathbf{w}: the covariance-adjusted normal to the boundary, pointing toward increasing class-0-versus-class-1 score
  • bb: the bias term that shifts the boundary based on the class means and priors
  • Σ,μ0,μ1,π0,π1\boldsymbol{\Sigma}, \boldsymbol{\mu}_0, \boldsymbol{\mu}_1, \pi_0, \pi_1: the shared covariance, class means, and class priors, as defined earlier

The sign convention matters: w⊤x+b\mathbf{w}^\top\mathbf{x} + b equals δ0(x)−δ1(x)\delta_0(\mathbf{x})-\delta_1(\mathbf{x}), so a positive value favors class 0 after accounting for the priors. In general, w\mathbf{w} is not parallel to μ0−μ1\boldsymbol{\mu}_0-\boldsymbol{\mu}_1. If the means differ and the priors are equal, the boundary is the hyperplane through their midpoint on which their Mahalanobis distances are equal. Increasing the prior of one class expands its winning region and moves this hyperplane toward the other class. If the means are identical, the normal is zero and the priors alone decide, or every point is tied when the priors are also equal.

Fisher's original formulation sought a linear combination w⊤x\mathbf{w}^\top\mathbf{x} maximizing the ratio of between-class to within-class variance. In the two-class case with nonsingular within-class covariance, the between-class scatter of the projected values is (w⊤(μ0−μ1))2(\mathbf{w}^\top(\boldsymbol{\mu}_0-\boldsymbol{\mu}_1))^2 and the within-class scatter is w⊤Σw\mathbf{w}^\top\boldsymbol{\Sigma}\mathbf{w}, so the ratio is

J(w)=(w⊤(μ0−μ1))2w⊤Σw,J(\mathbf{w}) = \frac{\bigl(\mathbf{w}^\top(\boldsymbol{\mu}_0-\boldsymbol{\mu}_1)\bigr)^2}{\mathbf{w}^\top\boldsymbol{\Sigma}\mathbf{w}},

where:

  • w\mathbf{w}: the direction onto which the data are projected
  • μ0−μ1\boldsymbol{\mu}_0-\boldsymbol{\mu}_1: the difference between the class means, setting the between-class separation
  • Σ\boldsymbol{\Sigma}: the shared (within-class) covariance, setting the within-class spread

When the class means differ, a maximizing nonzero direction is proportional to Σ−1(μ0−μ1)\boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_0-\boldsymbol{\mu}_1). Its sign and nonzero scale do not change the variance ratio. If the means coincide, J(w)=0J(\mathbf w)=0 for every nonzero direction, so this criterion does not select an informative direction. Under the shared-covariance Gaussian model with distinct means, the maximizing direction gives the generative classifier when paired with its prior-dependent threshold. The direction alone does not specify that threshold.

LDA has KdKd mean parameters and d(d+1)2\frac{d(d+1)}{2} shared symmetric-covariance parameters. Estimating the class priors adds K−1K-1 free parameters under ∑kπk=1\sum_k \pi_k=1, giving Kd+d(d+1)2+(K−1)Kd+\frac{d(d+1)}{2}+(K-1) in total. For fixed KK, the covariance term grows quadratically in dd, while the mean term grows linearly. In the multiclass setting, LDA needs at most min⁡(d,K−1)\min(d,K-1) discriminant directions. The centered class means have at most K−1K-1 independent directions, and applying an invertible covariance adjustment does not change this rank. The posterior-relevant directions in the original feature coordinates are the span of Σ−1(μk−μref)\boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_k-\boldsymbol{\mu}_{\mathrm{ref}}), where μref\boldsymbol{\mu}_{\mathrm{ref}} is one chosen reference-class mean. Projecting onto these directions retains the Gaussian LDA score differences; projecting onto the raw means need not.

Class-specific covariance and quadratic discriminants

QDA relaxes the shared-covariance assumption: each class has its own Σk\boldsymbol{\Sigma}_k. This can describe one class that is narrow and another that is broad or differently oriented. The shared-covariance cancellation is no longer available in general. Some terms can still coincide for particular parameter choices, but the general score must retain the full form

δk(x)=−12(x−μk)⊤Σk−1(x−μk)−12log⁡∣Σk∣+log⁡πk.\delta_k(\mathbf{x}) = -\tfrac{1}{2} (\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}_k^{-1} (\mathbf{x} - \boldsymbol{\mu}_k) - \tfrac{1}{2} \log |\boldsymbol{\Sigma}_k| + \log \pi_k.

where:

  • δk(x)\delta_k(\mathbf{x}): the discriminant score for class kk at point x\mathbf{x}
  • x\mathbf{x}: the feature vector being classified
  • μk\boldsymbol{\mu}_k: the mean vector of class kk
  • Σk\boldsymbol{\Sigma}_k: the class-specific covariance matrix for class kk
  • πk\pi_k: the prior probability of class kk
  • (x−μk)⊤Σk−1(x−μk)(\mathbf{x} - \boldsymbol{\mu}_k)^\top \boldsymbol{\Sigma}_k^{-1} (\mathbf{x} - \boldsymbol{\mu}_k): the squared Mahalanobis distance from x\mathbf{x} to class kk's mean, in class kk's own metric
  • ∣Σk∣|\boldsymbol{\Sigma}_k|: the determinant of the class kk covariance, penalizing diffuse class clouds

When Σk−1\boldsymbol{\Sigma}_k^{-1} varies with kk, the x⊤Σk−1x\mathbf{x}^\top\boldsymbol{\Sigma}_k^{-1}\mathbf{x} contribution is class dependent and must be retained. The log-determinant penalty must likewise be retained whenever the covariance determinants differ; different covariance matrices can still have the same determinant. The general discriminant is quadratic in x\mathbf{x}, with linear and second-order terms in its coordinates. Class-specific covariance therefore permits boundaries beyond a single flat cut, while particular parameter choices can still produce simpler or degenerate boundaries.

For two classes (k=0k = 0 and k=1k = 1), the boundary δ0(x)=δ1(x)\delta_0(\mathbf{x}) = \delta_1(\mathbf{x}) is

−12x⊤(Σ0−1−Σ1−1)x+(μ0⊤Σ0−1−μ1⊤Σ1−1)x+c0=0,-\tfrac{1}{2} \mathbf{x}^\top (\boldsymbol{\Sigma}_0^{-1} - \boldsymbol{\Sigma}_1^{-1}) \mathbf{x} + (\boldsymbol{\mu}_0^\top \boldsymbol{\Sigma}_0^{-1} - \boldsymbol{\mu}_1^\top \boldsymbol{\Sigma}_1^{-1}) \mathbf{x} + c_0 = 0,

which is the equation δ0(x)−δ1(x)=0\delta_0(\mathbf{x}) - \delta_1(\mathbf{x}) = 0 written with the class-0 terms leading.

where:

  • x\mathbf{x}: the feature vector at which the boundary is evaluated
  • Σ0,Σ1\boldsymbol{\Sigma}_0, \boldsymbol{\Sigma}_1: the covariance matrices of classes 0 and 1 (both assumed invertible)
  • μ0,μ1\boldsymbol{\mu}_0, \boldsymbol{\mu}_1: the mean vectors of classes 0 and 1
  • c0c_0: the constant term, defined explicitly below

The constant c0c_0 collects the terms that do not depend on x\mathbf{x}. Writing out δ0(x)−δ1(x)=0\delta_0(\mathbf{x}) - \delta_1(\mathbf{x}) = 0 in full (the form matching the displayed boundary equation) and keeping only the terms without x\mathbf{x} gives

c0=−12μ0⊤Σ0−1μ0+12μ1⊤Σ1−1μ1−12log⁡∣Σ0∣+12log⁡∣Σ1∣+log⁡π0π1.c_0 = -\tfrac{1}{2} \boldsymbol{\mu}_0^\top \boldsymbol{\Sigma}_0^{-1} \boldsymbol{\mu}_0 + \tfrac{1}{2} \boldsymbol{\mu}_1^\top \boldsymbol{\Sigma}_1^{-1} \boldsymbol{\mu}_1 - \tfrac{1}{2} \log |\boldsymbol{\Sigma}_0| + \tfrac{1}{2} \log |\boldsymbol{\Sigma}_1| + \log \frac{\pi_0}{\pi_1}.

where:

  • x\mathbf{x}: the feature vector
  • Σ0,Σ1\boldsymbol{\Sigma}_0, \boldsymbol{\Sigma}_1: the covariance matrices of classes 0 and 1
  • μ0,μ1\boldsymbol{\mu}_0, \boldsymbol{\mu}_1: the class mean vectors
  • π0,π1\pi_0, \pi_1: the class prior probabilities
  • c0c_0: the constant term collecting all contributions that do not depend on x\mathbf{x} (the mean-quadratic and log-determinant terms plus the log prior ratio)

The boundary equation has the form of a general quadratic in x\mathbf{x}: the symmetric matrix coefficient of the second-order term is −12(Σ0−1−Σ1−1)-\tfrac{1}{2}(\boldsymbol{\Sigma}_0^{-1} - \boldsymbol{\Sigma}_1^{-1}), the linear coefficient is μ0⊤Σ0−1−μ1⊤Σ1−1\boldsymbol{\mu}_0^\top \boldsymbol{\Sigma}_0^{-1} - \boldsymbol{\mu}_1^\top \boldsymbol{\Sigma}_1^{-1}, and c0c_0 is the scalar constant.

In two dimensions, a quadratic tie equation can describe conic sections such as ellipses, hyperbolas, and parabolas; in higher dimensions, the corresponding objects are quadrics. The eigenstructure of Σ0−1−Σ1−1\boldsymbol{\Sigma}_0^{-1}-\boldsymbol{\Sigma}_1^{-1} controls the quadratic curvature, but the linear and constant terms also matter. Together they determine the boundary's location, whether it is degenerate, and whether it exists at all. Some QDA models produce bounded class regions, with a narrow class winning near its center and a broad class winning in the tails. QDA can also reduce to a linear rule when the quadratic difference vanishes.

To see where the constants come from, write δ0(x)−δ1(x)\delta_0(\mathbf{x}) - \delta_1(\mathbf{x}) term by term, consistent with the δ0−δ1=0\delta_0 - \delta_1 = 0 sign convention used above. The x\mathbf{x}-independent contribution is

−12μ0⊤Σ0−1μ0+12μ1⊤Σ1−1μ1−12log⁡∣Σ0∣+12log⁡∣Σ1∣+log⁡π0π1,-\tfrac{1}{2} \boldsymbol{\mu}_0^\top \boldsymbol{\Sigma}_0^{-1} \boldsymbol{\mu}_0 + \tfrac{1}{2} \boldsymbol{\mu}_1^\top \boldsymbol{\Sigma}_1^{-1} \boldsymbol{\mu}_1 - \tfrac{1}{2} \log |\boldsymbol{\Sigma}_0| + \tfrac{1}{2} \log |\boldsymbol{\Sigma}_1| + \log \frac{\pi_0}{\pi_1},

matching c0c_0 above, while the x\mathbf{x}-dependent part yields the quadratic and linear coefficients already stated.

The number of free parameters in QDA is K⋅dK \cdot d (means) plus K−1K - 1 (priors) plus K⋅d(d+1)2K \cdot \frac{d(d+1)}{2} (covariances), so its covariance term grows KK times larger than LDA's. For independent Gaussian samples with a truly common covariance, the pooled covariance has lower entrywise sampling variance than an unbiased class estimate when that class has at least two observations and the other classes add residual degrees of freedom. Whether their flexibility improves held-out prediction depends on the population covariances and the available samples; parameter count alone does not determine the prediction error.

The covariance parameter count grows quadratically in the feature dimension and is multiplied by the number of classes for QDA. The next figure isolates that multiplier for a three-class problem, holding means and priors aside so the covariance cost is the only changing quantity.

VisualizationOut [4]
CollapseExpand
LDA and QDA covariance parameter counts versus feature dimension for three classes.
Covariance parameter counts for LDA and QDA as the feature dimension grows for three classes. QDA's count is exactly three times LDA's at every feature dimension, and the gap widens quadratically rather than staying constant.

Covariance estimation, shrinkage, and limitations

The formulas above describe the population rule when the means, covariances, and priors are known. In practice, these parameters must be estimated. The maximum-likelihood class priors are the empirical proportions, π^k=Nk/N\hat{\pi}_k=N_k/N, where NkN_k counts observations in class kk and N=∑jNjN=\sum_j N_j. The maximum-likelihood class means are μ^k=1Nk∑i:yi=kxi\hat{\boldsymbol{\mu}}_k=\frac{1}{N_k}\sum_{i:y_i=k}\mathbf{x}_i. For covariance estimates, this chapter uses the unbiased sample convention rather than the Gaussian maximum-likelihood convention:

Σ^k=1Nk−1∑i:yi=k(xi−μ^k)(xi−μ^k)⊤\hat{\boldsymbol{\Sigma}}_k = \frac{1}{N_k - 1} \sum_{i: y_i = k} (\mathbf{x}_i - \hat{\boldsymbol{\mu}}_k)(\mathbf{x}_i - \hat{\boldsymbol{\mu}}_k)^\top

where:

  • Σ^k\hat{\boldsymbol{\Sigma}}_k: the estimated covariance matrix of class kk
  • NkN_k: the number of observations in class kk
  • μ^k\hat{\boldsymbol{\mu}}_k: the empirical mean of class kk
  • xi\mathbf{x}_i: the ii-th feature vector, associated with label yiy_i

The Nk−1N_k-1 denominator gives the unbiased class covariance estimator for Nk>1N_k>1. When the centered class scatter is positive definite, the Gaussian covariance maximum-likelihood estimator divides that same scatter by NkN_k. If the scatter is singular, no finite positive-definite covariance maximizes this unconstrained Gaussian density likelihood. The relative difference between the two normalization factors vanishes as NkN_k grows. For LDA, the unbiased pooled covariance estimator is

Σ^=1N−K∑k=1K∑i:yi=k(xi−μ^k)(xi−μ^k)⊤,\hat{\boldsymbol{\Sigma}} = \frac{1}{N - K} \sum_{k=1}^K \sum_{i: y_i = k} (\mathbf{x}_i - \hat{\boldsymbol{\mu}}_k)(\mathbf{x}_i - \hat{\boldsymbol{\mu}}_k)^\top,

where:

  • Σ^\hat{\boldsymbol{\Sigma}}: the pooled estimate of the shared covariance matrix used by LDA
  • N=∑j=1KNjN = \sum_{j=1}^K N_j: the total number of observations across all classes
  • KK: the number of classes
  • NkN_k: the number of observations in class kk
  • μ^k\hat{\boldsymbol{\mu}}_k: the empirical mean of class kk
  • xi\mathbf{x}_i: the ii-th feature vector, associated with label yiy_i

This pools the within-class scatter and divides by the residual degrees of freedom N−KN-K. When the pooled scatter is positive definite, the shared-covariance Gaussian maximum-likelihood estimator instead divides that same scatter by NN. If the pooled scatter is singular, the unconstrained positive-definite Gaussian covariance likelihood has no finite maximum. These are different finite-sample normalization conventions; the worked example and primitive implementations below consistently use the unbiased denominators.

Pooling is the estimation analogue of the shared-covariance assumption. Under independent Gaussian samples with a truly common covariance, the pooled estimate has N−KN-K residual degrees of freedom rather than one class's Nk−1N_k-1. For a class with Nk>1N_k>1, pooling has no larger covariance-entry sampling variance under that model; the reduction is strict when the other classes add residual degrees of freedom, so N−K>Nk−1N-K>N_k-1. If class covariances differ, pooling instead estimates a compromise matrix.

For a centered class sample with Nk>1N_k>1, rank⁡(Σ^k)≤min⁡(d,Nk−1)\operatorname{rank}(\hat{\boldsymbol{\Sigma}}_k)\le\min(d,N_k-1). Thus 1<Nk≤d1<N_k\le d forces singularity: for d=100d=100 and Nk=50N_k=50, there are at most 49 nonzero eigenvalues and the inverse does not exist. Having Nk>dN_k>d is necessary, but not sufficient, for nonsingularity. LDA's pooled scatter similarly has rank at most min⁡(d,N−K)\min(d,N-K). Even when these matrices are full rank, small samples and strong feature dependencies can make covariance estimation unreliable. Rank, conditioning, and estimation uncertainty are separate checks.

Shrinkage estimators regularize the covariance by blending the empirical estimate with a low-dimensional target:

Σ^k(α)=(1−α)Σ^k+αT,\hat{\boldsymbol{\Sigma}}_k(\alpha) = (1 - \alpha) \hat{\boldsymbol{\Sigma}}_k + \alpha \mathbf{T},

where:

  • Σ^k(α)\hat{\boldsymbol{\Sigma}}_k(\alpha): the shrunk (regularized) covariance estimate for class kk at mixing level α\alpha
  • α\alpha: the mixing (shrinkage) parameter in [0,1][0, 1] controlling the bias-variance tradeoff; α=0\alpha = 0 recovers the empirical covariance and α=1\alpha = 1 yields the pure target
  • Σ^k\hat{\boldsymbol{\Sigma}}_k: the empirical covariance of class kk
  • T\mathbf{T}: the shrinkage target, for example the identity matrix I\mathbf{I} or a scaled version σ2I\sigma^2 \mathbf{I}

At α=0\alpha=0, the estimate is the empirical covariance; at α=1\alpha=1, it is the target T\mathbf{T}. If the empirical covariance is positive semidefinite and the target is positive definite, any α>0\alpha>0 makes their convex blend positive definite. A shared identity or positive scaled-identity target is spherical. The Ledoit-Wolf (2004) estimator provides a data-driven shrinkage approach. It should not be conflated with manually choosing a regularization strength. In scikit-learn 1.8, QDA's default 'svd' solver uses reg_param; its 'eigen' solver instead supports shrinkage, including 'auto'. LDA supports shrinkage='auto' with 'lsqr' or 'eigen'. The solver and rank limitations are specified below.

Two further limitations matter. A sufficiently extreme observation can dominate an empirical covariance and move the fitted boundary. The empirical mean and covariance have unbounded influence even when the reference distribution is Gaussian. Moving a contaminating point farther away makes its influence grow without bound. Gaussianity therefore does not make these estimators robust to contamination. Robust covariance estimators address a different estimation problem and are outside this chapter's scope.

Mathematical Properties

Discriminant analysis inherits several structural properties from its Gaussian foundation.

  • Invertible affine transformations preserve unregularized LDA and QDA predictions in exact arithmetic when their estimates are transformed consistently. If z=Ax+b\mathbf{z}=A\mathbf{x}+\mathbf{b} with invertible AA, then μk′=Aμk+b\boldsymbol{\mu}'_k=A\boldsymbol{\mu}_k+\mathbf{b} and Σk′=AΣkA⊤\boldsymbol{\Sigma}'_k=A\boldsymbol{\Sigma}_kA^\top. Mahalanobis distances are unchanged, while ∣Σk′∣=∣det⁡A∣2∣Σk∣|\boldsymbol{\Sigma}'_k|=|\det A|^2|\boldsymbol{\Sigma}_k|. The full Gaussian discriminants therefore change by −log⁡∣det⁡A∣-\log|\det A|, common to every class. Reduced LDA scores, after dropping their shared quadratic term, can acquire a different common term depending on x\mathbf{x}, but all score differences are still unchanged. This algebraic equivariance does not require Gaussian data; finite precision and a fixed regularization target can limit the invariance in practice.
  • Posterior probabilities are given by the softmax of the discriminant scores (the logits):
p(y=k∣x)=exp⁡(δk(x))∑j=1Kexp⁡(δj(x)).p(y = k \mid \mathbf{x}) = \frac{\exp(\delta_k(\mathbf{x}))}{\sum_{j=1}^K \exp(\delta_j(\mathbf{x}))}.

This means LDA and QDA are not just classifiers but probabilistic models: the same machinery that predicts labels can also predict class probabilities. In practice, these probabilities can be miscalibrated if the Gaussian assumption fails, which motivates the calibration methods discussed in later parts of the book. In the implementation below, we compute the class posteriors directly from the discriminant scores via this softmax.

  • Under the shared-covariance Gaussian model, the binary posterior is logistic in x\mathbf{x}. In the earlier sign convention, δ0−δ1=w⊤x+b\delta_0-\delta_1=\mathbf{w}^\top\mathbf{x}+b, so p(y=1∣x)=σ(−w⊤x−b)p(y=1\mid\mathbf{x})=\sigma(-\mathbf{w}^\top\mathbf{x}-b), where σ(t)=1/(1+exp⁡(−t))\sigma(t)=1/(1+\exp(-t)). Equivalently, its class-1 logit has coefficient Σ−1(μ1−μ0)\boldsymbol{\Sigma}^{-1}(\boldsymbol{\mu}_1-\boldsymbol{\mu}_0). Logistic regression fits this conditional linear-logit family directly. LDA obtains coefficients by fitting a joint Gaussian model; this imposes assumptions about feature distributions, rather than a smaller family of linear posterior logits.
  • QDA does not generally satisfy a linear-logistic relationship. Its log-odds is quadratic in x\mathbf{x}, so p(y=1∣x)=σ(f(x))p(y = 1 \mid \mathbf{x}) = \sigma(f(\mathbf{x})) holds with f(x)=log⁡(π1/π0)−12log⁡(∣Σ1∣/∣Σ0∣)−12(x−μ1)⊤Σ1−1(x−μ1)+12(x−μ0)⊤Σ0−1(x−μ0)f(\mathbf{x}) = \log(\pi_1/\pi_0) - \frac{1}{2}\log(|\boldsymbol{\Sigma}_1|/|\boldsymbol{\Sigma}_0|) - \frac{1}{2}(\mathbf{x}-\boldsymbol{\mu}_1)^\top\boldsymbol{\Sigma}_1^{-1}(\mathbf{x}-\boldsymbol{\mu}_1) + \frac{1}{2}(\mathbf{x}-\boldsymbol{\mu}_0)^\top\boldsymbol{\Sigma}_0^{-1}(\mathbf{x}-\boldsymbol{\mu}_0), a quadratic function of x\mathbf{x}. The quadratic term enters before the logistic transformation, so the gradient of the log-odds generally varies affinely across feature space.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy