I need to draw samples from a white noise process in order to implement a particular integral numerically.
How do I generate this with Python (i.e., numpy, scipy, etc.)?
You can achieve this through the numpy.random.normal
function, which draws a given number of samples from a Gaussian distribution.
import numpy
import matplotlib.pyplot as plt
mean = 0
std = 1
num_samples = 1000
samples = numpy.random.normal(mean, std, size=num_samples)
plt.plot(samples)
plt.show()
numpy.random.standard_normal(size=num_samples)
can also be used when mean=0, and std=1
Mar 4, 2016 at 0:29
Short answer is numpy.random.random()
. Numpy site description
But since I find more and more answers to similar questions written as numpy.random.normal
, I suspect a little description is needed. If I do understand Wikipedia (and a few lessons at the University) correctly, Gauss and White Noise are two separate things. White noise has Uniform distribution, not Normal (Gaussian).
import numpy.random as nprnd
import matplotlib.pyplot as plt
num_samples = 10000
num_bins = 200
samples = numpy.random.random(size=num_samples)
plt.hist(samples, num_bins)
plt.show()
This is my first answer, so if you correct mistakes possibly made by me here, I'll gladly update it. Thanks =)
White noise has Uniform distribution, not Normal (Gaussian).
White noise must have Uniform distribution over frequencies but it can have any distribution over time (for instance Normal).
Create random samples with normal distribution (Gaussian) with numpy.random.normal
:
import numpy as np
import seaborn as sns
mu, sigma = 0, 1 # mean and standard deviation
s = np.random.normal(mu, sigma, size=1000) # 1000 samples with normal distribution
# seaborn histogram with Kernel Density Estimation
sns.distplot(s, bins=40, hist_kws={'edgecolor':'black'})