Random Numbers in NumPy


In [ ]:
import numpy as np

The numpy.random module adds to the standard built-in Python random functions for generating efficiently whole arrays of sample values with many kinds of probability distributions.

Example: build a 4x4 array of samples from the standard normal distribution,


In [ ]:
samples = np.random.normal(size=(4,4))
samples

Advantages? Built-in Random Python only samples one value at a time and it is significantly less efficient. The following block builds an array with 10$^7$ normally distributed values:


In [ ]:
import random
N = 10000000
%timeit samples = [random.normalvariate(0,1) for i in range(N)]

Write the equivalent code using the np.random.normal() function and time it! Keep in mind that the NumPy function is vectorized!


In [ ]:

See the Numpy documentation site for detailed info on the numpy.random module

Random Walks

Using standard Python builtin functions, try to write a piece of code corresponding to a 1D Random walker initially located at 0 and taking steps of 1 or -1 with equal probability.

Hint: use a list to keep track of the path of your random walker and have a look at the random.randint() function to generate the steps.


In [ ]:

If it's too hard to start from scratch, you may want to peek at my solution.

Use matplotlib to plot the path of your random walker


In [ ]:
import matplotlib.pyplot as plt
%matplotlib inline
#plt.plot(INSERT THE NAME OF THE VARIABLE CONTAINING THE PATH)

Now think of a possible alternative code using NumPy. Keep in mind that:

  • NumPy offers arrays to store the path
  • numpy.random offers a vectorized version of random generating functions

Again, here is my solution.


In [ ]:

Let's have a look at it:


In [ ]:
#plt.plot(INSERT THE NAME OF THE VARIABLE CONTAINING THE PATH)

Now that your code is ready, execute it and find the minimum and maximum value along the path.


In [ ]:

Find the time (step) at which the random walker reaches a particular given value. How long did it take the walker to get to a value of 20 away from the starting position?

Tip: argmax method returns the index of the first occurence of the maximum value


In [ ]:

Simulating Many Random Walkers Simultaneously

In order to illustrate the capability of NumPy vs. Standard Python, let us suppose our aim was to simulate many random walkers. The NumPy code needs only to be slightly modified. We only need to add an extra dimension to our arrays!(one solution)


In [ ]:

Besides the plotting instructions, only one (or few, dependeding on your solution) line of code need to be modified!

Find a way to plot all the paths:


In [ ]:

Find out the following information:

  • Which walker ended farther away from the starting point?
  • What the farthest value sampled by any walker? Which walker?
  • What was the most commonly value visiter by the walkers?
  • How many walkers reach a value of 200 away from the origin?
  • Which walker is the fastest to reach point 200?

Exploring multiple dimensions

  1. Generalise your code with one random walker from 1D to 2D.
  2. Now, the final step, multiple 2D random walkers!
  3. Multiple velocities?
  4. Blockers?

In [ ]: