# 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.02347492
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] -2.975645e-03 -2.344941e-02 -1.844338e-02 1.647597e-02 -9.795139e-04
## [6] 5.037635e-03 9.252760e-03 -3.575821e-03 -6.832204e-02 2.053834e-02
## [11] -1.534253e-02 1.298810e-02 2.341726e-02 3.121199e-03 -1.348816e-03
## [16] -1.457046e-02 7.649371e-04 3.371695e-02 3.782471e-02 1.085971e-02
## [21] 1.491975e-02 -3.483244e-02 -1.405882e-02 1.717978e-02 -2.702758e-02
## [26] -1.264655e-02 4.679241e-03 1.095994e-02 3.206934e-02 -3.700855e-02
## [31] 2.918906e-02 2.698953e-02 1.157323e-03 -2.411801e-03 5.264835e-02
## [36] 2.791562e-02 -2.168244e-02 1.298480e-04 -1.734290e-02 2.196801e-02
## [41] 6.821352e-03 5.392110e-03 5.283699e-02 -1.346327e-02 3.676988e-02
## [46] -8.683608e-05 -1.071384e-02 -1.282907e-02 -2.749273e-03 6.921513e-03
## [51] 4.495758e-02 -9.525517e-03 1.489063e-02 2.569936e-02 -2.463284e-02
## [56] 1.895199e-02 2.819906e-03 -8.294100e-04 -1.505825e-02 8.818669e-03
## [61] 2.366732e-02 -2.922263e-03 2.502148e-02 4.188951e-02 -2.184690e-02
## [66] 1.119432e-02 1.052948e-02 3.298311e-03 -4.414924e-02 9.076979e-03
## [71] 2.503441e-02 2.707517e-02 5.648103e-02 -2.121610e-02 4.102979e-02
## [76] -2.147857e-03 3.722155e-02 -1.488833e-02 -1.812911e-02 -1.352890e-02
## [81] 1.307771e-02 3.896068e-02 -6.685259e-03 3.936542e-02 4.136924e-03
## [86] 2.173793e-03 2.334009e-02 -1.859110e-02 -2.374712e-02 -3.787347e-04
## [91] 1.834299e-02 2.178717e-02 -1.677340e-02 1.551339e-02 1.702882e-02
## [96] 3.250575e-02 3.080509e-03 2.943817e-02 3.331750e-02 -1.572436e-02
## [101] -2.016747e-02 -9.706746e-03 1.657743e-02 1.423408e-02 -1.873047e-02
## [106] 2.164331e-02 4.726478e-02 2.074560e-03 2.251346e-02 -1.914115e-02
## [111] -2.973694e-03 2.863973e-02 5.964567e-02 5.058268e-02 -1.213754e-02
## [116] 2.350452e-02 3.104873e-02 1.894140e-02 3.046791e-02 1.129218e-02
# 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.997
## 3 0.977
## 4 0.982
## 5 1.02
## 6 0.999
## 7 1.01
## 8 1.01
## 9 0.996
## 10 0.932
## # … 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.997
## 3 0.974
## 4 0.956
## 5 0.971
## 6 0.970
## 7 0.975
## 8 0.984
## 9 0.981
## 10 0.914
## # … with 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 9.161192
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.07
## 4 1.05
## 5 1.06
## 6 1.06
## 7 1.08
## 8 1.06
## 9 1.09
## 10 1.08
## # … with 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
## # … with 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.17 1.36 1.67 2.01 2.32 3.22 3.34
monte_carlo_sim_51 %>%
ggplot(aes(x = month, y = growth, col = sim)) +
geom_line() +
theme(legend.position = "none")
# Simplify the plot
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.36 2.01 1.11
monte_carlo_sim_51 %>%
group_by(sim) %>%
filter(last(growth) == sim_summary$max |
last(growth) == sim_summary$median |
last(growth) == sim_summary$min) %>%
# Plot
ggplot(aes(month, growth, col = sim)) +
geom_line() +
theme()