# 7 Capital Asset Pricing Model (CAPM)

## Overview

In this classwork, you will:

- Connect the mean-variance model to the idea of a market portfolio
- Understand the **CML** (Capital Market Line)
- Estimate a CAPM regression and interpret **alpha** and **beta**
- See why the CAPM can have **omitted variable bias**

## Simulated Data

We will simulate 120 months of returns:

- `ret_m`: overall market return
- `rf`: risk-free rate (short-term treasury bills or guaranteed bank account return over a short period)
- `z`: an omitted risk factor (size of firm, value stocks vs growth stocks, or stock momentum)
- `ret_i`: a single stock's return

```{r, message = F}
library(tidyverse)

set.seed(123)

n <- 120

data <- tibble(
  t = 1:n,
  date = seq.Date(from = as.Date("2015-01-01"), by = "month", length.out = n),
  # 3 types of shocks to impact different variables by different amounts
  macro_shock = rnorm(n, sd = .04),
  factor_shock = rnorm(n, sd = .03),
  firm_shock = rnorm(n, sd = .05),
  
  # monthly risk-free rate
  rf = rnorm(n, mean = 0.0015, sd = 0.0005),
  # market excess return with volatility
  ret_m = .006 + macro_shock + .3 * factor_shock + rnorm(n, sd = .01) + rf,
  # a second systematic factor
  z = .002 + .4 * macro_shock + factor_shock + rnorm(n, sd = 0.01),
  # true alpha, beta, and delta
  ret_i = .001 + 1.1 * ret_m + .7 * z + firm_shock
) %>%
  select(date, rf, ret_m, ret_i, z)
```

## Key Formulas

**Excess Return for the market m:**

$$\text{excess}_m = R_m - R_f$$
**CAPM regression:**

$$\text{excess}_i = \alpha + \beta \cdot \text{excess}_m + \varepsilon$$
Where $\beta$ is the sensitivity of the stock's (i) excess return to market (m) excess return.

## Question 1: `excess_m` and `excess_i`

Edit the variable definition of `data` to make two new variables: `excess_m` and `excess_i`, the return for the market $m$ and the stock $i$ in excess of the risk-free rate.

## Question 2: Visualize the CAPM

Visualize the relationship between the market excess returns (x-axis) and the stock excess returns (y-axis). Draw a scatterplot with a line of best fit (`geom_smooth(method = lm, se = FALSE)`).

```{r}

```

## Question 3: Capital Market Line

In the mean-variance framework, investors care about two things:

- Expected return
- Risk, measured by variance

When a risk-free asset exists, investors face a simple tradeoff: they can mix a guaranteed return with a risky portfolio.

The CML (Capital Market Line) describes the set of portfolios formed by combining the guaranteed return with a risky asset. On the x-axis is the portfolio risk (standard deviation), and on the y-axis is the portfolio's expected return.

Calculate: the mean and standard deviation of `ret_i` in the data set.

Then use `stat_function` to draw the CML for portfolios that are different combinations of a risk-free asset earning .15% (0.0015) guaranteed (sd = 0) and stock `i`.

```{r}
# data %>% 
#   ___

# ggplot() +
#  stat_function(fun = function(x) ____) +
#   labs(title = "Capital Market Line",
#        x = "Portfolio Risk",
#        y = "Expected Return")
```

Interpretation: as your tolerance for risk increases, you optimally shift more of your wealth into the risky asset and less into the guaranteed one, which raises your expected return.

## Question 4: the CAPM

Estimate the CAPM: `excess_i ~ excess_m`.

```{r}
# data %>%
#   ___
```

Estimate for alpha: ____; True alpha: ____
Estimate for beta: ____; True beta: ____

Given your estimate for beta:

- The stock (amplifies/dampens) market movements.
- When the market does well, the stock tends to (do very well/do very poorly).
- When the market does poorly, the stock tends to (do very well/do very poorly).

This stock is riskier than the market overall because it has **systematic risk:** risk that comes from economy-wide shocks like recessions and interest-rate changes that affect almost all assets and therefore can't be diversified away. This is in contrast with **idiosyncratic risk:** firm-specific noise like the CEO resigning or a major product launch flopping.

Alpha, on the other hand, is a comparison to a benchmark prediction. A positive alpha means the stock earned abnormally higher returns than CAPM predicts, outperforming on a risk-adjusted (beta) basis. But alpha is not a property of the stock alone; it's a property of the stock *relative to the pricing model*.

A persistent positive alpha points to:

- Markets are not fully efficient,
- The model is missing risk factors,
- Or the result is sampling noise.

Each anomaly paper we'll study is really a paper about alpha.

## Question 5: Omitted Variable Bias

In the data generating process you used, there are 3 types of shocks:

1. Macro shocks like recessions and interest rate surprises. These hit everything in the economy.
2. Factor-specific shocks like credit tightening or tech revaluations. These hit things like firm size, value, or momentum more strongly.
3. Idiosyncratic firm shock. These hit only the individual stock, like earnings surprises, lawsuits, and product launches.

Why should we expect omitted variable bias when we estimate `excess_i ~ excess_m`? Look at the variable definition of `data` closely.

If you control for `z` by adding `z` as an explanatory variable in the CAPM regression, should the omitted variable bias disappear, or will it still be present?

Estimate `excess_i ~ excess_m + z`. What is alpha and beta now? Are they closer to the true values?

```{r}
# data %>%
#   ___
```

Alpha: ____; truth: ____
Beta: ____; truth: ____

In real financial markets, returns are driven by shared economic shocks, not by perfectly isolated forces.

- Some shocks affect the entire economy and therefore move the market, many stocks, and some risk factors at the same time.
- Some shocks affect particular types of firms or strategies more strongly (for example, small firms, value stocks, or recent winners).
- Some shocks affect only an individual firm.

Because different returns load on the same underlying shocks, variables like market returns and other risk factors end up correlated, even though neither one "causes" the other directly.

This matters because when a regression omits a relevant source of risk, the estimated beta and alpha can be distorted. By simulating shared shocks, we can see omitted variable bias arise naturally, for the same reason it does in real data.

## Download this assignment



## Autograder



## Homework: Discussion Questions on DeBondt and Thaler (1985)

Find the pdf to the paper on Canvas.
Here are the [discussion questions](../discussions/debondt-thaler-discussion.qmd).


