2013-10-19 15:50:09 -03:00
|
|
|
:mod:`statistics` --- Mathematical statistics functions
|
|
|
|
=======================================================
|
|
|
|
|
|
|
|
.. module:: statistics
|
|
|
|
:synopsis: mathematical statistics functions
|
2016-06-11 16:02:54 -03:00
|
|
|
|
2013-10-19 15:50:09 -03:00
|
|
|
.. moduleauthor:: Steven D'Aprano <steve+python@pearwood.info>
|
|
|
|
.. sectionauthor:: Steven D'Aprano <steve+python@pearwood.info>
|
|
|
|
|
|
|
|
.. versionadded:: 3.4
|
|
|
|
|
2016-06-11 16:02:54 -03:00
|
|
|
**Source code:** :source:`Lib/statistics.py`
|
|
|
|
|
2013-10-19 15:50:09 -03:00
|
|
|
.. testsetup:: *
|
|
|
|
|
|
|
|
from statistics import *
|
|
|
|
__name__ = '<doctest>'
|
|
|
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
This module provides functions for calculating mathematical statistics of
|
|
|
|
numeric (:class:`Real`-valued) data.
|
|
|
|
|
2014-02-08 05:58:04 -04:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Unless explicitly noted otherwise, these functions support :class:`int`,
|
|
|
|
:class:`float`, :class:`decimal.Decimal` and :class:`fractions.Fraction`.
|
|
|
|
Behaviour with other types (whether in the numeric tower or not) is
|
|
|
|
currently unsupported. Mixed types are also undefined and
|
|
|
|
implementation-dependent. If your input data consists of mixed types,
|
|
|
|
you may be able to use :func:`map` to ensure a consistent result, e.g.
|
|
|
|
``map(float, input_data)``.
|
|
|
|
|
2013-10-19 15:50:09 -03:00
|
|
|
Averages and measures of central location
|
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
These functions calculate an average or typical value from a population
|
|
|
|
or sample.
|
|
|
|
|
|
|
|
======================= =============================================
|
|
|
|
:func:`mean` Arithmetic mean ("average") of data.
|
2016-08-23 13:34:25 -03:00
|
|
|
:func:`harmonic_mean` Harmonic mean of data.
|
2013-10-19 15:50:09 -03:00
|
|
|
:func:`median` Median (middle value) of data.
|
|
|
|
:func:`median_low` Low median of data.
|
|
|
|
:func:`median_high` High median of data.
|
|
|
|
:func:`median_grouped` Median, or 50th percentile, of grouped data.
|
|
|
|
:func:`mode` Mode (most common value) of discrete data.
|
|
|
|
======================= =============================================
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Measures of spread
|
|
|
|
------------------
|
|
|
|
|
|
|
|
These functions calculate a measure of how much the population or sample
|
|
|
|
tends to deviate from the typical or average values.
|
|
|
|
|
|
|
|
======================= =============================================
|
|
|
|
:func:`pstdev` Population standard deviation of data.
|
|
|
|
:func:`pvariance` Population variance of data.
|
|
|
|
:func:`stdev` Sample standard deviation of data.
|
|
|
|
:func:`variance` Sample variance of data.
|
|
|
|
======================= =============================================
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
|
|
|
|
Function details
|
|
|
|
----------------
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-11-04 02:30:50 -04:00
|
|
|
Note: The functions do not require the data given to them to be sorted.
|
|
|
|
However, for reading convenience, most of the examples show sorted sequences.
|
|
|
|
|
2013-10-19 15:50:09 -03:00
|
|
|
.. function:: mean(data)
|
|
|
|
|
2016-11-21 20:31:02 -04:00
|
|
|
Return the sample arithmetic mean of *data* which can be a sequence or iterator.
|
2013-10-21 03:57:26 -03:00
|
|
|
|
|
|
|
The arithmetic mean is the sum of the data divided by the number of data
|
|
|
|
points. It is commonly called "the average", although it is only one of many
|
|
|
|
different mathematical averages. It is a measure of the central location of
|
|
|
|
the data.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If *data* is empty, :exc:`StatisticsError` will be raised.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
Some examples of use:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> mean([1, 2, 3, 4, 4])
|
|
|
|
2.8
|
|
|
|
>>> mean([-1.0, 2.5, 3.25, 5.75])
|
|
|
|
2.625
|
|
|
|
|
|
|
|
>>> from fractions import Fraction as F
|
|
|
|
>>> mean([F(3, 7), F(1, 21), F(5, 3), F(1, 3)])
|
|
|
|
Fraction(13, 21)
|
|
|
|
|
|
|
|
>>> from decimal import Decimal as D
|
|
|
|
>>> mean([D("0.5"), D("0.75"), D("0.625"), D("0.375")])
|
|
|
|
Decimal('0.5625')
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2013-10-21 04:08:39 -03:00
|
|
|
The mean is strongly affected by outliers and is not a robust estimator
|
2013-10-21 03:57:26 -03:00
|
|
|
for central location: the mean is not necessarily a typical example of the
|
|
|
|
data points. For more robust, although less efficient, measures of
|
|
|
|
central location, see :func:`median` and :func:`mode`. (In this case,
|
|
|
|
"efficient" refers to statistical efficiency rather than computational
|
|
|
|
efficiency.)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
The sample mean gives an unbiased estimate of the true population mean,
|
|
|
|
which means that, taken on average over all the possible samples,
|
|
|
|
``mean(sample)`` converges on the true mean of the entire population. If
|
|
|
|
*data* represents the entire population rather than a sample, then
|
|
|
|
``mean(data)`` is equivalent to calculating the true population mean μ.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
2016-08-23 13:34:25 -03:00
|
|
|
.. function:: harmonic_mean(data)
|
|
|
|
|
|
|
|
Return the harmonic mean of *data*, a sequence or iterator of
|
|
|
|
real-valued numbers.
|
|
|
|
|
|
|
|
The harmonic mean, sometimes called the subcontrary mean, is the
|
2016-08-23 15:23:31 -03:00
|
|
|
reciprocal of the arithmetic :func:`mean` of the reciprocals of the
|
2016-08-23 13:34:25 -03:00
|
|
|
data. For example, the harmonic mean of three values *a*, *b* and *c*
|
|
|
|
will be equivalent to ``3/(1/a + 1/b + 1/c)``.
|
|
|
|
|
|
|
|
The harmonic mean is a type of average, a measure of the central
|
|
|
|
location of the data. It is often appropriate when averaging quantities
|
|
|
|
which are rates or ratios, for example speeds. For example:
|
|
|
|
|
|
|
|
Suppose an investor purchases an equal value of shares in each of
|
|
|
|
three companies, with P/E (price/earning) ratios of 2.5, 3 and 10.
|
|
|
|
What is the average P/E ratio for the investor's portfolio?
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> harmonic_mean([2.5, 3, 10]) # For an equal investment portfolio.
|
|
|
|
3.6
|
|
|
|
|
|
|
|
Using the arithmetic mean would give an average of about 5.167, which
|
|
|
|
is too high.
|
|
|
|
|
2016-08-23 15:23:31 -03:00
|
|
|
:exc:`StatisticsError` is raised if *data* is empty, or any element
|
2016-08-23 13:34:25 -03:00
|
|
|
is less than zero.
|
|
|
|
|
2016-08-23 15:23:31 -03:00
|
|
|
.. versionadded:: 3.6
|
|
|
|
|
2016-08-23 13:34:25 -03:00
|
|
|
|
2013-10-19 15:50:09 -03:00
|
|
|
.. function:: median(data)
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the median (middle value) of numeric data, using the common "mean of
|
|
|
|
middle two" method. If *data* is empty, :exc:`StatisticsError` is raised.
|
2016-11-21 20:31:02 -04:00
|
|
|
*data* can be a sequence or iterator.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
The median is a robust measure of central location, and is less affected by
|
|
|
|
the presence of outliers in your data. When the number of data points is
|
|
|
|
odd, the middle data point is returned:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median([1, 3, 5])
|
|
|
|
3
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
When the number of data points is even, the median is interpolated by taking
|
|
|
|
the average of the two middle values:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median([1, 3, 5, 7])
|
|
|
|
4.0
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
This is suited for when your data is discrete, and you don't mind that the
|
|
|
|
median may not be an actual data point.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2018-06-25 08:18:54 -03:00
|
|
|
If your data is ordinal (supports order operations) but not numeric (doesn't
|
|
|
|
support addition), you should use :func:`median_low` or :func:`median_high`
|
|
|
|
instead.
|
|
|
|
|
2014-09-27 18:00:58 -03:00
|
|
|
.. seealso:: :func:`median_low`, :func:`median_high`, :func:`median_grouped`
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: median_low(data)
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the low median of numeric data. If *data* is empty,
|
2016-11-21 20:31:02 -04:00
|
|
|
:exc:`StatisticsError` is raised. *data* can be a sequence or iterator.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
The low median is always a member of the data set. When the number of data
|
|
|
|
points is odd, the middle value is returned. When it is even, the smaller of
|
|
|
|
the two middle values is returned.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median_low([1, 3, 5])
|
|
|
|
3
|
|
|
|
>>> median_low([1, 3, 5, 7])
|
|
|
|
3
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Use the low median when your data are discrete and you prefer the median to
|
|
|
|
be an actual data point rather than interpolated.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: median_high(data)
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the high median of data. If *data* is empty, :exc:`StatisticsError`
|
2016-11-21 20:31:02 -04:00
|
|
|
is raised. *data* can be a sequence or iterator.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
The high median is always a member of the data set. When the number of data
|
|
|
|
points is odd, the middle value is returned. When it is even, the larger of
|
|
|
|
the two middle values is returned.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median_high([1, 3, 5])
|
|
|
|
3
|
|
|
|
>>> median_high([1, 3, 5, 7])
|
|
|
|
5
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Use the high median when your data are discrete and you prefer the median to
|
|
|
|
be an actual data point rather than interpolated.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
.. function:: median_grouped(data, interval=1)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the median of grouped continuous data, calculated as the 50th
|
|
|
|
percentile, using interpolation. If *data* is empty, :exc:`StatisticsError`
|
2016-11-21 20:31:02 -04:00
|
|
|
is raised. *data* can be a sequence or iterator.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median_grouped([52, 52, 53, 54])
|
|
|
|
52.5
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
In the following example, the data are rounded, so that each value represents
|
2016-11-26 07:43:28 -04:00
|
|
|
the midpoint of data classes, e.g. 1 is the midpoint of the class 0.5--1.5, 2
|
|
|
|
is the midpoint of 1.5--2.5, 3 is the midpoint of 2.5--3.5, etc. With the data
|
|
|
|
given, the middle value falls somewhere in the class 3.5--4.5, and
|
2013-10-21 03:57:26 -03:00
|
|
|
interpolation is used to estimate it:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])
|
|
|
|
3.7
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Optional argument *interval* represents the class interval, and defaults
|
|
|
|
to 1. Changing the class interval naturally will change the interpolation:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> median_grouped([1, 3, 3, 5, 7], interval=1)
|
|
|
|
3.25
|
|
|
|
>>> median_grouped([1, 3, 3, 5, 7], interval=2)
|
|
|
|
3.5
|
|
|
|
|
|
|
|
This function does not check whether the data points are at least
|
2013-10-21 03:57:26 -03:00
|
|
|
*interval* apart.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. impl-detail::
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Under some circumstances, :func:`median_grouped` may coerce data points to
|
|
|
|
floats. This behaviour is likely to change in the future.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
* "Statistics for the Behavioral Sciences", Frederick J Gravetter and
|
|
|
|
Larry B Wallnau (8th Edition).
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
* The `SSMEDIAN
|
2014-10-29 06:26:56 -03:00
|
|
|
<https://help.gnome.org/users/gnumeric/stable/gnumeric.html#gnumeric-function-SSMEDIAN>`_
|
2013-10-21 03:57:26 -03:00
|
|
|
function in the Gnome Gnumeric spreadsheet, including `this discussion
|
|
|
|
<https://mail.gnome.org/archives/gnumeric-list/2011-April/msg00018.html>`_.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
|
|
|
.. function:: mode(data)
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the most common data point from discrete or nominal *data*. The mode
|
|
|
|
(when it exists) is the most typical value, and is a robust measure of
|
|
|
|
central location.
|
|
|
|
|
|
|
|
If *data* is empty, or if there is not exactly one most common value,
|
|
|
|
:exc:`StatisticsError` is raised.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
``mode`` assumes discrete data, and returns a single value. This is the
|
|
|
|
standard treatment of the mode as commonly taught in schools:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> mode([1, 1, 2, 3, 3, 3, 3, 4])
|
|
|
|
3
|
|
|
|
|
|
|
|
The mode is unique in that it is the only statistic which also applies
|
|
|
|
to nominal (non-numeric) data:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> mode(["red", "blue", "blue", "red", "green", "red", "red"])
|
|
|
|
'red'
|
|
|
|
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
.. function:: pstdev(data, mu=None)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the population standard deviation (the square root of the population
|
|
|
|
variance). See :func:`pvariance` for arguments and other details.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
|
|
|
|
0.986893273527251
|
|
|
|
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
.. function:: pvariance(data, mu=None)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the population variance of *data*, a non-empty iterable of real-valued
|
|
|
|
numbers. Variance, or second moment about the mean, is a measure of the
|
|
|
|
variability (spread or dispersion) of data. A large variance indicates that
|
|
|
|
the data is spread out; a small variance indicates it is clustered closely
|
|
|
|
around the mean.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If the optional second argument *mu* is given, it should be the mean of
|
|
|
|
*data*. If it is missing or ``None`` (the default), the mean is
|
2013-10-19 16:10:01 -03:00
|
|
|
automatically calculated.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Use this function to calculate the variance from the entire population. To
|
|
|
|
estimate the variance from a sample, the :func:`variance` function is usually
|
|
|
|
a better choice.
|
|
|
|
|
|
|
|
Raises :exc:`StatisticsError` if *data* is empty.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]
|
|
|
|
>>> pvariance(data)
|
|
|
|
1.25
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If you have already calculated the mean of your data, you can pass it as the
|
|
|
|
optional second argument *mu* to avoid recalculation:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> mu = mean(data)
|
|
|
|
>>> pvariance(data, mu)
|
|
|
|
1.25
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
This function does not attempt to verify that you have passed the actual mean
|
|
|
|
as *mu*. Using arbitrary values for *mu* may lead to invalid or impossible
|
|
|
|
results.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
Decimals and Fractions are supported:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> from decimal import Decimal as D
|
|
|
|
>>> pvariance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")])
|
|
|
|
Decimal('24.815')
|
|
|
|
|
|
|
|
>>> from fractions import Fraction as F
|
|
|
|
>>> pvariance([F(1, 4), F(5, 4), F(1, 2)])
|
|
|
|
Fraction(13, 72)
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
When called with the entire population, this gives the population variance
|
|
|
|
σ². When called on a sample instead, this is the biased sample variance
|
|
|
|
s², also known as variance with N degrees of freedom.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If you somehow know the true population mean μ, you may use this function
|
|
|
|
to calculate the variance of a sample, giving the known population mean as
|
|
|
|
the second argument. Provided the data points are representative
|
|
|
|
(e.g. independent and identically distributed), the result will be an
|
|
|
|
unbiased estimate of the population variance.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
.. function:: stdev(data, xbar=None)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the sample standard deviation (the square root of the sample
|
|
|
|
variance). See :func:`variance` for arguments and other details.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
|
|
|
|
1.0810874155219827
|
|
|
|
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
.. function:: variance(data, xbar=None)
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Return the sample variance of *data*, an iterable of at least two real-valued
|
|
|
|
numbers. Variance, or second moment about the mean, is a measure of the
|
|
|
|
variability (spread or dispersion) of data. A large variance indicates that
|
|
|
|
the data is spread out; a small variance indicates it is clustered closely
|
|
|
|
around the mean.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If the optional second argument *xbar* is given, it should be the mean of
|
|
|
|
*data*. If it is missing or ``None`` (the default), the mean is
|
2013-10-19 16:10:01 -03:00
|
|
|
automatically calculated.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
Use this function when your data is a sample from a population. To calculate
|
|
|
|
the variance from the entire population, see :func:`pvariance`.
|
|
|
|
|
|
|
|
Raises :exc:`StatisticsError` if *data* has fewer than two values.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
|
|
|
|
>>> variance(data)
|
|
|
|
1.3720238095238095
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If you have already calculated the mean of your data, you can pass it as the
|
|
|
|
optional second argument *xbar* to avoid recalculation:
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> m = mean(data)
|
|
|
|
>>> variance(data, m)
|
|
|
|
1.3720238095238095
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
This function does not attempt to verify that you have passed the actual mean
|
|
|
|
as *xbar*. Using arbitrary values for *xbar* can lead to invalid or
|
2013-10-19 15:50:09 -03:00
|
|
|
impossible results.
|
|
|
|
|
|
|
|
Decimal and Fraction values are supported:
|
|
|
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
>>> from decimal import Decimal as D
|
|
|
|
>>> variance([D("27.5"), D("30.25"), D("30.25"), D("34.5"), D("41.75")])
|
|
|
|
Decimal('31.01875')
|
|
|
|
|
|
|
|
>>> from fractions import Fraction as F
|
|
|
|
>>> variance([F(1, 6), F(1, 2), F(5, 3)])
|
|
|
|
Fraction(67, 108)
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
This is the sample variance s² with Bessel's correction, also known as
|
|
|
|
variance with N-1 degrees of freedom. Provided that the data points are
|
|
|
|
representative (e.g. independent and identically distributed), the result
|
|
|
|
should be an unbiased estimate of the true population variance.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-21 03:57:26 -03:00
|
|
|
If you somehow know the actual population mean μ you should pass it to the
|
|
|
|
:func:`pvariance` function as the *mu* parameter to get the variance of a
|
|
|
|
sample.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
Exceptions
|
|
|
|
----------
|
|
|
|
|
|
|
|
A single exception is defined:
|
|
|
|
|
2013-10-20 18:52:54 -03:00
|
|
|
.. exception:: StatisticsError
|
2013-10-19 15:50:09 -03:00
|
|
|
|
2013-10-20 18:52:09 -03:00
|
|
|
Subclass of :exc:`ValueError` for statistics-related exceptions.
|
2013-10-19 15:50:09 -03:00
|
|
|
|
|
|
|
..
|
|
|
|
# This modelines must appear within the last ten lines of the file.
|
|
|
|
kate: indent-width 3; remove-trailing-space on; replace-tabs on; encoding utf-8;
|