Quant Science Profile picture
Jul 4 18 tweets 5 min read Read on X
Quants use principal component analysis to find alpha.

Blackrock uses it to manage $100s of billions in factor funds.

Northfield uses it to earn $10s of millions selling factors to investors.

Here’s how it’s done.

In a few lines of Python: Image
By reading this thread, you’ll be able to:

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

But first, a quick primer on PCA if you’re unfamiliar:
PCA is used in many ways including signal processing, image recognition, and of course quant finance.

PCA:

• Isolates factors that drive returns
• Explains the variance in a dataset
• Used for factor investing and risk management

Let’s dig in!
Start by importing the libraries.

sklearn is a package used to build statistical models for data analysis. Image
Next, build a portfolio. You can pick any stocks you want. Image
sklearn makes it easy to fit a PCA model and get the components.

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. Image
After you fit the model, grab the explained variance and components (remember the underscore). Image
If the description of PCA is unclear, a chart should help.

But first, build it. Image
The left shows the contribution of the components toward the total variance.

The first explains 54.9%.

This is usually driven by the overall market movement.

The right is the cumulative sum of the contribution of each component which explains 84.7% of total portfolio returns. Image
The forces that move stock prices are isolated as the principal components.

The market, interest rates, and the pandemic drive returns.

PCA lets you isolate these statistical factors to get an idea of how much the portfolio’s returns come from these unobserved features. Image
Similar stocks will be driven by similar factors.

PCA makes it easy to visualize it. Image
You can see the tech stocks and the gold stocks clustered together.

This component is more strongly driving the gold stocks.

Another great way to visualize this is with a scatter plot. Image
Another great way to visualize this is with a scatter plot. Image
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. Image
This analysis covered the time period during covid.

Gold stocks were bid up strongly as a hedge against inflation and uncertainty.

Tech stocks crashed as worries about economic health began.

These factors are hidden in the data.

PCA helps you find them.
Want to learn how to build algorithmic trading strategies in Python (that actually work)?

👉 Join us live for our free training (500 seats): learn.quantscience.io/become-a-pro-q…
That's a wrap! Over the next 24 days, I'm sharing my top 24 algorithmic trading concepts to help you get started.

If you enjoyed this thread:

1. Follow me @quantscience_ for more of these
2. RT the tweet below to share this thread with your audience
P.S. - Want to learn Algorithmic Trading Strategies that actually work?

I'm hosting a live workshop. Join here: learn.quantscience.io/qs-register

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Quant Science

Quant Science Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @quantscience_

Jul 4
🚨 BREAKING: I just stumbled upon this Machine Learning Python library for Algorithmic Trading that looks insane.

It's called AlphaPy.

This is what it does: Image
AlphaPy is a machine learning framework for both speculators and data scientists.

It is written in Python with the scikit-learn and pandas libraries, as well as many other helpful libraries for feature engineering and visualization.

Here's some of what it does:
1. Run machine learning models using scikit-learn and xgboost.

2. Create models for analyzing the markets with MarketFlow.

3. Predict sporting events with SportFlow.

4. Develop trading systems and analyze portfolios using MarketFlow and Quantopian’s pyfolio.
Read 11 tweets
Jul 1
A 37-page research paper reveals why stocks misbehave (and how hedge funds profit):

Investor Sentiment

Underreact & overreact for 4.2%+ alpha.

Here’s how: 🧵 Image
1. What Is Investor Sentiment?

Stocks underreact to single news (e.g., earnings) & overreact to trends.

A 1998 study shows it creates predictable returns. Image
2. The Data Behind the Strategy

Analyzed 1933-1986 U.S. stock data.

Underreaction gives 0.34 quarterly autocorrelation. Overreaction flips after 3-5 years
Read 10 tweets
Jun 29
How to use MACD for algorithmic trading Machine Learning.

Let's dive in. 🧵 Image
MACD (Moving Average Convergence Divergence) is most commonly used in Technical Trading.

But, it can be used as part of a factor model.

Let's see how. Image
1. What is MACD?

MACD is a trend-following momentum indicator that shows the relationship between two moving averages of a security's price.

The MACD is calculated by subtracting the long-term exponential moving average (EMA) from the short-term EMA.
Read 9 tweets
Jun 28
A 23-page research paper reveals the number 1 method Hedge Funds use to beat the market:

Time Series Momentum

This is how: 🧵 Image
1. What Is Time Series Momentum?

Time Series Momentum (TSMOM) bets on trends continuing. If a stock’s up, buy more; if down, sell. A 2011 study of 58 assets proved it works! Image
2. The Data Behind the Strategy

The TSMOM paper analyzed equities, currencies & more. T-stats showed consistent profits across 1-month lookbacks! Image
Read 9 tweets
Jun 27
How to create your own "mini" hedge fund with algorithmic trading and Python

A thread 🧵 Image
1. What is a Hedge Fund

Hedge funds pool money from wealthy individuals or institutions to seek higher, risk-adjusted returns across multiple markets.
While they often strive to outperform benchmarks like the S&P 500, the focus is usually on lowering risk (drawdowns) rather than purely maximizing returns.
Read 15 tweets
Jun 22
Mind-blowing tear sheets for your trading strategies.

1 line of Python code: Image
QuantStats performs portfolio profiling for analytics and risk metrics.

Grab it here: github.com/ranaroussi/qua…Image
1 more thing before you go...

Do you want to learn how I built an automated algorithmic trading system from scratch with Python?

I have a free workshop...
Read 6 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(