# Load packages
# Core
library(tidyverse)
library(tidyquant)
# time series
library(timetk)
Simulate future portfolio returns
five stocks: “SPY”, “EFA”, “IJS”, “EEM”, “AGG”
market: “SPY”
from 2012-12-31 to 2017-12-31
symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
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"))
# 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
# ?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
# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.005899133
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.02347489
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] -0.0084868919 0.0312228518 0.0280970305 -0.0082001505 0.0018584719
## [6] -0.0258792309 0.0345958176 0.0180622747 0.0059517662 0.0028183118
## [11] -0.0680182078 -0.0001613288 0.0259980709 -0.0159437905 0.0093500418
## [16] 0.0264809956 0.0448856505 0.0071531298 -0.0042016483 0.0049936724
## [21] -0.0011954927 -0.0040526641 -0.0033360873 -0.0229403022 0.0357044763
## [26] 0.0419644020 -0.0064802373 0.0058513503 0.0290480583 0.0407778499
## [31] 0.0383974807 0.0125616428 -0.0013686006 0.0009777797 0.0044943318
## [36] 0.0187498324 0.0055222855 0.0664791764 0.0345033009 0.0194096081
## [41] -0.0237587614 0.0333556529 -0.0381133912 -0.0034315801 0.0213932859
## [46] -0.0130155299 -0.0058145517 -0.0302385822 0.0449990505 0.0315130761
## [51] -0.0043706158 0.0181166909 -0.0042180063 -0.0031562121 0.0314127696
## [56] -0.0278016233 0.0339874256 0.0236191058 0.0381409742 0.0153325958
## [61] 0.0171861316 -0.0131673444 0.0491985477 0.0092468391 0.0201637402
## [66] 0.0258153473 -0.0294316860 0.0016665759 -0.0215407056 -0.0142887032
## [71] 0.0030197727 0.0051517473 -0.0193010070 0.0005276612 -0.0176749324
## [76] 0.0544549587 -0.0268113758 -0.0488230641 0.0030921468 -0.0166007671
## [81] 0.0210035680 -0.0401946446 0.0042704901 -0.0186983832 0.0095065508
## [86] 0.0176529787 0.0151628674 0.0304172852 -0.0045587373 0.0269712491
## [91] -0.0098624716 0.0444366283 0.0320230418 0.0061614626 -0.0062567165
## [96] 0.0045415358 0.0082756264 0.0427171572 0.0081493847 0.0059001569
## [101] -0.0219585579 -0.0006505693 0.0337360408 -0.0009205105 -0.0009967471
## [106] 0.0286919584 -0.0184559054 -0.0082824781 0.0213109045 0.0029454695
## [111] 0.0410511044 -0.0247455954 0.0277141128 0.0223369876 0.0050996197
## [116] 0.0168498562 -0.0063857639 -0.0348241599 -0.0043820948 0.0360062492
# 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.992
## 3 1.03
## 4 1.03
## 5 0.992
## 6 1.00
## 7 0.974
## 8 1.03
## 9 1.02
## 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.992
## 3 1.02
## 4 1.05
## 5 1.04
## 6 1.04
## 7 1.02
## 8 1.05
## 9 1.07
## 10 1.08
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 8.560687
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 286.
## 2 284.
## 3 287.
## 4 285.
## 5 290.
## 6 295.
# 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
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")