Skip to content
All library documents

Using Fama–French Factors to Benchmark a Single Stock

Notebook pyfolio

Summary

This notebook demonstrates a pyfolio workflow for examining one stock’s returns against the canonical Fama–French factors. It first plots rolling factor betas directly from the stock return series, then calculates those betas for use as benchmark returns in a Bayesian tear sheet. The example uses Facebook stock data and treats the final two months as an out-of-sample period.

The workflow drops initial missing beta values because the rolling calculation uses a trailing six-month window. The notebook describes the analysis setup but provides no numerical findings or interpretation of the plotted output. Its out-of-sample interval is a brief illustration, not evidence that the benchmark or model will generalize. The example also depends on pyfolio and its Bayesian analysis backend, and does not discuss factor-model assumptions, data quality, or how to interpret estimated betas.

Key ideas

  • Rolling Fama–French betas can summarize a stock’s changing exposure to common factors.
  • The example uses pyfolio to plot rolling betas and calculate them for a Bayesian tear sheet.
  • Initial rows are removed because the rolling estimates require a trailing data window.
  • The final two months are designated as the out-of-sample period in this illustration.
  • The notebook shows an analysis workflow but reports no conclusions about the stock’s performance.

Tags

Full text
# Fama French Benchmark Example


# Fama French Benchmark Example
In this notebook, we use pyfolio to analyze the returns of a single stock using the canonical Fama-French factors as the benchmark.

We plot the rolling betas to the Fama-French factors, and run the Bayesian tear sheet for an out-of-sample period of two months.

```python
import sys
sys.path.append('/Users/george/Desktop/pyfolio/')
sys.path
```

```python
import pyfolio as pf
import matplotlib.pyplot as plt
%matplotlib inline

# silence warnings
import warnings
warnings.filterwarnings('ignore')
```

```python
# Get the single stock returns
stock_rets = pf.utils.get_symbol_rets('FB')
```

```python
# With just the stock returns, we can plot the rolling betas to the Fama-French factors.
# No need to actually compute the rolling betas; pyfolio does that for us!
fig, ax = plt.subplots(figsize=[14, 6])
pf.plotting.plot_rolling_fama_french(stock_rets, ax=ax)
```

```python
# However, for the bayesian tear sheet, we will actually need the rolling betas,
# so use pyfolio to get them
rolling_beta = pf.timeseries.rolling_fama_french(stock_rets)

# pf.timeseries.rolling_beta defaults to a 6-month trailing window.
# Thus, the first 6 months' data will be NaNs, which we must drop
rolling_beta.dropna(inplace=True)
```

```python
rolling_beta.head()
```

```python
# Suppose the last 2 months were our out-of-sample period
out_of_sample = stock_rets.index[-60]

# Use pyfolio to run the bayesian tear sheet.
# The bayesian tear sheet's back end makes heavy use of pymc3, so there will be
# a lot of graphical output before the actual tear sheet
pf.tears.create_bayesian_tear_sheet(stock_rets, live_start_date=out_of_sample, benchmark_rets=rolling_beta)
```

```python

```
![notebook output](figures/p1_1.png)

Shown in full with attribution under the source's licence. Licence: Apache-2.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.