MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / variance

Function variance

tools/python-3.11.9-amd64/Lib/statistics.py:822–863  ·  view source on GitHub ↗

Return the sample variance of data. data should be an iterable of Real-valued numbers, with at least two values. The optional argument xbar, if given, should be the mean of the data. If it is missing or None, the mean is automatically calculated. Use this function when your d

(data, xbar=None)

Source from the content-addressed store, hash-verified

820
821
822def variance(data, xbar=None):
823 """Return the sample variance of data.
824
825 data should be an iterable of Real-valued numbers, with at least two
826 values. The optional argument xbar, if given, should be the mean of
827 the data. If it is missing or None, the mean is automatically calculated.
828
829 Use this function when your data is a sample from a population. To
830 calculate the variance from the entire population, see ``pvariance``.
831
832 Examples:
833
834 >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
835 >>> variance(data)
836 1.3720238095238095
837
838 If you have already calculated the mean of your data, you can pass it as
839 the optional second argument ``xbar`` to avoid recalculating it:
840
841 >>> m = mean(data)
842 >>> variance(data, m)
843 1.3720238095238095
844
845 This function does not check that ``xbar`` is actually the mean of
846 ``data``. Giving arbitrary values for ``xbar`` may lead to invalid or
847 impossible results.
848
849 Decimals and Fractions are supported:
850
851 >>> from decimal import Decimal as D
852 >>> variance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")])
853 Decimal('31.01875')
854
855 >>> from fractions import Fraction as F
856 >>> variance([F(1, 6), F(1, 2), F(5, 3)])
857 Fraction(67, 108)
858
859 """
860 T, ss, c, n = _ss(data, xbar)
861 if n < 2:
862 raise StatisticsError('variance requires at least two data points')
863 return _convert(ss / (n - 1), T)
864
865
866def pvariance(data, mu=None):

Callers

nothing calls this directly

Calls 3

_ssFunction · 0.85
StatisticsErrorClass · 0.85
_convertFunction · 0.70

Tested by

no test coverage detected