Exploring Random Walks and Brownian Motions with healthyR.ts

code
rtip
healthyrts
Author

Steven P. Sanderson II, MPH

Published

June 27, 2024

Introduction

In the world of time series analysis, Random Walks, Brownian Motion, and Geometric Brownian Motion are fundamental concepts used in various fields, including finance, physics, and biology. Today, we’ll explore these concepts using functions from the healthyR.ts package.

Random Walks

A Random Walk is a path that consists of a series of random steps. It’s a simple but powerful concept used to model seemingly unpredictable paths, such as stock prices or animal movements.

Let’s generate and plot some Random Walks using the ts_random_walk() function from healthyR.ts.

Function Syntax

ts_random_walk(
  .mean = 0,
  .sd = 0.1,
  .num_walks = 100,
  .periods = 100,
  .initial_value = 1000
)
  • .mean: The desired mean of the random walks.
  • .sd: The standard deviation of the random walks.
  • .num_walks: The number of random walks you want to generate.
  • .periods: The length of the random walk(s) you want to generate.
  • .initial_value: The initial value where the random walks should start.

Example

library(ggplot2)
library(healthyR.ts)

random_walk_data <- ts_random_walk(
  .mean = 0, 
  .sd = 0.1, 
  .num_walks = 10, 
  .periods = 100, 
  .initial_value = 1000
  )
head(random_walk_data)
# A tibble: 6 × 4
    run     x       y cum_y
  <dbl> <dbl>   <dbl> <dbl>
1     1     1  0.0725 1072.
2     1     2  0.182  1267.
3     1     3  0.110  1407.
4     1     4  0.0275 1445.
5     1     5 -0.0546 1367.
6     1     6  0.0712 1464.
random_walk_plot <- random_walk_data |>
  ggplot(
    mapping = aes(
      x = x,
      y = cum_y,
      color = factor(run),
      group = factor(run)
    )
  ) +
  geom_line(alpha = 0.8) +
  ts_random_walk_ggplot_layers(random_walk_data)
print(random_walk_plot)

This code generates 10 random walks over 100 periods, starting from an initial value of 1000. The resulting plot visualizes the paths of these random walks, each represented by a different color.

Brownian Motion

Brownian Motion, also known as Wiener Process, is a continuous-time stochastic process that is often used to model random movements in physics and finance.

Function Syntax

ts_brownian_motion(
  .time = 100,
  .num_sims = 10,
  .delta_time = 1,
  .initial_value = 0,
  .return_tibble = TRUE
)
  • .time: Total time of the simulation.
  • .num_sims: Total number of simulations.
  • .delta_time: Time step size.
  • .initial_value: Initial value of the simulation.
  • .return_tibble: Return a tibble (TRUE) or a matrix (FALSE).

Example

brownian_data <- ts_brownian_motion(
  .time = 100,
  .num_sims = 10,
  .delta_time = 1,
  .initial_value = 0,
  .return_tibble = TRUE
)
head(brownian_data)
# A tibble: 6 × 3
  sim_number       t     y
  <fct>        <int> <dbl>
1 sim_number 1     1     0
2 sim_number 2     1     0
3 sim_number 3     1     0
4 sim_number 4     1     0
5 sim_number 5     1     0
6 sim_number 6     1     0
brownian_plot <- ts_brownian_motion_plot(
  .data = brownian_data,
  .date_col = t,
  .value_col = y,
  .interactive = TRUE
)
brownian_plot

This code simulates 10 paths of Brownian Motion over 100 time units, starting from an initial value of 0. The ts_brownian_motion_plot() function creates a static plot of these simulations.

Geometric Brownian Motion

Geometric Brownian Motion (GBM) is a variation of Brownian Motion where the logarithm of the variable follows a Brownian Motion. It is commonly used to model stock prices in the Black-Scholes option pricing model.

Function Syntax

ts_geometric_brownian_motion(
  .num_sims = 100,
  .time = 25,
  .mean = 0,
  .sigma = 0.1,
  .initial_value = 100,
  .delta_time = 1/365,
  .return_tibble = TRUE
)
  • .num_sims: Total number of simulations.
  • .time: Total time of the simulation.
  • .mean: Expected return.
  • .sigma: Volatility.
  • .initial_value: Initial value of the simulation.
  • .delta_time: Time step size.
  • .return_tibble: Return a tibble (TRUE) or a matrix (FALSE).

Example

gbm_data <- ts_geometric_brownian_motion(
  .num_sims = 10, 
  .time = 25, 
  .mean = 0.05, 
  .sigma = 0.2, 
  .initial_value = 100
  )
head(gbm_data)
# A tibble: 6 × 3
  sim_number       t     y
  <fct>        <int> <dbl>
1 sim_number 1     1   100
2 sim_number 2     1   100
3 sim_number 3     1   100
4 sim_number 4     1   100
5 sim_number 5     1   100
6 sim_number 6     1   100
gbm_plot <- ts_brownian_motion_plot(
  .data = gbm_data,
  .date_col = t,
  .value_col = y,
  .interactive = TRUE
)
gbm_plot

This code simulates 10 paths of Geometric Brownian Motion over 25 time units with an expected return of 0.05 and volatility of 0.2. The ts_brownian_motion_plot() function again helps in visualizing the simulations.

Your Turn!

These functions offer a straightforward way to simulate and visualize complex stochastic processes. I encourage you to tweak the parameters, run your own simulations, and explore how different settings affect the outcomes. Whether you’re modeling stock prices or random movements in nature, these tools can provide valuable insights.

Feel free to check out the detailed documentation of these functions here and experiment with your own datasets.


Happy coding!