Alpha in Python
The excess return of a strategy above what is explained by its exposure to market risk (Beta).
Definition
Alpha (α) is the holy grail of quantitative finance — it represents the portion of a strategy's return that cannot be attributed to passive market exposure. Formally derived from Jensen's Alpha (1968), it is the intercept of a linear regression of portfolio returns against benchmark returns. A positive alpha means the strategy generates genuine skill-based edge beyond riding market tailwinds. It is the definitive answer to whether a quant strategy earns its fees or is merely a disguised, expensive index fund.
Quantitative Formula
Where is the portfolio return, is the risk-free rate, is the portfolio's sensitivity to the benchmark, and is the benchmark return. The term represents the return that can be explained purely by market exposure. Alpha is what remains after subtracting this systematic component.
Why It Matters in Backtesting
A backtest that reports 35% annualized returns during a period where the S&P 500 returned 28% and the strategy had a beta of 1.2 has actually produced negative alpha of -1.6%. This is one of the most common illusions in naive backtesting. Every quantitative backtest must decompose total returns into alpha and beta components before drawing any conclusions about genuine edge — a non-negative alpha is the minimum bar for a deployable strategy.
Python Implementation
import numpy as np
import pandas as pd
from scipy import stats
def calculate_alpha(portfolio_returns: pd.Series, benchmark_returns: pd.Series,
risk_free_rate: float = 0.02, trading_days: int = 252) -> dict:
"""
Calculates Jensen's Alpha and full regression diagnostics.
Both series must share the same index and represent daily returns.
"""
rf_daily = risk_free_rate / trading_days
excess_portfolio = portfolio_returns - rf_daily
excess_benchmark = benchmark_returns - rf_daily
aligned = pd.DataFrame({"portfolio": excess_portfolio, "benchmark": excess_benchmark}).dropna()
slope, intercept, r_value, p_value, std_err = stats.linregress(
aligned["benchmark"], aligned["portfolio"]
)
alpha_annualized = intercept * trading_days
return {
"alpha_annualized": alpha_annualized,
"beta": slope,
"r_squared": r_value ** 2,
"p_value": p_value,
"alpha_statistically_significant": p_value < 0.05,
"std_error": std_err
}Test this in a live environment
Stop running Jupyter notebooks locally. Paste this Alpha code directly into Valetha's Strategy Lab and run a full historical backtest in seconds.
Open the Python Strategy Lab