Generate White Noise in Python

Your goal

You need to randomly generate a white noise time series in Python.

Step-by-step tutorial

There are a couple ways to do this. We'll cover both.

Approach 1: Use the random module

You can use the standard random module to generate the white noise series:

>>> import random
>>> random.seed(14)
>>> white_noise = [random.gauss(0.0, 1.0) for i in range(1000)]

The Pandas Series has a describe() method that allows you to verify (informally) that this series has the statistical characteristics you'd expect of white noise.

>>> from pandas import Series
>>> white_noise_series = Series(white_noise)
>>> white_noise_series.describe()
count    1000.000000
mean        0.004942
std         0.984969
min        -2.848431
25%        -0.635941
50%         0.006516
75%         0.651805
max         3.558710
dtype: float64

(See for example the empirical rule.)

Here's a plot:

>>> import matplotlib.pyplot as plt
>>> plt.clf()
>>> plt.plot(white_noise)
[<matplotlib.lines.Line2D object at 0x127b20610>]
>>> plt.show()
A white noise series
A white noise series

Approach 2: Use NumPy

You can do the same thing a little more simply using NumPy:

>>> import numpy as np
>>> white_noise = np.random.normal(0.0, 1.0, 1000)

You can verify the statistics using a Pandas Series as we did in approach 1 above. Plotting is also the same as it was with approach 1.