Lesson 26: Random number generation

(c) 2016 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

In [1]:
import random

import numpy as np

# This is how we import the module of Matplotlib we'll be using
import matplotlib.pyplot as plt

# Some pretty Seaborn settings
import seaborn as sns
rc={'lines.linewidth': 2, 'axes.labelsize': 18, 'axes.titlesize': 18}
sns.set(rc=rc)

# The following is specific Jupyter notebooks
%matplotlib inline
%config InlineBackend.figure_formats = {'png', 'retina'}

Random number generation (RNG), besides being a song in the original off-Broadway run of Hedwig and the Angry Inch, is the process by which a string of random numbers may be drawn. Of course, the numbers are not completely random for several reasons.

  1. They are drawn from a probability distribution. The most common one is the uniform distribution on the domain $0 \le x < 1$, i.e., random numbers between zero and one.
  2. In most computer applications, including the ones we'll use in bootcamp, the random numbers are actually pseudorandom. They depend entirely on an input seed and are then generated by a deterministic algorithm from that seed.

This is a bit academic. Let's jump right in generating random numbers. Much of the random number generation functionality you will need is in the np.random module. Let's start by generating random numbers on a uniform interval

In [16]:
np.random.random(size=10)
Out[16]:
array([ 0.50090856,  0.03777642,  0.31012364,  0.40695874,  0.84394461,
        0.07173655,  0.56998497,  0.31675987,  0.53977887,  0.39812319])

The function random() in the np.random module generates random numbers on the interval $[0,1)$. The size kwarg is how many random numbers you wish to generate. They are returned as a NumPy array.

We can check to make sure it is appropriately drawing random numbers out of the uniform distribution by plotting the cumulative distribution functions, just like we did last time. We'll generate 100,000 random numbers and plot them along with the CDF.

In [17]:
# Generate sorted random numbers
x = np.sort(np.random.random(size=100000))

# Generate y-axis for CDF
y = np.arange(1, len(x)+1) / len(x)

# Plot CDF from random numbers (for plotting purposes, only plot 100 points)
plt.plot(x[::1000], y[::1000], marker='.', linestyle='none', markersize=10)

# Plot expected CDF (just a straight line from (0,0) to (1,1)
plt.plot([0, 1], [0, 1], 'k-')
Out[17]:
[<matplotlib.lines.Line2D at 0x12a2ab828>]

So, it looks like our random number generator is doing a good job.

Generating random numbers on the uniform interval is one of the most commonly used RNG techniques. In fact, many of the other contexts of RNG are derived from draws from the uniform distribution. For example, you can do "coin flips," that is random draws that are either zero or one, like this:

In [18]:
# Generate 20 random numbers on uniform interval
x = np.random.random(size=20)

# Make them coin flips
heads = x > 0.5

# Show which were heads, and count the number of heads
print(heads)
print('\nThere were', np.sum(heads), ' heads.')
[False False  True False False  True False  True False  True False  True
 False False  True False  True  True  True False]

There were 9  heads.

Seeding random number generators

Now, just to demonstrate that random number generation is deterministic, we will explicitly seed the random number generator (usually seeded with a number representing the date/time to avoid repeats) to show that we get the same random numbers.

In [19]:
# Seed the RNG
np.random.seed(42)

# Generate random numbers
np.random.random(size=10)
Out[19]:
array([ 0.37454012,  0.95071431,  0.73199394,  0.59865848,  0.15601864,
        0.15599452,  0.05808361,  0.86617615,  0.60111501,  0.70807258])
In [28]:
# Re-seed the RNG
np.random.seed(42)

# Generate random numbers
np.random.random(size=10)
Out[28]:
array([ 0.37454012,  0.95071431,  0.73199394,  0.59865848,  0.15601864,
        0.15599452,  0.05808361,  0.86617615,  0.60111501,  0.70807258])

The random numbers are exactly the same. If we choose a different seed, we get totally different random numbers.

In [29]:
# Seed with a number that is close to the answer to everything
np.random.seed(43)
np.random.random(size=10)
Out[29]:
array([ 0.11505457,  0.60906654,  0.13339096,  0.24058962,  0.32713906,
        0.85913749,  0.66609021,  0.54116221,  0.02901382,  0.7337483 ])

If you are writing unit tests (which we will describe tomorrow when we do test driven development), it is often useful to seed the random number generator to get reproducible results.

Drawing random numbers out of other distributions

We can also draw random numbers from other probability distributions. For example, say we wanted to draw random samples from a Normal distribution with mean $\mu$ and standard deviation $\sigma$.

