Simulate systems over timeLesson 37 of 76 · 8 min

Follow a random walk

Simulate complete paths and retain the distinction between steps and path outcomes.

A stochastic process describes how a random state changes over time. A random walk updates its position by adding a random step. One path is a complete sequence of steps, and one path outcome might be its final position or whether it ever crossed a boundary.

The matrix below stores one path per row and one time step per column. A cumulative sum creates the states. Final-position and ever-crossed events are different: a path can cross a threshold and return before the last step.

The simple walk is an authored mechanism with independent equally likely steps. It is useful for learning path bookkeeping. Applying it to real demand, physical movement or prices would require a domain argument about drift, dependence, time scale and boundary behaviour.

In the plot call, paths[:12] selects the first twelve paths. The transpose .T swaps rows and columns, so the plotting function draws each complete path as one line.

A path has more than a final state
125.850.575.2100-12-5.517.514PositionStep
125.850.575.2100-12-5.517.514PositionStep
Reference illustration. First three paths of the seeded published walk. Each line contains one hundred dependent positions. Finishing at or above 10 and ever reaching 10 are different events defined on a whole path.

Can a path cross +10 and still finish below +10?

Write a prediction (optional)

Seeded teaching simulation

Select Run Python below with the code unchanged.

Tools
About Python ↗

The code is editable. Press Ctrl+Enter or ⌘+Enter to run Python from the editor.

Published example output

This result comes from the original Python. Your runs appear above. Build environment ↗

P(final >= 10): 0.1783
P(ever >= 10): 0.33
Plot generated from the published Python during the build. Refer to its labelled axes and the text output.
Open full-size figure ↗

Read the first result

The ever-crossed event includes paths that later return. Keep all states needed by the target event rather than only the final value.

Try an edit

Replace rng.choice([-1, 1], size=(3000, 100)) with rng.choice([-1, 1], size=(3000, 100), p=[.45, .55]). Select Run Python again. Compare P(final >= 10) and P(ever >= 10) with the first run. How does favouring +1 affect the two path events?

Back to the Python editor

Scope. The walk is a finite illustrative process, not a validated forecast for a real time series.

Try a fresh case

For an inventory model, why can recording only the final stock be insufficient?

Choose an answer
Need a hint?
Hint 1

Distinguish final state from events along the path.

Hint 2

Name which timestamps the service requirement covers.

Your notes