<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/calculating-beta-in-the-capital-asset-pricing-model/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/02/08/capm-beta/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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.</p>
<p>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.</p>
<p>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.”<a rel="nofollow" href="https://rviews.rstudio.com/2018/02/08/capm-beta/#fn1" target="_blank"><sup>1</sup></a></p>
<p>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.</p>
<p>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.</p>
<p>We are going to be calculating beta in several ways: by-hand (for illustrative purposes), in the <code>xts</code> world with <code>PerformanceAnalytics</code>, in the tidyverse with <code>dplyr</code>, and in the <code>tidyquant</code> 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 <code>xts</code> or <code>tidyquant</code> 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).</p>
<p>We will be working with and calculating beta for our usual portfolio consisting of:</p>
<pre><code>+ 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%</code></pre>
<p>Before we can calculate beta for that portfolio, we need to find portfolio monthly returns, which was covered in <a rel="nofollow" href="https://rviews.rstudio.com/2017/10/11/from-asset-to-portfolio-returns/" target="_blank">this post</a>.</p>
<p>I won’t go through the logic again but the code is here:</p>
<pre><code>library(tidyquant)
library(tidyverse)
library(timetk)
library(tibbletime)
library(broom)

symbols &lt;- c(&quot;SPY&quot;,&quot;EFA&quot;, &quot;IJS&quot;, &quot;EEM&quot;,&quot;AGG&quot;)

prices &lt;- 
  getSymbols(symbols, src = 'yahoo', 
             from = &quot;2013-01-01&quot;,
             to = &quot;2017-12-31&quot;,
             auto.assign = TRUE, warnings = FALSE) %>% 
  map(~Ad(get(.))) %>%
  reduce(merge) %>% 
  `colnames&lt;-`(symbols)

prices_monthly &lt;- to.monthly(prices, indexAt = &quot;last&quot;, OHLC = FALSE)

asset_returns_xts &lt;- na.omit(Return.calculate(prices_monthly, method = &quot;log&quot;))

w &lt;- c(0.25, 0.25, 0.20, 0.20, 0.10)

portfolio_returns_xts_rebalanced_monthly &lt;- 
  Return.portfolio(asset_returns_xts, weights = w, rebalance_on = &quot;months&quot;) %>%
  `colnames&lt;-`(&quot;returns&quot;) 

asset_returns_long &lt;-  
  prices %>% 
  to.monthly(indexAt = &quot;last&quot;, OHLC = FALSE) %>% 
  tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %>%
  gather(asset, returns, -date) %>% 
  group_by(asset) %>%  
  mutate(returns = (log(returns) - log(lag(returns)))) %>% 
  na.omit()

portfolio_returns_tq_rebalanced_monthly &lt;- 
  asset_returns_long %>%
  tq_portfolio(assets_col  = asset, 
               returns_col = returns,
               weights     = w,
               col_rename  = &quot;returns&quot;,
               rebalance_on = &quot;months&quot;)</code></pre>
<p>We will be working with two objects of portfolio returns and one object of our individual asset returns:</p>
<pre><code>+ 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)</code></pre>
<p>Let’s get to it.</p>
<div>
<h3>CAPM and Market Returns</h3>
<p>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.</p>
<p>Let’s calculate our market return for SPY and save it as <code>market_return_xts</code>. 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.</p>
<pre><code>spy_monthly_xts &lt;- 
    getSymbols(&quot;SPY&quot;, 
               src = 'yahoo', 
               from = &quot;2013-01-01&quot;, 
               to = &quot;2017-12-31&quot;,
             auto.assign = TRUE, 
             warnings = FALSE) %>% 
    map(~Ad(get(.))) %>% 
    reduce(merge) %>%
    `colnames&lt;-`(&quot;SPY&quot;) %>% 
    to.monthly(indexAt = &quot;last&quot;, OHLC = FALSE)

market_returns_xts &lt;-
  Return.calculate(spy_monthly_xts, method = &quot;log&quot;) %>% 
  na.omit()</code></pre>
<p>We will also want a <code>data.frame</code> object of market returns, and will convert the <code>xts</code> object using <code>tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;)</code> from the <code>timetk</code> package.</p>
<pre><code>market_returns_tidy &lt;-
  market_returns_xts %>% 
    tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %>% 
    na.omit() %>%
    select(date, returns = SPY)

head(market_returns_tidy)</code></pre>
<pre><code>## # A tibble: 6 x 2
##         date     returns
##       </code></pre></div>