Use Python to build a beta hedged portfolio to concentrate alpha

By PyQuant News

Published: 2026-02-28

URL: https://pyquantnews.substack.com/p/seeking-alpha


Each week, I send out one Python tutorial to help you get started with algorithmic trading, market data analysis, and quant finance. Upgrade to a paid plan to access the code notebooks.


In today’s post, you’ll estimate alpha and beta with a simple regression and build a beta-hedged return series.

Alpha and beta in plain terms

Beta hedging removes the part of your returns explained by the market, so you can measure and trade what is specific to you.

This idea comes from early portfolio theory and was formalized in the CAPM work of Sharpe and others. It stuck because the decomposition is simple and it matches what desks observe, markets have shared risk that dominates short horizons.

How pros actually use it

Modern quant teams still start with this same regression view, then operationalize it inside their research and risk pipeline.

A typical workflow pulls daily returns for a book and a benchmark (often SPY, a sector ETF, or a futures contract), then runs a rolling regression to estimate time-varying beta. Risk teams use that beta to set hedges and limits, and systematic strategies use it to keep exposures stable so the backtest is not just “long the market with extra steps.” In Python, this is usually a small block of code using pandas returns and an OLS fit from statsmodels, then a second series constructed as asset return minus beta times benchmark return.

Why this matters early

If you learn beta hedging early, you stop guessing why your strategy worked and you start measuring what drove returns. You also catch common beginner mistakes faster, like thinking you found alpha when you really found leverage to SPY.

Let’s see how it works with Python.

This Substack is reader-supported. To get the code notebooks, consider becoming a free or paid subscriber.

Library installation

Install the third-party libraries we rely on so this notebook can pull market data, run a regression, and plot results end-to-end.

If you are running this outside a notebook (or in a locked-down environment), you may need to install packages in your project environment instead of using pip magic. If pip fails due to SSL or compiler issues, use your platform’s package manager or a prebuilt environment like Anaconda.

Imports and setup

We use numpy for numeric helpers, matplotlib for plotting, statsmodels for OLS regression (alpha/beta), yfinance to download TSLA and SPY prices, and statsmodels.regression for the specific OLS model class.

Pull daily price history for the asset (TSLA) and the market proxy (SPY) so we can measure how much TSLA moves with the market.

Using SPY as the benchmark makes “market background noise” concrete, which is the whole point of beta: quantify the shared risk. In real research, you’d be deliberate about the benchmark choice because beta is always “relative to something.”

Convert prices into daily returns, which is the right scale for a linear beta model and makes the regression interpretable as “percent move vs percent move.”

Beginners often regress prices by mistake and get misleading relationships driven by trends. Returns also keep the hedge logic consistent, because hedging is about offsetting day-to-day PnL swings, not matching price levels.

Estimate alpha and beta regression

Wrap the OLS fit into a small helper so we can reuse the exact same alpha/beta estimation on both the raw asset and the hedged portfolio.

This regression is the “separate market vs stock-specific” step: beta is the slope on SPY, and alpha is what’s left after accounting for SPY’s move. Even if we never trade the hedge, this is how we check whether our strategy is real signal or just disguised market exposure.

Fit the model on TSLA vs SPY so we can see how much of TSLA’s daily movement is explained by the market.

A high beta means “good TSLA picks” can still lose money on broad risk-off days, because the market component dominates short horizons. Printing alpha and beta makes that exposure visible instead of guessing from PnL.

Build a beta-hedged return series

Construct a simple hedge by shorting beta units of SPY against one unit of TSLA, then re-estimate beta to confirm the market exposure was actually removed.

This is the key workflow move: the hedged series is the part of returns that’s less explained by the market, which is closer to what we mean by “alpha.” In practice, desks often do this on a rolling window because beta changes through time, but the one-shot version teaches the core idea cleanly.

Visualize exposure and hedge effect

Plot return series and the regression line so we can see both time-domain behavior (daily swings) and cross-sectional behavior (slope vs SPY) in one place.

And the plot.

The results look something like this.

Now generate a scatter plot and the linear relationship.

The results look something like this.

And finally, the hedged portfolio.

The results look something like this.

The scatter plot gives an intuitive beta picture: the tighter and steeper the cloud, the more “market-driven” the asset is over this period. Overlaying the hedged series against TSLA and SPY helps us verify the hedge is doing what we intended—reducing co-movement—so we can evaluate skill without being fooled by broad market moves.

Your next steps

You can now estimate alpha and beta with a simple regression and build a beta-hedged return series that strips out broad market moves. That makes your backtests and live PnL easier to interpret, because you can separate market exposure from stock-specific signal and hedge the risk you didn’t mean to take.