# 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("NFLX", "AAPL", "VRTX")

# Using tq_get() ----
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 %>%

    # Calculate monthly returns
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 type = "log") %>%
    slice(-1) %>%
    ungroup() %>%

    # remane
    set_names(c("asset", "date", "returns"))

# period_returns = c("yearly", "quarterly", "monthly", "weekly")

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

symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()

w <- c(0.25,
       0.25,
       0.20)

w_tbl <- tibble(symbols, w)

4 Build a portfolio

portfolio_returns_rebalanced_monthly_tbl <- asset_returns_tbl %>%
    
    tq_portfolio(assets_col   = asset,
                 returns_col  = returns,
                 weights      = w_tbl,
                 col_rename   = "returns",
                 rebalance_on = "months")

portfolio_returns_rebalanced_monthly_tbl
## # A tibble: 60 × 2
##    date       returns
##    <date>       <dbl>
##  1 2013-01-31  0.119 
##  2 2013-02-28  0.0349
##  3 2013-03-28  0.0344
##  4 2013-04-30  0.100 
##  5 2013-05-31  0.0260
##  6 2013-06-28 -0.0495
##  7 2013-07-31  0.0691
##  8 2013-08-30  0.0455
##  9 2013-09-30  0.0177
## 10 2013-10-31  0.0214
## # ℹ 50 more rows
# write_rds(portfolio_returns_rebalanced_monthly_tbl,
#           "00_data/Ch03_portfolio_returns_rebalanced_monthly_tbl.rds")

5 Compute Standard Deviation

portfolio_sd_tidyquant_builtin_percent <- portfolio_returns_rebalanced_monthly_tbl %>%
    
    tq_performance(Ra = returns,
                   Rb = NULL, 
                   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.0492 0.0492
# Mean of portfolio returns
portfolio_mean_tidyquant_builtin_percent <- 
mean(portfolio_returns_rebalanced_monthly_tbl)

portfolio_mean_tidyquant_builtin_percent
## [1] NA

6 Plot: Expected Returns versus 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 AAPL       0.015  0.0695
## 2 NFLX       0.0446 0.133 
## 3 VRTX       0.0212 0.107 
## 4 Portfolio NA      0.0492

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.

Investing in a single stock like NFLX may offer a higher expected return compared to the portfolio, as NFLX has the highest mean return among the provided assets. Individual assets like NFLX have higher expected returns (0.0446) compared to AAPL (0.015) and VRTX (0.0212). However, this comes with higher risk. AAPL, with a lower expected return but also lower risk, might be a safer choice if seeking a balance between return and risk.