This is the beginning of a series on portfolio volatility, variance, and standard deviation. I realize that it’s a lot more fun to fantasize about analyzing stock returns, which is why television shows and websites constantly update the daily market returns and give them snazzy green and red colors. But good ol’ volatility is quite important in its own right, especially to finance geeks, aspiring finance geeks, and institutional investors. If you are, might become, or might ever work with/for any of those, this series should at least serve as a jumping-off point.
Briefly, our volatility project will proceed as follows:
Part 1
Portfolio Volatility Intro: by-hand, matrix algebra, built-in and compare to SPY
Visualizing Volatility Intro: chart portfolio sd over time - will have to roll apply
Shiny app to test different portfolios
Part 2
Asset Contributions to Volatility: by-hand, matrix algebra, built-in and visualize snapshot with bar graph
Chart contributions over time: flag any asset that surpasses a threshold
Shiny app to test different portfolios
Part 3
Minimum Variance Portfolio: find minimum variance portfolio weights
Shiny app to test different portfolios
A quick word of warning: this series begins at the beginning with portfolio standard deviation, builds up to a more compelling data visualization in the next post, and finally a nice Shiny app after that. R users with experience in the world of volatility may wish to skip this post and wait for the visualizations in the next one. That said, I would humbly offer a couple of benefits to the R code that awaits us.
First, volatility is important, possibly more important than returns. I don’t think any investment professional looks back on hours spent pondering volatility as a waste of time. Plus, today we’ll look at a new way to convert daily prices to monthly using the tidyquant package, and that might offer enough new substance.
Second, as always, we have an eye on making our work reproducible and reusable. This Notebook makes it exceedingly clear how we derive our final data visualizations on portfolio volatility. It’s a good template for other visualization derivations, even if standard deviation is old hat for you.
Okay, without further ado, here’s where we are headed today:
Import prices and calculate returns for 5 assets and construct a portfolio.
Calculate the standard deviation of monthly portfolio returns using three methods:
the old-fashioned equation
matrix algebra
a built-in function from performanceAnalytics
Compare those to the standard deviation of monthly SPY returns.
On to step 1, wherein we import prices and calculate returns for the 5 ETFs to be used in our portfolio. Those are AGG (a US bond fund), DBC (a commodities fund), EFA (a non-US equities fund), SPY (an S&amp;P500 ETF), VGT (a technology fund).
Let’s import prices and save them to an xts object.
# A vector of symbol for our ETFs.
symbols %
reduce(merge) %__% `colnames%
# Add a date column. mutate(date = ymd(row.names)) %__% # Remove the row.names column; it's not needed anymore.
select(-row.names) %__% # I like to have the date column as the first column. select(date, everything()) %__% # We need to gather into long format in order to use tq_transmute().
gather(asset, return, -date) %__%
group_by(asset) %__% # Use the function from tidyquant; note how easily we could change to
# a different time period like weekly or yearly.
tq_transmute(mutate_fun = periodReturn, period = &quot;monthly&quot;) %__%
# Put the results back to wide format.
spread(asset, monthly.returns) %__% # Convert back to an xts, so we can use the cov() and StdDev() functions.
as_xts(date_col = date)
head(portfolio_component_monthly_returns_xts)
## AGG DBC EFA SPY VGT
## 2013-01-31 -0.0050473186 0.021162123 0.02147558 0.02492127 -0.008422235
## 2013-02-28 0.0039858683 -0.047067088 -0.01288572 0.01275885 0.005237826
## 2013-03-28 -0.0009022828 0.006634722 0.01305393 0.03337511 0.026615970
## 2013-04-30 0.0073150908 -0.038081289 0.05018650 0.01921236 0.005349794
## 2013-05-31 -0.0217859064 -0.015607156 -0.03019051 0.02354709 0.042434166
## 2013-06-28 -0.0174136193 -0.028228925 -0.04611287 -0.01847773 -0.031675393
Take a quick look at the monthly returns above, to make sure things appear to be in order.
Now, on to constructing a portfolio and calculating volatility. To turn these five ETFs into a portfolio, we need to assign them weights. Let’s first create a weights vector.
weights