# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize expected returns and risk to make it easier to compare the performance of multiple assets and portfolios.

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("MCD", "WEN", "YUM", "DPZ", "SBUX")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",
                 from = "2015-12-31",
                 to   = "2020-12-31")

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>%
    
    tq_transmute(select     = adjusted,
                 mutate_fun = periodReturn,
                 period     = "quarterly",
                 type       = "log") %>%
    
    slice(-1) %>%
    
    ungroup() %>%

    set_names(c("asset", "date", "returns"))

3 Assign a weight to each asset (change the weigting scheme)

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "DPZ"  "MCD"  "SBUX" "WEN"  "YUM"
# weights
weights <- c(0.2, 0.2, 0.2, 0.2, 0.2)
weights
## [1] 0.2 0.2 0.2 0.2 0.2
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 DPZ         0.2
## 2 MCD         0.2
## 3 SBUX        0.2
## 4 WEN         0.2
## 5 YUM         0.2

4 Build a portfolio

# ?tq_portfolio

portfolio_returns_tbl <- asset_returns_tbl %>%
    tq_portfolio(assets_col = asset,
                 returns_col = returns,
                 weights = w_tbl,
                 rebalance_on = "quarters")

portfolio_returns_tbl
## # A tibble: 20 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2016-03-31           0.0756 
##  2 2016-06-30          -0.0354 
##  3 2016-09-30           0.0561 
##  4 2016-12-30           0.0692 
##  5 2017-03-31           0.0598 
##  6 2017-06-30           0.120  
##  7 2017-09-29          -0.0204 
##  8 2017-12-29           0.0585 
##  9 2018-03-29           0.0514 
## 10 2018-06-29          -0.0123 
## 11 2018-09-28           0.0866 
## 12 2018-12-31          -0.00966
## 13 2019-03-29           0.0988 
## 14 2019-06-28           0.100  
## 15 2019-09-30           0.00446
## 16 2019-12-31           0.0210 
## 17 2020-03-31          -0.227  
## 18 2020-06-30           0.199  
## 19 2020-09-30           0.113  
## 20 2020-12-30           0.0519

5 Compute Standard Deviation

portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
    
    tq_performance(Ra = portfolio.returns, performance_fun = table.Stats) %>%
    
    select(Stdev) %>%
    mutate(tq_sd = round(Stdev, 3))


# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.04301233

6 Plot: Expected Returns versus Risk

# Expected Returns vs Risk
sd_mean_tbl <- asset_returns_tbl %>%
    
    group_by(asset) %>% 
    tq_performance(Ra = returns,
                   performance_fun = table.Stats) %>%
    select(Mean = ArithmeticMean, Stdev) %>%
    ungroup() %>%
    
    # Add portfolio sd
    add_row(tibble(asset = "Portfolio",
                   Mean = portfolio_mean_tidyquant_builtin_percent,
                   Stdev = portfolio_sd_tidyquant_builtin_percent$tq_sd))

sd_mean_tbl
## # A tibble: 6 × 3
##   asset       Mean Stdev
##   <chr>      <dbl> <dbl>
## 1 DPZ       0.0644 0.114
## 2 MCD       0.0356 0.088
## 3 SBUX      0.0332 0.120
## 4 WEN       0.0405 0.150
## 5 YUM       0.0414 0.132
## 6 Portfolio 0.0430 0.085
sd_mean_tbl %>%
    
    ggplot(aes(x = Stdev, y = Mean, color = asset)) +
    geom_point() +
    ggrepel::geom_text_repel(aes(label = asset))

How should you expect your portfolio to perform relative to its assets in the portfolio? Would you invest all your money in any of the individual stocks instead of the portfolio? Discuss both in terms of expected return and risk.

Overall, my portfolio of fast food stocks performs better than each asset individually. The exception is DPZ arguably. Looking at the graph, my portfolio mean return falls at about 4.5%. The average returns of YUM and WEN are not far behind, but still lower than the overall portfolio, and with a higher volatility than is achieved via the portfolio. MCD and SBUX fall significantly lower in terms of returns, but are less volatile than YUM and WEN. All four of these assets are individually not as opportunistic as the portfolio. The exception is DPZ, which seems to offer almost 2% higher returns for only a small amount of added volatility (risk) as compared to the other assets.