Skip to main content

Section 24.7 Random Walks

A random walk
 1 
https://en.wikipedia.org/wiki/Random_walk
is a simulation technique that has been applied to solve problems in areas from physics to biology to materials science economics to who to recommend that people follow on Twitter
 2 
https://dl.acm.org/doi/10.1145/2488388.2488433
The basic idea is to think about a process in terms of a series of steps. These steps are done with some degree of randomness: maybe a fixed distance in a random direction, or in a fixed direction with a random distance. In a simulation, “direction” and “distance” may not mean actual physical dimensions. In a stock market simulation random walk, a step might be a price movement from one day to the next.
However, the Python turtle makes for a great visual way to do a random walk where “direction” and “distance” refer to actual physical movement. This example does a random walk where each step involves moving 10 pixels forward in one of the cardinal directions (north, south, east, west).
To generate a random direction, we are picking a random integer from 0-3 and then multiplying that by 90. This guarantees that the only numbers we can pick are 0, 90, 180, and 270. Try changing line 3 to direction = random.randrange(0, 360) and removing line 5. What is the result?

Checkpoint 24.7.1.

    What is the result of making those changes to lines 3 and 5?
  • The turtle still moves in the same way as before.
  • Did you remove line 5?
  • The turtle’s steps can go in any direction.
  • Correct.
  • The turtle goes in circles.
  • Did you try making the changes?
To simulate something like the stock market, we might want to do something like the following. Try running this program and think of the x dimension as time - the far left is day 0 and each step to the right is another day - and the y dimension as value - the price of a stock.
To take steps in this program, we track the current location of the turtle. To do a step, we increase currentX by 2 and change currentY by a random amount between -10 and 10. This will mean each step moves slightly to the right and move up/down by anywhere from -10 to 12.
One possible flaw with this program is that the change in stock prices uses a different distribution of random changes. Most days the price goes up or down by a very small amount. However, some days it climbs or drops by a lot more. Let’s figure out that logic.

Checkpoint 24.7.2.

This program should work like the one above. However, at each step, we want there to be a 2% chance that the stock change up to 40 in either direction instead of just 10. To do that, we will make a variable randValue hold a value between 1 and 100. If that is less than or equal to 2, then we will adjust currentY with a random value chosen from the larger range.
Order and indent the code blocks to build this logic. Make sure to update currentX and currentY before you do the goto .
Now that you have figured out the recipe, try using it in the program above. Does the graph look more like a stock market price history
 3 
https://finance.yahoo.com/chart/INTC#eyJpbnRlcnZhbCI6ImRheSIsInBlcml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