Reference · Statistics
stats.pvariance
stats.pvariance(list, num | null=) -> floatWhat it does
The population variance of a list of numbers: the mean of the squared distances
from the mean, dividing by the count. It is Python's statistics.pvariance, for
data that is the whole population; for a sample use
stats.variance, which divides by one less. The
answer is a float, computed with the compensated sums Python's fractions give.
Examples
print(stats.pvariance([1, 2, 3, 4]))print(stats.pvariance([0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]))1.251.25Edge cases
- One number is enough: its variance is
0.0. A constant list gives0.0too. - 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. - Ints and floats can mix; the answer is always a float.
print(stats.pvariance([5]), stats.pvariance([7, 7, 7]))print(stats.pvariance([1, 2, 3, 4], 0), stats.pvariance([1, 2, 3, 4], 2.5), stats.pvariance([1, 2, 3, 4], null))0.0 0.01.25 1.25 1.25Errors
No data is a runtime error, not an error value: an empty list or null stops the
request. So does an item that is not a number, or a second argument that is not a
number.
print(stats.pvariance([]))line 1: `stats.pvariance` requires at least one data pointprint(stats.pvariance([1, 2], "x"))line 1: `stats.pvariance` expects a number as argument 2, got strChaining
print([2, 4, 4, 4, 5, 5, 7, 9] | stats.pvariance)print([2, 4, 4, 4, 5, 5, 7, 9] | stats.pvariance | math.sqrt)4.02.0See also
stats.variance, stats.pstdev, stats.stdev, stats.mean
Thread 0 posts
On stats.pvariance(): 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