Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

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

Monte Carlo Simulation in R – Part IV

Posted November 5, 2019 at 9:42 am
Jonathan Regenstein
RStudio

Get up to speed with the required R packages and data – see Part I, Part II and Part III.

First, we need an empty matrix with 51 columns, an initial value of $1 and intuitive column names.

We will use the rep() function to create 51 columns with a 1 as the value and set_names() to name each column with the appropriate simulation number.

sims <- 51
starts <-
rep(1, sims) %>%
set_names(paste(“sim”, 1:sims, sep = “”))

  set_names(paste(“sim”, 1:sims, sep = “”))

Take a peek at starts to see what we just created and how it can house our simulations.

head(starts)
sim1 sim2 sim3 sim4 sim5 sim6
       1         1         1         1         1         1

tail(starts)

sim46 sim47 sim48 sim49 sim50 sim51
        1         1         1         1         1         1

51 columns, with a value of 1 in one row. This is where we will store the results of the 51 simulations.

Now we want to apply simulation_accum_1 to each of the 51 columns of the starts matrix and we will do that using the map_dfc() function from the purrr package.

map_dfc() takes a vector, in this case the columns of starts, and applies a function to it. By appending dfc() to the map_ function, we are asking the function to store each of its results as the column of a data frame (map_df() does the same thing, but stores results in the rows of a data frame). After running the code below, we will have a data frame with 51 columns, one for each of our simulations.

We also need to choose how many months to simulate (the N argument to our simulation function) and supply the distribution parameters as we did before. We do not supply the init_value argument because the init_value is 1, that same 1 which is in the 51 columns.

Stay tuned for the next installment, in which Jonathan will apply simulation_accum_1 to each of the 51 columns of the starts matrix and will do that using the map_dfc() function from the purrr package.

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.