TExcelTAD
Home › NumPy › Rayleigh Distribution

Rayleigh Distribution

The Rayleigh distribution describes the length of a 2-D vector whose x and y components are independent normal values with mean 0 and the same standard deviation. That situation shows up often:

  • wind speed, when the east–west and north–south components are random;
  • the distance from the bullseye of shots whose horizontal and vertical errors are normal;
  • the amplitude of radio signals in wireless communication (Rayleigh fading);
  • the height of ocean waves.
Parameter Meaning Default
scale σ, the standard deviation of each component (also the mode, the most likely value) 1.0
size output shape one value

Generating Rayleigh data

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
wind = rng.rayleigh(scale=4, size=8)      # wind speeds in m/s
print(wind.round(2))
print("strongest gust:", wind.max().round(2))
[ 8.77  8.65  8.74  2.99  1.66  6.82  6.72 10.  ]
strongest gust: 10.0

Like the chi-square and exponential distributions, Rayleigh values are never negative: they are lengths.

Building it from two normal components

The definition can be checked directly with np.hypot, which computes √(x² + y²):

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
sigma = 2.0
x = rng.normal(0, sigma, 200_000)
y = rng.normal(0, sigma, 200_000)
distance = np.hypot(x, y)
direct = rng.rayleigh(sigma, 200_000)

print(f"from components: mean {distance.mean():.3f}, std {distance.std():.3f}")
print(f"rng.rayleigh:    mean {direct.mean():.3f}, std {direct.std():.3f}")
print(f"theory mean sigma*sqrt(pi/2) = {sigma * np.sqrt(np.pi / 2):.3f}")
from components: mean 2.508, std 1.312
rng.rayleigh:    mean 2.505, std 1.310
theory mean sigma*sqrt(pi/2) = 2.507

Visualising

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
bins = np.linspace(0, 10, 81)
plt.figure(figsize=(6, 3.5))
for scale, color in [(0.5, "#e07a5f"), (1, "#4dabcf"), (2, "#81b29a"), (3, "#9d79bc")]:
    plt.hist(rng.rayleigh(scale, 50_000), bins=bins, density=True, histtype="step",
             linewidth=1.5, color=color, label=f"scale = {scale}")
plt.xlabel("value")
plt.ylabel("density")
plt.legend()
plt.tight_layout()
plt.show()
print("Plotted Rayleigh distributions for four scales")
Plotted Rayleigh distributions for four scales

Each curve rises from 0, peaks at scale and then has a tail to the right. A larger scale stretches the curve.

Example: darts

A player throws 1,000 darts. The horizontal and vertical errors are both normal with σ = 3 cm. How often does a dart land within 5 cm of the centre?

python · try it yourself
import numpy as np

rng = np.random.default_rng(9)
sigma = 3
dist = rng.rayleigh(sigma, 1000)
print(f"within 5 cm: {np.mean(dist < 5):.1%}")
print(f"theory:      {1 - np.exp(-5**2 / (2 * sigma**2)):.1%}")
print(f"median miss: {np.median(dist):.2f} cm")
within 5 cm: 75.4%
theory:      75.1%
median miss: 3.58 cm

Relation to chi-square

If R is Rayleigh with scale 1, then R² is chi-square with 2 degrees of freedom: the sum of two squared standard normals.

python · try it yourself
import numpy as np

rng = np.random.default_rng(42)
r_squared = rng.rayleigh(1, 200_000) ** 2
chi2 = rng.chisquare(2, 200_000)
print(f"R^2 mean {r_squared.mean():.3f}, chi2(2) mean {chi2.mean():.3f}")
R^2 mean 1.998, chi2(2) mean 1.996

Test yourself

The Rayleigh distribution models...

  1. The sum of two uniform values
  2. The length of a 2-D vector with independent normal components
  3. The number of events per hour
  4. The waiting time until an event

What is the most likely value (mode) of rng.rayleigh(scale=4)?

  1. 0
  2. 2
  3. 4
  4. 16

Which NumPy function computes sqrt(x**2 + y**2) element-wise?

  1. np.norm
  2. np.hypot
  3. np.dist
  4. np.radius