In [30]:
# Set parameters
mu = 10
sigma = 1

# Draw 10000 random samples
x = np.random.normal(mu, sigma, size=10000)

# Plot a histogram of our draws
_ = plt.hist(x, bins=100)

It looks Normal, but let's test the mean and standard deviation.

In [31]:
np.mean(x), np.std(x)
Out[31]:
(10.013847246071363, 0.99983608507701072)

Yup, mean of 10 and standard deviation of 1!

Selections from discrete distributions

The random numbers we have generated so far from from continuous probability distributions. We can also draw random numbers from discrete distributions. We already showed that we can do this for "coin flips," but we can do it for other distributions as well. Perhaps the most common example is a choice of a random integer from a set of integers. For example, if we wanted to draw one of four integers in {0, 1, 2, 3}, we can use the np.random.randint() function.

In [32]:
# Draw random integers on [0, 4), i.e., exclusive of last one.
np.random.randint(0, 4, 20)
Out[32]:
array([3, 1, 2, 0, 2, 0, 2, 3, 3, 3, 2, 3, 3, 3, 3, 1, 0, 0, 1, 3])

I bet you can guess where this might be useful! We can generate random DNA sequences.

In [33]:
# Key of bases
bases = 'ATGC'

# Draw random numbers for sequence
x = np.random.randint(0, 4, 50)

# Make sequence
seq_list = [None]*50
for i, b in enumerate(x):
    seq_list[i] = bases[b]
    
# Join the sequence
''.join(seq_list)
Out[33]:
'CTAGCTGGCTCGTCGCTCATTCTTTCCCTACTATGCTTTAGTACCTCATC'

There are other discrete distributions we can draw from, such as binomial, geometric, Poisson, etc., and the documentation describes how to use them.

Choosing elements from an array

It is often useful to randomly choose elements from an existing array. The np.random.choice() function does this. You equivalently could do this using np.random.randint(), where the integers represent indices in the array, except np.random.choice() has a great keyword argument, replace, which allows random draws with or without replacement. For example, say you had 100 samples that you wanted to send to a facility for analysis, but you can only afford to send 20. If we used np.random.randint(), we might have a problem.

In [34]:
np.random.randint(0, 51, 20)
Out[34]:
array([23, 21, 23, 14, 11, 44,  6, 50, 18,  5, 11, 29, 18, 39, 18, 26, 28,
       14, 32, 21])

Sample 18 was selected thrice and samples 11 and 23 were selected twice! We can use np.random.choice() instead.

In [35]:
np.random.choice(np.arange(51), 20, replace=False)
Out[35]:
array([20, 23, 36, 21, 17, 46,  5, 27, 16, 25, 35,  8, 48, 22, 50, 41,  6,
       28, 32, 19])

Shuffling an array

Similarly, the np.random.permutation() function is useful. It takes the entries in an array and shuffles them! Let's shuffle a deck of cards.

In [36]:
np.random.permutation(np.arange(53))
Out[36]:
array([27, 33, 47, 41,  2, 28, 35, 14, 50, 24,  8, 36, 29, 12, 38, 32, 37,
       30, 19, 39, 34, 25,  3, 21, 26, 20,  1, 48,  4, 17, 42,  5, 10, 15,
       11, 13,  6, 52, 22, 49,  7, 23, 16, 18,  0, 43, 46, 40, 44, 45, 31,
        9, 51])

The random module

Though it has far less functionality than np.random, the random module in the standard library has some useful functionality. In particular, the random.choice() method can take a string as an input and choose characters out of that string. For example, to generate random DNA sequences, we could just do the following.

In [37]:
# Make sequence
seq_list = [None]*50
for i in range(len(seq_list)):
    seq_list[i] = random.choice('ATGC')
    
# Join the sequence
''.join(seq_list)
Out[37]:
'GTCAGTGGGGGGTAACGGCTGAACAACACCCCGAACTAGATCCGCCGACT'

When do we need RNG?

Answer: VERY OFTEN! We will see many examples in the next lessons and in the exercises.

In many ways, probability is the language of biology. Molecular processes have energetics that are comparable to the thermal energy, which means they are always influenced by random thermal forces. The processes of the central dogma, including DNA replication, are no exceptions. This gives rise to random mutations, which are central to understanding how evolution works. If we want to understand them, it is often useful to use random number generators to model the processes.

RNG also comes up A LOT in data analysis. We'll close this lesson with a brief example.