Member-only story

Carry out a Monte Carlo random walk in Python

Crystal X
3 min readDec 16, 2024

--

In a recent post I discussed performing a Monte Carlo random walk in Excel, and that post can be found here:- https://medium.com/@tracyrenee61/create-a-random-walk-in-excel-af64d6f7d6d6

In this post I intend to carry out a Monte Carlo random walk in Python, which, in my opinion, might be easier to undertake because there are functions already created and in place for this purpose.

I have created the script using Google Colab because it is a free resource within the Google family of applications.

Once I created the program, I imported the libraries that I would need to use, being:-

  1. Numpy to perform numerical calculations
  2. Matplotlib to visualise the data

I initialised the parameters of:-

  1. 20 steps
  2. 1 simulation

I defined the function, random_walk, that will be used to perform the random walk:-

  1. I used numpy to initialise the steps as being 20 random steps.
  2. I defined the position as being the cumulative sum of the array elements of steps.

I then defined the variable, one_walk, which was the function, random_walk, being executed:-

--

--

Crystal X
Crystal X

Written by Crystal X

I have over five decades experience in the world of work, being in fast food, the military, business, non-profits, and the healthcare sector.

No responses yet