Skip to contents

Takes in the data from a tidy_ distribution function and applies a random walk calculation of either cum_prod or cum_sum to y.

Usage

tidy_random_walk(
  .data,
  .initial_value = 0,
  .sample = FALSE,
  .replace = FALSE,
  .value_type = "cum_prod"
)

Arguments

.data

The data that is being passed from a tidy_ distribution function.

.initial_value

The default is 0, this can be set to whatever you want.

.sample

This is a boolean value TRUE/FALSE. The default is FALSE. If set to TRUE then the y value from the tidy_ distribution function is sampled.

.replace

This is a boolean value TRUE/FALSE. The default is FALSE. If set to TRUE AND .sample is set to TRUE then the replace parameter of the sample function will be set to TRUE.

.value_type

This can take one of three different values for now. These are the following:

  • "cum_prod" - This will take the cumprod of y

  • "cum_sum" - This will take the cumsum of y

Value

An ungrouped tibble.

Details

Monte Carlo simulations were first formally designed in the 1940’s while developing nuclear weapons, and since have been heavily used in various fields to use randomness solve problems that are potentially deterministic in nature. In finance, Monte Carlo simulations can be a useful tool to give a sense of how assets with certain characteristics might behave in the future. While there are more complex and sophisticated financial forecasting methods such as ARIMA (Auto-Regressive Integrated Moving Average) and GARCH (Generalised Auto-Regressive Conditional Heteroskedasticity) which attempt to model not only the randomness but underlying macro factors such as seasonality and volatility clustering, Monte Carlo random walks work surprisingly well in illustrating market volatility as long as the results are not taken too seriously.

Author

Steven P. Sanderson II, MPH

Examples

tidy_normal(.sd = .1, .num_sims = 25) %>%
  tidy_random_walk()
#> # A tibble: 1,250 × 8
#>    sim_number     x        y     dx      dy      p        q random_walk_value
#>    <fct>      <int>    <dbl>  <dbl>   <dbl>  <dbl>    <dbl>             <dbl>
#>  1 1              1  0.0714  -0.333 0.00281 0.762   0.0714            0.0714 
#>  2 1              2  0.159   -0.320 0.00727 0.944   0.159             0.242  
#>  3 1              3  0.0641  -0.307 0.0172  0.739   0.0641            0.321  
#>  4 1              4 -0.0278  -0.294 0.0372  0.390  -0.0278            0.284  
#>  5 1              5 -0.0193  -0.281 0.0740  0.424  -0.0193            0.260  
#>  6 1              6 -0.188   -0.268 0.135   0.0298 -0.188             0.0224 
#>  7 1              7 -0.0128  -0.255 0.227   0.449  -0.0128            0.00930
#>  8 1              8  0.00734 -0.243 0.354   0.529   0.00734           0.0167 
#>  9 1              9  0.122   -0.230 0.512   0.888   0.122             0.140  
#> 10 1             10  0.0262  -0.217 0.691   0.603   0.0262            0.170  
#> # ℹ 1,240 more rows