Today we will continue our portfolio fun by calculating the CAPM beta of our portfolio returns. That will entail fitting a linear model and, when we get to visualization next time, considering the meaning of our results from the perspective of asset returns.
By way of brief background, the Capital Asset Pricing Model (CAPM) is a model, created by William Sharpe, that estimates the return of an asset based on the return of the market and the asset’s linear relationship to the return of the market. That linear relationship is the stock’s beta coefficient, or just good ol’ beta.
CAPM was introduced back in 1964, garnered a Nobel for its creator, and, like many ephocally important theories, has been widely used, updated, criticized, debunked, revived, re-debunked, etc. Fama and French have written that CAPM “is the centerpiece of MBA investment courses. Indeed, it is often the only asset pricing model taught in these courses…[u]nfortunately, the empirical record of the model is poor.”1
With that, we will forge ahead with our analysis because calculating CAPM betas can serve as a nice template for more complex models in a team’s work and sometimes it’s a good idea to start with a simple model, even if it hasn’t stood up to empirical rigor. Plus, it might have been questioned by future research, but it’s still an iconic model that we should learn and love.
We are going to focus on one particular aspect of CAPM: beta. Beta, as we noted above, is the beta coefficient of an asset that results from regressing the returns of that asset on market returns. It captures the linear relationship between the asset/portfolio and the market. For our purposes, it’s a good vehicle for exploring reproducible flows for modeling or regressing our portfolio returns on the market returns. Even if your team dislikes CAPM in favor of more nuanced models, these code flows can serve as a good base for the building of those more complex models.
We are going to be calculating beta in several ways: by-hand (for illustrative purposes), in the xts world with PerformanceAnalytics, in the tidyverse with dplyr, and in the tidyquant world. These seem to be the most popular paradigms for doing financial time series work, and even within a team there can be differing preferences. I don’t think everyone needs to grind through their work using each paradigm, but I do think it’s helpful to be fluent, or, at least, conversant, in the various worlds. If you’re a tidyverse type of person but need to collaborate with an xts or tidyquant enthusiast, it will help if each of you is familiar with the three universes (though at some point ya just have to choose a code flow and get stuff done).
We will be working with and calculating beta for 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 this post.
I won’t go through the logic again but the code is here:
library(tidyquant)
library(tidyverse)
library(timetk)
library(tibbletime)
library(broom)
symbols %
reduce(merge) %__% `colnames% 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 two objects of portfolio returns and one object of our individual asset returns:
+ portfolio_returns_xts_rebalanced_monthly (an xts of monthly returns)
+ portfolio_returns_tq_rebalanced_monthly (a tibble of monthly returns)
+ asset_returns_long (a tidy tibble of monthly returns for those 5 assets above)
Let’s get to it.
CAPM and Market Returns
Our first step is to make a choice about which asset to use as a proxy for the market return, and we will go with the SPY ETF, effectively treating the S&amp;P 500 as the market. That’s going to make our calculations substantively uninteresting because (1) SPY is 25% of our portfolio and (2) we have chosen assets and a time period (2013 - 2017) in which correlations with SPY have been high. It will offer one benefit in the way of a sanity check, which I’ll note below. With those caveats in mind, feel free to choose a different asset for the market return and try to reproduce this work, or construct a different portfolio that does not include SPY.
Let’s calculate our market return for SPY and save it as market_return_xts. Note the start date is “2013-01-01” and the end date is “2017-12-31”, so we will be working with five years of returns.
spy_monthly_xts % map(~Ad(get(.))) %__% reduce(merge) %__%
`colnames% to.monthly(indexAt = &quot;last&quot;, OHLC = FALSE)
market_returns_xts % na.omit()
We will also want a data.frame object of market returns, and will convert the xts object using tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) from the timetk package.
market_returns_tidy % tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %__% na.omit() %__%
select(date, returns = SPY)
head(market_returns_tidy)
## # A tibble: 6 x 2
## date returns
## ## 1 2013-02-28 0.01267837
## 2 2013-03-28 0.03726809
## 3 2013-04-30 0.01903021
## 4 2013-05-31 0.02333503
## 5 2013-06-28 -0.01343411
## 6 2013-07-31 0.05038580
We have a market_returns_tidy object. Let’s make sure it’s periodicity aligns perfectly with our portfolio returns periodicity
portfolio_returns_tq_rebalanced_monthly %__% mutate(market_returns = market_returns_tidy$returns) %__%
head()
## # A tibble: 6 x 3
## date returns market_returns
## ## 1 2013-02-28 -0.0008696129 0.01267837
## 2 2013-03-28 0.0186624381 0.03726809
## 3 2013-04-30 0.0206248856 0.01903021
## 4 2013-05-31 -0.0053529694 0.02333503
## 5 2013-06-28 -0.0229487618 -0.01343411
## 6 2013-07-31 0.0411705818 0.05038580
Note that if the periodicities did not align, mutate() would have thrown an error in the code chunk above.
Calculating CAPM Beta
There are several R code flows to calculate portfolio beta but first let’s have a look at the equation.
$${\beta}_{portfolio} = cov(R_p, R_m)/\sigma_m $$
\[{\beta}_{portfolio} = cov(R_p, R_m)/\sigma_m \]
Portfolio beta is equal to the covariance of the portfolio returns and market returns, divided by the variance of market returns.
We can calculate the numerator, or covariance of portfolio and market returns, with cov(portfolio_returns_xts_rebalanced_monthly, market_returns_tidy$returns) and the denominator with var(market_return$returns).
Our portfolio beta is equal to:
cov(portfolio_returns_xts_rebalanced_monthly,market_returns_tidy$returns)/var(market_returns_tidy$returns)
## [,1]
## returns 0.9010689
That beta is quite near to 1 as we were expecting - after all, SPY is a big part of this portfolio.
We can also calculate portfolio beta by finding the beta of each of our assets and then multiplying by asset weights. That is, another equation for portfolio beta is the weighted sum of the asset betas:
$${\beta}_{portfolio} ={\sum_{i=1}^n}W _i~{\beta}_i $$
\[{\beta}_{portfolio} ={\sum_{i=1}^n}W _i~{\beta}_i \]
To use that method with R, we first find the beta for each of our assets, and this gives us an opportunity to introduce a code flow for running regression analysis.
We need to regress each of our individual asset returns on the market return. We could do that for asset 1 with lm(asset_return_1 ~ market_returns_tidy$returns), and then again for asset 2 with lm(asset_return_2 ~ market_returns_tidy$returns), etc. for all five of our assets. But if we had a 50-asset portfolio, that would be impractical. Instead let’s write a code flow and use map() to regress all of our assets and calculate betas with one call.
We will start with our asset_returns_long tidy data frame and will then run nest(-asset).
beta_assets %
na.omit() %__% nest(-asset)
beta_assets
## # A tibble: 5 x 2
## asset data
## ## 1 SPY ## 2 EFA ## 3 IJS ## 4 EEM ## 5 AGG That nest(-asset) changed our data frame so that there are two columns: one called asset that holds our asset name and one called data that holds a list of returns for each asset. We have now ‘nested’ a list of returns within a column.
Now we can use map() to apply a function to each of those nested lists and store the results in a new column via the mutate() function. The whole piped command is mutate(model = map(data, ~ lm(returns ~ market_returns_tidy$returns, data = .)))
beta_assets % na.omit() %__% nest(-asset) %__% mutate(model = map(data, ~ lm(returns ~ market_returns_tidy$returns, data = .))) beta_assets
## # A tibble: 5 x 3
## asset data model
## ## 1 SPY ## 2 EFA ## 3 IJS ## 4 EEM ## 5 AGG We now have three columns: asset which we had before, data which we had before, and model which we just added. The model column holds the results of the regression lm(returns ~ market_returns_tidy$returns, data = .) that we ran for each of our assets. Those results are a beta and an intercept for each of our assets, but they are not in a great format for presentation to others, or even readability by ourselves.
Let’s tidy up our results with the tidy() function from the broom package. We want to apply that function to our model column and will use the mutate() and map() combination again. The complete call is to mutate(model = map(model, tidy)).
beta_assets % na.omit() %__% nest(-asset) %__% mutate(model = map(data, ~ lm(returns ~ market_returns_tidy$returns, data = .))) %__%
mutate(model = map(model, tidy))
beta_assets
## # A tibble: 5 x 3
## asset data model
## ## 1 SPY ## 2 EFA ## 3 IJS ## 4 EEM ## 5 AGG We are getting close now, but the model column holds nested data frames. Have a look and see that they are nicely formatted data frames:
beta_assets$model
## [[1]]
## term estimate std.error statistic
## 1 (Intercept) 1.806734e-18 1.136381e-18 1.589902e+00
## 2 market_returns_tidy$returns 1.000000e+00 3.899949e-17 2.564136e+16
## p.value
## 1 0.1173886
## 2 0.0000000
## ## [[2]]
## term estimate std.error statistic
## 1 (Intercept) -0.005427739 0.002908978 -1.865858
## 2 market_returns_tidy$returns 0.945476441 0.099833320 9.470550
## p.value
## 1 6.720983e-02
## 2 2.656258e-13
## ## [[3]]
## term estimate std.error statistic
## 1 (Intercept) -0.001693293 0.003639218 -0.4652905
## 2 market_returns_tidy$returns 1.120583127 0.124894444 8.9722416
## p.value
## 1 6.434963e-01
## 2 1.713903e-12
## ## [[4]]
## term estimate std.error statistic
## 1 (Intercept) -0.00811518 0.004785237 -1.695878
## 2 market_returns_tidy$returns 0.95562574 0.164224722 5.819013
## p.value
## 1 9.536495e-02
## 2 2.841106e-07
## ## [[5]]
## term estimate std.error statistic
## 1 (Intercept) 0.001888304 0.001230331 1.5347933
## 2 market_returns_tidy$returns -0.005419543 0.042223776 -0.1283529
## p.value
## 1 0.1303671
## 2 0.8983215
Still, I don’t like to end up with nested data frames, so let’s unnest() that model column.
beta_assets % na.omit() %__% nest(-asset) %__% mutate(model = map(data, ~ lm(returns ~ market_returns_tidy$returns, data = .))) %__%
mutate(model = map(model, tidy)) %__% unnest(model)
beta_assets
## # A tibble: 10 x 6
## asset term estimate std.error
## ## 1 SPY (Intercept) 1.806734e-18 1.136381e-18
## 2 SPY market_returns_tidy$returns 1.000000e+00 3.899949e-17
## 3 EFA (Intercept) -5.427739e-03 2.908978e-03
## 4 EFA market_returns_tidy$returns 9.454764e-01 9.983332e-02
## 5 IJS (Intercept) -1.693293e-03 3.639218e-03
## 6 IJS market_returns_tidy$returns 1.120583e+00 1.248944e-01
## 7 EEM (Intercept) -8.115180e-03 4.785237e-03
## 8 EEM market_returns_tidy$returns 9.556257e-01 1.642247e-01
## 9 AGG (Intercept) 1.888304e-03 1.230331e-03
## 10 AGG market_returns_tidy$returns -5.419543e-03 4.222378e-02
## # ... with 2 more variables: statistic , p.value Now that looks human-readable and presentable. We will do one further cleanup and get rid of the intercept results, since we are isolating the betas.
beta_assets % na.omit() %__% nest(-asset) %__% mutate(model = map(data, ~ lm(returns ~ market_returns_tidy$returns, data = .))) %__% unnest(model %__% map(tidy)) %__% filter(term == &quot;market_returns_tidy$returns&quot;) %__% select(-term)
beta_assets
## # A tibble: 5 x 5
## asset estimate std.error statistic p.value
## ## 1 SPY 1.000000000 3.899949e-17 2.564136e+16 0.000000e+00
## 2 EFA 0.945476441 9.983332e-02 9.470550e+00 2.656258e-13
## 3 IJS 1.120583127 1.248944e-01 8.972242e+00 1.713903e-12
## 4 EEM 0.955625743 1.642247e-01 5.819013e+00 2.841106e-07
## 5 AGG -0.005419543 4.222378e-02 -1.283529e-01 8.983215e-01
A quick sanity check on those asset betas should reveal that SPY has beta of 1 with itself.
beta_assets %__% select(asset, estimate) %__% filter(asset == &quot;SPY&quot;)
## # A tibble: 1 x 2
## asset estimate
## ## 1 SPY 1
Now let’s see how our combination of these assets leads to a portfolio beta.
Let’s assign portfolio weights as we chose above.
w % na.omit() %__% tq_performance(Ra = returns, Rb = market_return, performance_fun = CAPM.beta) %__% `colnames