In two previous posts, we calculated and then visualized the CAPM beta of a portfolio by fitting a simple linear model.
Today, we move beyond CAPM’s simple linear regression and explore the Fama French (FF) multi-factor model of equity risk/return. For more background, have a look at the original article published in The Journal Financial Economics, Common risk factors in the returns on stocks and bonds.
The FF model extends CAPM by regressing portfolio returns on several variables, in addition to market returns. From a general data science point of view, FF extends CAPM’s simple linear regression, where we had one independent variable, to a multiple linear regression, where we have numerous independent variables.
We are going to look at the FF 3-factor model, which tests the explanatory power of (1) market returns (same as CAPM), (2) firm size (small versus big) and (3) firm value (book to market ratio). The firm value factor is labeled as HML in FF, which stands for high-minus-low and refers to a firm’s book-to-market ratio. When we regress portfolio returns on the HML factor, we are investigating how much of the returns are the result of including stocks with a high book-to-market ratio (sometimes called the value premium, because high book-to-market stocks are called value stocks).
A large portion of this post covers importing data from the FF website and wrangling it for use with our portfolio returns. We will see that wrangling the data is conceptually easy to understand but practically time-consuming to implement. However, mashing together data from disparate sources is a necessary skill for anyone in industry that has data streams from different vendors and wants to get creative about how to use them. Once the data is wrangled, fitting the model is not time-consuming.
Today, we will be working with our usual portfolio consisting of:
+ SPY (S&amp;P500 fund) weighted 25%
+ EFA (a non-US equities fund) weighted 25%
+ IJS (a small-cap value fund) weighted 20%
+ EEM (an emerging-mkts fund) weighted 20%
+ AGG (a bond fund) weighted 10%
Before we can calculate beta for that portfolio, we need to find portfolio monthly returns, which was covered in my previous post, Introduction to Portfolio Returns. I won’t go through the logic again, but the code is here:
library(tidyquant)
library(tidyverse)
library(timetk)
library(broom)
library(glue)
symbols %
reduce(merge) %__% `colnames%
gather(asset, returns, -date) %__% group_by(asset) %__% mutate(returns = (log(returns) - log(lag(returns)))) %__% na.omit()
portfolio_returns_tq_rebalanced_monthly %
tq_portfolio(assets_col = asset, returns_col = returns,
weights = w,
col_rename = &quot;returns&quot;,
rebalance_on = &quot;months&quot;)
We will be working with one object of portfolio returns:
+ portfolio_returns_tq_rebalanced_monthly
Let’s get to it.
Importing and Wrangling the Fama French Factors
Our first task is to get the FF data and, fortunately, FF make their factor data available on the internet. We will document each step for importing and cleaning this data, to an extent that might be overkill. Frustrating for us now, but a time-saver later when we need to update this model or extend to the 5-factor case.
Have a look at the FF website. The data are packaged as zip files, so we will need to do a bit more than call read_csv(). Let’s use the tempfile() function from base R to create a variable called temp. This is where we will put the zipped file.
temp %
mutate(date = lubridate::rollback(date + months(1))) %__% head(1)
## # A tibble: 1 x 1
## date ## ## 1 1990-07-31
There are other ways we could have gotten around this issue - most notably, way back in the beginning, we could have indexed our portfolio returns to indexAt = firstof - but it was a good chance to introduce the rollback() function, and we will not always have that option. Sometimes two data sets are thrown at us and we have to wrangle them from there.
Finally, we want only the FF factor data that aligns with our portfolio data, so we filter() by the first() and last() date in our portfolio returns object.
Global_3_Factors % rename(date = X1) %__% mutate_at(vars(-date), as.numeric) %__% mutate(date = rollback(ymd(parse_date_time(date, &quot;%Y%m&quot;) + months(1)))) %__% filter(date __= first(portfolio_returns_tq_rebalanced_monthly$date) &amp; date % mutate(MKT_RF = Global_3_Factors$`Mkt-RF`/100,
SMB = Global_3_Factors$SMB/100,
HML = Global_3_Factors$HML/100,
RF = Global_3_Factors$RF/100,
R_excess = round(returns - RF, 4))
head(ff_portfolio_returns, 4)
## # A tibble: 4 x 8
## date returns `Mkt-RF` SMB HML RF MKT_RF R_excess
## ## 1 2013-01-31 0.0308 5.46 0.00140 0.0201 0. 0.0546 0.0308 ## 2 2013-02-28 -0.000870 0.100 0.00330 -0.00780 0. 0.00100 -0.000900
## 3 2013-03-31 0.0187 2.29 0.00830 -0.0203 0. 0.0229 0.0187 ## 4 2013-04-30 0.0206 3.02 -0.0121 0.00960 0. 0.0302 0.0206
We now we have one object with our portfolio returns and FF factors, and can proceed to the simplest part of our exercise from a coding perspective, and the only part that our bosses/colleagues/clients/investors will care about: the modeling and visualization.
Luckily, we can copy/paste the flow from our CAPM work, now that we have the data in a nice format. CAPM used simple linear regression, whereas FF uses multiple regression with many independent variables. Accordingly, our 3-factor FF equation is lm(R_excess ~ MKT_RF + SMB + HML.
We will make one addition to the CAPM code flow, which is to include the 95% confidence interval for our coefficients. We do that by setting tidy(model, conf.int = T, conf.level = .95).
ff_dplyr_byhand % do(model = lm(R_excess ~ MKT_RF + SMB + HML, data = .)) %__% tidy(model, conf.int = T, conf.level = .95)
ff_dplyr_byhand %__% mutate_if(is.numeric, funs(round(., 3))) %__% select(-statistic)
## term estimate std.error p.value conf.low conf.high
## 1 (Intercept) -0.001 0.001 0.195 -0.004 0.001
## 2 MKT_RF 0.894 0.036 0.000 0.823 0.965
## 3 SMB 0.056 0.076 0.458 -0.095 0.208
## 4 HML 0.030 0.061 0.629 -0.092 0.151
Our model object now contains a conf.high and conf.low column to hold our confidence interval min and max values.
We can pipe these results to ggplot() and create a scatter of coefficients with confidence intervals. I don’t want to plot the intercept so will filter it out of the code flow.
We add the confidence intervals with geom_errorbar(aes(ymin = conf.low, ymax = conf.high)).
ff_dplyr_byhand %__% mutate_if(is.numeric, funs(round(., 3))) %__%
filter(term != &quot;(Intercept)&quot;) %__% ggplot(aes(x = term, y = estimate, shape = term, color = term)) + geom_point() +
geom_errorbar(aes(ymin = conf.low, ymax = conf.high)) +
labs(title = &quot;FF 3-Factor Coefficients for Our Portfolio&quot;,
subtitle = &quot;nothing in this post is investment advice&quot;,
x = &quot;&quot;,
y = &quot;coefficient&quot;,
caption = &quot;data source: Fama French website and yahoo! Finance&quot;) +
theme_minimal() +
theme(plot.title = element_text(hjust = 0.5),
plot.subtitle = element_text(hjust = 0.5),
plot.caption = element_text(hjust = 0))
The results here are predictable because, as with CAPM, we are regressing a portfolio that contains the market on 3 factors, one of which is the market. Thus, the market factor dominates this model and the other two factors contain zero in their confidence bands.
Next time we will complicate this work by calculating FF factor coefficients for multiple funds/portfolios, examining rolling R squared’s and visualizing the results. See you then!