Skip to main content

Rectangular Distribution in Python


In this tutorial you will learn:

  • What is a Rectangular Distribution?
  • Rectangular Distribution Implementation in python
  • Visualization of Rectangular Distribution

Rectangular Distribution

Rectangular distribution is a distribution that has a constant probability, it is also known as uniform distribution. It is a family of symmetric probability distributions and the outputs of the distribution lies between certain bounds. These bounds are defined by two parameters which are the maximum and minimum values. The difference between the bounds defines the interval length, all intervals of the same length on the distribution's support are equally probable.

Rectangular Distribution in Python

Rectangular distribution in python is implemented using uniform() function. The rectangular() function takes in three parameters. First parameter “size” is the mandatory parameter and it is size of the output array which could be multi dimensional. The second parameter “a” is the lower bound, and if not explicitly defined, its value is taken as 0. The third parameter “b” is the upper parameter and if not explicitly defined, its value is taken as 1.
Lets take an example, we will generate an 1D array(1,7) of rectangular distribution having the 7 as lower limit(a) and 14 as upper limit(b). Here you can observe that if we re run the program again and again, the values of the distribution will keep on changing.

  1. #importing the random module
  2. from numpy import random
  3. #applying the uniform function with lower limit 7 and upper limit 14
  4. res_arr= random.uniform(low=7, high=14, size=7)
  5. #printing the results
  6. print('1D array having Rectangular Distribution within limits 7 and 14:/n')
  7. print(res_arr)

In this example we will generate a 2D array of rectangular distribution having size (3,4), in the first line of code we are importing random module from numpy library and in third line we are calling the uniform function with upper and lower limit as 2 and 6 respectively.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using uniform function to generate rectangular distribution of size 3 x 4 with lower limit 2 and upper limit 6
  4. res_arr = random.uniform(low = 2, high=6, size=(3,4))
  5. print('2D Rectangular Distribution as output from uniform() function:\n')
  6. #printing the result
  7. print(res_arr)

Lets take another example, in this example we will generate a 3D array of rectangular distribution of the size(3,2,5) with default upper and lower limit.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using uniform function to generate uniform distribution of size 3 x 2 x 5
  4. res = random.uniform(size=(3,2,5))
  5. print('3D Rectangular Distribution as output from uniform() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Rectangular Distribution

In this example, for our better understanding of rectangular distribution itself and the syntax, we will plot rectangular distribution on a graph. Here we will again use distplot() function for graph plot. The upper and lower limit will be 4 and 8 respectively, and the total of 50 values will be used for this plot. Here you can observe that the plot of graph is rectangular shaped, this is the reason behind the uniform distribution being called as rectangular distribution.

  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 Uniform function to generate Rectangular Distribution of size 100 with lower limit 4 and upper limit 8
  6. sb.distplot(random.uniform(size=100, low=4, high=8),hist=False)
  7. #plotting the graph
  8. mpl.show()

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.