TExcelTAD
Home › NumPy › Normal Distribution

Normal Distribution

The normal distribution (also called the Gaussian distribution, after the mathematician Carl Friedrich Gauss) is the most important distribution in statistics. Its density is the famous symmetric bell curve: most values sit near the middle, and values far from the middle become rarer and rarer.

It appears everywhere because of the central limit theorem: whenever a quantity is the sum of many small, independent effects, it tends to be normally distributed. Measurement errors, heights, blood pressure and exam scores are classic examples.

Generating normal data

Call rng.normal(loc, scale, size):

Parameter Meaning Default
loc the mean μ, where the peak of the bell is 0.0
scale the standard deviation σ, how wide the bell is 1.0
size output shape one value
python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
heights = rng.normal(loc=170, scale=8, size=(2, 4))   # adult heights in cm
print(heights.round(1))
print(rng.standard_normal(3).round(3))                # loc=0, scale=1
[[172.4 161.7 176.  177.5]
 [154.4 159.6 171.  167.5]]
[-0.017 -0.853  0.879]

rng.standard_normal() is the special case with mean 0 and standard deviation 1, the standard normal distribution.

Checking the sample

With a large sample, the sample mean and standard deviation should be close to loc and scale:

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
heights = rng.normal(170, 8, size=100_000)
print(f"mean: {heights.mean():.2f}   std: {heights.std():.2f}")
print(f"min:  {heights.min():.1f}  max: {heights.max():.1f}")
mean: 169.97   std: 8.03
min:  134.9  max: 210.1

The 68-95-99.7 rule

For any normal distribution, a fixed share of values falls within 1, 2 and 3 standard deviations of the mean. Let's confirm it on simulated data:

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
mu, sigma = 100, 15                      # e.g. IQ-style scores
x = rng.normal(mu, sigma, 200_000)

for k in (1, 2, 3):
    inside = np.mean(np.abs(x - mu) < k * sigma)
    print(f"within {k} sigma: {inside:.3%}")
within 1 sigma: 68.201%
within 2 sigma: 95.451%
within 3 sigma: 99.713%

That gives the rule of thumb: about 68 % within 1σ, 95 % within 2σ, 99.7 % within 3σ. A value more than 3σ from the mean is unusual enough to be worth a second look, which is a simple way to flag outliers.

Visualising the bell curve

We draw a smooth density curve (KDE, introduced in the Seaborn lesson) for three samples. With seaborn installed you could write sns.kdeplot(sample) instead of the gaussian_kde line.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import gaussian_kde

rng = np.random.default_rng(42)
xs = np.linspace(-6, 6, 400)
plt.figure(figsize=(6, 3.5))
for sigma, color in [(0.5, "#e07a5f"), (1, "#4dabcf"), (2, "#81b29a")]:
    sample = rng.normal(0, sigma, 5000)
    plt.plot(xs, gaussian_kde(sample)(xs), color=color, label=f"sigma = {sigma}")
plt.title("Normal distributions with mean 0")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted three normal curves with different standard deviations")
Plotted three normal curves with different standard deviations

All three curves are centred at 0 (the loc). A small σ gives a tall, narrow peak; a large σ a low, wide one. The area under each curve is always 1.

A practical use: simulating measurement noise

Real measurements are a true value plus noise. Adding normal noise to a clean signal is the standard way to create realistic test data:

python · try it yourself
import numpy as np

rng = np.random.default_rng(1)
true_weight = 250.0                                # grams
scale_readings = true_weight + rng.normal(0, 1.5, size=6)
print(scale_readings.round(2))
print("average of readings:", scale_readings.mean().round(2))
[250.52 251.23 250.5  248.05 251.36 250.67]
average of readings: 250.39

Averaging several noisy readings gives an estimate much closer to the true value than a single reading usually does.

Legacy note: np.random.normal(loc, scale, size) and np.random.randn(...) are the old global-state equivalents of rng.normal and rng.standard_normal.

Test yourself

In rng.normal(loc=5, scale=2, size=10), what does scale set?

  1. The mean
  2. The standard deviation
  3. The number of values
  4. The maximum value

Roughly what share of normally distributed values lies within 2 standard deviations of the mean?

  1. 50 %
  2. 68 %
  3. 95 %
  4. 100 %

What does increasing the standard deviation do to the bell curve?

  1. Shifts it right
  2. Makes it wider and flatter
  3. Makes it taller and narrower
  4. Makes it asymmetric