# How to calculate mahalanobis distance in SIMCA where different number of PCs are obtained for each class

I am working on a software that does SIMCA using mahalanobis distances with the following steps(excluding the classification of new objects for simplicity):

1. Center each class individually
2. Apply PCA to each class
3. Find the optimal number of PCs for each class
4. Calculate the mahalanobis distance of each sample to each class
5. Display Coomans plot

The problem: Most of the times, I am ending up with different number PCs for each class(step 3) and it doesn't allow me to calculate mahalanobis distances since the dimensions doesn't match in this case. I am currently using the minimum of that numbers which leads to nonsense results even though the data is "good".

What is the proper way of handling this issue? Or am I doing something else wrong?

Note: The paper on the subject referenced from articles that uses SIMCA and Coomans plot is missing from online libraries including the original publisher: "Potential pattern recognition in chemical and medical decision making (D. Coomans and I. Broeckaert)"

Thus, I am also looking for a resource which explains this method step by step.

• No, if I understood your code, Because I am already reducing the same number of variables to n via PCA. My problem is having different number of principle components for each class. Having multiple PCs(or your in your case, variables) is not the problem. – theGD Dec 13 '15 at 21:47

bit late to the party, but here we go:

(it is unclear which "it" does not allow you to calculate Mahalanobis distances)

SIMCA models each class individually. Mahalanobis distance in SIMCA refers to the distribution of training cases for the class in question in the PC coordinate system for that class.

Thus, you calculate Mahalanobis distance of points $X$ with respect to class B using class B rotation (loadings') including the number of PCs for that class. The Mahalanobis distance is then calculated against the scores distribution for class B training cases. Both the projected point and the scores use the same principal components (including the same number).

SIMCA Class $B$:

1. project class B training cases: yields scores $T_{B}^{(ntrainB~x~ncompB)}$ and loadings $P_B^{(ncompB~\times~p)}$
2. calculate variance-covariance-matrix $S_B^{(ncompB~\times~ncompB)} = \frac{1}{ntrainB - 1} T_B'T_B$

for prediction:

1. first project: $X_B = X^{(npred~\times~p)} P'^{(p~\times~ncompB)}$,
2. then calculate Mahalanobis distance $d_B$ of $X_B^{(npred~\times~ncompB)}$ against the class B scores $T_{B}^{(ntrainB~x~ncompB)}$

There is no conflict in the number of variates, it is $ncompB$ for both the prediction case scores as well as the trainings case scores.

As for literature, I don't think the book you refer to is the original publication.
Chapter 33 – Supervised Pattern Recognition (Handbook of Chemometrics and Qualimetrics: Part B, aka blue book) refers to Wold, S. Pattern recognition by means of disjoint principal components models, Pattern Recognition, 8, 127–139 (1976). DOI: 10.1016/0031-3203(76)90014-5

• Thank you for your detailed explanation and for providing the reference that I have been looking for. – theGD Jul 11 '16 at 10:57