Search code examples
rfinancemontecarlomarkov-chainsmcmc

Store Markov Chain Montecarlo simulations into dataframe and visualize it


I'm trying to simulate the evolution of stock prices, so I decided to use a MCMC simulation (I don't know if there's a library that could help me with it though). What I intend to do is:

  1. Create a function for simulating the evolution of a stock price, given a number of parameters:
price.path <- function(p, t, meanr, sdr){
  score <- c(0,rnorm(t))
  retorno <- meanr + sdr*score
  prices <- p*exp(cumsum(retorno))
}

Which returns a vector of the stochastic process of the price.

  1. Run the simulation multiple times (up to 100,000), and store every path found. I thought that it would be ideal to store this in a dataframe.

I didn't found a way to store all the vectors into a dataframe without having to name each column (naming every column would be unnecessary and a waste of time), so I decided to use purrr. I tried to use the map function to get a list, which needed no names. Afterward I would use map_dfc function, to turn the list into a dataframe with trivial names (...1, ...2, ...3, etc.), I tried this with rnorm and it worked fine.

map(seq(1,run), price.path, 
    p = 20,
    t = 10,
    meanr = 0.00008,
    sdr = 0.00015)

Where "run" is an integer. This approach didn't work, because the map function "thinks" the vector I gave it in the first argument must be used in the function. I don't want this, since I'm just using the vector as a placeholder and it has no use or real meaning. It basically just counts what run of the price path it is, which is really irrelevant. This causes map to throw an error, because one of the arguments is unused.

  1. Lastly, I want to use ggplot and another map function to visualize every price path. I haven't gotten to this part yet, since I'm kinda stuck in the second step.

I would appreciate if you guys could give me some help with step 2. Also some insights into my procedure so far are okay and a little help in the visualizing part are greatly welcomed. Thanks in advance.


Solution

  • To get around your first problem with map, use formula notation like so:

    library(tidyverse)
    
    run <- 1000
    all_sims <- map(seq(1,run), ~ price.path(p = 20, t = 10, meanr = 0.00008, sdr = 0.00015))
    

    You can then store the results in a long-format dataframe. I've also created columns to count each iteration, and each simulation run.

    df1 <- tibble(
      simvalues = unlist(all_sims),
      iteration = rep(seq(1,11), run),
      sim_number = unlist(map(1:run, ~ rep(.x, 11)))
    )
    

    You can then plot the results to achieve something like:

    df1 %>% 
      ggplot(aes(x = iteration, y = simvalues, color = sim_number)) +
      geom_path()
    

    MCMC ggplot