variance() is a function from Python’s built-in statistics module used to calculate the sample variance of a dataset. Sample variance measures how far the data values are spread from their mean. Mathematically, sample variance is calculated as:
s^2 = \frac{\sum_{i=1}^{n} (x_i - \bar{x})^2}{n - 1}
Where:
x_i = each data value\bar{x} = sample mean- n = number of data points
This function should be used when the data represents a sample, not the entire population (for population variance, use pvariance()). variance() requires at least two data points; otherwise, it raises a StatisticsError.
Example: This example calculates the variance of a small sample dataset.
import statistics
a = [4, 6, 8, 10]
print(statistics.variance(a))
Output
6.666666666666667
Syntax
statistics.variance(data, xbar=None)
Parameters:
- data: Iterable containing numeric values.
- xbar (optional): Precomputed mean of the dataset.
Return: Returns the sample variance of the given data.
Examples
Example 1: This example calculates the variance of a list of floating-point numbers. The function automatically computes the mean.
import statistics
d = [2.5, 3.0, 3.5, 4.0, 4.5]
print(statistics.variance(d))
Output
0.625
Example 2: This example uses integer values including negative numbers to show that variance() works with different numeric types.
from statistics import variance
d = (-5, -1, 0, 3, 7)
print(variance(d))
Output
20.2
Example 3: This example demonstrates the use of the optional xbar parameter. We first compute the mean separately and then pass it to variance().
import statistics
d = (1, 2, 3, 4, 5)
m = statistics.mean(d)
print(statistics.variance(d, xbar=m))
Output
2.5
Explanation: statistics.variance(d, xbar=m) uses the provided mean m instead of recalculating it internally.