View source: R/gen-discrete-walk.R
discrete_walk | R Documentation |
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.
discrete_walk(
.num_walks = 25,
.n = 100,
.upper_bound = 1,
.lower_bound = -1,
.upper_probability = 0.5,
.initial_value = 100,
.dimensions = 1
)
.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. |
.dimensions |
The default is 1. Allowable values are 1, 2 and 3. |
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.
A tibble containing the generated random walks with columns depending on the number of dimensions:
walk_number
: Factor representing the walk number.
step_number
: Step index.
y
: If .dimensions = 1
, the value of the walk at each step.
x
, y
: If .dimensions = 2
, the values of the walk in two dimensions.
x
, y
, z
: If .dimensions = 3
, the values of the walk in three dimensions.
The following are also returned based upon how many dimensions there are and could be any of x, y and or z:
cum_sum
: Cumulative sum of dplyr::all_of(.dimensions)
.
cum_prod
: Cumulative product of dplyr::all_of(.dimensions)
.
cum_min
: Cumulative minimum of dplyr::all_of(.dimensions)
.
cum_max
: Cumulative maximum of dplyr::all_of(.dimensions)
.
cum_mean
: Cumulative mean of dplyr::all_of(.dimensions)
.
Steven P. Sanderson II, MPH
Other Generator Functions:
brownian_motion()
,
geometric_brownian_motion()
,
random_normal_drift_walk()
,
random_normal_walk()
set.seed(123)
discrete_walk()
set.seed(123)
discrete_walk(.dimensions = 3) |>
head() |>
t()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.