# Load packages

# Core
library(tidyverse)
library(tidyquant)

# time series
library(timetk)

Goal

Simulate future portfolio returns

five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG”

market: “SPY”

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",    
                 from = "2012-12-31",
                 to   = "2017-12-31")

2 Convert prices to returns

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

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AGG" "EEM" "EFA" "IJS" "SPY"
# weights
weights <- c(0.25, 0.25, 0.2, 0.2, 0.1)
weights
## [1] 0.25 0.25 0.20 0.20 0.10
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AGG        0.25
## 2 EEM        0.25
## 3 EFA        0.2 
## 4 IJS        0.2 
## 5 SPY        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", 
                 col_rename = "returns")

portfolio_returns_tbl
## # A tibble: 60 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0204 
##  2 2013-02-28 -0.00239
##  3 2013-03-28  0.0121 
##  4 2013-04-30  0.0174 
##  5 2013-05-31 -0.0128 
##  6 2013-06-28 -0.0247 
##  7 2013-07-31  0.0321 
##  8 2013-08-30 -0.0224 
##  9 2013-09-30  0.0511 
## 10 2013-10-31  0.0301 
## # ℹ 50 more rows

5 Simulating growth of a dollar

# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.005899134
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.02347493
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
##   [1] -4.083250e-02 -2.367904e-02  2.136623e-02 -4.228475e-02  5.195523e-02
##   [6] -6.591993e-03  1.565687e-02  5.586288e-02 -1.403147e-02 -3.034322e-02
##  [11]  2.699580e-02 -8.507786e-03 -6.685196e-03 -1.191088e-02 -9.465720e-04
##  [16] -7.832867e-03  4.382644e-02  1.445580e-02  1.539713e-03  2.179880e-02
##  [21]  2.196824e-03 -1.011522e-02 -1.823323e-02  2.823963e-04 -1.061482e-02
##  [26] -4.272469e-03 -2.958370e-02  1.490782e-02 -6.569322e-03 -1.612754e-03
##  [31]  3.596628e-02 -5.157085e-03  1.808325e-02 -2.232898e-02  2.516978e-02
##  [36]  5.603407e-03 -1.157075e-02  9.891943e-03 -3.679096e-02  5.660111e-02
##  [41]  3.341617e-02  2.609679e-02  2.034446e-02 -2.072964e-02 -3.400697e-02
##  [46] -6.412563e-03  3.689197e-02  5.231295e-03  2.263701e-02  5.315863e-02
##  [51] -1.536070e-02  5.476737e-02  1.349881e-02  1.470367e-02  2.937865e-02
##  [56]  2.316380e-02 -1.215675e-02  3.300537e-03  2.286260e-02  1.299513e-02
##  [61]  5.001158e-03 -3.002129e-02  6.267549e-03 -2.022697e-02 -8.453475e-03
##  [66] -1.182710e-02  1.221311e-02  2.248478e-02 -7.276517e-03  2.303737e-03
##  [71]  2.631493e-02  1.952305e-02  8.006387e-03 -1.561327e-02  1.155522e-02
##  [76]  2.278592e-04 -2.500149e-02  2.516151e-02  1.683830e-02  2.880140e-02
##  [81] -3.895232e-03  1.165626e-02  3.730854e-02  6.359262e-03  6.075890e-03
##  [86] -8.472808e-03  4.630779e-05  8.658059e-03  1.137102e-02  2.742422e-03
##  [91]  2.559758e-02  1.742127e-02  5.168364e-02 -4.766689e-03 -4.483439e-03
##  [96]  3.355599e-02  2.321384e-04  2.828144e-02  6.527136e-03  2.094865e-02
## [101]  4.075086e-02  1.424924e-02  3.173190e-02 -2.840387e-02 -2.459915e-02
## [106] -1.829115e-02  2.822411e-02 -2.123454e-02  1.589371e-02  2.099728e-02
## [111] -3.070517e-03 -3.565692e-02 -1.371217e-04  1.072905e-02 -1.037740e-02
## [116]  1.877482e-02 -2.622007e-02  3.349118e-02  4.537831e-02  6.238672e-02
# Add a dollar
simulated_returns_add_1 <- tibble(returns = c(1, 1 + simulated_monthly_returns))
simulated_returns_add_1
## # A tibble: 121 × 1
##    returns
##      <dbl>
##  1   1    
##  2   0.959
##  3   0.976
##  4   1.02 
##  5   0.958
##  6   1.05 
##  7   0.993
##  8   1.02 
##  9   1.06 
## 10   0.986
## # ℹ 111 more rows
# Calculate the cumulative growth of a dollar
simulated_growth <- simulated_returns_add_1 %>%
    mutate(growth = accumulate(returns, function(x, y) x*y)) %>%
    select(growth)

