larray.random.normal
- larray.random.normal(loc=0.0, scale=1.0, axes=None, meta=None) Array [source]
Draw random samples from a normal (Gaussian) distribution.
Its probability density function is often called the bell curve because of its characteristic shape (see the example below)
- Parameters
- locfloat or array_like of floats
Mean (“centre”) of the distribution.
- scalefloat or array_like of floats
Standard deviation (spread or “width”) of the distribution.
- axesint, tuple of int, str, Axis or tuple/list/AxisCollection of Axis, optional
Minimum axes the resulting array must have. Defaults to None. The resulting array axes will be the union of those mentioned in
axes
and those ofloc
andscale
. Ifloc
andscale
are scalars andaxes
is None, a single value is returned. Otherwise, if the resulting axes have a shape of, e.g.,(m, n, k)
, thenm * n * k
samples are drawn.- metalist of pairs or dict or Metadata, optional
Metadata (title, description, author, creation_date, …) associated with the array. Keys must be strings. Values must be of type string, int, float, date, time or datetime.
- Returns
- Array or scalar
Drawn samples from the parameterized normal distribution.
Notes
The normal distributions occurs often in nature. For example, it describes the commonly occurring distribution of samples influenced by a large number of tiny, random disturbances, each with its own unique distribution [2].
The probability density function for the Gaussian distribution, first derived by De Moivre and 200 years later by both Gauss and Laplace independently [2], is
\[p(x) = \frac{1}{\sqrt{ 2 \pi \sigma^2 }} e^{ - \frac{ (x - \mu)^2 } {2 \sigma^2} },\]where \(\mu\) is the mean and \(\sigma\) the standard deviation. The square of the standard deviation, \(\sigma^2\), is called the variance.
The function has its peak at the mean, and its “spread” increases with the standard deviation (the function reaches 0.607 times its maximum at \(x + \sigma\) and \(x - \sigma\) [2]). This implies that la.random.normal is more likely to return samples lying close to the mean, rather than those far away.
References
- 1
Wikipedia, “Normal distribution”, http://en.wikipedia.org/wiki/Normal_distribution
- 2(1,2,3)
P. R. Peebles Jr., “Central Limit Theorem” in “Probability, Random Variables and Random Signal Principles”, 4th ed., 2001, pp. 51, 51, 125.
Examples
Generate a 2 x 3 array with numbers drawn from the distribution:
>>> la.random.normal(0, 1, axes=(2, 3)) {0}*\{1}* 0 1 2 0 0.3564325741877542 0.8944149721039006 1.7206904920773107 1 0.6904447654719367 -0.09395966570976753 0.185136309092257
With named and labelled axes
>>> la.random.normal(0, 1, axes='a=a0,a1;b=b0..b2') a\b b0 b1 b2 a0 2.3096106652701827 -0.4269082412118316 -1.0862791566867225 a1 0.8598817639620348 -2.386411240813283 0.10116503197279443
With varying loc and scale (each depending on a different axis)
>>> mu = la.sequence('a=a0,a1', initial=5, inc=5) >>> mu a a0 a1 5 10 >>> sigma = la.sequence('b=b0..b2', initial=1) >>> sigma b b0 b1 b2 1 2 3 >>> la.random.normal(mu, sigma) a\b b0 b1 b2 a0 5.939369790854615 2.5043856460438403 8.33560126941519 a1 10.759526714752091 10.093213549397403 11.705881778249683
Draw 1000 samples from the distribution:
>>> mu, sigma = 0, 0.1 # mean and standard deviation >>> sample = la.random.normal(mu, sigma, 1000)
Verify the mean and the variance:
>>> abs(mu - la.mean(sample)) < 0.01 True >>> abs(sigma - la.std(sample, ddof=1)) < 0.01 True
Display the histogram of the samples, along with the probability density function:
>>> import matplotlib.pyplot as plt >>> count, bins, ignored = plt.hist(sample, 30, normed=True) >>> pdf = 1 / (sigma * la.sqrt(2 * la.pi)) \ ... * la.exp(- (bins - mu) ** 2 / (2 * sigma ** 2)) >>> _ = plt.plot(bins, pdf, linewidth=2, color='r') >>> plt.show()