The simplest way to generate random walks with RandomWalker is using
the automatic function rw30().
RandomWalker provides rw30() as a quick way to generate
random walks without specifying any parameters. This is perfect for:
# Generate 30 random walks
walks <- rw30()
# View the data
head(walks, 10)
#> # A tibble: 10 × 3
#> walk_number step_number y
#> <fct> <int> <dbl>
#> 1 1 1 0
#> 2 1 2 -0.950
#> 3 1 3 -0.185
#> 4 1 4 -0.991
#> 5 1 5 -1.84
#> 6 1 6 -0.548
#> 7 1 7 -0.722
#> 8 1 8 -0.122
#> 9 1 9 -1.12
#> 10 1 10 -1.05The rw30() function: 1. Generates 30 random
walks 2. Each with 100 steps 3. Using
normal distribution (mean = 0, sd = 1) 4. Starting at
0 5. Returns a tidy tibble
It’s equivalent to:
rw30()
#> # A tibble: 3,000 × 3
#> walk_number step_number y
#> <fct> <int> <dbl>
#> 1 1 1 0
#> 2 1 2 -2.11
#> 3 1 3 -2.22
#> 4 1 4 -1.54
#> 5 1 5 -0.579
#> 6 1 6 0.633
#> 7 1 7 1.50
#> 8 1 8 1.76
#> 9 1 9 1.05
#> 10 1 10 2.09
#> # ℹ 2,990 more rowsColumns: - walk_number: Factor (1-30)
identifying each walk - step_number: Integer (1-100) for
each step - y: The random walk values
Note: Cumulative columns such as cum_sum,
cum_prod, cum_min, cum_max, and
cum_mean are not included by default. You can add them
using rand_walk_helper() or tidyverse operations if
needed. ## Understanding the Output
Each walk consists of 100 steps:
Since steps are drawn from N(0,1):
The function stores metadata:
# Overall statistics
rw30() |> summarize_walks(.value = y) |>
head()
#> Warning: There was 1 warning in `dplyr::summarize()`.
#> ℹ In argument: `geometric_mean = exp(mean(log(y)))`.
#> Caused by warning in `log()`:
#> ! NaNs produced
#> # A tibble: 1 × 16
#> fns fns_name dimensions mean_val median range quantile_lo quantile_hi
#> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 rw30 Rw30 1 0.404 0.274 48.6 -12.5 14.4
#> # ℹ 8 more variables: variance <dbl>, sd <dbl>, min_val <dbl>, max_val <dbl>,
#> # harmonic_mean <dbl>, geometric_mean <dbl>, skewness <dbl>, kurtosis <dbl>
# By walk
rw30() |>
summarize_walks(.value = y, .group_var = walk_number) |>
head(10)
#> Warning: There were 30 warnings in `dplyr::summarize()`.
#> The first warning was:
#> ℹ In argument: `geometric_mean = exp(mean(log(y)))`.
#> ℹ In group 1: `walk_number = 1`.
#> Caused by warning in `log()`:
#> ! NaNs produced
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 29 remaining warnings.
#> # A tibble: 10 × 17
#> walk_number fns fns_name dimensions mean_val median range quantile_lo
#> <fct> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 rw30 Rw30 1 1.92 1.37 9.07 -1.78
#> 2 2 rw30 Rw30 1 4.45 3.63 12.8 -0.000459
#> 3 3 rw30 Rw30 1 1.41 0.716 14.1 -3.86
#> 4 4 rw30 Rw30 1 1.62 2.07 11.1 -2.58
#> 5 5 rw30 Rw30 1 -12.1 -10.6 24.3 -23.3
#> 6 6 rw30 Rw30 1 -2.84 -2.39 17.9 -10.3
#> 7 7 rw30 Rw30 1 1.14 1.56 11.8 -5.14
#> 8 8 rw30 Rw30 1 2.77 2.40 15.6 -2.07
#> 9 9 rw30 Rw30 1 -0.577 -2.33 16.5 -6.67
#> 10 10 rw30 Rw30 1 -10.0 -11.4 22.4 -19.7
#> # ℹ 9 more variables: quantile_hi <dbl>, variance <dbl>, sd <dbl>,
#> # min_val <dbl>, max_val <dbl>, harmonic_mean <dbl>, geometric_mean <dbl>,
#> # skewness <dbl>, kurtosis <dbl># Custom analysis
rw30() |>
group_by(walk_number) |>
summarize(
final_value = last(y),
max_value = max(y),
min_value = min(y),
volatility = sd(y)
) |>
head(10)
#> # A tibble: 10 × 5
#> walk_number final_value max_value min_value volatility
#> <fct> <dbl> <dbl> <dbl> <dbl>
#> 1 1 19.5 19.8 -1.13 5.35
#> 2 2 15.9 15.9 -5.85 4.60
#> 3 3 8.41 11.4 -5.74 4.32
#> 4 4 4.09 5.52 -6.09 2.75
#> 5 5 -19.0 5.80 -19.6 8.01
#> 6 6 -16.5 1.53 -16.5 4.53
#> 7 7 -1.47 3.38 -8.13 3.39
#> 8 8 -10.2 0 -14.1 4.53
#> 9 9 -3.11 5.47 -3.11 2.18
#> 10 10 2.54 5.32 -2.94 1.83# Walk that went highest
max_walk <- rw30() |>
subset_walks(.value = "y", .type = "max")
# Walk that went lowest
min_walk <- rw30() |>
subset_walks(.value = "y", .type = "min")
# Visualize extremes
max_walk |> visualize_walks()walks <- rw30()
# Get only first 10 walks
walks |>
filter(walk_number %in% as.character(1:10)) |>
visualize_walks()# Show variability
walks <- rw30()
# Distribution of final positions
walks |>
group_by(walk_number) |>
slice_max(step_number) |>
ggplot(aes(x = y)) +
geom_histogram(bins = 15, fill = "steelblue", alpha = 0.7) +
geom_vline(xintercept = 0, color = "red", linetype = "dashed") +
theme_minimal() +
labs(
title = "Distribution of Final Positions",
subtitle = "30 random walks, 100 steps each",
x = "Final Position",
y = "Count"
)# Test if variance grows linearly with steps
walks <- rw30()
variance_by_step <- walks |>
group_by(step_number) |>
reframe(
variance = var(y),
theoretical = step_number # For N(0,1), var = n
)
ggplot(variance_by_step, aes(x = step_number)) +
geom_line(aes(y = variance, color = "Observed"), linewidth = 1) +
geom_line(aes(y = theoretical, color = "Theoretical"), linewidth = 1, linetype = "dashed") +
scale_color_manual(values = c("Observed" = "blue", "Theoretical" = "red")) +
theme_minimal() +
labs(
title = "Variance Growth in Random Walk",
subtitle = "Observed vs Theoretical (Var = n)",
x = "Step Number",
y = "Variance",
color = ""
)random_normal_walk() insteadrw30() has no parameters, which means:
# ❌ Can't change number of walks
# rw30(.num_walks = 50) # Error!
# ✅ Use random_normal_walk() instead
random_normal_walk(.num_walks = 50)
# ❌ Can't change number of steps
# rw30(.n = 200) # Error!
# ✅ Use random_normal_walk() instead
random_normal_walk(.n = 200)
# ❌ Can't change distribution parameters
# rw30(.mu = 0.1) # Error!
# ✅ Use random_normal_walk() instead
random_normal_walk(.mu = 0.1)rw30() uses normal distribution exclusively:
When rw30() doesn’t fit your needs:
# Generate walks
walks <- rw30()
# Show that mean displacement is zero
walks |>
group_by(step_number) |>
summarize(mean_position = mean(y)) |>
ggplot(aes(x = step_number, y = mean_position)) +
geom_line(color = "blue", linewidth = 1) +
geom_hline(yintercept = 0, linetype = "dashed", color = "red") +
theme_minimal() +
labs(
title = "Mean Position Over Time",
subtitle = "Averages to zero (red line)",
x = "Step",
y = "Mean Position"
)# Show that standard deviation grows as sqrt(n)
walks |>
group_by(step_number) |>
reframe(
sd_position = sd(y),
theoretical = sqrt(step_number)
) |>
ungroup() |>
ggplot(aes(x = step_number)) +
geom_line(aes(y = sd_position, color = "Observed"), linewidth = 1) +
geom_line(aes(y = theoretical, color = "Theoretical"), linewidth = 1, linetype = "dashed") +
scale_color_manual(values = c("Observed" = "blue", "Theoretical" = "red")) +
theme_minimal() +
labs(
title = "Standard Deviation Growth",
subtitle = "Should follow sqrt(n) (red dashed line)",
x = "Step",
y = "Standard Deviation",
color = ""
)# Find when walks first cross a threshold
walks <- rw30()
first_crossing <- walks |>
group_by(walk_number) |>
filter(y >= 5) |>
slice_min(step_number, n = 1) |>
select(walk_number, first_crossing_time = step_number)
# Some walks may never cross
n_crossed <- nrow(first_crossing)
cat(sprintf("%d out of 30 walks crossed 5\n", n_crossed))
#> 21 out of 30 walks crossed 5
# Distribution of first crossing times
if (n_crossed > 0) {
ggplot(first_crossing, aes(x = first_crossing_time)) +
geom_histogram(bins = 20, fill = "steelblue", alpha = 0.7) +
theme_minimal() +
labs(
title = "First Passage Time Distribution",
subtitle = "Time to first cross level 5",
x = "Step Number",
y = "Count"
)
}# Find maximum distance from origin
walks <- rw30()
max_excursion <- walks |>
group_by(walk_number) |>
summarize(
max_positive = max(y),
max_negative = min(y),
max_excursion = max(abs(y))
)
# Visualize
max_excursion |>
ggplot(aes(x = max_excursion)) +
geom_histogram(bins = 15, fill = "steelblue", alpha = 0.7) +
theme_minimal() +
labs(
title = "Distribution of Maximum Excursions",
subtitle = "Maximum absolute distance from origin",
x = "Maximum Excursion",
y = "Count"
)Once you’re comfortable with rw30(), explore:
vignette("getting-started")vignette("home")Ready for more control? Check out the function reference for customizable random walks!