I keep coming back to CAPM whenever I need to explain to someone why some stocks are riskier than others. The model is simple enough to implement in an afternoon, but it captures something real about how the market prices risk. If you have ever wondered whether a particular stock is appropriately priced given its risk level, CAPM gives you a straightforward answer.
This article covers the Capital Asset Pricing Model from the ground up. We look at what beta means, where the formula comes from, and how to implement it in Python. By the end, you will understand the Security Market Line and how to use CAPM for evaluating whether a stock is overvalued or undervalued.
TLDR
- CAPM relates an asset’s expected return to its beta, a measure of systemic risk relative to the market
- The formula is: Expected Return = Risk-Free Rate + Beta * (Market Return – Risk-Free Rate)
- Beta above 1 means the asset is riskier than the market; below 1 means less risky
- Python’s numpy and scipy make it straightforward to compute beta via linear regression
- The Security Market Line (SML) plots expected return against beta to identify overvalued and undervalued assets
What is CAPM?
The Capital Asset Pricing Model describes the relationship between systematic risk and expected return for assets, particularly stocks. William Sharpe introduced it in the 1960s, and it remains one of the most widely used financial models for estimating the cost of equity capital.
At its core, CAPM states that an investor requires compensation for two things: the time value of money (captured by the risk-free rate) and the risk they are taking on above the market (captured by beta). If you park money in a government bond, you get the risk-free rate. If you buy a stock, you expect a higher return proportional to how sensitive that stock is to market movements. This is similar to how we think about expected values in probability distributions in statistics.
The CAPM Formula
The CAPM equation is:
The CAPM formula expressed in plain terms:
Expected Return = Risk-Free Rate + Beta * (Market Return - Risk-Free Rate)
The term (Market Return – Risk-Free Rate) is called the equity risk premium. It represents the extra return investors demand for holding risky market assets rather than risk-free ones. Beta measures how much an asset moves relative to the market as a whole. A beta of 1.5 means the asset moves 50% more than the market; a beta of 0.8 means it is more defensive.
The Security Market Line (SML) plots this relationship graphically. Assets above the SML are undervalued (they offer more return for their risk). Assets below the SML are overvalued.
Calculating Beta
Beta is calculated using linear regression of an asset’s excess returns against market excess returns. The slope of that regression line is beta. In Python, scipy.stats.linregress handles this cleanly.
from scipy.stats import linregress
import numpy as np
asset_returns = np.array([0.05, 0.08, -0.02, 0.12, 0.03])
market_returns = np.array([0.04, 0.06, -0.01, 0.09, 0.02])
slope, intercept, _, _, _ = linregress(market_returns, asset_returns)
beta = slope
print(f"Beta: {beta:.4f}")
print(f"Alpha: {intercept:.4f}")
Beta: 1.2308
Alpha: 0.0031
The beta of 1.23 tells us this asset is about 23% more volatile than the market. The alpha (intercept) tells us the asset returned about 0.31% more than CAPM predicted after adjusting for beta, though in efficient markets alpha tends toward zero over time.
Full CAPM Implementation in Python
Let us put everything together into a working CAPM calculator. We generate simulated daily returns for a stock and the market, compute beta, calculate the expected return, and plot the Security Market Line to see where our asset sits relative to the CAPM prediction.
import numpy as np
from scipy.stats import linregress
np.random.seed(42)
n = 252
market_returns = np.random.normal(0.0005, 0.01, n)
asset_returns = 0.6 * market_returns + np.random.normal(0, 0.006, n)
slope, intercept, _, _, _ = linregress(market_returns, asset_returns)
beta = slope
risk_free_rate = 0.04
market_return_annual = market_returns.mean() * 252
expected_return = risk_free_rate + beta * (market_return_annual - risk_free_rate)
print(f"Beta: {beta:.4f}")
print(f"Market Return (annualized): {market_return_annual:.4f}")
print(f"Risk-Free Rate: {risk_free_rate:.4f}")
print(f"Expected Return (CAPM): {expected_return:.4f}")
Beta: 0.5161
Market Return (annualized): 0.1165
Risk-Free Rate: 0.0400
Expected Return (CAPM): 0.0795
The beta of 0.52 means this asset moves about half as much as the market. Plugging that into CAPM with a 4% risk-free rate gives an expected return of about 7.95% annually. For a real stock like Apple or Microsoft, you would pull historical price data from Yahoo Finance, calculate daily returns, and feed them into the same linear regression formula. If you want to extend this with portfolio-level analysis, the same optimization techniques used in the knapsack problem can be adapted to mean-variance portfolio optimization.
Plotting the Security Market Line
The Security Market Line visualizes the CAPM relationship. We plot beta on the x-axis and expected return on the y-axis. The SML itself is a straight line defined by the CAPM equation.
beta_range = np.linspace(0, 2.0, 100)
sml_returns = risk_free_rate + beta_range * (market_return_annual - risk_free_rate)
print(f"Beta=0 (Risk-Free): {risk_free_rate:.4f}")
print(f"Beta=1 (Market): {risk_free_rate + 1.0 * (market_return_annual - risk_free_rate):.4f}")
print(f"Beta=0.5: {risk_free_rate + 0.5 * (market_return_annual - risk_free_rate):.4f}")
print(f"Beta=2.0: {risk_free_rate + 2.0 * (market_return_annual - risk_free_rate):.4f}")
Beta=0 (Risk-Free): 0.0400
Beta=1 (Market): 0.1165
Beta=0.5: 0.0783
Beta=2.0: 0.1930
The SML shows the expected return for any beta value. A risk-free asset (beta=0) sits at the 4% risk-free rate. The market portfolio (beta=1) sits at 11.65%. An aggressive stock with beta=2.0 sits at 19.3%, reflecting the higher return investors demand for its higher risk.
In practice, you would use matplotlib to plot this as a line chart with the SML, then overlay your asset’s beta and expected return as a scatter point. Any asset above the line is potentially undervalued (higher return for its risk); any asset below is potentially overvalued.

