# 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("SPY", "WMT", "COST", "AMZN")
prices <- tq_get(x = symbols, 
                 get = "stock.prices",
                 from = "2012-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) %>%
    #Remove the first row, but since data is group, it will remove the first line of each group
    
    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] "AMZN" "COST" "SPY"  "WMT"
# weights
weights <- c(0.30, 0.30, 0.20, 0.20)
weights
## [1] 0.3 0.3 0.2 0.2
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN        0.3
## 2 COST        0.3
## 3 SPY         0.2
## 4 WMT         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: 52 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-03-28           0.0803 
##  2 2013-06-28           0.0316 
##  3 2013-09-30           0.0586 
##  4 2013-12-31           0.117  
##  5 2014-03-31          -0.0705 
##  6 2014-06-30           0.00731
##  7 2014-09-30           0.0314 
##  8 2014-12-31           0.0602 
##  9 2015-03-31           0.0792 
## 10 2015-06-30          -0.0154 
## # ℹ 42 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.0785 0.0785
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- 
mean(portfolio_returns_tbl$portfolio.returns)
#$ makes it select just certain value, here $portfolio.returns = only select data from this column

portfolio_mean_tidyquant_builtin_percent
## [1] 0.04419643

6 Plot: Expected Returns versus Risk

###Expected Returns vs 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 AMZN      0.0548 0.152 
## 2 COST      0.0477 0.0809
## 3 SPY       0.0336 0.0727
## 4 WMT       0.0338 0.08  
## 5 Portfolio 0.0442 0.0785
sd_mean_tbl %>%
    
    ggplot(aes(x = Stdev, y = Mean, color = asset)) +
    geom_point() +
    geom_text(aes(label = asset),
            vjust = 1.5, #Nudge labels downs
            hjust = 0.5, #Center horizontally
            size  = 4)

        #if put - before nb = change the side, up or down, vert or horizon

24 Months Rolling Volatility

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: 29 × 2
##    date       rolling_sd
##    <date>          <dbl>
##  1 2018-12-31     0.0674
##  2 2019-03-29     0.0698
##  3 2019-06-28     0.0701
##  4 2019-09-30     0.0703
##  5 2019-12-31     0.0686
##  6 2020-03-31     0.0669
##  7 2020-06-30     0.0713
##  8 2020-09-30     0.0733
##  9 2020-12-31     0.0733
## 10 2021-03-31     0.0753
## # ℹ 19 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.

I would expect my portfolio to perform higher than SPY and WMT, and lower than COST and AMZN. Overall, I expect my portfolio to have a balanced performance relative to its assets (as seen with the Standard deviation). In both return and risk, AMZN and COST are higher, while the portfolio is in a more moderate zone in both aspects, and the rest of the stocks are lower (or in line for WMT) in risk, and lower in return. Based on what we have, COST looks like the best bet, because it has more return for almost the same amount of risk as the portfolio.

I would not invest all my money in one individual stock instead I would invest in the portfolio as a whole. Seeing as the portfolio is the one that is the most constant in its performance and stable (less volatile). This would allow me to better balance the risk and return of my investment. It is the one option that would give me the most return for the risk I take. COST is the only stock that is close to the portfolio in that way, but it has more risk. Thus, the portfolio has the highest returns for the lowest risk.