# Load packages

# Core
library(tidyverse)
library(tidyquant)

# Source function
# source("../00_scripts/simulate_accumulation.R")

1 Import stock prices

Revise the code below.

symbols <- c("UAL", "LUV", "DAL", "AAL")

prices <- tq_get(x    = symbols,
                 get  = "stock.prices",    
                 from = "2012-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

Revise the code for weights.

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAL" "DAL" "LUV" "UAL"
# weights
weights <- c(0.4, 0.3, 0.2, 0.1)
weights
## [1] 0.4 0.3 0.2 0.1
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAL         0.4
## 2 DAL         0.3
## 3 LUV         0.2
## 4 UAL         0.1

4 Build a 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: 150 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0910 
##  2 2013-02-28  0.00218
##  3 2013-03-28  0.184  
##  4 2013-04-30  0.0137 
##  5 2013-05-31  0.0376 
##  6 2013-06-28 -0.0377 
##  7 2013-07-31  0.128  
##  8 2013-08-30 -0.128  
##  9 2013-09-30  0.151  
## 10 2013-10-31  0.136  
## # ℹ 140 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.00516951
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.103914
# Construct a normal distribution
simulated_monthly_returns <- rnorm(240, mean_port_return, stddev_port_return)
simulated_monthly_returns
##   [1] -0.0645389063 -0.1204551172  0.0118087008  0.0218728654  0.2348337993
##   [6] -0.1116935645 -0.1301102373 -0.1335867589  0.0651687604  0.1369131307
##  [11]  0.0126115755 -0.0423985951 -0.0583294679  0.0609237111  0.0967588417
##  [16]  0.1202649307  0.0600305591 -0.2519171845  0.1668204649  0.0523589414
##  [21] -0.1182400524 -0.0322181915 -0.0344530390 -0.2818467292 -0.0447543857
##  [26]  0.1419295611 -0.0575303248 -0.0346783514  0.0129618771 -0.0546539843
##  [31] -0.0042800064  0.0695415751 -0.1800634385  0.0916422270  0.1572451601
##  [36]  0.1516561922  0.0843807679  0.1515496005  0.0042901754  0.0792726999
##  [41]  0.0885973730 -0.0197342968 -0.0971212650 -0.0754243628  0.0854720739
##  [46]  0.0034563040  0.0459504255 -0.0234847679  0.1527849786  0.0692410199
##  [51] -0.0713946413 -0.0778078865  0.1630212582  0.0475355795  0.0265904273
##  [56] -0.0793946601  0.0420936815 -0.0301924332 -0.0728314396  0.0260782373
##  [61] -0.0427799139 -0.0713992067 -0.0338401316  0.0752777385 -0.0795071739
##  [66]  0.0297612524  0.1088983381  0.1124001062  0.0556158585  0.0404075736
##  [71] -0.0441250279  0.0638440112  0.1104491731  0.0589298584 -0.0869511131
##  [76] -0.1954588724 -0.0054973131 -0.0506316323 -0.0504222913  0.1427201505
##  [81] -0.1334196971 -0.1551462208 -0.1044697743  0.1467234454  0.0852206643
##  [86] -0.0376403705  0.1557107044 -0.0733117094  0.1839668169 -0.0377154525
##  [91] -0.1186814145 -0.0257692090 -0.0485089721  0.0823226659 -0.0813087012
##  [96] -0.0218203641  0.0685167243  0.0516171259  0.1565290948 -0.0480559838
## [101]  0.0235723389  0.1083372624 -0.0779521674  0.0926631236  0.1401468375
## [106] -0.0643749828  0.2453351774  0.1387554080  0.0789184472  0.0685145718
## [111]  0.0526574618  0.1469114164  0.1070500744 -0.0056847404 -0.0477757891
## [116] -0.0263835395 -0.1445118798  0.0086363850 -0.0611387932 -0.1033371008
## [121]  0.0002226198  0.1834776995  0.2070400256 -0.0202635010 -0.1084336721
## [126] -0.0422081174  0.0348200730 -0.0811491338  0.0311614022  0.3301899698
## [131] -0.2707473505  0.1720928250 -0.0137443432  0.0182843260 -0.0218645379
## [136]  0.1263369639  0.1029327868  0.2348577661 -0.1304875798 -0.0171059679
## [141]  0.1106440231  0.0194295869  0.0633577072 -0.0095884502 -0.1381132062
## [146] -0.0666907778  0.2220334889  0.1256533182  0.0261061012 -0.0696255127
## [151]  0.0040457630  0.0008981494 -0.1305507850 -0.1437196150 -0.0146577928
## [156] -0.1032793568  0.0020191621 -0.1726372644  0.0004726234 -0.1580498604
## [161]  0.0776113664  0.0703885001  0.1059917049 -0.1110379920 -0.0380755636
## [166] -0.0535149023  0.0217367451  0.1917589454  0.1900939868  0.0395872283
## [171]  0.1900666371 -0.1197023510 -0.1652089241 -0.0751328792 -0.0761689687
## [176]  0.0696480568  0.0270041934 -0.0239328291  0.0324719491  0.1416313358
## [181] -0.0817788146 -0.1636826698 -0.0271774974 -0.1083015714  0.0610953157
## [186]  0.0201331421 -0.0241752790  0.2792940208 -0.0465130601  0.0552936341
## [191] -0.0266695281  0.0631702350  0.0499792297  0.1358084738  0.0673945817
## [196] -0.1769297904 -0.0981526276 -0.0645784443 -0.0946074794 -0.0256588209
## [201] -0.0102259002 -0.1155005059  0.0518880405 -0.0827658401  0.0258512423
## [206]  0.0397308738 -0.2068173947 -0.1605522590  0.0683688910 -0.0016218167
## [211]  0.0814743482 -0.1042481854  0.0484414756 -0.0243207713  0.0308561997
## [216]  0.0904259824 -0.1569174276  0.0137943967 -0.0303163931 -0.0329601441
## [221] -0.0016799117 -0.1097780343 -0.0227719869 -0.1097450583 -0.0674261956
## [226] -0.0600158611  0.1096735191  0.1167355633  0.0242460951 -0.1253581272
## [231] -0.0019124132 -0.1273292896  0.0819287234 -0.0075337380 -0.0483479399
## [236] -0.0893686270  0.0676245069 -0.1932349297  0.0088250021  0.1433915393
# Add a dollar
simulated_returns_add_1 <- tibble(returns = c(1, 1 + simulated_monthly_returns))
simulated_returns_add_1
## # A tibble: 241 × 1
##    returns
##      <dbl>
##  1   1    
##  2   0.935
##  3   0.880
##  4   1.01 
##  5   1.02 
##  6   1.23 
##  7   0.888
##  8   0.870
##  9   0.866
## 10   1.07 
## # ℹ 231 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: 241 × 1
##    growth
##     <dbl>
##  1  1    
##  2  0.935
##  3  0.823
##  4  0.832
##  5  0.851
##  6  1.05 
##  7  0.933
##  8  0.812
##  9  0.703
## 10  0.749
## # ℹ 231 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] -3.033291

6 Simulation function

No need

simulate_accumulation <- function(initial_value, n = 240, 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)
}

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:240) %>%
  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: 12,291 × 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
## # ℹ 12,281 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 240 Months") +
  theme(plot.title = element_text(hjust = 0.5))

10 Line Plot of Simulations with Max, Median, and Min

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 240 Months",
       subtitle = "Max, Median, and Min Simulations") +
  theme(plot.title = element_text(hjust = 0.5),
        plot.subtitle = element_text(hjust = 0.5))

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% 
##  0.03  0.49  1.58  3.90 16.56

Based on the Monte Carlo simulation results, how much should you expect from your $100 investment after 20 years? What is the best-case scenario? What is the worst-case scenario? What are limitations of this simulation analysis?

Based off of the simulation, I can expect my best-case scenario growth to be at around 16.56, with my worst-case scenario sitting at 0.03. The limitation of running a simulation analysis is the lack of accounting for major events that could impact these investments. Analyzing historical data to make future inferences is an educated guess at best, rather than a direct play-out.