library(tidyverse)
library(RSQLite)
Replicating Fama-French Factors
You are reading Tidy Finance with R. You can find the equivalent chapter for the sibling Tidy Finance with Python here.
In this chapter, we provide a replication of the famous Fama-French factor portfolios. The Fama-French factor models are a cornerstone of empirical asset pricing Fama and French (2015). On top of the market factor represented by the traditional CAPM beta, the three-factor model includes the size and value factors to explain the cross section of returns. Its successor, the five-factor model, additionally includes profitability and investment as explanatory factors.
We start with the three-factor model. We already introduced the size and value factors in Value and Bivariate Sorts, and their definition remains the same: size is the SMB factor (small-minus-big) that is long small firms and short large firms. The value factor is HML (high-minus-low) and is long in high book-to-market firms and short in low book-to-market counterparts.
After the replication of the three-factor model, we move to the five factors by constructing the profitability factor RMW (robust-minus-weak) as the difference between the returns of firms with high and low operating profitability and the investment factor CMA (conservative-minus-aggressive) as the difference between firms with high versus low investment rates.
The current chapter relies on this set of R packages.
Data Preparation
We use CRSP and Compustat as data sources, as we need the same variables to compute the factors in the way Fama and French do it. Hence, there is nothing new below, and we only load data from our SQLite
-database introduced in Accessing and Managing Financial Data and WRDS, CRSP, and Compustat.1
<- dbConnect(
tidy_finance SQLite(),
"data/tidy_finance_r.sqlite",
extended_types = TRUE
)
<- tbl(tidy_finance, "crsp_monthly") |>
crsp_monthly select(
permno, gvkey, date, ret_excess,
mktcap, mktcap_lag, exchange|>
) collect()
<- tbl(tidy_finance, "compustat") |>
compustat select(gvkey, datadate, be, op, inv) |>
collect()
<- tbl(tidy_finance, "factors_ff3_monthly") |>
factors_ff3_monthly select(date, smb, hml) |>
collect()
<- tbl(tidy_finance, "factors_ff5_monthly") |>
factors_ff5_monthly select(date, smb, hml, rmw, cma) |>
collect()
Yet when we start merging our dataset for computing the premiums, there are a few differences to Value and Bivariate Sorts. First, Fama and French form their portfolios in June of year \(t\), whereby the returns of July are the first monthly return for the respective portfolio. For firm size, they consequently use the market capitalization recorded for June. It is then held constant until June of year \(t+1\).
Second, Fama and French also have a different protocol for computing the book-to-market ratio. They use market equity as of the end of year \(t - 1\) and the book equity reported in year \(t-1\), i.e., the datadate
is within the last year. Hence, the book-to-market ratio can be based on accounting information that is up to 18 months old. Market equity also does not necessarily reflect the same time point as book equity. The other sorting variables are analogously to book equity taken from year \(t-1\).
To implement all these time lags, we again employ the temporary sorting_date
-column. Notice that when we combine the information, we want to have a single observation per year and stock since we are only interested in computing the breakpoints held constant for the entire year. We ensure this by a call of distinct()
at the end of the chunk below.
<- crsp_monthly |>
size filter(month(date) == 6) |>
mutate(sorting_date = date %m+% months(1)) |>
select(permno, exchange, sorting_date, size = mktcap)
<- crsp_monthly |>
market_equity filter(month(date) == 12) |>
mutate(sorting_date = ymd(str_c(year(date) + 1, "0701)"))) |>
select(permno, gvkey, sorting_date, me = mktcap)
<- compustat |>
book_to_market mutate(sorting_date = ymd(str_c(year(datadate) + 1, "0701"))) |>
select(gvkey, sorting_date, be) |>
inner_join(market_equity, join_by(gvkey, sorting_date)) |>
mutate(bm = be / me) |>
select(permno, sorting_date, me, bm)
<- size |>
sorting_variables inner_join(
join_by(permno, sorting_date)
book_to_market, |>
) drop_na() |>
distinct(permno, sorting_date, .keep_all = TRUE)
Portfolio Sorts
Next, we construct our portfolios with an adjusted assign_portfolio()
function. Fama and French rely on NYSE-specific breakpoints to independently form two portfolios in the size dimension at the median and three portfolios in the dimension of book-to-market at the 30- and 70-percentiles. The sorts for book-to-market require an adjustment to the function in Value and Bivariate Sorts because it would not produce the right breakpoints. Instead of n_portfolios
, we now specify percentiles
, which takes the sequence of breakpoints as an object specified in the function’s call. Specifically, we give percentiles = c(0, 0.3, 0.7, 1)
to the function. Additionally, we perform an inner_join()
with our return data to ensure that we only use traded stocks when computing the breakpoints as a first step.
<- function(data,
assign_portfolio
sorting_variable,
percentiles) {<- data |>
breakpoints filter(exchange == "NYSE") |>
pull({{ sorting_variable }}) |>
quantile(
probs = percentiles,
na.rm = TRUE,
names = FALSE
)
<- data |>
assigned_portfolios mutate(portfolio = findInterval(
pick(everything()) |>
pull({{ sorting_variable }}),
breakpoints,all.inside = TRUE
|>
)) pull(portfolio)
return(assigned_portfolios)
}
<- sorting_variables |>
portfolios group_by(sorting_date) |>
mutate(
portfolio_size = assign_portfolio(
data = pick(everything()),
sorting_variable = size,
percentiles = c(0, 0.5, 1)
),portfolio_bm = assign_portfolio(
data = pick(everything()),
sorting_variable = bm,
percentiles = c(0, 0.3, 0.7, 1)
)|>
) ungroup() |>
select(permno, sorting_date,
portfolio_size, portfolio_bm)
Alternatively, you can implement the same portfolio sorts using the assign_portfolio()
function from the tidyfinance
package, which we omit to avoid repeating almost the same code chunk as above.
Next, we merge the portfolios to the return data for the rest of the year. To implement this step, we create a new column sorting_date
in our return data by setting the date to sort on to July of \(t-1\) if the month is June (of year \(t\)) or earlier or to July of year \(t\) if the month is July or later.
<- crsp_monthly |>
portfolios mutate(sorting_date = case_when(
month(date) <= 6 ~ ymd(str_c(year(date) - 1, "0701")),
month(date) >= 7 ~ ymd(str_c(year(date), "0701"))
|>
)) inner_join(portfolios, join_by(permno, sorting_date))
Fama-French Three-Factor Model
Equipped with the return data and the assigned portfolios, we can now compute the value-weighted average return for each of the six portfolios. Then, we form the Fama-French factors. For the size factor (i.e., SMB), we go long in the three small portfolios and short the three large portfolios by taking an average across either group. For the value factor (i.e., HML), we go long in the two high book-to-market portfolios and short the two low book-to-market portfolios, again weighting them equally.
<- portfolios |>
factors_replicated group_by(portfolio_size, portfolio_bm, date) |>
summarize(
ret = weighted.mean(ret_excess, mktcap_lag), .groups = "drop"
|>
) group_by(date) |>
summarize(
smb_replicated = mean(ret[portfolio_size == 1]) -
mean(ret[portfolio_size == 2]),
hml_replicated = mean(ret[portfolio_bm == 3]) -
mean(ret[portfolio_bm == 1])
)
Replication Evaluation
In the previous section, we replicated the size and value premiums following the procedure outlined by Fama and French. The final question is then: how close did we get? We answer this question by looking at the two time-series estimates in a regression analysis using lm()
. If we did a good job, then we should see a non-significant intercept (rejecting the notion of systematic error), a coefficient close to 1 (indicating a high correlation), and an adjusted R-squared close to 1 (indicating a high proportion of explained variance).
<- factors_ff3_monthly |>
test inner_join(factors_replicated, join_by(date)) |>
mutate(
across(c(smb_replicated, hml_replicated), ~round(., 4))
)
To test the success of the SMB factor, we hence run the following regression:
<- lm(smb ~ smb_replicated, data = test)
model_smb summary(model_smb)
Call:
lm(formula = smb ~ smb_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.020503 -0.001449 0.000052 0.001440 0.015049
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.000107 0.000129 -0.83 0.4
smb_replicated 0.988076 0.004228 233.68 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.00352 on 748 degrees of freedom
Multiple R-squared: 0.986, Adjusted R-squared: 0.986
F-statistic: 5.46e+04 on 1 and 748 DF, p-value: <2e-16
The results for the SMB factor are really convincing, as all three criteria outlined above are met and the coefficient is 0.99 and the R-squared is at 99 percent.
<- lm(hml ~ hml_replicated, data = test)
model_hml summary(model_hml)
Call:
lm(formula = hml ~ hml_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.02352 -0.00285 -0.00021 0.00218 0.03388
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.000353 0.000215 1.64 0.1
hml_replicated 0.961842 0.007105 135.37 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.00587 on 748 degrees of freedom
Multiple R-squared: 0.961, Adjusted R-squared: 0.961
F-statistic: 1.83e+04 on 1 and 748 DF, p-value: <2e-16
The replication of the HML factor is also a success, although at a slightly lower coefficient of 0.96 and an R-squared around 96 percent.
The evidence hence allows us to conclude that we did a relatively good job in replicating the original Fama-French size and value premiums, although we do not know their underlying code. From our perspective, a perfect match is only possible with additional information from the maintainers of the original data.
Fama-French Five-Factor Model
Now, let us move to the replication of the five-factor model. We extend the other_sorting_variables
table from above with the additional characteristics operating profitability op
and investment inv
. Note that the drop_na()
statement yields different sample sizes as some firms with be
values might not have op
or inv
values.
<- compustat |>
other_sorting_variables mutate(sorting_date = ymd(str_c(year(datadate) + 1, "0701"))) |>
select(gvkey, sorting_date, be, op, inv) |>
inner_join(market_equity,
join_by(gvkey, sorting_date)) |>
mutate(bm = be / me) |>
select(permno, sorting_date, me, be, bm, op, inv)
<- size |>
sorting_variables inner_join(
other_sorting_variables, join_by(permno, sorting_date)
|>
) drop_na() |>
distinct(permno, sorting_date, .keep_all = TRUE)
In each month, we independently sort all stocks into the two size portfolios. The value, profitability, and investment portfolios, on the other hand, are the results of dependent sorts based on the size portfolios. We then merge the portfolios to the return data for the rest of the year just as above.
<- sorting_variables |>
portfolios group_by(sorting_date) |>
mutate(
portfolio_size = assign_portfolio(
data = pick(everything()),
sorting_variable = size,
percentiles = c(0, 0.5, 1)
|>
)) group_by(sorting_date, portfolio_size) |>
mutate(
across(c(bm, op, inv), ~assign_portfolio(
data = pick(everything()),
sorting_variable = .,
percentiles = c(0, 0.3, 0.7, 1)),
.names = "portfolio_{.col}"
)|>
) ungroup() |>
select(permno, sorting_date,
portfolio_size, portfolio_bm,
portfolio_op, portfolio_inv)
<- crsp_monthly |>
portfolios mutate(sorting_date = case_when(
month(date) <= 6 ~ ymd(str_c(year(date) - 1, "0701")),
month(date) >= 7 ~ ymd(str_c(year(date), "0701"))
|>
)) inner_join(portfolios, join_by(permno, sorting_date))
Now, we want to construct each of the factors, but this time the size factor actually comes last because it is the result of averaging across all other factor portfolios. This dependency is the reason why we keep the table with value-weighted portfolio returns as a separate object that we reuse later. We construct the value factor, HML, as above by going long the two portfolios with high book-to-market ratios and shorting the two portfolios with low book-to-market.
<- portfolios |>
portfolios_value group_by(portfolio_size, portfolio_bm, date) |>
summarize(
ret = weighted.mean(ret_excess, mktcap_lag),
.groups = "drop"
)
<- portfolios_value |>
factors_value group_by(date) |>
summarize(
hml_replicated = mean(ret[portfolio_bm == 3]) -
mean(ret[portfolio_bm == 1])
)
For the profitability factor, RMW, we take a long position in the two high profitability portfolios and a short position in the two low profitability portfolios.
<- portfolios |>
portfolios_profitability group_by(portfolio_size, portfolio_op, date) |>
summarize(
ret = weighted.mean(ret_excess, mktcap_lag),
.groups = "drop"
)
<- portfolios_profitability |>
factors_profitability group_by(date) |>
summarize(
rmw_replicated = mean(ret[portfolio_op == 3]) -
mean(ret[portfolio_op == 1])
)
For the investment factor, CMA, we go long the two low investment portfolios and short the two high investment portfolios.
<- portfolios |>
portfolios_investment group_by(portfolio_size, portfolio_inv, date) |>
summarize(
ret = weighted.mean(ret_excess, mktcap_lag),
.groups = "drop"
)
<- portfolios_investment |>
factors_investment group_by(date) |>
summarize(
cma_replicated = mean(ret[portfolio_inv == 1]) -
mean(ret[portfolio_inv == 3])
)
Finally, the size factor, SMB, is constructed by going long the nine small portfolios and short the nine large portfolios.
<- bind_rows(
factors_size
portfolios_value,
portfolios_profitability,
portfolios_investment|>
) group_by(date) |>
summarize(
smb_replicated = mean(ret[portfolio_size == 1]) -
mean(ret[portfolio_size == 2])
)
We then join all factors together into one dataframe and construct again a suitable table to run tests for evaluating our replication.
<- factors_size |>
factors_replicated full_join(factors_value, join_by(date)) |>
full_join(factors_profitability, join_by(date)) |>
full_join(factors_investment, join_by(date))
<- factors_ff5_monthly |>
test inner_join(factors_replicated, join_by(date)) |>
mutate(
across(c(smb_replicated, hml_replicated,
~round(., 4))
rmw_replicated, cma_replicated), )
Let us start the replication evaluation again with the size factor:
<- lm(smb ~ smb_replicated, data = test)
model_smb summary(model_smb)
Call:
lm(formula = smb ~ smb_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.01901 -0.00185 0.00021 0.00198 0.01373
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -0.000187 0.000133 -1.41 0.16
smb_replicated 0.963914 0.004246 227.01 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.00357 on 724 degrees of freedom
Multiple R-squared: 0.986, Adjusted R-squared: 0.986
F-statistic: 5.15e+04 on 1 and 724 DF, p-value: <2e-16
The results for the SMB factor are quite convincing as all three criteria outlined above are met and the coefficient is 0.96 and the R-squared is at 99 percent.
<- lm(hml ~ hml_replicated, data = test)
model_hml summary(model_hml)
Call:
lm(formula = hml ~ hml_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.04624 -0.00407 -0.00037 0.00402 0.03661
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.000584 0.000295 1.98 0.048 *
hml_replicated 0.988715 0.010066 98.22 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.00792 on 724 degrees of freedom
Multiple R-squared: 0.93, Adjusted R-squared: 0.93
F-statistic: 9.65e+03 on 1 and 724 DF, p-value: <2e-16
The replication of the HML factor is also a success, although at a slightly higher coefficient of 0.99 and an R-squared around 93 percent.
<- lm(rmw ~ rmw_replicated, data = test)
model_rmw summary(model_rmw)
Call:
lm(formula = rmw ~ rmw_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.021280 -0.003120 0.000037 0.003229 0.018101
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.35e-05 2.02e-04 0.17 0.87
rmw_replicated 9.50e-01 8.81e-03 107.88 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.00539 on 724 degrees of freedom
Multiple R-squared: 0.941, Adjusted R-squared: 0.941
F-statistic: 1.16e+04 on 1 and 724 DF, p-value: <2e-16
We are also able to replicate the RMW factor quite well with a coefficient of 0.95 and an R-squared around 94 percent.
<- lm(cma ~ cma_replicated, data = test)
model_cma summary(model_cma)
Call:
lm(formula = cma ~ cma_replicated, data = test)
Residuals:
Min 1Q Median 3Q Max
-0.015179 -0.002656 -0.000104 0.002479 0.021454
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.000629 0.000168 3.75 0.00019 ***
cma_replicated 0.964087 0.007935 121.50 < 2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.0045 on 724 degrees of freedom
Multiple R-squared: 0.953, Adjusted R-squared: 0.953
F-statistic: 1.48e+04 on 1 and 724 DF, p-value: <2e-16
Finally, the CMA factor also replicates well with a coefficient of 0.96 and an R-squared around 95 percent.
Overall, our approach seems to replicate the Fama-French five-factor models just as well as the three factors.
Exercises
- Fama and French (1993) claim that their sample excludes firms until they have appeared in Compustat for two years. Implement this additional filter and compare the improvements of your replication effort.
- On his homepage, Kenneth French provides instructions on how to construct the most common variables used for portfolio sorts. Try to replicate the univariate portfolio sort return time series for
E/P
(earnings/price) provided on his homepage and evaluate your replication effort using regressions.
References
Footnotes
Note that Fama and French (1992) claim to exclude financial firms. To a large extent this happens through using industry format “INDL”, as we do in WRDS, CRSP, and Compustat. Neither the original paper, nor Ken French’s website, or the WRDS replication contains any indication that financial companies are excluded using additional filters such as industry codes.↩︎