Reference · Statistics
stats.variance
stats.variance(list, num | null=) -> floatWhat 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]))1.66666666666666671.3720238095238095Edge cases
- At least two numbers are needed: one number is an error, not
0.0. - A constant list gives
0.0. - A second argument, a mean already known, is accepted, but the answer is the spread
about the data's own mean whatever is given, as in Python before 3.11: a correction
in the sum cancels the difference. Leave it out, or pass
null. - The sample variance of two lists that are the same is their covariance.
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]))0.0 0.51.6666666666666667 1.6666666666666667 1.6666666666666667trueErrors
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.
print(stats.variance([5]))line 1: `stats.variance` requires at least 2 data pointsprint(stats.variance([]))line 1: `stats.variance` requires at least 2 data pointsChaining
let xs = [2, 4, 4, 4, 5, 5, 7, 9]print(xs | stats.variance | round(3))print(len(xs) > 1 && (xs | stats.variance) > 0)4.571trueSee also
stats.pvariance, stats.stdev, stats.pstdev, stats.covariance, stats.mean
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