symbols <- c("SPY", "EFA", "IJS", "EEM", "AGG")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2012-12-31",
to = "2017-12-31")
## Convert prices to returns (monthly)
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")
## Warning in check_weights(weights, assets_col, map, x): Sum of weights does not
## equal 1.
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.005899134
# Get standard deviation of portfolio returns
stddev_port_return <- sd(portfolio_returns_tbl$returns)
stddev_port_return
## [1] 0.02347494
# Construct a normal distribution
simulated_monthly_returns <- rnorm(120, mean_port_return, stddev_port_return)
simulated_monthly_returns
## [1] -0.0033087895 -0.0042424559 -0.0246210263 -0.0207361549 -0.0206841982
## [6] 0.0070039301 0.0035992028 0.0269577319 0.0029968246 -0.0026840615
## [11] -0.0325819017 -0.0067267067 -0.0089475437 0.0078977011 0.0234304260
## [16] 0.0133298306 0.0083300624 -0.0039167356 -0.0230026709 -0.0054876713
## [21] 0.0183082556 0.0201197018 0.0028296467 0.0200378930 -0.0322601043
## [26] 0.0117525523 0.0346486760 0.0121619242 0.0370838438 0.0132519813
## [31] -0.0205417410 0.0047181316 -0.0142431338 -0.0188127243 0.0090851724
## [36] 0.0156208340 0.0063323486 -0.0094460545 0.0460302655 -0.0019017857
## [41] 0.0476230581 0.0216324813 0.0298632137 0.0467182925 -0.0298735220
## [46] 0.0296953024 0.0145433317 0.0297917000 0.0165572539 -0.0063333380
## [51] 0.0294203585 0.0061045598 0.0117084602 0.0382180958 0.0155339395
## [56] -0.0239284836 0.0365760367 -0.0072438766 0.0211712951 0.0046521606
## [61] 0.0124791279 0.0025521955 -0.0052080959 0.0047609634 0.0350563545
## [66] 0.0395935628 -0.0187201412 0.0198242217 0.0261398595 0.0069149509
## [71] -0.0099501741 -0.0091008151 0.0041963775 -0.0160806511 -0.0102968580
## [76] 0.0118279463 0.0391809075 0.0415211931 -0.0324317901 0.0055056332
## [81] -0.0160890981 -0.0240728615 -0.0441085604 -0.0202529208 -0.0058168432
## [86] -0.0073665968 0.0102189696 0.0100573411 -0.0260167443 0.0361806534
## [91] 0.0070846194 -0.0086102952 -0.0001407496 -0.0342253851 -0.0273649871
## [96] 0.0150948634 -0.0049383194 0.0078395202 0.0137499101 0.0018569191
## [101] 0.0082740619 0.0284089192 -0.0016556125 0.0104324942 -0.0033777629
## [106] -0.0171560241 -0.0374572721 -0.0085889620 0.0316494186 -0.0153008875
## [111] -0.0100160354 0.0124557191 -0.0118389174 -0.0033511450 0.0355868391
## [116] 0.0182947515 0.0035310950 0.0104963574 0.0532994375 -0.0105995558
# 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.996
## 4 0.975
## 5 0.979
## 6 0.979
## 7 1.01
## 8 1.00
## 9 1.03
## 10 1.00
## # ℹ 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.992
## 4 0.968
## 5 0.948
## 6 0.928
## 7 0.935
## 8 0.938
## 9 0.964
## 10 0.966
## # ℹ 111 more rows
# Check the compound annual growth rate
cagr <- ((simulated_growth$growth[nrow(simulated_growth)]^(1/10)) - 1) * 100
cagr
## [1] 5.345261
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 327.
## 2 325.
## 3 334.
## 4 334.
## 5 337.
## 6 341.
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(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_carle_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")
## New names:
## • `growth` -> `growth...1`
## • `growth` -> `growth...2`
## • `growth` -> `growth...3`
## • `growth` -> `growth...4`
## • `growth` -> `growth...5`
## • `growth` -> `growth...6`
## • `growth` -> `growth...7`
## • `growth` -> `growth...8`
## • `growth` -> `growth...9`
## • `growth` -> `growth...10`
## • `growth` -> `growth...11`
## • `growth` -> `growth...12`
## • `growth` -> `growth...13`
## • `growth` -> `growth...14`
## • `growth` -> `growth...15`
## • `growth` -> `growth...16`
## • `growth` -> `growth...17`
## • `growth` -> `growth...18`
## • `growth` -> `growth...19`
## • `growth` -> `growth...20`
## • `growth` -> `growth...21`
## • `growth` -> `growth...22`
## • `growth` -> `growth...23`
## • `growth` -> `growth...24`
## • `growth` -> `growth...25`
## • `growth` -> `growth...26`
## • `growth` -> `growth...27`
## • `growth` -> `growth...28`
## • `growth` -> `growth...29`
## • `growth` -> `growth...30`
## • `growth` -> `growth...31`
## • `growth` -> `growth...32`
## • `growth` -> `growth...33`
## • `growth` -> `growth...34`
## • `growth` -> `growth...35`
## • `growth` -> `growth...36`
## • `growth` -> `growth...37`
## • `growth` -> `growth...38`
## • `growth` -> `growth...39`
## • `growth` -> `growth...40`
## • `growth` -> `growth...41`
## • `growth` -> `growth...42`
## • `growth` -> `growth...43`
## • `growth` -> `growth...44`
## • `growth` -> `growth...45`
## • `growth` -> `growth...46`
## • `growth` -> `growth...47`
## • `growth` -> `growth...48`
## • `growth` -> `growth...49`
## • `growth` -> `growth...50`
## • `growth` -> `growth...51`
# 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%
## 1.17 1.59 1.98 2.40 3.88
count_ncol_numeric <- function(.data) {
#body
ncol
}
##Visualizing simulations with ggplot
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 $1 over 120 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 3.88 1.98 1.17
# 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 $1 over 120 months",
subtitle = "Maximum, Median, and Mimimum Simulation")