Close Navigation
Learn more about IBKR accounts
Monte Carlo Simulation in R – Part II

Monte Carlo Simulation in R – Part II

Posted June 4, 2019
Jonathan Regenstein
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 <- 
simulated_returns_add_1 %>%
    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

We’re ready to visualize!

Jonathan Regenstein, Director of Financial Services, RStudio.
@jkregenstein  @rstudio. For additional R scripts, see the “Reproducible Finance with R: Code Flows and Shiny Apps for Portfolio Analysis” article.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from RStudio and is being posted with its permission. The views expressed in this material are solely those of the author and/or RStudio and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.