Skip to content

Commit

Permalink
Merge pull request #56 from spsanderson/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
spsanderson authored Aug 1, 2024
2 parents 03883fa + 2a76303 commit 8f461fc
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
16 changes: 16 additions & 0 deletions R/gen-brown-motion-geometric.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ geometric_brownian_motion <- function(.num_walks = 25, .n = 100,
)
}

# .mu and .sigma and .detla_time must be >= 0
if (mu < 0 | sigma < 0 | delta_time < 0){
rlang::abort(
message = "The parameters of `.mu`, `.sigma`, and `.delta_time` must be >= 0.",
use_cli_format = TRUE
)
}

# .num_walks and .n must be >= 1
if (num_sims < 1 | t < 1){
rlang::abort(
message = "The parameters of `.num_walks` and `.n` must be >= 1.",
use_cli_format = TRUE
)
}

# matrix of random draws - one for each day for each simulation
rand_matrix <- matrix(stats::rnorm(t * num_sims), ncol = num_sims, nrow = t)
colnames(rand_matrix) <- 1:num_sims
Expand Down
18 changes: 17 additions & 1 deletion R/gen-brown-motion.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ NULL
#' @rdname brownian_motion

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

# Tidyeval ----
num_sims <- as.numeric(.num_walks)
Expand All @@ -73,6 +73,22 @@ brownian_motion <- function(.num_walks = 25, .n = 100, .delta_time = 1,
)
}

# .num_walks and .n must be >= 1
if (num_sims < 1 | t < 1){
rlang::abort(
message = "The parameters of `.num_walks` and `.n` must be >= 1.",
use_cli_format = TRUE
)
}

# .delta_time must be > 0
if (delta_time <= 0){
rlang::abort(
message = "The parameter `.delta_time` must be > 0.",
use_cli_format = TRUE
)
}

if (!is.logical(return_tibble)){
rlang::abort(
message = "The parameter `.return_tibble` must be either TRUE/FALSE",
Expand Down
43 changes: 43 additions & 0 deletions R/gen-discrete-walk.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,49 @@ discrete_walk <- function(.num_walks = 25, .n = 100, .upper_bound = 1,
lower_probability <- 1 - upper_probability
initial_value <- as.numeric(.initial_value)

# Checks
if (!is.integer(num_walks) | num_walks < 1) {
rlang::abort(
message = "The number of walks must be an integer greater than 0.",
use_cli_format = TRUE
)
}

if (!is.integer(periods) | periods < 1) {
rlang::abort(
message = "The number of periods must be an integer greater than 0.",
use_cli_format = TRUE
)
}

if (!is.numeric(upper_bound)) {
rlang::abort(
message = "The upper bound must be a numeric value.",
use_cli_format = TRUE
)
}

if (!is.numeric(lower_bound)) {
rlang::abort(
message = "The lower bound must be a numeric value.",
use_cli_format = TRUE
)
}

if (!is.numeric(upper_probability) | upper_probability < 0 | upper_probability > 1) {
rlang::abort(
message = "The upper probability must be a numeric value between 0 and 1.",
use_cli_format = TRUE
)
}

if (!is.numeric(initial_value)) {
rlang::abort(
message = "The initial value must be a numeric value.",
use_cli_format = TRUE
)
}

res <- tidyr::expand_grid(walk_number = factor(1:num_walks), x = 1:periods) |>
dplyr::group_by(walk_number) |>
dplyr::mutate(y = replicate(
Expand Down
44 changes: 44 additions & 0 deletions R/gen-random-normal-walk-drift.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ NULL
random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0,
.sd = 1, .drift = 0.1, .initial_value = 0) {

# Convert inputs to appropriate types
num_walks <- as.integer(.num_walks)
num_steps <- as.integer(.n)
mu <- as.numeric(.mu)
Expand All @@ -55,6 +56,49 @@ random_normal_drift_walk <- function(.num_walks = 25, .n = 100, .mu = 0,
initial_value <- as.numeric(.initial_value)
dr <- seq(from = drift, to = drift * num_steps, length.out = num_steps)

# Checks
if (num_walks <= 0) {
rlang::abort(
message = "Number of walks must be a positive integer.",
use_cli = TRUE
)
}

if (num_steps <= 0) {
rlang::abort(
message = "Number of steps must be a positive integer.",
use_cli = TRUE
)
}

if (sd <= 0) {
rlang::abort(
message = "Standard deviation must be a positive number.",
use_cli = TRUE
)
}

if (is.na(mu)) {
rlang::abort(
message = "Mean must be a number.",
use_cli = TRUE
)
}

if (is.na(drift)) {
rlang::abort(
message = "Drift must be a number.",
use_cli = TRUE
)
}

if (is.na(initial_value)) {
rlang::abort(
message = "Initial value must be a number.",
use_cli = TRUE
)
}

# 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)
Expand Down

0 comments on commit 8f461fc

Please sign in to comment.