Project 4: How Far Does a Random Walker Get?

Author

Your Name

Model and assumptions

Explain the difference between the distance traveled along the path and the displacement. Which one is random? State two assumptions of the walk model, and name one real situation where at least one of them would be unrealistic.

A function for one walk

Complete the function. Remove #| eval: false when your code is ready to run.

final_distance <- function(number_of_steps) {
  x <- 0
  y <- 0
  for (step in 1:number_of_steps) {
    # TODO: flip a fair coin to move x one unit east (+1) or west (-1).
    # TODO: flip a second, independent coin to move y north (+1) or south (-1).
  }
  # TODO: return the displacement from (0, 0).
}

Check the function on walks short enough to work out by hand:

# One step: what should every result be?
# TODO: call final_distance(1) several times.

# Two steps: which displacements are possible?
two_step_results <- numeric(1000)
for (run in 1:1000) {
  two_step_results[run] <- final_distance(2)
}
table(round(two_step_results, 3))

Do the results match your hand reasoning?

Repeated simulations

set.seed(2026)   # you may choose a different seed
step_counts <- c(10, 25, 50, 100, 200, 400)
runs <- 500
mean_distance <- numeric(length(step_counts))

for (k in seq_along(step_counts)) {
  distances <- numeric(runs)
  # TODO: fill distances with 500 simulated displacements for step_counts[k].
  # TODO: store their mean in mean_distance[k].
}

walk_results <- data.frame(steps = step_counts, mean_distance = mean_distance)
walk_results

Now repeat only the 100-step case with a different seed:

set.seed(4)   # a different seed
# TODO: simulate 500 walks of 100 steps and compute their mean displacement.
# TODO: compute the percentage change from your earlier 100-step mean.

By about what percentage did the mean change? How many digits of your table do you trust?

Fit and check a power law

# 1. Mean displacement vs. number of steps
# TODO: plot walk_results$mean_distance against walk_results$steps.

# 2. Log-log plot
# TODO: plot log(mean_distance) against log(steps).

# 3. Fit the line on the log-log scale
power_fit <- lm(log(mean_distance) ~ log(steps), data = walk_results)
p <- unname(coef(power_fit)[2])        # slope = the power
a <- unname(exp(coef(power_fit)[1]))   # exp(intercept) = the coefficient
# TODO: display p and a.

# 4. Fitted curve on the original data
step_grid <- seq(10, 400, by = 1)
# TODO: replot the original data and add lines(step_grid, a * step_grid^p).

# 5. Residuals
# TODO: plot resid(power_fit) against log(walk_results$steps), with a line at 0.

Describe the shape of the data, the quality of the fit, and the residuals. If a walk has four times as many steps, about how many times farther does it typically end up?

Predict, then test

Make your prediction first:

predicted_1000 <- a * 1000^p
predicted_1000

Then run the test:

# TODO: simulate 500 walks of 1,000 steps and compute their mean displacement.
# TODO: compute the relative error of predicted_1000, as a percentage.

Compare the relative error with the seed-to-seed variation from earlier. Is 1,000 steps an extrapolation? Explain why the prediction did or didn’t work well.

Conclusion

How does the typical distance grow with the number of steps? Describe one way your conclusion could be wrong or limited.

Back to top