Skip to content

Commit

Permalink
Merge pull request #32 from spsanderson/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
spsanderson authored Jul 20, 2024
2 parents eef9ba4 + ad843ac commit 2ba4f01
Show file tree
Hide file tree
Showing 25 changed files with 291 additions and 110 deletions.
2 changes: 1 addition & 1 deletion R/auto-rw30.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ rw30 <- function() {
attr(walks_long, "mu") <- mu
attr(walks_long, "sd") <- sd
attr(walks_long, "fns") <- "rw30"
attr(res, "dimension") <- 1
attr(walks_long, "dimension") <- 1

return(walks_long)
}
20 changes: 10 additions & 10 deletions R/gen-brown-motion.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ NULL
#' @export
#' @rdname brownian_motion

brownian_motion <- function(.n = 100, .num_walks = 10, .delta_time = 1,
brownian_motion <- function(.num_walks = 10, .n = 100, .delta_time = 1,
.initial_value = 0, .return_tibble = TRUE) {

# Tidyeval ----
Expand Down Expand Up @@ -84,11 +84,11 @@ brownian_motion <- function(.n = 100, .num_walks = 10, .delta_time = 1,
colnames(rand_matrix) <- 1:num_sims

# Get the Brownian Motion and convert to price paths
ret <- apply(rbind(rep(initial_value, num_sims), rand_matrix), 2, cumsum)
res <- apply(rbind(rep(initial_value, num_sims), rand_matrix), 2, cumsum)

# Return
if (return_tibble){
ret <- ret |>
res <- res |>
dplyr::as_tibble() |>
dplyr::mutate(t = 1:(t+1)) |>
tidyr::pivot_longer(-t) |>
Expand All @@ -99,13 +99,13 @@ brownian_motion <- function(.n = 100, .num_walks = 10, .delta_time = 1,
}

# Return ----
attr(ret, "time") <- .n
attr(ret, "num_sims") <- .num_walks
attr(ret, "delta_time") <- .delta_time
attr(ret, "initial_value") <- .initial_value
attr(ret, "return_tibble") <- .return_tibble
attr(ret, "fns") <- "brownian_motion"
attr(res, "n") <- .n
attr(res, "num_walks") <- .num_walks
attr(res, "delta_time") <- .delta_time
attr(res, "initial_value") <- .initial_value
attr(res, "return_tibble") <- .return_tibble
attr(res, "fns") <- "brownian_motion"
attr(res, "dimension") <- 1

return(ret)
return(res)
}
4 changes: 2 additions & 2 deletions R/gen-discrete-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ discrete_walk <- function(.num_walks = 25, .n = 100, .upper_bound = 1,
dplyr::ungroup()

# Attributes
attr(res, "num_walks") <- num_walks
attr(res, "n") <- n
attr(res, "n") <- periods
attr(res, "num_walks") <- .num_walks
attr(res, "upper_bound") <- upper_bound
attr(res, "lower_bound") <- lower_bound
attr(res, "upper_probability") <- upper_probability
Expand Down
24 changes: 12 additions & 12 deletions R/gen-geom-brown-motion.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ geometric_brownian_motion <- function(.num_walks = 25, .n = 100,
colnames(rand_matrix) <- 1:num_sims

# get GBM and convert to price paths
ret <- exp((mu - sigma * sigma / 2) * delta_time + sigma * rand_matrix * sqrt(delta_time))
ret <- apply(rbind(rep(initial_value, num_sims), ret), 2, cumprod)
res <- exp((mu - sigma * sigma / 2) * delta_time + sigma * rand_matrix * sqrt(delta_time))
res <- apply(rbind(rep(initial_value, num_sims), res), 2, cumprod)

# Return
if (return_tibble){
ret <- ret |>
res <- res |>
dplyr::as_tibble() |>
dplyr::mutate(t = 1:(t+1)) |>
tidyr::pivot_longer(-t) |>
Expand All @@ -116,15 +116,15 @@ geometric_brownian_motion <- function(.num_walks = 25, .n = 100,
dplyr::arrange(walk_number, x)
}

attr(ret, "time") <- .n
attr(ret, "num_sims") <- .num_walks
attr(ret, "mean") <- .mu
attr(ret, "sigma") <- .sigma
attr(ret, "initial_value") <- .initial_value
attr(ret, "delta_time") <- .delta_time
attr(ret, "return_tibble") <- .return_tibble
attr(ret, "fns") <- "geometric_brownian_motion"
attr(res, "n") <- .n
attr(res, "num_walks") <- .num_walks
attr(res, "mean") <- .mu
attr(res, "sigma") <- .sigma
attr(res, "initial_value") <- .initial_value
attr(res, "delta_time") <- .delta_time
attr(res, "return_tibble") <- .return_tibble
attr(res, "fns") <- "geometric_brownian_motion"
attr(res, "dimension") <- 1

return(ret)
return(res)
}
4 changes: 2 additions & 2 deletions R/gen-random-drift-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0,
dplyr::select(walk_number, x, y) |>
dplyr::arrange(walk_number, x)

attr(walks_long, "n") <- num_steps
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"
attr(res, "dimension") <- 1
attr(walks_long, "dimension") <- 1

return(walks_long)
}
2 changes: 1 addition & 1 deletion R/gen-random-normal-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ random_normal_walk <- function(.num_walks = 25, .n = 100, .mu = 0, .sd = .1,
# dplyr::ungroup()

# Attributes
attr(res, "num_walks") <- num_walks
attr(res, "n") <- n
attr(res, "num_walks") <- num_walks
attr(res, "mu") <- mu
attr(res, "sd") <- sd
attr(res, "initial_value") <- initial_value
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html

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

3 changes: 2 additions & 1 deletion 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-19T16:59Z
last_built: 2024-07-20T00:41Z
urls:
reference: https://www.spsanderson.com/RandomWalker/reference
article: https://www.spsanderson.com/RandomWalker/articles
Binary file modified docs/reference/Rplot001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 11 additions & 10 deletions docs/reference/brownian_motion.html

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

Binary file added docs/reference/discrete_walk-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 2ba4f01

Please sign in to comment.