# Monte Carlo Simulation in R – Part II

Contributor:
RStudio
Visit: RStudio

Director of Financial Services Practice at RStudio

For a list of R packages, and instructions on how to download the data, see Part I.

That data is now ready to be converted into the cumulative growth of a dollar. We can use either accumulate() from purrr or cumprod(). Let’s use both of them with mutate() and confirm consistent, reasonable results.

``````simulated_growth <-
mutate(growth1 = accumulate(returns, function(x, y) x * y),
growth2 = accumulate(returns, `*`),
growth3 = cumprod(returns)) %>%
select(-returns)

tail(simulated_growth)``````
``````# A tibble: 6 x 3
growth1 growth2 growth3

1    2.70    2.70    2.70
2    2.61    2.61    2.61
3    2.58    2.58    2.58
4    2.57    2.57    2.57
5    2.68    2.68    2.68
6    2.67    2.67    2.67``````

We just ran 3 simulations of dollar growth over 120 months. We passed in the same monthly returns, and that’s why we got 3 equivalent results.

Are they reasonable? What compound annual growth rate (CAGR) is implied by this simulation?

``````cagr <-
((simulated_growth\$growth1[nrow(simulated_growth)]^
(1/10)) - 1) * 100

cagr <- round(cagr, 2)``````

This simulation implies an annual compounded growth of 10.32%. That seems reasonable given our actual returns have all been taken from a raging bull market. Remember, the above code is a simulation based on sampling from a normal distribution. If you re-run this code on your own, you will get a different result.

If we feel good about this first simulation, we can run several more to get a sense for how they are distributed. Before we do that, let’s create several different functions that could run the same simulation.

Several Simulation Functions

Let’s build 3 simulation functions that incorporate the accumulate() and cumprod() workflows above. We have confirmed they give consistent results so it’s a matter of stylistic preference as to which one is chosen in the end. Perhaps you feel that one is more flexible or extensible or fits better with your team’s code flows.

Each of the below functions needs 4 arguments: N for the number of months to simulate (we chose 120 above), init_value for the starting value (we used \$1 above) and the mean-standard deviation pair to create draws from a normal distribution. We choose N and init_value, and derive the mean-standard deviation pair from our portfolio monthly returns.

Here is our first growth simulation function using accumulate().

``````simulation_accum_1 <- function(init_value, N, mean, stdev) {
tibble(c(init_value, 1 + rnorm(N, mean, stdev))) %>%
`colnames<-`("returns") %>%
mutate(growth =
accumulate(returns,
function(x, y) x * y)) %>%
select(growth)
}``````

Almost identical, here is the second simulation function using accumulate().

``````simulation_accum_2 <- function(init_value, N, mean, stdev) {
tibble(c(init_value, 1 + rnorm(N, mean, stdev))) %>%
`colnames<-`("returns") %>%
mutate(growth = accumulate(returns, `*`)) %>%
select(growth)
}``````

Finally, here is a simulation function using cumprod().

``````simulation_cumprod <- function(init_value, N, mean, stdev) {
tibble(c(init_value, 1 + rnorm(N, mean, stdev))) %>%
`colnames<-`("returns") %>%
mutate(growth = cumprod(returns)) %>%
select(growth)
}``````

Here is a function that uses all three methods, in case we want a fast way to re-confirm consistency.

``````simulation_confirm_all <- function(init_value, N, mean, stdev) {
tibble(c(init_value, 1 + rnorm(N, mean, stdev))) %>%
`colnames<-`("returns") %>%
mutate(growth1 = accumulate(returns, function(x, y) x * y),
growth2 = accumulate(returns, `*`),
growth3 = cumprod(returns)) %>%
select(-returns)
}``````

Let’s test that confirm_all() function with an init_value of 1, N of 120, and our parameters.

``````simulation_confirm_all_test <-
simulation_confirm_all(1, 120,
mean_port_return, stddev_port_return)

tail(simulation_confirm_all_test)``````
``````# A tibble: 6 x 3
growth1 growth2 growth3

1    2.93    2.93    2.93
2    2.89    2.89    2.89
3    3.01    3.01    3.01
4    3.18    3.18    3.18
5    3.21    3.21    3.21
6    3.31    3.31    3.31``````