We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
ts_geometric_brownian_motion()
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")
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")
The text was updated successfully, but these errors were encountered:
add attributes
Sorry, something went wrong.
f0071e3
Merge pull request #399 from spsanderson/development
5825557
Fixes #397
spsanderson
No branches or pull requests
Function:
Examples:
The text was updated successfully, but these errors were encountered: