Learn R Programming

RandomWalker (version 0.1.0)

discrete_walk: Discrete Sampled Walk

Description

The discrete_walk function generates multiple random walks over discrete time periods. Each step in the walk is determined by a probabilistic sample from specified upper and lower bounds. This function is useful for simulating stochastic processes, such as stock price movements or other scenarios where outcomes are determined by a random process.

Usage

discrete_walk(
  .num_walks = 25,
  .n = 100,
  .upper_bound = 1,
  .lower_bound = -1,
  .upper_probability = 0.5,
  .initial_value = 100
)

Value

A tibble containing the simulated walks, with columns for the walk number, time period, and various cumulative metrics (sum, product, min, max).

Arguments

.num_walks

Total number of simulations.

.n

Total time of the simulation.

.upper_bound

The upper bound of the random walk.

.lower_bound

The lower bound of the random walk.

.upper_probability

The probability of the upper bound. Default is 0.5. The lower bound is calculated as 1 - .upper_probability.

.initial_value

The initial value of the random walk. Default is 100.

Author

Steven P. Sanderson II, MPH

Details

The function discrete_walk simulates random walks for a specified number of simulations (.num_walks) over a given total time (.n). Each step in the walk is either the upper bound or the lower bound, determined by a probability (.upper_probability). The initial value of the walk is set by the user (.initial_value), and the cumulative sum, product, minimum, and maximum of the steps are calculated for each walk. The results are returned in a tibble with detailed attributes, including the parameters used for the simulation.

See Also

Other Generator Functions: brownian_motion(), geometric_brownian_motion(), random_normal_drift_walk(), random_normal_walk()

Examples

Run this code
library(ggplot2)

set.seed(123)
discrete_walk()

set.seed(123)
discrete_walk(.num_walks = 30, .n = 250, .upper_probability = 0.55) |>
ggplot(aes(x = x, y = cum_sum)) +
 geom_line(aes(group = walk_number), alpha = .618, color = "steelblue") +
 theme_minimal() +
 theme(legend.position = "none") +
 geom_smooth(method = "lm", se = FALSE)

Run the code above in your browser using DataLab