Understanding Rayleigh Distribution in Python
The Rayleigh distribution is widely used in signal processing and related fields. It's particularly useful for modeling the magnitude of a vector with two independent Gaussian components (such as wind speed or scattered signals).
Key Parameters:
- scale: Also known as the standard deviation, it controls the "spread" or "flatness" of the distribution (default is 1.0).
- size: Defines the shape of the output array of samples.
Program:
Generating Rayleigh Distribution Samples
Here's how to draw random samples from a Rayleigh distribution using NumPy:
from numpy import random
# Draw samples from Rayleigh distribution with scale 2 and shape (2, 3)
x = random.rayleigh(scale=2, size=(2, 3))
print(x)
Output:
Visualizing the Rayleigh Distribution
We can visualize the Rayleigh distribution using Seaborn and Matplotlib to better understand its shape:
Program:
from numpy import random
import matplotlib.pyplot as plt
import seaborn as sns
# Generate 1000 random samples and plot the KDE (Kernel Density Estimate)
sns.displot(random.rayleigh(size=1000), kind="kde")
plt.title("Rayleigh Distribution")
plt.show()
Rayleigh Distribution vs. Chi-Square Distribution
Interestingly, there's a mathematical connection between the Rayleigh and Chi-square distributions.
When the scale parameter is set to 1 (unit standard deviation) and the Chi-square distribution has 2 degrees of freedom, both distributions are equivalent.
This similarity arises because:
- Rayleigh distribution models the magnitude of a 2D vector of independent standard normal variables.
- Chi-square distribution with 2 degrees of freedom also represents the sum of squares of two standard normal variables.