How to isolate alpha with principal component analysis

December 10, 2022
Facebook logo.
Twitter logo.
LinkedIn logo.
Get this code in Google Colab

How to isolate alpha with principal component analysis

How to isolate alpha with principal component analysis

In today’s issue, I’m going to show you how to use principal component analysis (PCA) to analyze a portfolio of stocks.

PCA is used widely in data science. It’s a way to reduce the number of dimensions in a data set. In a stock portfolio, a dimension might be a column of returns for one of the stocks. In a portfolio of 100 stocks, there are 100 dimensions. PCA converts those 100 dimensions into the few that explain the most variance in the data. In other words, the dimensions hold the most information about the data.

Quants use PCA in many ways

PCA isolates the statistical return drivers of a portfolio. These drivers are called “alpha factors” (or just factors) because they create returns that are not explained by a benchmark. (In a previous newsletter issue, you learned how to hedge beta to get exposure to alpha.) Quants use factors in trading strategies. First, they isolate the components. Then they buy the stocks with the largest exposure to a factor and sell the stocks with the smallest exposure to a factor. Today you’ll learn how to do this in Python.

By reading this issues, you’ll be able to:

  1. Get stock data
  2. Fit a PCA model
  3. Visualize the components
  4. Isolate the alpha factors

Let's go!

Step 1: Get the data

Start by importing the libraries. sklearn is a package used to build statistical models for data analysis.

1import yfinance as yf
2
3import pandas as pd
4import numpy as np
5from sklearn.decomposition import PCA
6import matplotlib.pyplot as plt

Next, build a portfolio. You can pick any stocks you want.

1symbols = [
2    'IBM',
3    'MSFT',
4    'META',
5    'INTC',
6    'NEM',
7    'AU',
8    'AEM',
9    'GFI'
10]
11data = yf.download(symbols, start="2020-01-01", end="2022-11-30")
12portfolio_returns = data['Adj Close'].pct_change().dropna()

Step 2: Fit a PCA model

sklearn makes it easy to fit a PCA model and get the components.

1pca = PCA(n_components=3)
2pca.fit(portfolio_returns)

The n_components argument tells sklearn how many of the top components to return. Fit the model with the portfolio returns and the algorithm will look for the top three components that explain most of the variance in the returns.

1pct = pca.explained_variance_ratio_
2pca_components = pca.components_

After you fit the model, grab the explained variance and components (remember the underscore).

Step 3: Visualize the components

If the description of PCA is unclear, these charts should help.

1cum_pct = np.cumsum(pct)
2x = np.arange(1,len(pct)+1,1)
3
4plt.subplot(1, 2, 1)
5plt.bar(x, pct * 100, align="center")
6plt.title('Contribution (%)')
7plt.xlabel('Component')
8plt.xticks(x)
9plt.xlim([0, 4])
10plt.ylim([0, 100])
11
12plt.subplot(1, 2, 2)
13plt.plot(x, cum_pct * 100, 'ro-')
14plt.title('Cumulative contribution (%)')
15plt.xlabel('Component')
16plt.xticks(x)
17plt.xlim([0, 4])
18plt.ylim([0, 100])
PQN #022: How to isolate alpha with principal component analysis

The chart on the left shows the contribution of the top three components toward the total variance. In other words, these components contribute the most to the information in the data. The first component explains 54.9% of the variation in the portfolio returns. In stock portfolios, this is usually driven by the overall market movement.

The chart on the right is the cumulative sum of the contribution of each component. It shows the top three components explain 84.7% of total portfolio returns. There’s no magic cumulative contribution but two or three components explaining close to 85% of the contribution is a good result.

So what does this actually mean?

Step 4: Isolate the alpha factors

There are forces that move stock prices that we can't see. These latent factors are picked up through PCA and isolated as the principal components. The overall stock market is usually a strong driver of returns. Macroeconomic forces like interest rates and the pandemic drive returns, too. PCA lets you isolate these statistical factors to get an idea of how much the portfolio’s returns come from these unobserved features.

Let’s take a look.

1X = np.asarray(portfolio_returns)
2
3factor_returns = X.dot(pca_components.T)
4
5factor_returns = pd.DataFrame(
6    columns=["f1", "f2", "f3"], 
7    index=portfolio_returns.index,
8    data=factor_returns
9)
10
11factor_returns.head()

First, muliply the portfolio returns by the principle components. The dot function makes sure every return is multiplied by each of the components. the T function transposes the DataFrame. The resulting DataFrame gives you how much of that day’s portfolio return is a result of each of the three factors.

Similar stocks will be driven by similar factors. PCA makes it easy to visualize this.

1factor_exposures = pd.DataFrame(
2    index=["f1", "f2", "f3"], 
3    columns=portfolio_returns.columns,
4    data = pca_components
5).T
6
7factor_exposures.f1.sort_values().plot.bar()
PQN #022: How to isolate alpha with principal component analysis

Analyze the first component. Create a DataFrame with the components, grab the first factor, sort stocks by the component, and plot them. You can see the tech stocks and the gold stocks clustered together. This component is more strongly driving the gold stocks. Can you guess what it is? (Hint: What major global event took place between January 2020 through November 2022?)

Another great way to visualize this is with a scatter plot.

1labels = factor_exposures.index
2data = factor_exposures.values
3plt.scatter(data[:, 0], data[:, 1])
4plt.xlabel('factor exposure of PC1')
5plt.ylabel('factor exposure of PC2')
6
7for label, x, y in zip(labels, data[:, 0], data[:, 1]):
8    plt.annotate(
9        label,
10        xy=(x, y), 
11        xytext=(-20, 20),
12        textcoords='offset points',
13        arrowprops=dict(arrowstyle='->', connectionstyle='arc3,rad=0')
PQN #022: How to isolate alpha with principal component analysis

You can see how the stocks are driven by the first two principal components. Gold stocks were largely driven by factor one and tech stocks were negatively driven by factor two.

This analysis covered the time period during covid. Gold stocks were bid up strongly as a hedge against inflation and uncertainty. You might consider the first factor as a “covid factor” representing uncertainty across the market. Tech stocks crashed as worries of economic health began.

Well, that's it for today. I hope you enjoyed it.

See you again next week.

Man with glasses and a wristwatch, wearing a white shirt, looking thoughtfully at a laptop with a data screen in the background.