 # Linear and Quadratic Discriminant Analysis: Tutorial

This tutorial explains Linear Discriminant Analysis (LDA) and Quadratic Discriminant Analysis (QDA) as two fundamental classification methods in statistical and probabilistic learning. We start with the optimization of decision boundary on which the posteriors are equal. Then, LDA and QDA are derived for binary and multiple classes. The estimation of parameters in LDA and QDA are also covered. Then, we explain how LDA and QDA are related to metric learning, kernel principal component analysis, Mahalanobis distance, logistic regression, Bayes optimal classifier, Gaussian naive Bayes, and likelihood ratio test. We also prove that LDA and Fisher discriminant analysis are equivalent. We finally clarify some of the theoretical concepts with simulations we provide.

## Authors

##### This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

## 1 Introduction

Assume we have a dataset of instances with sample size and dimensionality and . The ’s are the class labels. We would like to classify the space of data using these instances. Linear Discriminant Analysis (LDA) and Quadratic discriminant Analysis (QDA) (Friedman et al., 2009) are two well-known supervised classification methods in statistical and probabilistic learning. This paper is a tutorial for these two classifiers where the theory for binary and multi-class classification are detailed. Then, relations of LDA and QDA to metric learning, kernel Principal Component Analysis (PCA), Fisher Discriminant Analysis (FDA), logistic regression, Bayes optimal classifier, Gaussian naive Bayes, and Likelihood Ratio Test (LRT) are explained for better understanding of these two fundamental methods. Finally, some experiments on synthetic datasets are reported and analyzed for illustration.

## 2 Optimization for the Boundary of Classes

First suppose the data is one dimensional,

. Assume we have two classes with the Cumulative Distribution Functions (CDF)

and

, respectively. Let the Probability Density Functions (PDF) of these CDFs be:

 f1(x)=∂F1(x)∂x, (1) f2(x)=∂F2(x)∂x, (2)

respectively.

We assume that the two classes have normal (Gaussian) distribution which is the most common and default distribution in the real-world applications. The mean of one of the two classes is greater than the other one; we assume

