Python Program for Quartile Deviation

Quartile Deviation

The quartile deviation is the absolute measure of dispersion. It is computed by dividing the difference between the top and bottom quartiles in half.

The quartile deviation is the absolute measure of dispersion, where dispersion is the amount by which the values in the distribution differ from the mean value.

Even if there is only one exceptionally high or low value in the data, the range’s utility as a measure of dispersion is reduced or diminished.

To calculate the quartile deviation, we must divide the data into four portions, each of which has 25% of the values.

The quartile deviation of the data is computed by dividing the difference between the top (75%) and bottom (25%) quartiles by half.

Implementation:

Let’s look at how to use Python to calculate a dataset’s quartile deviation.

To compute it in Python, first, create a dataset, then find the quartile1, quartile2, and quartile3 from the data, and last create a function that returns the product of half the difference between quartile3 and quartile1.

Program for Quartile Deviation in Python

Method #1: Using numpy Library (Static Input)

Approach:

  • Import the numpy library as np using the import function.
  • Pass the lower limit, upper limit, and step size with some random numbers to the range() function and convert it into a list using the list() function. It is considered as the dataset.
  • Store it in a variable.
  • Pass the above dataset,  0.25  deviation as the arguments to the quantile() function and store it in a variable.
  • Pass the above dataset,  0.50  deviation as the arguments to the quantile() function and store it in another variable.
  • Pass the above dataset,  0.75 deviation as the arguments to the quantile() function and store it in another variable.
  • Print the above three variables i.e, quartile1, quartile1, and quartile1.
  • Create a function say quartile_deviatn() which accepts two numbers as the arguments and returns half of the difference of two numbers.
  • Inside the function, return the value half of the difference of two numbers ((x – y)/2).
  • Pass the quartile3, quartile1 as arguments to the quartile_deviatn() function and store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import the numpy library as np using the import function.
import numpy as np
# Pass the lower limit, upper limit, and step size with some random numbers to the
# range() function and convert it into a list using the list() function.
# It is considered as the dataset.
# Store it in a variable.
gvn_dataset = list(range(70, 200, 10))
# Pass the above dataset,  0.25  deviation as the arguments to the quantile()
# function and store it in a variable.
quartle_1 = np.quantile(gvn_dataset, 0.25)
# Pass the above dataset,  0.50  deviation as the arguments to the quantile()
# function and store it in another variable.
quartle_2 = np.quantile(gvn_dataset, 0.50)
# Pass the above dataset,  0.75 deviation as the arguments to the quantile()
# function and store it in another variable.
quartle_3 = np.quantile(gvn_dataset, 0.75)
# Print the above three variables i.e, quartile1, quartile1, and quartile1.
print("The 1st Quartile =  ", quartle_1)
print("The 2nd Quartile =  ", quartle_2)
print("The 3rd Quartile =  ", quartle_3)
# Create a function say quartile_deviatn() which accepts two numbers as the
# arguments and returns half of the difference of two numbers.


def quartile_deviatn(x, y):
    # Inside the function, return the value half of the difference of two numbers
    # ((x - y)/2).
    return (x - y)/2


# Pass the quartile3, quartile1 as arguments to the quartile_deviatn() function
# and store it in a variable.
rslt = quartile_deviatn(quartle_3, quartle_1)
# Print the above result.
print("The result is = ", rslt)

Output:

The 1st Quartile = 100.0
The 2nd Quartile = 130.0
The 3rd Quartile = 160.0
The result is = 30.0

Method #2: Using numpy Library (User Input)

Approach:

  • Import the numpy library as np using the import function.
  • Give the lower limit as user input using the int(input()) function and store it in a variable.
  • Give the upper limit as user input using the int(input()) function and store it in another variable.
  • Give the step size as user input using the int(input()) function and store it in another variable.
  • Pass the lower limit, upper limit, and step size with some random numbers to the range() function and convert it into a list using the list() function. It is considered as the dataset.
  • Store it in a variable.
  • Pass the above dataset,  0.25  deviation as the arguments to the quantile() function and store it in a variable.
  • Pass the above dataset,  0.50  deviation as the arguments to the quantile() function and store it in another variable.
  • Pass the above dataset,  0.75 deviation as the arguments to the quantile() function and store it in another variable.
  • Print the above three variables i.e, quartile1, quartile1, and quartile1.
  • Create a function say quartile_deviatn() which accepts two numbers as the arguments and returns half of the difference of two numbers.
  • Inside the function, return the value half of the difference of two numbers ((x – y)/2).
  • Pass the quartile3, quartile1 as arguments to the quartile_deviatn() function and store it in a variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import the numpy library as np using the import function.
import numpy as np
# Give the lower limit as user input using the int(input()) function and
# store it in a variable.
gvnlwr_lmt = int(input("Enter some random number = "))
# Give the upper limit as user input using the int(input()) function and
# store it in another variable.
gvnupr_lmt = int(input("Enter some random number = "))
# Give the step size as user input using the int(input()) function and
# store it in another variable.
gvn_step = int(input("Enter some random number = "))
# Pass the lower limit, upper limit, and step size with some random numbers to the
# range() function and convert it into a list using the list() function.
# It is considered as the dataset.
# Store it in a variable.
gvn_dataset = list(range(gvnlwr_lmt, gvnupr_lmt, gvn_step))
# Pass the above dataset,  0.25  deviation as the arguments to the quantile()
# function and store it in a variable.
quartle_1 = np.quantile(gvn_dataset, 0.25)
# Pass the above dataset,  0.50  deviation as the arguments to the quantile()
# function and store it in another variable.
quartle_2 = np.quantile(gvn_dataset, 0.50)
# Pass the above dataset,  0.75 deviation as the arguments to the quantile()
# function and store it in another variable.
quartle_3 = np.quantile(gvn_dataset, 0.75)
# Print the above three variables i.e, quartile1, quartile1, and quartile1.
print("The 1st Quartile =  ", quartle_1)
print("The 2nd Quartile =  ", quartle_2)
print("The 3rd Quartile =  ", quartle_3)
# Create a function say quartile_deviatn() which accepts two numbers as the
# arguments and returns half of the difference of two numbers.


def quartile_deviatn(x, y):
    # Inside the function, return the value half of the difference of two numbers
    # ((x - y)/2).
    return (x - y)/2


# Pass the quartile3, quartile1 as arguments to the quartile_deviatn() function
# and store it in a variable.
rslt = quartile_deviatn(quartle_3, quartle_1)
# Print the above result.
print("The result is = ", rslt)

Output:

Enter some random number = 45 
Enter some random number = 90 
Enter some random number = 5 
The 1st Quartile = 55.0 
The 2nd Quartile = 65.0 
The 3rd Quartile = 75.0 
The result is = 10.0