Rayleigh Distributon in Python

In this tutorial you will learn:

  • What is Rayleigh Distribution?
  • Rayleigh Distribution Implementation in python
  • Visualization of Rayleigh Distribution

Rayleigh Distribution

Rayleigh Distribution is a continuous probability distribution named after English Lord Rayleigh. This distribution is for non-negative random variables and it is essentially a chi square distribution with two degrees of freedom. Rayleigh distribution is used to model the wind speeds, wave heights and sound radiations. It also gives measure the life time of electrical components like capacitors, resistors, transistors etc.

Rayleigh Distribution in Python

The random module of python’s NumPy library provide an inbuilt function rayleigh() for implementation of Rayleigh Distribution. The rayleigh() function takes in two parameters, first parameter is the “size” of the array which we require as an output and it is the mandatory parameter. The second parameter “scale” is an optional parameter and it is the standard deviation which decides the flatness of distribution, if this parameter is not explicitly defined its value is taken as 1.0 by default. Lets take an example, we will generate a 1D array(1,5) of Rayleigh distribution with scale 2.0. In this code we can see that we are importing the random module in the second line of the code and in the fourth line we are applying the Rayleigh distribution with output size parameter (1,5) and standard deviation “scale” 2.0.
  1. #importing the random module
  2. from numpy import random
  3. #applying the Rayleigh function
  4. res_arr= random.rayleigh(size=4,scale=2.0)
  5. #printing the results
  6. print('1D array of size(1,5) having Rayleigh distribution with scale 2.0 :\n')
  7. print(res_arr)
Lets take another example, in this example we will generate a 2D array of Rayleigh distribution having size (2,5) with default standard deviation.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Rayleigh function to generate Rayleigh distribution of size 2 x 5 with default scale value
  4. res_arr = random.rayleigh(size=(2,5))
  5. print('2D Rayleigh Distribution as output from rayleigh() function:\n')
  6. #printing the result
  7. print(res_arr)
In this example we will generate a 3D array of Rayleigh distribution of the size(2,4,2) with standard deviation 1.5
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Rayleigh function to generate Rayleigh distribution of size 2 x 4 x 2
  4. res = random.rayleigh(size=(2,4,2), scale=1.5)
  5. print('3D Rayleigh Distribution as output from rayleigh() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Rayleigh Distribution

In this example we will visualize the Rayleigh Distribution with default value of standard deviation using the displot function of seaborn library.
  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using Rayleigh function to generate distributions of size 2000 with standard deviation 1
  6. sb.distplot(random.rayleigh(size=2000), hist=False, label='Rayleigh Distribution')
  7. #plotting the graph
  8. mpl.show()

Add new comment