. An instance belongs to one of these two classes:

 x∼{N(μ1,σ21),if x∈C1,N(μ2,σ22),if x∈C2, (3)

where and denote the first and second class, respectively.

For an instance , we may have an error in estimation of the class it belongs to. At a point, which we denote by , the probability of the two classes are equal; therefore, the point is on the boundary of the two classes. As we have , we can say as shown in Fig. 1. Therefore, if or the instance belongs to the first and second class, respectively. Hence, estimating or to respectively belong to the second and first class is an error in estimation of the class. This probability of the error can be stated as:

 P(error)=P(x>x∗,x∈C1)+P(x

As we have , we can say:

 P(error)= P(x>x∗|x∈C1)P(x∈C1) (5) +P(x

which we want to minimize:

 minimizex∗   P(error), (6)

by finding the best boundary of classes, i.e., .

According to the definition of CDF, we have:

 P(xx∗,x∈C1)=1−F1(x∗), (7) P(x

According to the definition of PDF, we have:

 P(x∈C1)=f1(x)=π1, (9) P(x∈C2)=f2(x)=π2, (10)

where we denote the priors and by and , respectively. Figure 1: Two Gaussian density functions where they are equal at the point x∗.

Hence, Eqs. (5) and (6) become:

 minimizex∗   (1−F1(x∗))π1+F2(x∗)π2. (11)

We take derivative for the sake of minimization:

 ∂P(error)∂x∗=−f1(x∗)π1+f2(x∗)π2set=0, ⟹f1(x∗)π1=f2(x∗)π2. (12)

Another way to obtain this expression is equating the posterior probabilities to have the equation of the boundary of classes:

 P(x∈C1|X=x)set=P(x∈C2|X=x). (13)

According to Bayes rule, the posterior is:

 P(x∈C1|X=x) =P(X=x|x∈C1)P(x∈C1)P(X=x) =f1(x)π1∑|C|k=1P(X=x|x∈Ck)πk, (14)

where is the number of classes which is two here. The and are the likelihood (class conditional) and prior probabilities, respectively, and the denominator is the marginal probability.

Therefore, Eq. (13) becomes:

 f1(x)π1∑|C|i=1P(X=x|x∈Ci)πi set=f2(x)π2∑|C|i=1P(X=x|x∈Ci)πi ⟹f1(x)π1=f2(x)π2. (15)

Now let us think of data as multivariate data with dimensionality . The PDF for multivariate Gaussian distribution, is:

 f(x)=1√(2π)d|Σ|exp(−(x−μ)⊤Σ−1(x−μ)2), (16)

where , is the mean, is the covariance matrix, and is the determinant of matrix. The in this equation should not be confused with the (prior) in Eq. (12) or (15). Therefore, the Eq. (12) or (15) becomes:

 1√(2π)d|Σ1|exp(−(x−μ1)⊤Σ−11(x−μ1)2)π1 (17) =1√(2π)d|Σ2|exp(−(x−μ2)⊤Σ−12(x−μ2)2)π2,

where the distributions of the first and second class are and , respectively.

## 3 Linear Discriminant Analysis for Binary Classification

In Linear Discriminant Analysis (LDA), we assume that the two classes have equal covariance matrices:

 Σ1=Σ2=Σ. (18)

Therefore, the Eq. (17) becomes:

 1√(2π)d|Σ|exp(−(x−μ1)⊤Σ−1(x−μ1)2)π1 =1√(2π)d|Σ|exp(−(x−μ2)⊤Σ−1(x−μ2)2)π2, =exp(−(x−μ2)⊤Σ−1(x−μ2)2)π2, (a)⟹−12(x−μ1)⊤Σ−1(x−μ1)+ln(π1) =−12(x−μ2)⊤Σ−1(x−μ2)+ln(π2),

where takes natural logarithm from the sides of equation.

We can simplify this term as:

 (x−μ1)⊤Σ−1(x−μ1)=(x⊤−μ⊤1)Σ−1(x−μ1) =x⊤Σ−1x−x⊤Σ−1μ1−μ⊤1Σ−1x+μ⊤1Σ−1μ1 (a)=x⊤Σ−1x+μ⊤1Σ−1μ1−2μ⊤1Σ−1x, (19)

where is because as it is a scalar and is symmetric so . Thus, we have:

 −12x⊤Σ−1x−12μ⊤1Σ−1μ1+μ⊤1Σ−1x+ln(π1) =−12x⊤Σ−1x−12μ⊤2Σ−1μ2+μ⊤2Σ−1x+ln(π2).

Therefore, if we multiply the sides of equation by , we have:

 2(Σ−1(μ2−μ1))⊤x (20)

which is the equation of a line in the form of . Therefore, if we consider Gaussian distributions for the two classes where the covariance matrices are assumed to be equal, the decision boundary of classification is a line. Because of linearity of the decision boundary which discriminates the two classes, this method is named linear discriminant analysis.

For obtaining Eq. (20), we brought the expressions to the right side which was corresponding to the second class; therefore, if we use as the left-hand-side expression (function) in Eq. (20):

 δ(x):=2(Σ−1(μ2−μ1))⊤x (21)

the class of an instance is estimated as:

 ˆC(x)={1,if δ(x)<0,2,if δ(x)>0. (22)

If the priors of two classes are equal, i.e., , the Eq. (20) becomes:

 2(Σ−1(μ2−μ1))⊤x (23) +(μ1−μ2)⊤Σ−1(μ1−μ2)=0,

whose left-hand-side expression can be considered as in Eq. (22).

## 4 Quadratic Discriminant Analysis for Binary Classification

In Quadratic Discriminant Analysis (QDA), we relax the assumption of equality of the covariance matrices:

 Σ1≠Σ2, (24)

which means the covariances are not necessarily equal (if they are actually equal, the decision boundary will be linear and QDA reduces to LDA).

Therefore, the Eq. (17) becomes:

 1√(2π)d|Σ1|exp(−(x−μ1)⊤Σ−11(x−μ1)2)π1 =1√(2π)d|Σ2|exp(−(x−μ2)⊤Σ−12(x−μ2)2)π2, (a)⟹−d2ln(2π)−12ln(|Σ1|) −12(x−μ1)⊤Σ−11(x−μ1)+ln(π1) =−d2ln(2π)−12ln(|Σ2|) −12(x−μ2)⊤Σ−12(x−μ2)+ln(π2),

where takes natural logarithm from the sides of equation. According to Eq. (19), we have:

 −12ln(|Σ1|)−12x⊤Σ−11x−12μ⊤1Σ−11μ1 +μ⊤1Σ−11x+ln(π1) =−12ln(|Σ2|)−12x⊤Σ−12x−12μ⊤2Σ−12μ2 +μ⊤2Σ−12x+ln(π2).

Therefore, if we multiply the sides of equation by , we have:

 x⊤(Σ1−Σ2)−1x+2(Σ−12μ2−Σ−11μ1)⊤x (25) +(μ⊤1Σ−11μ1−μ⊤2Σ−12μ2)+ln(|Σ1||Σ2|) +2ln(π2π1)=0,

which is in the quadratic form . Therefore, if we consider Gaussian distributions for the two classes, the decision boundary of classification is quadratic. Because of quadratic decision boundary which discriminates the two classes, this method is named quadratic discriminant analysis.

For obtaining Eq. (25), we brought the expressions to the right side which was corresponding to the second class; therefore, if we use as the left-hand-side expression (function) in Eq. (25):

 δ(x):=x⊤(Σ1−Σ2)−1x+2(Σ−12μ2−Σ−11μ1)⊤x (26) +(μ⊤1Σ−11μ1−μ⊤2Σ−12μ2)+ln(|Σ1||Σ2|)+2ln(π2π1),

the class of an instance is estimated as the Eq. (22).

If the priors of two classes are equal, i.e., , the Eq. (20) becomes:

 x⊤(Σ1−Σ2)−1x+2(Σ−12μ2−Σ−11μ1)⊤x (27) +(μ⊤1Σ−11μ1−μ⊤2Σ−12μ2)+ln(|Σ1||Σ2|)=0,

whose left-hand-side expression can be considered as in Eq. (22).

## 5 LDA and QDA for Multi-class Classification

Now we consider multiple classes, which can be more than two, indexed by . Recall Eq. (12) or (15) where we are using the scaled posterior, i.e., . According to Eq. (16), we have:

 fk(x)πk =1√(2π)d|Σk|exp(−(x−μk)⊤Σ−1k(x−μk)2)πk.

Taking natural logarithm gives:

 ln(fk(x)πk)= −d2ln(2π)−12ln(|Σk|) −12(x−μk)⊤Σ−1k(x−μk)+ln(πk).

We drop the constant term which is the same for all classes (note that this term is multiplied before taking the logarithm). Thus, the scaled posterior of the -th class becomes:

 δk(x):= −12ln(|Σk|) (28) −12(x−μk)⊤Σ−1k(x−μk)+ln(πk).

In QDA, the class of the instance is estimated as:

 ˆC(x)=argmaxk δk(x), (29)

because it maximizes the posterior of that class. In this expression, is Eq. (28).

In LDA, we assume that the covariance matrices of the classes are equal:

 Σ1=⋯=Σ|C|=Σ. (30)

Therefore, the Eq. (28) becomes:

 δk(x)=−12ln(|Σ|) −12(x−μk)⊤Σ−1(x−μk)+ln(πk)=−12ln(|Σ|) −12x⊤Σ−1x−12μ⊤kΣ−1μk+μ⊤kΣ−1x+ln(πk).

We drop the constant terms and which are the same for all classes (note that before taking the logarithm, the term is multiplied and the term is multiplied as an exponential term). Thus, the scaled posterior of the -th class becomes:

 δk(x):=μ⊤kΣ−1x−12μ⊤kΣ−1μk+ln(πk). (31)

In LDA, the class of the instance is determined by Eq. (29), where is Eq. (31), because it maximizes the posterior of that class.

In conclusion, QDA and LDA deal with maximizing the posterior of classes but work with the likelihoods (class conditional) and priors.

## 6 Estimation of Parameters in LDA and QDA

In LDA and QDA, we have several parameters which are required in order to calculate the posteriors. These parameters are the means and the covariance matrices of classes and the priors of classes.

The priors of the classes are very tricky to calculate. It is somewhat a chicken and egg problem because we want to know the class probabilities (priors) to estimate the class of an instance but we do not have the priors and should estimate them. Usually, the prior of the -th class is estimated according to the sample size of the -th class:

 ˆπk=nkn, (32)

where and are the number of training instances in the

-th class and in total, respectively. This estimation considers Bernoulli distribution for choosing every instance out of the overall training set to be in the

-th class.

The mean of the

-th class can be estimated using the Maximum Likelihood Estimation (MLE), or Method of Moments (MOM), for the mean of a Gaussian distribution:

 Rd∋ˆμk=1nkn∑i=1xiI(C(xi)=k), (33)

where is the indicator function which is one and zero if its condition is satisfied and not satisfied, respectively.

In QDA, the covariance matrix of the -th class is estimated using MLE:

 Rd×d∋ˆΣk= (34) 1nkn∑i=1(xi−ˆμk)(xi−ˆμk)⊤I(C(xi)=k).

Or we can use the unbiased estimation of the covariance matrix:

 Rd×d∋ˆΣk= (35) 1nk−1n∑i=1(xi−ˆμk)(xi−ˆμk)⊤I(C(xi)=k).

In LDA, we assume that the covariance matrices of the classes are equal; therefore, we use the weighted average of the estimated covariance matrices as the common covariance matrix in LDA:

 Rd×d∋ˆΣ=∑|C|k=1nkˆΣk∑|C|r=1nr=∑|C|k=1nkˆΣkn, (36)

where the weights are the cardinality of the classes.

## 7 LDA and QDA are Metric Learning!

Recall Eq. (28

) which is the scaled posterior for the QDA. First, assume that the covariance matrices are all equal (as we have in LDA) and they all are the identity matrix:

 Σ1=⋯=Σ|C|=I, (37)

which means that all the classes are assumed to be spherically distributed in the dimensional space. After this assumption, the Eq. (28) becomes:

 δk(x)= −12(x−μk)⊤(x−μk)+ln(πk), (38)

because , , and . If we assume that the priors are all equal, the term is constant and can be dropped:

 δk(x)= −12(x−μk)⊤(x−μk)=−12d2k, (39)

where is the Euclidean distance from the mean of the -th class:

 dk=||x−μk||2=√(x−μk)⊤(x−μk). (40)

Thus, the QDA or LDA reduce to simple Euclidean distance from the means of classes if the covariance matrices are all identity matrix and the priors are equal. Simple distance from the mean of classes is one of the simplest classification methods where the used metric is Euclidean distance. Figure 2: The QDA and LDA where the covariance matrices are identity matrix. For equal priors, the QDA and LDA reduce to simple classification using Euclidean distance from means of classes. Changing the prior modifies the location of decision boundary where even one point can be classified differently for different priors.

The Eq. (39) has a very interesting message. We know that in metric Multi-Dimensional Scaling (MDS) (Cox & Cox, 2000) and kernel Principal Component Analysis (PCA), we have (see (Ham et al., 2004) and Chapter 2 in (Strange & Zwiggelaar, 2014)):

 K=−12HDH, (41)

where is the distance matrix whose elements are the distances between the data instances, is the kernel matrix over the data instances, is the centering matrix, and . If the elements of the distance matrix are obtained using Euclidean distance, the MDS is equivalent to Principal Component Analysis (PCA) (Jolliffe, 2011).

Comparing Eqs. (39) and (41) shows an interesting connection between the posterior of a class in QDA and the kernel over the the data instances of the class. In this comparison, the Eq. (41) should be considered for a class and not the entire data, so , , and .

Now, consider the case where still the covariance matrices are all identity matrix but the priors are not equal. In this case, we have Eq. (38). If we take an exponential (inverse of logarithm) from this expression, the becomes a scale factor (weight). This means that we still are using distance metric to measure the distance of an instance from the means of classes but we are scaling the distances by the priors of classes. If a class happens more, i.e., its prior is larger, it must have a larger posterior so we reduce the distance from the mean of its class. In other words, we move the decision boundary according to the prior of classes (see Fig. 2).

As the next step, consider a more general case where the covariance matrices are not equal as we have in QDA. We apply Singular Value Decomposition (SVD) to the covariance matrix of the

-th class:

 Σk=UkΛkU⊤k,

where the left and right matrices of singular vectors are equal because the covariance matrix is symmetric. Therefore:

 Σ−1k=UkΛ−1kU⊤k,

where

because it is an orthogonal matrix. Therefore, we can simplify the following term:

 (x−μk)⊤Σ−1k(x−μk) =(x−μk)⊤UkΛ−1kU⊤k(x−μk) =(U⊤kx−U⊤kμk)⊤Λ−1k(U⊤kx−U⊤kμk).

As is a diagonal matrix with non-negative elements (because it is covariance), we can decompose it as:

 Λ−1k=Λ−1/2kΛ−1/2k.

Therefore:

 (U⊤kx−U⊤kμk)⊤Λ−1k(U⊤kx−U⊤kμk) =(U⊤kx−U⊤kμk)⊤Λ−1/2kΛ−1/2k(U⊤kx−U⊤kμk) (a)=(Λ−1/2kU⊤kx−Λ−1/2kU⊤kμk)⊤ (Λ−1/2kU⊤kx−Λ−1/2kU⊤kμk),

where is because because it is diagonal. We define the following transformation:

 ϕk:x↦Λ−1/2kU⊤kx, (42)

which also results in the transformation of the mean: . Therefore, the Eq. (28) can be restated as:

 δk(x)=−12ln(|Σk|) (43) −12(ϕk(x)−ϕk(μk))⊤(ϕk(x)−ϕk(μk))+ln(πk).

Ignoring the terms and , we can see that the transformation has changed the covariance matrix of the class to identity matrix. Therefore, the QDA (and also LDA) can be seen as simple comparison of distances from the means of classes after applying a transformation to the data of every class. In other words, we are learning the metric using the SVD of covariance matrix of every class. Thus, LDA and QDA can be seen as metric learning (Yang & Jin, 2006; Kulis, 2013) in a perspective. Note that in metric learning, a valid distance metric is defined as (Yang & Jin, 2006):

 d2A(x,μk):=||x−μk||2A=(x−μk)⊤A(x−μk), (44)

where is a positive semi-definite matrix, i.e., . In QDA, we are also using . The covariance matrix is positive semi-definite according to the characteristics of covariance matrix. Moreover, according to characteristics of a positive semi-definite matrix, the inverse of a positive semi-definite matrix is positive semi-definite so . Therefore, QDA is using metric learning (and as will be discussed in next section, it can be seen as a manifold learning method, too).

It is also noteworthy that the QDA and LDA can also be seen as Mahalanobis distance (McLachlan, 1999; De Maesschalck et al., 2000) which is also a metric:

 d2M(x,μ):=||x−μ||2M=(x−μ)⊤Σ−1(x−μ), (45)

where is the covariance matrix of the cloud of data whose mean is

. The intuition of Mahalanobis distance is that if we have several data clouds (e.g., classes), the distance from the class with larger variance should be scaled down because that class is taking more of the space so it is more probable to happen. The scaling down shows in the inverse of covariance matrix. Comparing

in QDA or LDA with Eq. (45) shows that QDA and LDA are sort of using Mahalanobis distance.

## 8 Lda ?≡ Fda

In the previous section, we saw that LDA and QDA can be seen as metric learning. We know that metric learning can be seen as a family of manifold learning methods. We briefly explain the reason of this assertion: As , we can say . Therefore, Eq. (44) becomes:

 ||x−μk||2A =(x−μk)⊤UU⊤(x−μk) =(U⊤x−U⊤μk)⊤(U⊤x−U⊤μk),

which means that metric learning can be seen as comparison of simple Euclidean distances after the transformation which is a projection into a subspace with projection matrix . Thus, metric learning is a manifold learning approach. This gives a hint that the Fisher Discriminant Analysis (FDA) (Fisher, 1936; Welling, 2005), which is a manifold learning approach (Tharwat et al., 2017), might have a connection to LDA; especially, because the names FDA and LDA are often used interchangeably in the literature. Actually, other names of FDA are Fisher LDA (FLDA) and even LDA.

We know that if we project (transform) the data of a class using a projection vector to a dimensional subspace (), i.e.:

 x↦u⊤x, (46)

for all data instances of the class, the mean and the covariance matrix of the class are transformed as:

 μ↦u⊤μ, (47) Σ↦u⊤Σu, (48)

because of characteristics of mean and variance.

The Fisher criterion (Xu & Lu, 2006) is the ratio of the between-class variance, , and within-class variance, :

 f:=σ2bσ2w=(u⊤μ2−u⊤μ1)2u⊤Σ2u+u⊤Σ1u=(u⊤(μ2−μ1))2u⊤(Σ2+Σ1)u. (49)

The FDA maximizes the Fisher criterion:

 maximizeu (u⊤(μ2−μ1))2u⊤(Σ2+Σ1)u, (50)

which can be restated as:

 maximizeu (u⊤(μ2−μ1))2, (51) subject to u⊤(Σ2+Σ1)u=1,

according to Rayleigh-Ritz quotient method (Croot, 2005). The Lagrangian (Boyd & Vandenberghe, 2004) is:

 L=(u⊤(μ2−μ1))2−λ(u⊤(Σ2+Σ1)u−1),

where is the Lagrange multiplier. Equating the derivative of to zero gives:

 ∂L∂u=2(μ2−μ1)2u−2λ(Σ2+Σ1)u%set=0 ⟹(μ2−μ1)2u=λ(Σ2+Σ1)u,

which is a generalized eigenvalue problem

according to (Ghojogh et al., 2019b)

. The projection vector is the eigenvector of

; therefore, we can say:

 u∝(Σ2+Σ1)−1(μ2−μ1)2.

In LDA, the equality of covariance matrices is assumed. Thus, according to Eq. (18), we can say:

 u∝(2Σ)−1(μ2−μ1)2∝Σ−1(μ2−μ1)2. (52)

According to Eq. (46), we have:

 u⊤x∝(Σ−1(μ2−μ1)2)⊤x. (53)

Comparing Eq. (53) with Eq. (23) shows that LDA and FDA are equivalent up to a scaling factor (note that this term is multiplied as an exponential factor before taking logarithm to obtain Eq. (23), so this term a scaling factor). Hence, we can say:

 LDA≡FDA. (54)

In other words, FDA projects into a subspace. On the other hand, according to Section 7, LDA can be seen as a metric learning with a subspace where the Euclidean distance is used after projecting onto that subspace. The two subspaces of FDA and LDA are the same subspace. It should be noted that in manifold (subspace) learning, the scale does not matter because all the distances scale similarly.

Note that LDA assumes one (and not several) Gaussian for every class and so does the FDA. That is why FDA faces problem for multi-modal data (Sugiyama, 2007).

## 9 Relation to Logistic Regression

According to Eqs. (16) and (32), Gaussian and Bernoulli distributions are used for likelihood (class conditional) and prior, respectively, in LDA and QDA. Thus, we are making assumptions for the likelihood and prior, although we finally work with posterior in LDA and QDA according to Eq. (15). Logistic regression (Kleinbaum et al., 2002) says why do we make assumptions on the likelihood and prior when we want to work on posterior finally. Let us make assumption directly for the posterior.

In logistic regression, first a linear function is applied to the data to have where and include the intercept. Then, logistic function is used in order to have a value in range to simulate probability. Therefore, in logistic regression, the posterior is assumed to be:

 P(C(x)|X=x) (55) =(exp(β⊤x′)1+exp(β⊤x′))C(x)(11+exp(β⊤x′))1−C(x),

where for the two classes. Logistic regression considers the coefficient as the parameter to be optimized and uses Newton’s method (Boyd & Vandenberghe, 2004) for the optimization. Therefore, in summary, logistic regression makes assumption on the posterior while LDA and QDA make assumption on likelihood and prior.

## 10 Relation to Bayes Optimal Classifier and Gaussian Naive Bayes

The Bayes classifier maximizes the posteriors of the classes (Murphy, 2012):

 ˆC(x