# 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("WMT", "AAPL", "NFLX", "GOOGL")

prices <- tq_get(x   = symbols,
                get  = "stock,prices",
                from = "2012-12-31",
                to   = "2017-12-31")
prices
## # A tibble: 5,040 × 8
##    symbol date        open  high   low close   volume adjusted
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>    <dbl>    <dbl>
##  1 WMT    2012-12-31  22.5  22.8  22.5  22.7 21037500     17.4
##  2 WMT    2013-01-02  23.0  23.1  22.8  23.1 31172400     17.7
##  3 WMT    2013-01-03  23.1  23.1  22.8  22.9 26730300     17.6
##  4 WMT    2013-01-04  22.9  23.1  22.8  23.0 19314000     17.7
##  5 WMT    2013-01-07  22.9  23.0  22.7  22.8 18604200     17.5
##  6 WMT    2013-01-08  22.8  23.0  22.7  22.9 17600700     17.5
##  7 WMT    2013-01-09  22.9  22.9  22.7  22.9 15165600     17.5
##  8 WMT    2013-01-10  22.9  23.0  22.6  22.8 34361400     17.5
##  9 WMT    2013-01-11  22.9  22.9  22.7  22.9 18673500     17.6
## 10 WMT    2013-01-14  22.8  22.9  22.7  22.8 16471200     17.5
## # ℹ 5,030 more rows

2 Convert prices to returns (monthly)

asset_returns_tbl <- prices %>%
    
    group_by(symbol) %>%
    
    tq_transmute(select     = adjusted,
                 mutate_fun = periodReturn,
                 period     = "monthly",
                 type       = "log") %>%
    slice(-1) %>%
    ungroup() %>%
    set_names(c("asset", "date", "returns"))

asset_returns_tbl
## # A tibble: 240 × 3
##    asset date         returns
##    <chr> <date>         <dbl>
##  1 AAPL  2013-01-31 -0.156   
##  2 AAPL  2013-02-28 -0.0256  
##  3 AAPL  2013-03-28  0.00285 
##  4 AAPL  2013-04-30  0.000271
##  5 AAPL  2013-05-31  0.0222  
##  6 AAPL  2013-06-28 -0.126   
##  7 AAPL  2013-07-31  0.132   
##  8 AAPL  2013-08-30  0.0804  
##  9 AAPL  2013-09-30 -0.0217  
## 10 AAPL  2013-10-31  0.0920  
## # ℹ 230 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL"  "GOOGL" "NFLX"  "WMT"
# weights
weights <- c(0.35, 0.30, 0.20, 0.15)
weights
## [1] 0.35 0.30 0.20 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL       0.35
## 2 GOOGL      0.3 
## 3 NFLX       0.2 
## 4 WMT        0.15

4 Build a portfolio

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

portfolio_returns_tbl
## # A tibble: 60 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-01-31           0.0849 
##  2 2013-02-28           0.0362 
##  3 2013-03-28           0.00894
##  4 2013-04-30           0.0435 
##  5 2013-05-31           0.0287 
##  6 2013-06-28          -0.0555 
##  7 2013-07-31           0.0849 
##  8 2013-08-30           0.0350 
##  9 2013-09-30           0.0216 
## 10 2013-10-31           0.0949 
## # ℹ 50 more rows

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, 4))

portfolio_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
##    Stdev  tq_sd
##    <dbl>  <dbl>
## 1 0.0476 0.0476
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.02087329

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: 5 × 3
##   asset       Mean  Stdev
##   <chr>      <dbl>  <dbl>
## 1 AAPL      0.015  0.0695
## 2 GOOGL     0.0182 0.0539
## 3 NFLX      0.0446 0.133 
## 4 WMT       0.0083 0.0471
## 5 Portfolio 0.0209 0.0476
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.

I would expect my portfolio to perform relatively well in comparison to the individual assets included in it. According to the plot (Netflix excluded which I will discuss), the portfolio has overall the lowest risk(about 4.7% Stdev) and highest average return per month(about 2.1%). Statistically speaking, it’s not only the safest option to invest in but will also generate the best returns.

However, back to Netflix, technically speaking it has far and above the highest average returns. Generating a whopping 4.5% on average per month, but average does not paint the full picture here. It has a standard deviation of about 13.3% meaning it could, on any given month, deliver a vastly higher or vastly lower return rate. If I was willing to take great risks for potential upside on my returns, I would invest in Netflix here but being the safe person I am, it’s simply too dangerous of a stock to dump all my money in.