Return the population variance of ``data``. data should be a sequence or iterable of Real-valued numbers, with at least one value. The optional argument mu, if given, should be the mean of the data. If it is missing or None, the mean is automatically calculated. Use this function t
(data, mu=None)
| 564 | |
| 565 | |
| 566 | def pvariance(data, mu=None): |
| 567 | """Return the population variance of ``data``. |
| 568 | |
| 569 | data should be a sequence or iterable of Real-valued numbers, with at least one |
| 570 | value. The optional argument mu, if given, should be the mean of |
| 571 | the data. If it is missing or None, the mean is automatically calculated. |
| 572 | |
| 573 | Use this function to calculate the variance from the entire population. |
| 574 | To estimate the variance from a sample, the ``variance`` function is |
| 575 | usually a better choice. |
| 576 | |
| 577 | Examples: |
| 578 | |
| 579 | >>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25] |
| 580 | >>> pvariance(data) |
| 581 | 1.25 |
| 582 | |
| 583 | If you have already calculated the mean of the data, you can pass it as |
| 584 | the optional second argument to avoid recalculating it: |
| 585 | |
| 586 | >>> mu = mean(data) |
| 587 | >>> pvariance(data, mu) |
| 588 | 1.25 |
| 589 | |
| 590 | Decimals and Fractions are supported: |
| 591 | |
| 592 | >>> from decimal import Decimal as D |
| 593 | >>> pvariance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")]) |
| 594 | Decimal('24.815') |
| 595 | |
| 596 | >>> from fractions import Fraction as F |
| 597 | >>> pvariance([F(1, 4), F(5, 4), F(1, 2)]) |
| 598 | Fraction(13, 72) |
| 599 | |
| 600 | """ |
| 601 | # http://mathworld.wolfram.com/Variance.html |
| 602 | |
| 603 | T, ss, c, n = _ss(data, mu) |
| 604 | if n < 1: |
| 605 | raise StatisticsError('pvariance requires at least one data point') |
| 606 | return _convert(ss / n, T) |
| 607 | |
| 608 | |
| 609 | def stdev(data, xbar=None): |
nothing calls this directly
no test coverage detected
searching dependent graphs…