Reference · Statistics

stats.variance

signature
stats.variance(list, num | null=) -> float

What it does

The sample variance of a list of numbers: the sum of the squared distances from the mean, divided by one less than the count (Bessel's correction). It is Python's statistics.variance, for data that is a sample of a larger population; for the whole population use stats.pvariance. The answer is a float, computed with compensated sums so it is Python's exact result.

Examples

print(stats.variance([1, 2, 3, 4]))print(stats.variance([2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]))
output
1.66666666666666671.3720238095238095

Edge cases

print(stats.variance([7, 7, 7]), stats.variance([1.0, 2.0]))print(stats.variance([1, 2, 3, 4], 0), stats.variance([1, 2, 3, 4], 2.5), stats.variance([1, 2, 3, 4], null))print(stats.variance([1, 2, 3]) == stats.covariance([1, 2, 3], [1, 2, 3]))
output
0.0 0.51.6666666666666667 1.6666666666666667 1.6666666666666667true

Errors

Fewer than two numbers is a runtime error, not an error value: an empty list, null or a list of one stops the request. So does an item that is not a number.

this one fails
print(stats.variance([5]))
output
line 1: `stats.variance` requires at least 2 data points
this one fails
print(stats.variance([]))
output
line 1: `stats.variance` requires at least 2 data points

Chaining

let xs = [2, 4, 4, 4, 5, 5, 7, 9]print(xs | stats.variance | round(3))print(len(xs) > 1 && (xs | stats.variance) > 0)
output
4.571true

See also

stats.pvariance, stats.stdev, stats.pstdev, stats.covariance, stats.mean

Thread
9Useful? Like it, or add to its thread below.

Thread 0 posts

On stats.variance(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo