Principle Component Analysis of Stock Market and Factor Model with R

Introduction

Principle Component Analysis is often used as a tool of dimension reduction and helps visualize high dimension data set. It generates a set of factors that linearly explain a large portion of total variation of data.

In quantitative finance, PCA has interesting applications. For example, when using PCA to decompose return of future contract or fix income security with different term to maturity, the first principle component resembles the level of overall contract, the second resembles the slope and the third is very similar to the curvature.

In addition, PCA is closely related to factor analysis, whose idea is very similar to the factor model in finance. The return in factor model is driven by orthogonal factors, just like what PCA does.

The estimation of covariance matrix has always been one of the top topics in quantitative asset management. Factor model provides a way to restrict the covariance matrix of stocks to factors, thus reducing noise of estimation. The simplest way to test if a factor model works is to backtest if the Global Minimum Variance Portfolio estimated by factor model is smaller than before.

In this project I will first review some PCA theories and related linear algebras, then, its application in stock market and the construction of Orthogonal factor model. Later on, I will get things more serious and do a backtest to see if this factor model works.

Suppose our data has observations and stocks, so the covariance matrix of stocks should be a matrix. We have: in which P is columns of orthogonal eigenvectors, a matrix. is a diagonal matrix of eigenvalues.

Suppose Then:

In finance words, eigenvector contains columns of weights. For each weight , we construct a new portfolio called eigen-portfolios from , so now we have eigen-portfolios instead of stocks. Because has a diagonal covariance matrix, the eigen-portfolios are uncorrelated with each other and have variance

The eigenvectors are constructed in a way to keep as more variance as possible. As a result, the first few eigen-portfolios contains a great amount of fraction of total variance.

To start PCA, we need to normalize data by subtracting the mean and dividing by standard diviation. This will make our result not affacted by the scale of data.

Different PCs in the plot are actually trying to capture different kinds of variation. PC1 is capturing all variation as much as possible so it is not surprising that most time PC1 behave like a equal weight portfolio (at least compared with other portfolios).

The remaining PCs are trying to capture variantion that orthogonal to PC1, which means they are uncorrelated with the equal-weighted portfolio.

As a result, we can invest in P uncorrelated eigen-portfolios instead of P correlated stocks. In such way, it is much easier to make investment decision.

After transfering to return of portfolios to price, this is how them look like:

Grey lines are prices of actual 48 industries portfolios.

We can also check how many eigen-portfolios represent how much variance by

The first 4 principle components explain almost 70% of total variance! That is a lot, since all portfolios are from different industries that are not closely correlated. For example, if we use future contracts with different term to maturity here, the first 4 PCs can explain more than 99% of total variance.

Orthogonal Factor Model

Below are some properties of orthogonal multi-factor model:

Intuitively speaking, the returns are driven by a constant mean and multiple factors and idiosyncratic random term. The factors are uncorrelated to each other, which means the returns are driven by independent factors. The idiosyncratic terms could be related to each other. For example, a market shock impact stocks in the same industries.

The factors are unobservable or unknown. They could be the SMB or HML factors from Fama-French model, or could be other macroeconomics factors as well, so we use PCA to estimate factors from data.

By definition:

If we keep all eigen-vectors, which is to take . Then:

In this model, all the variance from data is explained and the noise is 0, the numbers of factor are equal to numbers of stock. Apparently this is useless because we highly over-fit the model.

However, if choose the first m Principle Components that explain most of variance to be the factor and the remaining factors become part of noise , the model become legit.

Backtesting

In the previous section, the main purpose is to show what an orthogonal factor model looks like.

From now on, let’s make it more serious and see if the model can indeed helps us construct a more efficient portfolio.

The idea is simple. Remember the purpose of our factor model is to reduce the estimation error in covariance matrix. We can calculate the minimum variance portfolio using in sample data first with simple estimated covariance matrix then with factor constrained covariance matrix and compare their out of sample result. If factor constrained covariance matrix indeed outperform simply estimation, the model is proved to improve estimation accuracy.