# 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("HMC", "WMT", "TGT")

prices <- tq_get(x = symbols, 
                 get = "stock.prices", 
                 from = "2012-12-31", 
                 to  = "2017-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: 180 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 HMC   2013-01-31  0.0201 
##  2 HMC   2013-02-28 -0.00665
##  3 HMC   2013-03-28  0.0263 
##  4 HMC   2013-04-30  0.0440 
##  5 HMC   2013-05-31 -0.0622 
##  6 HMC   2013-06-28 -0.00318
##  7 HMC   2013-07-31 -0.00296
##  8 HMC   2013-08-30 -0.0328 
##  9 HMC   2013-09-30  0.0640 
## 10 HMC   2013-10-31  0.0466 
## # … with 170 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "HMC" "TGT" "WMT"
# weights 
weight <- c(0.20, 0.20, 0.60)
weight
## [1] 0.2 0.2 0.6
w_tbl <- tibble(symbols, weight)
w_tbl
## # A tibble: 3 × 2
##   symbols weight
##   <chr>    <dbl>
## 1 HMC        0.2
## 2 TGT        0.2
## 3 WMT        0.6

4 Build a portfolio

# ?tq_portfolio

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

portfolio_returns_tbl
## # A tibble: 60 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2013-01-31           0.0231 
##  2 2013-02-28           0.0152 
##  3 2013-03-28           0.0595 
##  4 2013-04-30           0.0375 
##  5 2013-05-31          -0.0330 
##  6 2013-06-28          -0.00539
##  7 2013-07-31           0.0340 
##  8 2013-08-30          -0.0660 
##  9 2013-09-30           0.0221 
## 10 2013-10-31           0.0340 
## # … with 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.0369 0.0369
# Mean of portfolio of returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.005570158

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: 4 × 3
##   asset        Mean  Stdev
##   <chr>       <dbl>  <dbl>
## 1 HMC       0       0.0532
## 2 TGT       0.0043  0.0609
## 3 WMT       0.0083  0.0471
## 4 Portfolio 0.00557 0.0369
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.

Based on expected return and risk of the chart, I would invest in the portfolio. Standard deviation would be the measure of volatile. A high standard deviation tends to have a wider range in returns. So depending on the return and risk of the buyer, a more consistent option would be the portfolio. If I was going to pick between the 3 stocks I would buy Walmart. The mean stock price is high but the standard deviation is still lower than the other 2 stocks. Walmart’s stock would have a wide range of returns but could potentially help make a better risk/reward option.