# 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.005899128
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.0234749
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
##   [1] -0.0119289787  0.0248803101  0.0070925250  0.0017355149 -0.0125435115
##   [6]  0.0097962814  0.0272328902 -0.0065500616  0.0081258324  0.0034690051
##  [11]  0.0363668895 -0.0121488815 -0.0043616891  0.0528124988  0.0229473138
##  [16] -0.0219244543 -0.0044566840  0.0367613869  0.0293236944  0.0330488814
##  [21]  0.0289866375  0.0159577548 -0.0267733637 -0.0157568184 -0.0153873565
##  [26] -0.0122445831  0.0045198259  0.0277468654  0.0221593556 -0.0264126459
##  [31]  0.0090764208 -0.0368587595 -0.0192030192 -0.0041077605 -0.0110927750
##  [36]  0.0171907839 -0.0176138880 -0.0195730479  0.0241806807  0.0196732687
##  [41]  0.0271270442  0.0066637072  0.0053440406 -0.0269443715  0.0146610968
##  [46] -0.0153156752  0.0014188435  0.0252321245  0.0174909375  0.0446893781
##  [51] -0.0111376122 -0.0002239654  0.0116347166 -0.0136996474  0.0260365414
##  [56] -0.0122949742  0.0204862768 -0.0152470324  0.0255498530  0.0051998637
##  [61]  0.0199839081  0.0083605073  0.0084261088  0.0115632641 -0.0003362293
##  [66]  0.0120567324  0.0460377010  0.0099702684 -0.0368276218  0.0334887896
##  [71]  0.0076180524  0.0288262224  0.0134662988 -0.0072901387  0.0263591006
##  [76]  0.0420067172  0.0052278989  0.0079640677 -0.0099611313 -0.0007714796
##  [81] -0.0228736753  0.0055703372 -0.0188962221  0.0037827325 -0.0533166953
##  [86] -0.0291540848 -0.0088558398 -0.0144620685  0.0354928827  0.0077410524
##  [91]  0.0021734339  0.0088045618  0.0116885338 -0.0028757396  0.0254778410
##  [96]  0.0278855930  0.0126268896  0.0145409837  0.0064902481  0.0265212576
## [101] -0.0240621862 -0.0146681901  0.0327159445  0.0009665242 -0.0038937955
## [106]  0.0504854836 -0.0503848495  0.0600292627 -0.0344236003  0.0050661209
## [111] -0.0096488363  0.0243022706  0.0359048236  0.0018120438 -0.0221499335
## [116]  0.0233547598 -0.0117029380  0.0100652561 -0.0209325572 -0.0171418243
# 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.988
##  3   1.02 
##  4   1.01 
##  5   1.00 
##  6   0.987
##  7   1.01 
##  8   1.03 
##  9   0.993
## 10   1.01 
## # ℹ 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.988
##  3  1.01 
##  4  1.02 
##  5  1.02 
##  6  1.01 
##  7  1.02 
##  8  1.05 
##  9  1.04 
## 10  1.05 
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 6.088843

6 Simulation function

simulate_accumulation <- function(initial_value, n = 120, mu = mean_port_return, sigma = stddev_port_return) {
  tibble(returns = c(initial_value, 1 + rnorm(n, mu, sigma))) %>%
    mutate(growth = accumulate(returns, function(x, y) x * y)) %>%
    select(growth)
}

dump(list = c("simulation_accumulation"), file = "../00_scripts/simulate_accumulation.R")

7 Running multiple simulations

set.seed(1234)

sims <- 51
starts <- rep(1, sims) %>% set_names(paste0("sim", 1:sims))

monte_carlo_sim_51 <- map_dfc(.x = starts, .f = ~ simulate_accumulation(initial_value = .x)) %>%
  mutate(month = 0:120) %>%
  select(month, everything()) %>%
  set_names(c("month", names(starts))) %>%
  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     0 sim1       1
##  2     0 sim2       1
##  3     0 sim3       1
##  4     0 sim4       1
##  5     0 sim5       1
##  6     0 sim6       1
##  7     0 sim7       1
##  8     0 sim8       1
##  9     0 sim9       1
## 10     0 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(show.legend = FALSE) +
  labs(title = "Simulations of $1 Growth Over 120 Months") +
  theme(plot.title = element_text(hjust = 0.5))

9 Max, Median, Min Plot

sim_summary <- monte_carlo_sim_51 %>%
  group_by(sim) %>%
  summarize(growth = last(growth)) %>%
  ungroup()

extremes <- monte_carlo_sim_51 %>%
  group_by(sim) %>%
  filter(last(growth) %in% c(max(sim_summary$growth), median(sim_summary$growth), min(sim_summary$growth))) %>%
  ungroup()

extremes %>%
  ggplot(aes(x = month, y = growth, color = sim)) +
  geom_line() +
  labs(title = "Simulations of $1 Growth Over 120 Months",
       subtitle = "Max, Median, and Min Simulations") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

10 Summary Quantiles

monte_carlo_sim_51 %>%
  group_by(sim) %>%
  summarize(growth = last(growth)) %>%
  pull(growth) %>%
  quantile(probs = c(0, 0.25, 0.5, 0.75, 1)) %>%
  round(2)
##   0%  25%  50%  75% 100% 
## 1.17 1.59 1.98 2.40 3.88