What is the statistics.pvariance() method in Python?

The statistics.pvariance() method in Python is used to return the population variance of a given data.

This method is unlike the statistics.variance() method, which calculates the variance of a sample of data. Instead, the statistics.pvariance() method calculates the variance of an entire population.

Variance is the sum of the squared distances of each term distributed from the mean of the data divided by the number of observations. It is given by:

S2=(xixˉ)2n1\Large S^2=\frac{\sum(x_i-\bar{x})^2}{n-1}

  • S2S^{2} = Variance
  • xix_i = The value of one observation
  • xˉ\bar{x} = The mean value of the data
  • nn = The number of observations

Syntax

statistics.pvariance(data, xbar)

Parameters

Parameter

Value

data

This is required. It is the data to be used. It can be a list, a sequence, or iterator

xbar

This is optional. It is the mean of the data provided. If the value is not provided Python calculates the mean of the data by default.

Return value

statistics.pvariance() method returns a float value that represents the population variance of a given data.

Example

Let’s use the statistics.pvariance() method to calculate the population variance for a given data.

import statistics
# creating a data set
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# to obtain the variance
the_pvariance = statistics.pvariance(data)
print('The population variance is', the_pvariance)

Explanation

  • Line 1: We import the statistics module.
  • Line 4: We create a data set called data.
  • Line 6: We use the statistics.pvariance() method to obtain the population variance of the data and assign the value to another variable the_pvariance.
  • Line 7: We print the_pvariance, which contains the population variance of the given data.

Free Resources