# Load packages

# Core
library(tidyverse)
library(tidyquant)
library(PerformanceAnalytics)
library(ggrepel)

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("TSLA", "NKE", "JPM", "NVDA", "AAPL")

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

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: 722 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AAPL  2006-02-28 -0.0976 
##  2 AAPL  2006-03-31 -0.0880 
##  3 AAPL  2006-04-28  0.115  
##  4 AAPL  2006-05-31 -0.164  
##  5 AAPL  2006-06-30 -0.0427 
##  6 AAPL  2006-07-31  0.171  
##  7 AAPL  2006-08-31 -0.00162
##  8 AAPL  2006-09-29  0.126  
##  9 AAPL  2006-10-31  0.0519 
## 10 AAPL  2006-11-30  0.123  
## # ℹ 712 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "JPM"  "NKE"  "NVDA" "TSLA"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL       0.25
## 2 JPM        0.25
## 3 NKE        0.2 
## 4 NVDA       0.2 
## 5 TSLA       0.1

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: 155 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2006-02-28           0.00753
##  2 2006-03-31           0.0164 
##  3 2006-04-28           0.0486 
##  4 2006-05-31          -0.108  
##  5 2006-06-30          -0.0276 
##  6 2006-07-31           0.0683 
##  7 2006-08-31           0.0590 
##  8 2006-09-29           0.0585 
##  9 2006-10-31           0.0596 
## 10 2006-11-30           0.0511 
## # ℹ 145 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.0598 0.0598
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.0138627

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 AAPL      0.018  0.0938
## 2 JPM       0.0078 0.0819
## 3 NKE       0.0139 0.0629
## 4 NVDA      0.0146 0.134 
## 5 TSLA      0.0259 0.143 
## 6 Portfolio 0.0139 0.0598
sd_mean_tbl %>%
    
    ggplot(aes(x = Stdev, y = Mean, color = asset)) +
    geom_point() +
    ggrepel::geom_text_repel(aes(label = asset))

rolling_sd_tbl <- portfolio_returns_tbl %>%
    
    tq_mutate(select     = portfolio.returns, 
              mutate_fun = rollapply,
              width      = 24,
              FUN        = sd,
              col_rename = "rolling_sd") %>%
    
    na.omit() %>%
    select(date, rolling_sd)

rolling_sd_tbl
## # A tibble: 132 × 2
##    date       rolling_sd
##    <date>          <dbl>
##  1 2008-01-31     0.0558
##  2 2008-02-29     0.0599
##  3 2008-03-31     0.0606
##  4 2008-04-30     0.0617
##  5 2008-05-30     0.0559
##  6 2008-06-30     0.0675
##  7 2008-07-31     0.0687
##  8 2008-08-29     0.0680
##  9 2008-09-30     0.0687
## 10 2008-10-31     0.0715
## # ℹ 122 more rows
rolling_sd_tbl %>%
    
    ggplot(aes(x = date, y = rolling_sd)) +
    geom_line(color = "cornflowerblue") +
    
    # Formatting
    scale_y_continuous(labels = scales::percent_format()) +
    
    # Labeling
    labs(x = NULL,
         y = NULL,
         title = "24-Month Rolling Volatility") +
    theme(plot.title = element_text(hjust = 0.5))

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.

The portfolio performs more steadily and with less risk than the individual stocks. The second graph shows that the portfolio has a lower standard deviation compared to the single stocks. While it doesn’t have the highest return like Tesla or Apple, it still gives a good return with much less risk. The first graph also shows that volatility can rise and fall over time, and a portfolio helps protect against big drops. Overall, it’s safer to invest in the portfolio instead of putting all your money in one stock.