Monte Carlo Simulation in R – Part IV


Visit: RStudio


Director of Financial Services Practice at 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.

sim1 sim2 sim3 sim4 sim5 sim6
       1         1         1         1         1         1


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 Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight 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 permission from RStudio. The views expressed in this material are solely those of the author and/or RStudio and IBKR 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 sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. 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.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.