# 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("NVDA", "MSFT", "AMD", "INTL")
prices <- tq_get(x    = symbols, 
                 get  = "stock.prices",
                 from = "2017-12-31",
                 to   = "2023-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) %>%
    
    ungroup() %>%
    
    set_names(c("asset", "date", "returns"))

asset_returns_tbl
## # A tibble: 73 × 3
##    asset date        returns
##    <chr> <date>        <dbl>
##  1 AMD   2018-06-29  0.400  
##  2 AMD   2018-09-28  0.723  
##  3 AMD   2018-12-31 -0.515  
##  4 AMD   2019-03-29  0.324  
##  5 AMD   2019-06-28  0.174  
##  6 AMD   2019-09-30 -0.0465 
##  7 AMD   2019-12-31  0.459  
##  8 AMD   2020-03-31 -0.00832
##  9 AMD   2020-06-30  0.146  
## 10 AMD   2020-09-30  0.444  
## # ℹ 63 more rows

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AMD"  "INTL" "MSFT" "NVDA"
# weights
weights <- c(0.35, 0.35, 0.15, 0.15)
weights
## [1] 0.35 0.35 0.15 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AMD        0.35
## 2 INTL       0.35
## 3 MSFT       0.15
## 4 NVDA       0.15

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: 23 × 2
##    date       portfolio.returns
##    <date>                 <dbl>
##  1 2018-06-29           0.156  
##  2 2018-09-28           0.302  
##  3 2018-12-31          -0.309  
##  4 2019-03-29           0.181  
##  5 2019-06-28           0.0673 
##  6 2019-09-30          -0.00133
##  7 2019-12-31           0.225  
##  8 2020-03-31           0.0146 
##  9 2020-06-30           0.145  
## 10 2020-09-30           0.214  
## # ℹ 13 more rows

5 Compute Standard Deviation

portfolia_sd_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
    
    tq_performance(Ra = portfolio.returns, 
                   performance_fun = table.Stats) %>%
    
    select(Stdev) %>%
    mutate(tq_sd = round(Stdev, 4))

portfolia_sd_tidyquant_builtin_percent
## # A tibble: 1 × 2
##   Stdev tq_sd
##   <dbl> <dbl>
## 1 0.161 0.161
# Mean of portfolio returns
porfolio_mean_tidyquant_builtin_percent <- mean(portfolio_returns_tbl$portfolio.returns)

porfolio_mean_tidyquant_builtin_percent
## [1] 0.06712771

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  = porfolio_mean_tidyquant_builtin_percent,
                   Stdev = portfolia_sd_tidyquant_builtin_percent$tq_sd))

sd_mean_tbl
## # A tibble: 5 × 3
##   asset       Mean  Stdev
##   <chr>      <dbl>  <dbl>
## 1 AMD       0.117  0.299 
## 2 INTL      0.0418 0.0694
## 3 MSFT      0.0644 0.114 
## 4 NVDA      0.0938 0.314 
## 5 Portfolio 0.0671 0.161
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.

Answer to Question

In the case of this portfolio I would expect the portfolio to preform better relative to the individual stocks long term. Reason for this is because we look at the risk and return, NVDA and AMD are looking at some serious return, but with exeptionaly high risk so long term I would be skeptical about “gambling” with all my money in just one of these stocks. We can also look at MSFT and INTL, INTL has a low risk and low reward I would only invest in this stock as a long term option, or if the dividend pay was significant (The dividend isn’t high, But I still own this stock in my personal portfolio as the stock is relatively safe, and alowes me to take more risks in other sectors). MSFT is in almost the same boat, but I would say worse since the gap between the reward and risk is pretty significant with risk beeing much higher. Based on the data and the weight I would trust that the preformance of the portfolio will out preform most of the stock (in the long run).