# 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

# Choose stocks
symbols <- c("CRWD", "AMZN", "SHOP","TTD", "NVDA")

prices <- tq_get(x = symbols, 
                 get  = "stock.prices", 
                 from = "2022-01-01")

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"))

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMZN" "CRWD" "NVDA" "SHOP" "TTD"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)

w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 Ă— 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMZN       0.25
## 2 CRWD       0.25
## 3 NVDA       0.2 
## 4 SHOP       0.2 
## 5 TTD        0.1

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 = "months" )

portfolio_returns_tbl
## # A tibble: 33 Ă— 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2022-02-28           -0.0201
##  2 2022-03-31            0.0490
##  3 2022-04-29           -0.286 
##  4 2022-05-31           -0.0995
##  5 2022-06-30           -0.118 
##  6 2022-07-29            0.146 
##  7 2022-08-31           -0.0400
##  8 2022-09-30           -0.135 
##  9 2022-10-31            0.0275
## 10 2022-11-30           -0.0149
## # ℹ 23 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.113 0.113
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

portfolio_mean_tidyquant_builtin_percent
## [1] 0.01642933

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 AMZN       0.0069 0.102
## 2 CRWD       0.0156 0.162
## 3 NVDA       0.051  0.162
## 4 SHOP      -0.0049 0.186
## 5 TTD        0.0159 0.129
## 6 Portfolio  0.0164 0.113
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.

You should expect around a 1.6 percent return per quarter with slight risk, this portfolio is above the center line, so it gives a little bit more return compared to it’s riskiness. The best stock for me would be NVDA, sue to it having a high chance for return while also being risky, which I like. A risk adverse person would be better off with a stock like AMZN, it is safe and provides small returns. A stock like SHOP is very risky and provides a low mean of returns. This stock may not be a good pick for any investor based on the data. TTD is also a decent stock for risk aversion, it has provided around 1.5 percent returns with medium-low risk.