# Load packages
# Core
library(tidyverse)
library(tidyquant)
# Source function
source("../00_scripts/simulate_accumulation.R")
Revise the code below.
symbols <- c("HD", "WMT", "F", "IBM", "JPM")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2025-12-10")
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] "F" "HD" "IBM" "JPM" "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 F 0.25
## 2 HD 0.25
## 3 IBM 0.2
## 4 JPM 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.0506
## 2 2013-02-28 0.00678
## 3 2013-03-28 0.0287
## 4 2013-04-30 0.0240
## 5 2013-05-31 0.0775
## 6 2013-06-28 -0.0301
## 7 2013-07-31 0.0491
## 8 2013-08-30 -0.0633
## 9 2013-09-30 0.0252
## 10 2013-10-31 0.00963
## # ℹ 146 more rows
# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.009724604
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.05195607
# Construct a normal distribution
simulated_monthly_returns <- rnorm(240, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] 0.0236820495 -0.0587267183 -0.0037476840 -0.0362635311 -0.0106078124
## [6] 0.0257227924 0.1146601140 -0.0396999401 -0.0380302025 0.0243671659
## [11] -0.0499606174 -0.1282801839 -0.0340256935 -0.0387326675 0.0618651231
## [16] 0.0501222699 -0.0167981548 0.0394532221 -0.0849230767 0.0472621634
## [21] -0.0534445057 -0.0134657687 -0.0477559981 0.0441348605 0.0418831407
## [26] 0.0465021250 0.0552355601 0.0483688044 0.0074128200 -0.0406446511
## [31] -0.0160982302 -0.0289590041 0.0459218454 -0.0056121985 -0.0064079679
## [36] 0.0577717616 0.0475426608 -0.0214405657 -0.0349436503 0.0267064517
## [41] 0.0904932961 -0.0098731142 0.0020854427 0.0263235068 0.0271479251
## [46] 0.0465964459 0.0047724199 0.0649228743 -0.0295654040 0.0464397290
## [51] 0.0472660474 0.0560949258 0.0435005750 -0.0713673985 0.0442208364
## [56] 0.0291081831 -0.0176341557 0.0543122722 0.0430634188 -0.0369258818
## [61] -0.0077158777 -0.0516316866 -0.0047138600 0.0605784045 0.0467160616
## [66] -0.0180869434 -0.0682962418 0.1316127868 0.0761068837 -0.0309699888
## [71] -0.0867084848 0.1126297279 0.0730047413 0.0224845649 0.0920490795
## [76] 0.0016242879 -0.0885388734 0.0183079857 -0.1105330211 -0.0284696015
## [81] 0.0200116981 0.0898056251 -0.0250683082 -0.0327991745 -0.0281640328
## [86] -0.0497678532 -0.0025295729 0.0676590109 0.0371701586 0.0385511267
## [91] -0.0229624506 0.0447218143 0.0610372049 0.0195723818 0.1063950358
## [96] -0.0207132237 -0.0329950461 -0.0124471281 0.1135603508 0.0473395007
## [101] 0.0069216680 -0.0610956737 -0.0345307272 0.0065790644 0.0227012786
## [106] 0.0749404402 0.0265924560 -0.0407980128 -0.0421057488 0.1140782321
## [111] -0.0031726531 -0.0943106549 -0.0067421154 -0.0105193798 0.0958473163
## [116] 0.1634775794 -0.0545965780 0.0360918893 0.0289375105 -0.0768977327
## [121] -0.0043292660 -0.1179564799 0.0395734740 0.0542287795 0.0165274194
## [126] 0.0162089117 -0.0261153400 0.0143979216 -0.0633706392 -0.0813224605
## [131] 0.0701203306 -0.0225550142 0.0612972442 0.0179027949 0.0314071064
## [136] 0.0835153803 0.0775784568 0.0565224130 0.0707421850 0.0263938159
## [141] 0.0015754430 0.0670792333 0.0250433564 0.0538318228 -0.0471341751
## [146] 0.0018885664 0.0652222246 0.0013670955 -0.0394032583 -0.0041264502
## [151] 0.0111281168 0.0462894475 0.0844789948 -0.0691859553 0.0804339023
## [156] 0.0167971870 -0.0406234575 -0.0599323452 -0.0283157423 -0.0503134103
## [161] 0.0113484304 -0.0011843253 -0.0252617253 -0.0225234895 0.0919252373
## [166] 0.0501892014 -0.0611231042 -0.0098374855 -0.0275454550 0.0478653987
## [171] -0.0908332176 -0.0087215967 -0.0047942038 0.0039946175 -0.0130236123
## [176] -0.0008874724 -0.0014896960 0.0569970913 -0.0179265111 0.0488488083
## [181] -0.0764587703 0.0495605299 -0.0016096226 -0.0025603211 -0.0516156554
## [186] 0.0347897585 0.0973922302 0.0343056777 0.0234758901 0.0428816523
## [191] 0.0123349324 0.0567875253 0.0088964896 0.1002497811 -0.0199516400
## [196] 0.0609826160 -0.0170928971 -0.0008953829 0.0003687493 -0.0013616436
## [201] -0.0105433060 -0.0507215921 0.0562800131 0.0240578961 0.0671550625
## [206] 0.0227651537 0.0208270032 -0.0242192250 0.0680863358 0.0665327046
## [211] -0.0698048753 0.0596757105 0.0503673177 -0.0013265553 0.0252941465
## [216] -0.0042964407 -0.0226070416 0.0094076674 -0.0049961374 -0.0139114862
## [221] 0.0060789642 0.0410375598 -0.0755945087 -0.0287556677 0.0662839933
## [226] -0.0085183490 0.0518617208 -0.0231981802 0.0009241394 0.0806289578
## [231] -0.0055865903 -0.0095300214 0.0520661212 -0.1070008076 -0.0453452630
## [236] -0.0005611176 -0.0950666389 0.0141724988 0.0485354747 -0.0657241351
# 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 1.02
## 3 0.941
## 4 0.996
## 5 0.964
## 6 0.989
## 7 1.03
## 8 1.11
## 9 0.960
## 10 0.962
## # ℹ 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 1.02
## 3 0.964
## 4 0.960
## 5 0.925
## 6 0.915
## 7 0.939
## 8 1.05
## 9 1.00
## 10 0.967
## # ℹ 231 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 20.11041
## 6 Simulation function
***No need***
## 7 Running multiple simulations
``` r
# Create a vector of 1s as a starting point
sims <- 51
starts <- rep(100, sims) %>%
set_names(paste0("sim", 1:sims))
starts
## sim1 sim2 sim3 sim4 sim5 sim6 sim7 sim8 sim9 sim10 sim11 sim12 sim13
## 100 100 100 100 100 100 100 100 100 100 100 100 100
## sim14 sim15 sim16 sim17 sim18 sim19 sim20 sim21 sim22 sim23 sim24 sim25 sim26
## 100 100 100 100 100 100 100 100 100 100 100 100 100
## sim27 sim28 sim29 sim30 sim31 sim32 sim33 sim34 sim35 sim36 sim37 sim38 sim39
## 100 100 100 100 100 100 100 100 100 100 100 100 100
## sim40 sim41 sim42 sim43 sim44 sim45 sim46 sim47 sim48 sim49 sim50 sim51
## 100 100 100 100 100 100 100 100 100 100 100 100
# Simulate
# for reproducible research
set.seed(1234)
monte_carle_sim_51 <- starts %>%
# Simulate
map_dfc(.x = .,
.f = ~simulate_accumulation(initial_value = .x,
N = 240,
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")
# Find quantiles
monte_carle_sim_51 %>%
group_by(sim) %>%
summarise(growth = last(growth)) %>%
ungroup() %>%
pull(growth) %>%
quantile(probs = c(0, 0.25, 0.5, 0.75, 1)) %>%
round(2)
## 0% 25% 50% 75% 100%
## 139.66 539.19 930.88 1519.26 3117.72
monte_carle_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 $100 over 240 months")
# Step 1 Summarize data into max, median, and min of last value
sim_summary <- monte_carle_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 3118. 931. 140.
# Step 2 Plot
monte_carle_sim_51 %>%
# Filter for max, median, min sim
group_by(sim) %>%
filter(last(growth) == sim_summary$max |
last(growth) == sim_summary$median |
last(growth) == sim_summary$min) %>%
ungroup() %>%
# Plot
ggplot(aes(x = month, y = growth, color = sim)) +
geom_line() +
theme(legend.position = "none") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
labs(title = "Simulating Growth of $100 over 240 months",
subtitle = "Maximum, Median, and Mimimum Simulation")
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? I would expect my $100 dollars to be around $1,000 in 20 years, with average growth looking to be about 10x. As for the best case, I would see around $3,000, and worst case staying around $100. Some limitations of this simulation, are the inability to account fro major black swan events, as well as the need to provide accurate data, if you want to get out acurate results.