Skip to content

Commit

Permalink
Merge pull request #23 from spsanderson/development
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
spsanderson authored Jul 17, 2024
2 parents 82098fe + 75a01ea commit 5bb7175
Show file tree
Hide file tree
Showing 19 changed files with 738 additions and 36 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(geometric_brownian_motion)
export(random_normal_drift_walk)
export(random_normal_walk)
export(rw30)
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ None
## New Features
1. Fix #9 - Add Function `rw30()` to generate 30 random walks of 100 steps each.
2. Fix #17 - Add Function `geometric_brownian_motion()`
3. Fix #18 - Add Function `random_normal_drift_walk()`

## Minor Improvements and Fixes
None
1 change: 0 additions & 1 deletion R/auto-rw30.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ rw30 <- function() {
walks_tibble <- dplyr::tibble(
x = 1:num_steps,
!!!stats::setNames(walks, 1:num_walks)
#!!!stats::setNames(walks, paste0("walk_", 1:num_walks))
)

# Pivot the tibble longer
Expand Down
95 changes: 95 additions & 0 deletions R/gen-random-drift-walk.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#' Generate Multiple Random Walks with Drift
#'
#' @family Generator Functions
#'
#' @author Steven P. Sanderson II, MPH
#'
#' @details
#' This function generates multiple random walks with a specified drift.
#' Each walk is generated using a normal distribution for the steps, with an
#' additional drift term added to each step.
#'
#' @description
#' This function generates a specified number of random walks, each consisting
#' of a specified number of steps. The steps are generated from a normal
#' distribution with a given mean and standard deviation. An additional drift
#' term is added to each step to introduce a consistent directional component
#' to the walks.
#'
#' @param .num_walks Integer. The number of random walks to generate. Default is 25.
#' @param .n Integer. The number of steps in each random walk. Default is 100.
#' @param .mu Numeric. The mean of the normal distribution used for generating steps. Default is 0.
#' @param .sd Numeric. The standard deviation of the normal distribution used for generating steps. Default is 1.
#' @param .drift Numeric. The drift term to be added to each step. Default is 0.1.
#'
#' @examples
#' library(ggplot2)
#'
#' walks <- random_normal_drift_walk(.num_walks = 10, .n = 50, .mu = 0, .sd = 1,
#' .drift = 0.05)
#' ggplot(walks, ggplot2::aes(x = x, y = y, group = walk_number, color = walk_number)) +
#' geom_line() +
#' labs(title = "Random Walks with Drift", x = "Time", y = "Value") +
#' theme_minimal() +
#' theme(legend.position = "none")
#'
#' @return A tibble in long format with columns `walk_number`, `x` (step index),
#' and `y` (walk value). The tibble has attributes for the number of walks,
#' number of steps, mean, standard deviation, and drift.
#'
#' @name random_normal_drift_walk
NULL
#' @rdname random_normal_drift_walk
#' @export

random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0,
.sd = 1, .drift = 0.1) {

num_walks <- as.integer(.num_walks)
num_steps <- as.integer(.n)
mu <- as.numeric(.mu)
sd <- as.numeric(.sd)
drift <- as.numeric(.drift)
#dr <- seq(from = drift, to = n, length.out = n)

# Function to generate a single random walk
single_random_walk_with_drift <- function(num_steps, mu, sd, drift) {
wn <- stats::rnorm(n = num_steps, mean = mu, sd = sd)
rw <- cumsum(stats::rnorm(n = num_steps, mean = mu, sd = sd))
res <- wn + rw + drift
return(res)
}

# Generate the walks
walks <- replicate(
num_walks,
single_random_walk_with_drift(num_steps, mu, sd, drift),
simplify = FALSE
)

# Create a tibble with the walks
walks_tibble <- dplyr::tibble(
x = 1:num_steps,
!!!stats::setNames(walks, 1:num_walks)
)

# Pivot the tibble longer
walks_long <- tidyr::pivot_longer(
walks_tibble,
cols = -x,
names_to = "walk_number",
values_to = "y"
) |>
dplyr::mutate(walk_number = factor(walk_number)) |>
dplyr::select(walk_number, x, y) |>
dplyr::arrange(walk_number, x)

attr(walks_long, "num_walks") <- num_walks
attr(walks_long, "num_steps") <- num_steps
attr(walks_long, "mu") <- mu
attr(walks_long, "sd") <- sd
attr(walks_long, "drift") <- drift
attr(walks_long, "fns") <- "random_normal_drift_walk"

return(walks_long)
}
11 changes: 10 additions & 1 deletion docs/articles/getting-started.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pkgdown: 2.1.0
pkgdown_sha: ~
articles:
getting-started: getting-started.html
last_built: 2024-07-16T02:18Z
last_built: 2024-07-17T02:25Z
urls:
reference: https://www.spsanderson.com/RandomWalker/reference
article: https://www.spsanderson.com/RandomWalker/articles
Loading

0 comments on commit 5bb7175

Please sign in to comment.