BLOG

Behind the numbers.

Code examples, market analysis, and data quality deep-dives.

Is AI Capex Paying Back Fast Enough? Revenue Hurdle Forecasting in Python
Could Shorter AI Asset Lives Hit Earnings? Depreciation Stress Test in Python
How Much AI Capex Risk Can a Portfolio Remove? Constrained Optimization in Python
Is the AI Capex Trade Crowded? Rolling Volatility and Sector Rotation in Python
Did the AI Boom Come From Existing S&P 500 Members? Point-in-Time Momentum Test in Python
Is AI Revenue Circular? Customer-Vendor Capex Loop Analysis in Python
Is the AI Trade Connected to Private Credit? Rolling Correlation Network in Python
Is Apollo More Balance-Sheet Sensitive Than Peers? Leverage Screen in Python
Are AI Earnings Supported by Cash Flow? Accrual and Capex Screen in Python
Can Defensive Stocks Hedge AI Drawdowns? Basket Regime Test in Python
How Fast Does the Market Price In Fed Decisions? FOMC Event Study in Python
How Much Are Options Sellers Overpaid? The Variance Risk Premium in Python
Which Companies Have the Worst Earnings Quality? Sloan Accrual Screen with Geographic Revenue Data in Python
Does the Oil-to-Gold Ratio Signal Recessions? XLE/GLD Backtest in Python
Is AI Spending Crowding Out Free Cash Flow? Capex Sustainability Across the Mag 7 in Python
Does a Long Energy / Short Bonds Portfolio Capture Inflation Surprises? Factor Construction in Python
Can a Hidden Markov Model Detect Oil Market Regimes? HMM Analysis in Python
Do Grain Prices Predict Food Inflation? Granger Causality Test in Python
Does the Corporate Credit Spread Predict Stock Market Crashes? BAA-AAA Spread Analysis in Python
Do Oil Stocks Hedge Inflation? Rolling Beta Analysis in Python
Which Stocks Are Most Rate-Sensitive? Equity Duration via Bond Beta in Python
Which Companies Have the Highest Accrual Ratios? Earnings Quality Screening in Python
Is Alpha Persistent or Decaying? Rolling Sharpe Ratio Analysis in Python
Are Markets Trending or Mean-Reverting? Hurst Exponent Analysis in Python
Is Consumer Discretionary vs Staples a Leading Indicator? XLY/XLP Ratio Analysis in Python
Does Heavy Capex Predict Future Stock Returns? Capital Expenditure Analysis in Python
How to Estimate Cost of Equity Using CAPM in Python
Is Volatility Predictable? Testing for Volatility Clustering in Python
Which Industrials Are Overleveraged? Net Debt to EBITDA Screening in Python
GM Before and After Bankruptcy: Why Entity Resolution Matters for Financial Data
What Is Adjusted Beta? Merrill Lynch Beta Shrinkage in Python
How Good Is a Stock Pick? Information Ratio and Tracking Error in Python
Do Stock Returns Follow a Normal Distribution? Testing for Fat Tails in Python
Which Large Caps Have the Highest Free Cash Flow Yield? FCF Screening in Python
Which Sectors Won Over 5 Years? Sector Rotation Analysis in Python
How to Forecast Stock Volatility with GARCH Models in Python
Are Stock Prices Mean-Reverting? Augmented Dickey-Fuller Test in Python
How to Calculate CAPM Alpha and Beta with Regression in Python
How to Compare Sector Sharpe Ratios and Sortino Ratios in Python
DELL: Why Stitching Historical Price Data Together Is Wrong
How to Analyze Drawdown and Recovery for Bank Stocks in Python
How to Screen SaaS Stocks by Revenue Growth and Cash Flow in Python
How to Screen REITs by Dividend Yield and Valuation in Python
How Correlated Are the Magnificent 7? Intra-Group Correlation in Python
AAPL vs XOM: Do Individual Stocks Have Seasonal Patterns?
How to Rank Large-Cap Stocks by Momentum in Python
How to Build a Multi-Endpoint Financial Dashboard in Python
How to Compare Volatility Across Energy Stocks in Python
How to Screen Healthcare Stocks by Valuation in Python
How to Build a Sector Correlation Matrix for Portfolio Diversification in Python
How to Find Oversold and Overbought Stocks Using Z-Scores in Python
How to Measure Earnings Quality: Cash Flow vs Net Income in Python
How to Build a Multi-Factor Stock Screen in Python (Value + Momentum + Quality)
How to Build a Simple DCF Model for Any Stock in Python
How to Screen Tech Stocks by Revenue Growth in Python
How to Screen Stocks by Balance Sheet Health in Python
Is "Sell in May" Real? SPY Monthly Seasonality Over 10 Years
How to Compare Sector Performance YTD Using Python
How to Track S&P 500 Additions and Removals Over Time in Python
How to Screen Dividend Stocks by Yield and Quality in Python
How to Calculate Max Drawdown and Recovery Time for Any Stock in Python
How to Compare Profitability Across Mega-Cap Tech Stocks in Python
Why Ticker Symbols Are Unreliable: The Recycling Problem Every Quant Should Know
How to Calculate and Compare Stock Volatility in Python
How to Screen Blue-Chip Stocks by P/E Ratio in Python
How to Track Companies Through Ticker Changes, Bankruptcies, and Renames in Python
S&P 500 Turnover: How Much the Index Has Changed Since 2010
How to Calculate Stock Beta and Correlation in Python
← All articles

