spsanderson / healthyR.ts

A time-series companion package to healthyR
https://www.spsanderson.com/healthyR.ts/
Other
19 stars 3 forks source link

Create function `ts_geometric_brownian_motion()` #397

Closed spsanderson closed 1 year ago

spsanderson commented 1 year ago

Function:

ts_geometric_brownian_motion <- function(.num_sims = 100, .time = 25, 
                                         .mean = 0, .sigma = 0.1, 
                                         .initial_value = 100, 
                                         .delta_time = 1./365,
                                         .return_tibble = TRUE) {

  # Tidyeval ----
  # Thank you to https://robotwealth.com/efficiently-simulating-geometric-brownian-motion-in-r/
  num_sims <- as.numeric(.num_sims)
  t <- as.numeric(.time)
  mu <- as.numeric(.mean)
  sigma <- as.numeric(.sigma)
  initial_value <- as.numeric(.initial_value)
  delta_time <- as.numeric(.delta_time)
  return_tibble <- as.logical(.return_tibble)

  # matrix of random draws - one for each day for each simulation
  rand_matrix <- matrix(rnorm(t * num_sims), ncol = num_sims, nrow = t)  
  colnames(rand_matrix) <- paste0("sim_number ", 1:num_sims)

  # get GBM and convert to price paths
  ret <- exp((mu - sigma * sigma / 2) * delta_time + sigma * rand_matrix * sqrt(delta_time))
  ret <- apply(rbind(rep(initial_value, num_sims), ret), 2, cumprod)

  # Return
  if (return_tibble){
    ret <- ret %>%
      dplyr::as_tibble() %>%
      dplyr::mutate(t = 1:(t+1)) %>%
      dplyr::select(t, dplyr::everything()) %>%
      tidyr::pivot_longer(-t)
  }

  return(ret)
}

Examples:

ts_geometric_brownian_motion(.num_sims = 25, .return_tibble = TRUE) %>% 
  ggplot(aes(x = t, y = value, group = name, color = name)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = "none")

image

ts_geometric_brownian_motion(.num_sims = 2000, .return_tibble = TRUE) %>% 
  ggplot(aes(x = t, y = value, group = name, color = name)) +
  geom_line() +
  theme_minimal() +
  theme(legend.position = "none")

image

spsanderson commented 1 year ago

add attributes