simulated_growth
## # A tibble: 121 × 1
##    growth
##     <dbl>
##  1  1    
##  2  0.959
##  3  0.936
##  4  0.956
##  5  0.916
##  6  0.964
##  7  0.957
##  8  0.972
##  9  1.03 
## 10  1.01 
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 8.209101

6 Simulation function

simulate_accumulation <- function(initial_value, N, mean_return, sd_return) {
   
    # Add a dollar
    simulated_returns_add_1 <- tibble(returns = c(initial_value, 1 + rnorm(N, mean_return, sd_return)))
    
    # Calculate the cumulative growth of a dollar
    simulated_growth <- simulated_returns_add_1 %>%
        mutate(growth = accumulate(returns, function(x, y) x*y)) %>%
        select(growth)
    
    return(simulated_growth)
    
}

simulate_accumulation(initial_value = 100, N = 240, mean_return = 0.005, sd_return = 0.01) %>%
    tail()
## # A tibble: 6 × 1
##   growth
##    <dbl>
## 1   390.
## 2   390.
## 3   391.
## 4   393.
## 5   399.
## 6   402.

7 Running multiple simulations

# Create a vector of 1s as a starting point
sims <- 51
starts <- rep(1, sims) %>%
    set_names(paste0("sim", 1:sims))

starts
##  sim1  sim2  sim3  sim4  sim5  sim6  sim7  sim8  sim9 sim10 sim11 sim12 sim13 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## sim14 sim15 sim16 sim17 sim18 sim19 sim20 sim21 sim22 sim23 sim24 sim25 sim26 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## sim27 sim28 sim29 sim30 sim31 sim32 sim33 sim34 sim35 sim36 sim37 sim38 sim39 
##     1     1     1     1     1     1     1     1     1     1     1     1     1 
## sim40 sim41 sim42 sim43 sim44 sim45 sim46 sim47 sim48 sim49 sim50 sim51 
##     1     1     1     1     1     1     1     1     1     1     1     1
# Simulate
# for reproducible research
set.seed(1234)

monte_carlo_sim_51 <- starts %>%
    
    # Simulate
    map_dfc(.x = .,
            .f = ~simulate_accumulation(initial_value = .x, 
                                        N             = 120, 
                                        mean_return   = mean_port_return, 
                                        sd_return     = stddev_port_return)) %>%
    
    # Add column month
    mutate(month = 1:nrow(.)) %>%
    select(month, everything()) %>%
    
    # Rearrange column names
    set_names(c("month", names(starts))) %>%
    
    # Transform to long form
    pivot_longer(cols = -month, names_to = "sim", values_to = "growth")

monte_carlo_sim_51
## # A tibble: 6,171 × 3
##    month sim   growth
##    <int> <chr>  <dbl>
##  1     1 sim1       1
##  2     1 sim2       1
##  3     1 sim3       1
##  4     1 sim4       1
##  5     1 sim5       1
##  6     1 sim6       1
##  7     1 sim7       1
##  8     1 sim8       1
##  9     1 sim9       1
## 10     1 sim10      1
## # ℹ 6,161 more rows

8 Visualizing simulations with ggplot

monte_carlo_sim_51 %>%
    
    ggplot(aes(x = month, y = growth, color = sim)) +
    geom_line() +
    theme(legend.position = "none") + 
    theme(plot.title = element_text(hjust = 0.5)) +
    
    labs(title = "Simulating growth of $1 over 120 months")