How to Calculate CAPM Alpha and Beta with Regression in Python

What’s the question?

The Capital Asset Pricing Model (CAPM) predicts that a stock’s expected return is a linear function of its sensitivity to the overall market. Beta measures that sensitivity: a beta of 1.5 means the stock is expected to move 1.5% for every 1% move in the market. Alpha is the residual — the portion of a stock’s return that CAPM cannot explain through market exposure alone. Positive alpha means the stock outperformed what its market sensitivity would predict; negative alpha means it lagged. The question is: for a set of major U.S. stocks, how much of their return is explained by market exposure (beta), and how much is genuine idiosyncratic outperformance or underperformance (alpha)?

The approach

CAPM is estimated via ordinary least squares (OLS) regression. For each stock, daily excess returns (stock return minus the risk-free rate) are regressed against the market’s daily excess returns (SPY return minus the risk-free rate). The slope of the regression line is beta, the intercept is daily alpha (annualized by multiplying by 252 trading days), and R-squared measures the fraction of the stock’s return variance explained by market movements. A 5% annualized risk-free rate is assumed, consistent with prevailing Treasury yields.

Eight stocks spanning multiple sectors are tested: AAPL, MSFT, NVDA, AMZN, META, XOM, JNJ, and JPM. The interpretation threshold is set at +/-5% annualized alpha: above +5% is classified as OUTPERFORM, below -5% as UNDERPERFORM, and within the range as FAIR.

import xfinlink as xfl
import pandas as pd
import numpy as np
from scipy import stats

xfl.set_api_key("your_key")  # free at https://xfinlink.com/signup

RF_DAILY = 0.05 / 252
tickers = ["AAPL", "MSFT", "NVDA", "AMZN", "META", "XOM", "JNJ", "JPM"]
df = xfl.prices(tickers + ["SPY"], period="1y", fields=["return_daily"])
returns = df.pivot_table(index="date", columns="ticker", values="return_daily").dropna()
market_excess = returns["SPY"] - RF_DAILY

for ticker in tickers:
    stock_excess = returns[ticker] - RF_DAILY
    slope, intercept, r_value, p_value, std_err = stats.linregress(market_excess, stock_excess)
    alpha_ann = intercept * 252
    print(f"{ticker}: beta={slope:.2f} alpha={alpha_ann:+.1%} R2={r_value**2:.3f}")

Output:

=== CAPM Regression: Alpha, Beta, R² (1Y vs SPY, Rf=5%) ===

Ticker    Beta   Alpha (ann)      R²   Ann Return        Interpretation
----------------------------------------------------------------------
JNJ       0.06       +39.4%   0.002      +55.5%            OUTPERFORM
XOM      -0.25       +35.3%   0.017      +38.7%            OUTPERFORM
NVDA      1.75       +22.3%   0.397      +74.2%            OUTPERFORM
AAPL      0.98       +12.5%   0.278      +40.6%            OUTPERFORM
AMZN      1.43        -3.7%   0.341      +27.9%                  FAIR
JPM       0.99        -8.6%   0.318      +14.2%          UNDERPERFORM
MSFT      0.87       -29.3%   0.198       -9.8%          UNDERPERFORM
META      1.47       -33.7%   0.261       -6.0%          UNDERPERFORM

What this tells us

JNJ is the most striking result in this analysis. It delivered +55.5% with a beta of 0.06 and an R² of 0.002 — meaning virtually none of its return is explained by market movements. Its +39.4% alpha is almost entirely idiosyncratic, driven by company-specific factors (product approvals, litigation outcomes, spin-off activity) rather than broad market direction. For practical purposes, JNJ behaved as a market-neutral stock this year.

XOM’s negative beta (-0.25) indicates that energy moved inversely to the broader market during this period. Despite this contrarian behavior, XOM still delivered +38.7% in absolute returns, producing +35.3% in alpha. A negative-beta stock with positive alpha is the theoretical ideal for portfolio diversification: it contributes returns while hedging market drawdowns.

Nvidia’s alpha of +22.3% is notable because it exists on top of already-high market exposure (beta of 1.75). NVDA returned +74.2% for the year; CAPM attributes approximately 52 percentage points of that to its amplified market sensitivity, leaving 22.3% as genuine outperformance. Its R² of 0.397 is the highest in the group, confirming that market exposure is a meaningful driver of NVDA returns — but not the only one.

META and MSFT show large negative alphas (-33.7% and -29.3%), indicating significant underperformance relative to what their market betas would predict. META’s beta of 1.47 implies it should have captured amplified market gains, yet it returned -6.0% — a substantial shortfall that reflects company-specific headwinds.

The low R² values across the board (ranging from 0.002 to 0.397) indicate that the single-factor CAPM explains a minority of these stocks’ return variance. Multi-factor models (Fama-French three-factor, Carhart four-factor) would likely explain more of the variance by incorporating size, value, and momentum factors.

So what?

Alpha and beta decomposition separates skill (or luck) from market exposure. A portfolio manager who delivered +74% by holding NVDA at 1.75x beta took substantially more market risk than one who delivered +55% with JNJ at 0.06x beta. The alpha metric makes this comparison explicit. When evaluating stock performance or portfolio manager returns, always decompose the total return into its beta-driven and alpha-driven components — raw returns alone conflate market participation with genuine outperformance.

Built with xfinlink — free financial data API for Python. pip install xfinlink
← All articles