statistics.variance

Full name
statistics.variance
Library
statistics
Syntax

statistics.variance(data, xbar=None)

Description

The statistics.variance function returns the variance of the sample made up of the elements in data.

Parameters
  • data: Iterable for whose elements you want to obtain the variance.
  • xbar: Optional argument. Value from which deviations are calculated. If omitted, they are obtained with respect to the mean value of the data.
Result

The statistics.variance function returns a value of type float.

Examples

We can obtain the variance of the values of a list with the following code:

statistics.variance([1, 3, 3, 6])
4.25

In this second example we are going to generate a list made up of a thousand random values extracted from a Gaussian distribution with mean 5 and standard deviation 1:

import random
y = [random.gauss(5, 1) for _ in range(1000)]

Let's show the histogram:

import matplotlib.pyplot as plt
plt.figure(figsize = (8, 4))
plt.hist(y, bins = 10)
plt.grid()
plt.show()
statistics.variance

Let's obtain its variance below:

statistics.variance(y)
0.9670098132686832

If we specify as argument xbar the value 10, the deviations will be calculated with respect to this value:

statistics.variance(y, xbar = 10)
25.81368026377278
Submitted by admin on Mon, 04/05/2021 - 08:56