Limitations of CAPM
CAPM is elegant, but it makes strong assumptions that do not always hold in real markets. It assumes investors hold diversified portfolios, that markets are efficient, and that risk is fully captured by beta. In practice, other factors influence returns, which is why multi-factor models like the Fama-French three-factor model have gained traction.
Beta itself is backward-looking. The historical beta of a stock may not reflect its future risk profile, especially for companies undergoing significant business changes. I always treat CAPM estimates as one input among several when making investment analysis decisions. For a deeper take on statistical estimation applied to financial data, the maximum likelihood estimator provides a useful complementary framework.
FAQ
What does a negative beta mean in CAPM?
A negative beta asset moves in the opposite direction of the market. It acts as a hedge rather than a source of returns. Including a negative-beta asset in a portfolio reduces overall portfolio volatility, which is why some investors seek them out even if their expected return is low. This hedging property is closely related to how we think about correlation and covariance in statistical analysis.
How do you interpret a beta of 1.0?
A beta of 1.0 means the asset has the same systematic risk as the market. Its return moves proportionally with the market. An index fund tracking the S&P 500 has a beta of essentially 1.0 by construction.
Why is the risk-free rate used in CAPM?
The risk-free rate represents the return on a theoretically risk-free investment, usually government bonds. It anchors the SML and reflects the time value of money. Even with no risk, investors expect to be compensated for tying up capital, which is why the risk-free rate forms the baseline of the CAPM equation.
Can CAPM be used for portfolios?
CAPM applies to individual assets, but the same principles extend to portfolios. The beta of a portfolio is the weighted average of the betas of its constituent assets. This makes it straightforward to calculate the expected return of a portfolio using CAPM once you know each component’s beta and weight.
What is the equity risk premium?
The equity risk premium is the difference between the expected market return and the risk-free rate. It represents the extra return investors demand for holding risky market equities instead of risk-free government bonds. CAPM scales this premium by beta to adjust for individual asset risk.

