Classification
Linear Discriminant Analysis, LDA
2023-12-20
Linear discriminant analysis is a supervised learning method that uses labeled data to find a projection vector that separates classes. In the example below, the green and red classes overlap when projected onto line A, making it difficult to identify a decision boundary. Projecting the same data onto line P makes the boundary easier to identify.

In other words, when searching for a projection surface, the distance between the centroids of two classes should be maximized, meaning that the variance between classes should be large, while the variance within each class should be small. Mathematically, this can be expressed as finding a projection vector w = (m1, m2, ..., md, c) of d components, which performs dot product with the source parameters, X=(x1,x2,...,xd) to produce the following projected value.
Projected value: \(y=m_1x_1+m_2x_2+\cdots +m_dx_d+c\)
Our objective function to be maximize is defined by
\(\max_{w} S_B/S_W\)
Here, SB represents the variance among groups, while SW represents the variance within groups. The variance calculation should be done on the projection surface.
The among group variance, SB is defined as follows,
\(S_B=\sum _k^{\ }(\overline {X}_k-\overline{\overline{X}})^2=(\overline {X}_1-\overline{\overline X})^2+(\overline {X}_2-\overline{\overline X})^2+\cdots +(\overline {X}_d-\overline{\overline X})^2\)
where \(\overline {X}_k\) is the mean of each class, and
\(\overline{\overline X}\) is the total mean of all the samples.
The within group variance, SW is calculated per individual Class,
\(S_W=\sum _{\ }^{\ }\sum _k^{\ }(X-\overline {X}_k)^2=\sum _{k=1}^{\ }(X-\overline {X}_1)^2+\sum _{k=2}^{\ }(X-\overline {X}_2)^2+\cdots +\sum _{k=d}^{\ }(X-\overline {X}_d)^2\)
Our goal is to find the decision boundary among classes on the projection plane, and it can be achieved by preforming (1) the optimization with respect to the projection vector, w, and (2) weighted mean by the number of samples. For example, the decision boundary between Class A and B is
\(Cutoff_{AB}=(\overline {y}_An_A+\overline{y}_Bn_B)/(n_A+n_B)\) where \(\overline {y}_A, \overline {y}_B\) are the mean of Class A and Class B on the projected surface, and \(n_A, n_B\) are the sample size of Class A and Class B, respectively.
We use the Iris dataset to show the LDA classification in Excel.
(step 1) Allocate cells as the weight vectors as many as the number of the source parameter + 1 for bias, and initialize with random numbers by RAND(). In our case, since d=4, we need 5 cells as shown below.

(step 2) Perform dot product between X and w to generate projected values
= SUMPRODUCT(A2:D2,I$3:L$3)+M$3

(step 3) Calculate the mean of the projected values of each class and the sample size of each class, and total mean of all the samples.
The mean of each Class =AVERAGEIFS(O$2:O$151,$E$2:$E$151,G7)
The sample size of each Class =COUNTIFS($E$2:$E$151,G7)
Total mean = Average(O$2:O$151)

(step 4) In order to calculate the within Class variance later, first calculate the distance of each sample from the mean of the Class where it belongs to. This can be done by using nested IF statement as below, i.e.
IF(Class A, distance for Class A, IF(Class B, distance for Class B, (otherwise) distance for Class C))
=IF(E2=G$7,(O2-H$7)^2,IF(E2=G$8,(O2-H$8)^2, (O2-H$9)^2))

(step 5) Calculate SB, SW, & SB/SW
SB=(H7-H10)^2+(H8-H10)^2+(H9-H10)^2
SW=SUM(P2:P151)

(step 6) Run Solver to maximize the SB/SW with respect to w.r.t the coefficients of w.

If the Solver keep falling into a local minima, i.e. failing to produce meaningful results, it is due to the not adequate initial conditions. Please perform linear regression (y vs. X) by LINEST or Matrix Inversion Method (even though it is classification problem) and take the coefficients output as the initial guess.
(step 7) Calculate the decision boundary between Classes.
For example, the decision boundary between Class 1 & 2 on the projected surface is (H7*I7+H8*I8)/(I7+I8)

(step 8) Classification of any new sample or test data can be done by (1) projecting to the w vector (2) comparing with decision boundaries.
<Note>
AVERAGEIFS(Range1, Range2, condition) return the average of the value in Range1 from the samples which satisfy the condition in the Range2.
COUNTIFS(Range, condition) return the number of samples which satisfy the condition in the Range.