# Load packages
# Core
library(tidyverse)
library(tidyquant)
library(ggplot2)
library(timetk)
symbols <- c("WMT", "TGT", "COST", "HD", "M")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2025-12-06")
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"))
Revise the code for weights.
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "COST" "HD" "M" "TGT" "WMT"
# 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 COST 0.25
## 2 HD 0.25
## 3 M 0.2
## 4 TGT 0.2
## 5 WMT 0.1
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: 156 Ă— 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0378
## 2 2013-02-28 0.0224
## 3 2013-03-28 0.0451
## 4 2013-04-30 0.0405
## 5 2013-05-31 0.0319
## 6 2013-06-28 -0.00314
## 7 2013-07-31 0.0328
## 8 2013-08-30 -0.0715
## 9 2013-09-30 0.0124
## 10 2013-10-31 0.0316
## # ℹ 146 more rows
# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.009404048
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.05598326
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] 0.0037949192 -0.0781037365 0.0115591942 0.0286923947 0.0330604966
## [6] 0.0022414189 -0.0002638147 0.0105429992 -0.0239615729 0.0250224402
## [11] -0.0456805679 -0.0139089126 0.0397611114 -0.0747356346 -0.0644503115
## [16] -0.0201583313 0.0170107722 0.0394875407 -0.0948628738 0.0262406278
## [21] 0.0372439307 -0.0466512337 0.0491963436 -0.1118704191 -0.0153468858
## [26] 0.0717957633 0.0588692109 0.0436521424 0.0412608512 0.0192404538
## [31] -0.0394562359 0.0643716105 0.0625891868 -0.0631324641 -0.0417115080
## [36] 0.0410055656 0.0264222196 0.0067748925 0.0434502633 -0.0806546799
## [41] -0.0478840606 0.0004647828 0.0211521649 -0.0040575482 0.0981437848
## [46] 0.0591006722 0.0415914186 -0.0186397494 0.0112100578 0.0968872666
## [51] -0.0553058254 -0.0645869218 0.0079473452 0.0010828059 -0.0573440675
## [56] -0.0101259387 0.0644258793 0.0103408073 -0.0471496567 -0.0681211812
## [61] -0.0158532312 0.0385085303 0.0224356864 0.0430806128 0.0576806292
## [66] 0.0175528644 0.0943599990 0.1071336413 -0.0077695046 -0.0137156275
## [71] 0.0593595841 0.0045701572 -0.0189609827 0.0314271362 0.0895313303
## [76] -0.0244480627 -0.0679864614 0.0243498424 0.0384604238 -0.0402145206
## [81] -0.0109573591 0.0221197932 -0.0240287167 0.0331012355 -0.0524913458
## [86] -0.1471495990 0.0639754741 0.0425480275 -0.0752169920 -0.0112036736
## [91] 0.0453635567 -0.0585968845 -0.0230144413 -0.0095612620 -0.0487226578
## [96] 0.0620465043 -0.0538660736 0.0584047961 -0.0060767291 -0.1149124933
## [101] -0.0289738371 -0.0275635431 -0.0678107163 -0.0094197953 -0.0286114791
## [106] -0.0492810636 0.0357617118 0.0708717565 -0.0635830027 0.0653257482
## [111] 0.0204400403 0.0811143199 0.0052422955 -0.0073533135 -0.0937219805
## [116] 0.0299578645 -0.0752363943 -0.0483049841 0.0517757505 -0.0829652516
# 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 1.00
## 3 0.922
## 4 1.01
## 5 1.03
## 6 1.03
## 7 1.00
## 8 1.000
## 9 1.01
## 10 0.976
## # ℹ 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 1.00
## 3 0.925
## 4 0.936
## 5 0.963
## 6 0.995
## 7 0.997
## 8 0.997
## 9 1.01
## 10 0.983
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] -2.235242
simulate_accumulation <- function(init_value, N, mean, stdev) {
tibble(returns = c(init_value, 1 + rnorm(N, mean, stdev))) %>%
mutate(growth = accumulate(returns, function(x, y) x*y)) %>%
select(growth)
}
simulate_accumulation(1, 120, mean_port_return, stddev_port_return)
## # A tibble: 121 Ă— 1
## growth
## <dbl>
## 1 1
## 2 1.03
## 3 1.08
## 4 1.11
## 5 1.19
## 6 1.13
## 7 1.18
## 8 1.20
## 9 1.20
## 10 1.12
## # ℹ 111 more rows
# Save the function
dump(list = c("simulate_accumulation"), file = "../00_scripts/simulate_accumulation.R")
# Create a vector of 1s as a starting point
sims <- 51
starts <- rep(1, sims) %>%
set_names(paste("sim", 1:sims, sep = ""))
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
monte_carlo_sim_51 <- starts %>%
# Simulate
map_dfc(simulate_accumulation,
N = 120,
mean = mean_port_return,
stdev = stddev_port_return) %>%
# Add the column, month
mutate(month = seq(1:nrow(.))) %>%
# Arrange column names
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 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
# Calculate the quantiles for simulated values
probs <- c(.005, .025, .25, .5, .75, .975, .995)
monte_carlo_sim_51 %>%
group_by(sim) %>%
summarise(growth = last(growth)) %>%
ungroup() %>%
pull(growth) %>%
# Find the quantiles
quantile(probs = probs) %>%
round(2)
## 0.5% 2.5% 25% 50% 75% 97.5% 99.5%
## 1.03 1.05 1.65 2.30 3.58 5.74 6.21
monte_carlo_sim_51 %>%
ggplot(aes(x = month, y = growth, col = sim)) +
geom_line() +
theme(legend.position = "none")
sim_summary <- monte_carlo_sim_51 %>%
group_by(sim) %>%
summarise(growth = last(growth)) %>%
ungroup() %>%
summarise(max = max(growth),
median = median(growth),
min = min(growth))
sim_summary
## # A tibble: 1 Ă— 3
## max median min
## <dbl> <dbl> <dbl>
## 1 6.35 2.30 1.03
monte_carlo_sim_51 %>%
group_by(sim) %>%
filter(last(growth) == sim_summary$max |
last(growth) == sim_summary$median |
last(growth) == sim_summary$min) %>%
ggplot(aes(month, growth, col = sim)) +
geom_line() +
theme()
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?
You should expect roughly $220-$230 from your $100 investment after 20 years. The best case scenario (the green line) reaches about $800. The worst case scenario (the red line) drops below $50. The limitations of the simulation analysis are that it assumed past volatility patterns will continue, which may not reflect the future market. There are only a limited number of scenarios that were simulated, so extreme events might not be captured. The simulation also doesn’t account for fees, taxes, or changes in economic conditions.