Project 6: Random Walk
Problem Statement
Farmer John has an old grandparent (“Pa”) that likes to wander off randomly when working in the barn. Pa starts from the barn and every second take one step in a random direction North, South, East or West. What is Pa’s expected distance away from the barn after 1000 steps? If Pa takes many steps, will Pa be likely to move ever further from the origin, or be more likely to wander back to the origin over and over, and end up not far from where she started? Let’s write a simulation to find out.
This particular barn is in the center of a large grassy field. One day Pa starts to wander off, and notices that the grass has been mysteriously cut (by John) to resemble graph paper. Notice that after one step Pa is always exactly one unit away from the start. Let’s assume that Pa wanders eastward from the initial location on the first step. How far away might Pa be from the initial location after the second step? John sees that with a probability of 0.25 Pa will be 0 units away, with a probability of 0.25 Pa will be 2 units away, and with a probability of 0.5 Pa will be √2 units away. So, on average Pa will be further away after two steps than after one step. What about the third step? If the second step is to the north or south, the third step will bring the farmer closer to origin half the time and further half the time. If the second step is to the west (back to the start), the third step will be away from the origin. If the second step is to the east, the third step will be closer to the origin a quarter of the time, and further away three quarters of the time.
It seems like the more steps Pa takes, the greater the expected distance from the origin. We could continue this exhaustive enumeration of possibilities and perhaps develop a pretty good intuition about how this distance grows with respect to the number of steps. However, it is getting pretty tedious, so it seems like a better idea to write a program to do it for us.
However, there are a couple more twists to the situation. Pa’s wife “Mi-Ma”, another grandparent of John’s, also likes to wander away randomly, but riding an old mule. The mule goes South twice as often as any other direction. Lastly, John’s favorite hog “Reg” has an odd habit of wandering off too, but only randomly going east or west at each step, never north or south. People think he’s a sun-follower, but nobody’s really sure. John figures your Python program ought to model these two as well, while you’re at it.
Program Requirements
- Your program should accept three command line arguments as parameters:
- a list of comma-separated “walk lengths” to simulate,
- the number of trials, or times to try each walk length, and
- which type of walk we are modeling—“Pa”, “Mi-Ma” or “Reg” or “all”.
-
If the user enters incorrect command line arguments, print a usage message and exit
-
For each walker, for each walk length, calculate the ending position (x,y) after each trial of that walk length. Assume the walker always starts at 0,0 for each new trial.
-
For each walker, for each walk length, use your calculated ending position to calculate the walk distance (Hint: Pythagorean Theorem)
- For each walker, for each walk length, with your list of walking distances, calculate the following statistical information:
- mean is the average distance over all walks of that length as the crow flies.
- max is the longest distance as the crow flies.
- min is the shortest distance as the crow flies.
- CV is the Coefficient of Variance, defined as the standard deviation divided by the mean.
-
Output your statistical calculations to the console, formatted exactly as shown in the directions.
-
For each walker, for a walk length of 100, calculate the ending position (x,y) for 50 trials.
- With those (x,y) positions ready, using Python turtle, graph each walker’s ending points. It should look very similar to the picture at the top of this page.
- Pa’s turtle should be a black circle.
- Mi-ma’s turtle should be a green square.
- Reg’s turtle should be a red arrow.
Directions
-
Develop your algorithm in detail before writing any code. Read the problem statement very closely several times to make sure you understand all the small details.
-
Develop your algorithm in detail before writing any code. Read the problem statement very closely several times to make sure you understand all the small details.
- Accept the assignment in GitHub by opening the Assignment Invitation URL link provided in your Canvas assignment.
- This video provides step-by-step directions for completing GitHub assignments.
-
From inside your GitHub repository, click the Work in Repl.it button.
-
Open the
exercise.py
file in thesrc
directory, and begin coding your solution. -
Update the module docstring with your information.
-
Format your console output exactly as shown below:
Pa random walk of 100 steps Mean = 8.5 CV = 0.6 Max = 19.8 Min = 1.4 Pa random walk of 1000 steps Mean = 31.4 CV = 0.5 Max = 57.0 Min = 1.4 Mi-Ma random walk of 100 steps Mean = 26.7 CV = 0.4 Max = 52.6 Min = 7.6 Mi-Ma random walk of 1000 steps Mean = 243.4 CV = 0.2 Max = 318.2 Min = 156.2 Reg random walk of 100 steps Mean = 7.6 CV = 0.8 Max = 22.0 Min = 0.0 Reg random walk of 1000 steps Mean = 33.2 CV = 0.7 Max = 86.0 Min = 0.0
-
Execute your finished program, and capture a screenshot of your scatter plot.
-
Click the Version Control icon in the left menu, type a memo in the What did you change text box, then click Commit & push
- Upload a screenshot of your scatter plot to the screenshot directory.