# 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
## # … with 50 more rows
# Get mean portfolio return
mean_port_return <- mean(portfolio_returns_tbl$returns)
mean_port_return
## [1] 0.005899135
# 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] -3.544649e-03 4.290299e-02 -3.355401e-02 1.775814e-05 7.608219e-02
## [6] 1.895069e-02 3.177871e-02 1.008645e-02 1.887204e-02 -2.500742e-02
## [11] 1.876245e-02 2.431999e-02 -1.688573e-02 1.653601e-02 1.604130e-02
## [16] -1.519483e-03 -1.854190e-02 -4.035271e-02 -2.348845e-02 -2.048489e-02
## [21] 1.819082e-02 -3.644387e-02 2.531574e-02 6.871658e-03 -1.414441e-02
## [26] 1.028338e-02 -2.173951e-02 3.492215e-02 1.171242e-03 -4.795327e-02
## [31] -1.848076e-02 2.754569e-03 3.795826e-02 -5.427725e-03 5.931826e-02
## [36] -2.081119e-02 4.336704e-02 -2.107270e-02 -2.006721e-04 -8.184868e-03
## [41] 6.779204e-03 6.781006e-04 1.336110e-02 7.805824e-03 -4.883081e-03
## [46] -8.904649e-03 2.004937e-02 5.948368e-02 -3.464680e-02 9.918054e-03
## [51] 3.522533e-02 -2.156368e-03 3.807262e-02 -2.222520e-02 1.256906e-02
## [56] 3.211653e-02 3.997483e-03 3.812672e-02 1.808955e-02 -2.324729e-03
## [61] -2.956314e-02 1.158363e-02 -3.443827e-03 2.273017e-02 1.378798e-02
## [66] -3.836512e-02 -1.468493e-02 -1.147984e-02 -2.589835e-02 -5.600555e-03
## [71] 3.981943e-03 -1.198811e-02 2.409009e-02 -1.658966e-03 2.422882e-02
## [76] 3.805282e-02 -1.865683e-02 -6.976115e-02 -1.488060e-03 4.372952e-02
## [81] -1.997989e-02 6.826712e-03 2.634909e-02 -1.951819e-04 -2.387744e-02
## [86] -4.101435e-03 1.029167e-02 2.445451e-02 -1.849521e-02 1.682107e-02
## [91] 4.567900e-02 4.769171e-02 3.397435e-02 -9.738229e-03 2.199080e-02
## [96] 6.952801e-03 -2.609383e-02 -7.460390e-03 3.992194e-02 1.621527e-02
## [101] 4.032196e-02 1.376655e-02 -2.471769e-02 1.291571e-02 1.033526e-02
## [106] -2.090658e-02 -2.746622e-02 -1.204153e-02 -1.104698e-02 -1.599854e-02
## [111] 4.990136e-02 6.236561e-04 1.220762e-04 1.880770e-02 5.492215e-03
## [116] -2.573948e-02 -9.899000e-03 1.091744e-02 6.634365e-03 9.736488e-03
# 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.996
## 3 1.04
## 4 0.966
## 5 1.00
## 6 1.08
## 7 1.02
## 8 1.03
## 9 1.01
## 10 1.02
## # … with 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.996
## 3 1.04
## 4 1.00
## 5 1.00
## 6 1.08
## 7 1.10
## 8 1.14
## 9 1.15
## 10 1.17
## # … with 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 5.004646
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 317.
## 2 326.
## 3 328.
## 4 333.
## 5 335.
## 6 333.
# 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
## # … with 6,161 more rows
# Find quantiles
monte_carlo_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%
## 1.17 1.59 1.98 2.40 3.88
## 8 Visualizing simulations with ggplot
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")
# Line plot with Max, Median and Min.
# Step 1 Summarize data into maximum, median, and minimum of last value
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 3.88 1.98 1.17
# Step 2 Plot
monte_carlo_sim_51 %>%
# Filter for max, median, and 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 $1 over 120 months",
subtitle = "Maximum, Median, and Minimum Simulation")