Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create function ts_geometric_brownian_motion() #397

Closed
spsanderson opened this issue Jan 9, 2023 · 1 comment
Closed

Create function ts_geometric_brownian_motion() #397

spsanderson opened this issue Jan 9, 2023 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@spsanderson
Copy link
Owner

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
Copy link
Owner Author

add attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

No branches or pull requests

1 participant