How to calculate variance of a list in Python without NumPy

In this article, we will learn how to calculate the variance of a list in Python using different methods.

Variance

  • Variance is an important mathematical tool used in statistics. It is used to handle large amounts of data.
  • It is the square of the standard deviation for a given data set.
  • Variance is also known as the second central moment of a distribution.
  • It is calculated by the mean of the square minus square of the mean of a given data set.
  • Formula:
    Var(X)= E[(X- μ )^2]

You can check this out: Find variance of a list in Python using NumPy

Syntax: Python variance()

Python provides an inbuilt function to calculate the variance of a list. The syntax is given below along with the explanation of its parameters.

variance( [data], mean )

  1. [data]: It contains the list of data whose variance is to be calculated.
  2. mean: It is an optional parameter. It takes the value of the actual mean.

Rudimentary method to calculate variance of a list in Python

This is the simplest method used to calculate the variance of a list. In the example given below, we calculate the mean and then using the formula given above, we calculate the variance.
No inbuilt function is used in this.

list1 = [10, 20, 30, 40, 50, 60] 
 
print("The original list is : " + str(list1)) 
length= int(len(list1))
mean = sum(list1) / length 
ans = sum((i - mean) ** 2 for i in list1) / length
  
print("The variance of list is : " + str(ans)) 

Output:

The original list is : [10, 20, 30, 40, 50, 60]
The variance of list is : 291.6666666666667

 

Variance of a list using inbuilt function

Example 1: In this example, we use the inbuilt function but the optional parameter i.e. mean is not mentioned. This function makes it very easy to calculate the variance of a given data set.

import statistics  
  
list1 = [10, 70, 30, 90, 20, 30] 
  
print("The original list is : " + str(list1)) 
  
ans = statistics.variance(list1) 
  
print("The variance of list is : " + str(ans)) 

Output:

The original list is : [10, 70, 30, 90, 20, 30]
The variance of list is : 976.6666666666666

Example 2: In this, we use the inbuilt function. We mention both the parameters in it.

import statistics 
  
list1 = (1, 1.2, 1.3, 1.4, 1.5, 1.6)  
mean = statistics.mean(list1) 
print("Variance of Sample set is % s" 
    %(statistics.variance(list1, xbar = mean))) 

Output:

Variance of Sample set is 0.046666666666666676

These are the ways to easily calculate the variance of a given list.

2 responses to “How to calculate variance of a list in Python without NumPy”

  1. Anbarasan C says:

    While calculating the variance using the same values for both inbuilt function and rudimentary method, I’m getting irrelevant answers… can I able to know the reason?

  2. Shalini Gupta says:

    While calculating the variance using the inbuilt function it will calculate the population variance instead of the sample variance. To get the correct answer you must provide the sample mean and sample element values to get the correct answer. 

Leave a Reply

Your email address will not be published. Required fields are marked *