Principal Component Analysis
The analytical technique that will end this journey toward understanding linear algebra is the principal component analysis (PCA). But don’t worry, as I bring good news! By now, you know everything that is needed to understand this methodology. In essence, the principal component analysis is a data projection into a new set of axes, or a change of basis that occurs via a linear transformation. Mappings are not a problem for us as we are now experts in manoeuvring vectors through space. So far, we know that when a linear transformation is involved, a matrix is needed, but we still need to understand the goal of this new method to define this linear transformation.
So I will put it out there. The principal component analysis will create a new set of axes called the principal axes, where we will project the data and get these so-called principal components. These are a linear combination of the original features that will be equipped with outstanding characteristics. Characteristics which are not only uncorrelated, but the first components also capture most of the variance in the data, which makes this methodology a good technique for reducing the dimensions of complex data sets. While often seen in a dimensionality reduction context, this technique also has other applications, as these new relationships are latent variables.
Cool, we know what it does and where we can use it, so let’s define this linear transformation. The word axes was involved in the explanation, meaning we need orthogonal vectors. We also know that by definition, if we have a symmetrical positive matrix, the eigenvectors are not only orthogonal but they also have positive and real eigenvalues. Multiplying a matrix by its transpose or the other way around will result in a symmetrical matrix, and with this, we can accommodate a matrix of any size.
Let’s pause and check where we are because I am sensing that the desired result is near. We have defined PCA and we’ve concluded we need to get a new set of axes onto which we can project data. The eigenvectors cover this, but there is one piece missing. These so called components needed to reflect variance. I want to bring to your attention a specific case of matrix multiplication where we multiply AT by A. Let’s think about this operation in terms of dot products. We will end up with a symmetrical matrix where the non-diagonal entries represent how much the rows relate to the columns.
With this, we are one step away from a covariance matrix. The numbers in this particular matrix reflect how much variables vary with each other. The only thing we’re missing is a way to centre the data around the mean. Given that the covariance matrix is symmetric positive, its eigenvalues are positive and the eigenvectors are perpendicular. Considering that the eigenvalues represent a scaling factor that comes from a covariance matrix, the largest value will correspond to the highest direction of variance; therefore, the correspondent eigenvector will be the first principal component.
We can apply this same logic to each of the other vectors and then order them. The result will be the principal axes on which we can project the data to obtain the principal components. I know that’s a lot of words, but no numbers, and no equations! That’s not our style! So let’s consider an example, a fabricated one, but still, it will help us comprehend this technique. Say that we have the following set of data:
The data can represent, for example, the features of users for some online casino games. Our goal is to transform this data with principal component analysis, so the first step is to calculate a covariance matrix. For that, we need to centre the data. To do this, we can use the following equation:
|
|
(6.1) |
This means that, for each element in column j, we will subtract the mean of the same column and then divide the results by the standard deviation of this same column. If we do this to each column, we have standardised the data. So let’s start by computing each column’s mean and standard deviation:
Applying equation 6.1, the table with standardised data is as follows:
We are now ready for linear algebra, so let’s go from that ugly table into a more familiar format in which to display data, a matrix. And that is right, it will be good old matrix A! So let A be such that:
So AT is:
One more step, and we will have the desired covariance matrix. The only thing we’re missing is financial freedom, oh, oops… Sorry, my mind drifts away sometimes. I meant to say, we need to multiply AT by A and then divide by the number of entries. Shall we call the resultant of this operation matrix M?
So as expected, M is a symmetrical matrix. Where are the eigenvectors? There are several ways to get them. One is to use the eigen decomposition, so let’s start by exploring that. This numerical technique will return three matrices, two of which will have what we are after, the eigenvalues and the eigenvectors. This time I will use a computer to calculate the eigenvectors and eigenvalues. We are looking for a representation of M like:
Where P is:
And:
Right, so P is a matrix with the eigenvectors, and this will be where we find our principal axes. It happens to be already sorted by eigenvalue magnitude. On the other hand, in the matrix Σ we have all the eigenvalues. These represent what can be called the explainability of the variance, how much of the variance present in the data is ”captured” by each component. This means we can derive excellent insights from these magnitudes when we want to apply principal component analysis as a dimensionality reduction technique. For example, we could percentage the eigenvalues and then understand how much variance they each explain:
|
|
(6.2) |
We have five eigenvalues, and we can sum them using 6.2, where λi represents the eigenvalues. Now, by dividing each of them by five, we find the individual percentage of variance that each distinct eigenvalue explains.
We can see that two components, namely PC1 and PC2 explain 76% of the variability. Considering the case of dimensionality reduction, let’s pick the two largest eigenvalues and transform our original data set to create the first two components for each user in the data set. Choosing the first two components will reduce the columns of P. We will call this new version Pt, and it has the following format:
The linear combinations that represent the first two components are described by the following two equations:
|
|
(6.3) |
|
|
(6.4) |
The only thing missing is to project the data into this new space. We need to transform our versions of the scaled data with the matrix Pt. For that, we can use the following equation:
|
|
(6.5) |
By performing the matrix multiplication in 6.5, we will create the first and second components for all users in the features set:
These components are also called latent or hidden variables; relationships that are hidden in the data and are the result of linear combinations. It is possible to give these some meaning (a way of interpretation), and for that, we can use the expressions that define them, as shown in 6.3 and 6.4 that define them.
I want to point out that this step is dependent on the business problem and what you think makes most sense. This is probably one of the only times when mathematics is similar to poetry. Some will disagree with me and say that equations are pure poetry, and whilst I may not have reached that level of sensitivity, I do wonder what it’s like to live with that state of mind. Maybe it is excellent.
Anyway, back to our example. So, let’s say that principal component number one could be intensity. The higher positive coefficients in equation 6.3 are those that correspond to totalBets, totalWon, and averageBetSize. The second principal component is harder to attribute a meaning to, but risk exposure could make sense as these players have significant losses:
Seeing as we reduced the data from five dimensions to two and we attributed some meaning to the new components, we can plot these new coordinates and see if anything interesting comes from it:
This is somehow an interesting perspective in the context of gambling. Some players like to risk their balance but play in a less intense session. We can also observe a case of a player that enjoys risk and intensity. These hidden variables are excellent indicators for situations where human emotion is involved, as they can capture some of that temperament.
There are other methods that can be leveraged to get to the principal components. While we have so far explored the eigen decomposition, it is also possible to calculate these metrics via the single value decomposition. We defined M as a covariance matrix while A was the matrix with entries that are a standardised version of the data in the original features table. So, M can be defined by the following equation:
|
|
(6.6) |
In equation 6.6, N is the number of rows in matrix A. Now, say that, instead of performing an eigen decomposition on M, we choose to use the single value decomposition and we do it on matrix A. It follows that:
Substituting this information into equation 6.6 it follows that:
We can manipulate that equation a little bit:
Finally:
|
|
(6.7) |
From here, we can conclude that the right singular vectors are the principal directions. Following the same logic, just as we did with the eigen decomposition, we will obtain the principal components by:
And A is equal to UΣV T, so:
Which means that:
So the principal components are given by UΣ where the entries of Σ are calculated by:
Conclusion
PCA is a crucial tool for dimensionality reduction, helping to simplify data analysis by transforming high-dimensional data into a lower-dimensional space while preserving as much variance as possible. Understanding and applying PCA enables effective data reduction and visualization, providing valuable insights into complex datasets.