The walk
Use the same rules as Module 9.5. The particle starts at \((0, 0)\). On each step it moves one unit east or west and one unit north or south, chosen by two independent fair coin flips (runif(1) < 0.5). After \(n\) steps, its displacement is the straight-line distance from the start:
\[ d = \sqrt{x_{\text{final}}^2 + y_{\text{final}}^2}. \]
One walk gives one random value of \(d\). The mean displacement over many walks estimates the typical distance for that number of steps. That mean is what you’ll model.
Fitting a power law
If the mean displacement \(D\) follows a power law,
\[ D = a\,n^{p}, \]
then taking the natural log of both sides gives
\[ \ln D = \ln a + p \ln n. \]
That’s a straight line in \(\ln n\), with slope \(p\) and intercept \(\ln a\). So you can fit it with lm(log(D) ~ log(n)), just as lm() fit lines to data in Module 8.3. The slope is \(p\), and exp() of the intercept is \(a\).
What your report should show
1. Model and assumptions
In your own words, explain the difference between the distance traveled along the path after \(n\) steps and the displacement. Which one is random? State two assumptions of this walk model, and name one real situation (a molecule, an animal, a person) where at least one of them would be unrealistic.
2. A function for one walk
Write a function final_distance(number_of_steps) that simulates one walk and returns its displacement. Check it on cases you can work out by hand:
- After 1 step, what must the displacement always be? Run your function several times with
number_of_steps = 1and confirm it. - After 2 steps, list the possible displacements. Run the function many times (for example, 1,000) with
number_of_steps = 2and usetable(round(results, 3))to confirm that only those values appear.
3. Repeated simulations
Use set.seed() once at the top of this section. For each step count \(n = 10, 25, 50, 100, 200,\) and \(400\), simulate 500 walks and record the mean displacement. Present a table with columns for \(n\) and mean displacement.
Then check how much your estimates depend on chance: repeat only the \(n = 100\) case with a different seed. By about what percentage did the mean change? What does that suggest about how many digits of your table are trustworthy?
4. Fit and check a power law
- Plot mean displacement against \(n\) with axis labels. Describe the shape.
- Plot \(\ln D\) against \(\ln n\). Does it look close to a straight line?
- Fit
lm(log(D) ~ log(n))and report \(p\) and \(a\). - Plot your fitted curve \(D = a\,n^p\) on the original data (not the log–log plot), over \(n\) from 10 to 400.
- Plot the residuals of the log–log fit against \(\ln n\). Do they look like random scatter, or is there a pattern?
In one or two sentences, interpret \(p\) in plain language: if a walk has four times as many steps, about how many times farther does it typically end up?
5. Predict, then test
Before simulating any 1,000-step walks, use your fitted model to predict the mean displacement after 1,000 steps. Show the prediction in your report, generated by code that runs before the test simulation.
Now simulate 500 walks of 1,000 steps and compute their mean displacement. Report the relative error of your prediction,
\[ \text{relative error} = \frac{|\text{predicted} - \text{simulated}|}{\text{simulated}}, \]
as a percentage. Is that error larger or smaller than the seed-to-seed variation you found in Section 3? Is 1,000 steps an extrapolation from your data? Explain why it worked as well as it did, or why it didn’t.
6. Conclusion
Answer the question in a short paragraph: how does the typical distance grow with the number of steps? Then describe one way your conclusion could be wrong or limited. Consider the number of runs, the range of \(n\) you used, or the assumptions of the walk itself.
Evaluation
This project is graded holistically on a 10-point scale. I will look for a correct and checked walk function, a clear table of repeated simulations with an honest look at their variability, a power-law fit with readable graphs and a residual check, a prediction made before the test simulation, and a conclusion that distinguishes Monte Carlo noise from the limits of the model.
Before submitting
- Render from a fresh R session and inspect the HTML output.
- Check that every simulation uses 500 walks and the same walk rules.
- Label every graph’s axes, and say which plots use logs.
- Submit both the
.qmdand.htmlfiles to Moodle.