statistics.rst 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
:mod:`statistics` --- Mathematical statistics functions
=======================================================

.. module:: statistics
   :synopsis: mathematical statistics functions
.. moduleauthor:: Steven D'Aprano <steve+python@pearwood.info>
.. sectionauthor:: Steven D'Aprano <steve+python@pearwood.info>

.. versionadded:: 3.4

.. testsetup:: *

   from statistics import *
   __name__ = '<doctest>'

**Source code:** :source:`Lib/statistics.py`

--------------

This module provides functions for calculating mathematical statistics of
numeric (:class:`Real`-valued) data.

23 24 25 26 27 28 29 30 31 32
.. 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)``.

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
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.
: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.
=======================  =============================================

48 49 50 51 52 53 54 55 56 57 58 59
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.
=======================  =============================================
60

61 62 63

Function details
----------------
64

65 66 67
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.

68 69
.. function:: mean(data)

70 71 72 73 74 75 76
   Return the sample arithmetic mean of *data*, a sequence or iterator of
   real-valued numbers.

   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.
77

78
   If *data* is empty, :exc:`StatisticsError` will be raised.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

   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::

99
      The mean is strongly affected by outliers and is not a robust estimator
100 101 102 103 104
      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.)
105

106 107 108 109 110
      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 μ.
111 112 113 114


.. function:: median(data)

115 116
   Return the median (middle value) of numeric data, using the common "mean of
   middle two" method.  If *data* is empty, :exc:`StatisticsError` is raised.
117

118 119 120
   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:
121 122 123 124 125 126

   .. doctest::

      >>> median([1, 3, 5])
      3

127 128
   When the number of data points is even, the median is interpolated by taking
   the average of the two middle values:
129 130 131 132 133 134

   .. doctest::

      >>> median([1, 3, 5, 7])
      4.0

135 136
   This is suited for when your data is discrete, and you don't mind that the
   median may not be an actual data point.
137

Berker Peksag's avatar
Berker Peksag committed
138
   .. seealso:: :func:`median_low`, :func:`median_high`, :func:`median_grouped`
139 140 141 142


.. function:: median_low(data)

143 144
   Return the low median of numeric data.  If *data* is empty,
   :exc:`StatisticsError` is raised.
145

146 147 148
   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.
149 150 151 152 153 154 155 156

   .. doctest::

      >>> median_low([1, 3, 5])
      3
      >>> median_low([1, 3, 5, 7])
      3

157 158
   Use the low median when your data are discrete and you prefer the median to
   be an actual data point rather than interpolated.
159 160 161 162


.. function:: median_high(data)

163 164
   Return the high median of data.  If *data* is empty, :exc:`StatisticsError`
   is raised.
165

166 167 168
   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.
169 170 171 172 173 174 175 176

   .. doctest::

      >>> median_high([1, 3, 5])
      3
      >>> median_high([1, 3, 5, 7])
      5

177 178
   Use the high median when your data are discrete and you prefer the median to
   be an actual data point rather than interpolated.
179 180


181
.. function:: median_grouped(data, interval=1)
182

183 184 185
   Return the median of grouped continuous data, calculated as the 50th
   percentile, using interpolation.  If *data* is empty, :exc:`StatisticsError`
   is raised.
186 187 188 189 190 191

   .. doctest::

      >>> median_grouped([52, 52, 53, 54])
      52.5

192 193 194 195 196
   In the following example, the data are rounded, so that each value represents
   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
   interpolation is used to estimate it:
197 198 199 200 201 202

   .. doctest::

      >>> median_grouped([1, 2, 2, 3, 4, 4, 4, 4, 4, 5])
      3.7

203 204
   Optional argument *interval* represents the class interval, and defaults
   to 1.  Changing the class interval naturally will change the interpolation:
205 206 207 208 209 210 211 212 213

   .. 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
214
   *interval* apart.
215 216 217

   .. impl-detail::

218 219
      Under some circumstances, :func:`median_grouped` may coerce data points to
      floats.  This behaviour is likely to change in the future.
220 221 222

   .. seealso::

223 224
      * "Statistics for the Behavioral Sciences", Frederick J Gravetter and
        Larry B Wallnau (8th Edition).
225 226 227

      * Calculating the `median <http://www.ualberta.ca/~opscan/median.html>`_.

228
      * The `SSMEDIAN
229
        <https://help.gnome.org/users/gnumeric/stable/gnumeric.html#gnumeric-function-SSMEDIAN>`_
230 231
        function in the Gnome Gnumeric spreadsheet, including `this discussion
        <https://mail.gnome.org/archives/gnumeric-list/2011-April/msg00018.html>`_.
232 233 234 235


.. function:: mode(data)

236 237 238 239 240 241
   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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

   ``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'


260
.. function:: pstdev(data, mu=None)
261

262 263
   Return the population standard deviation (the square root of the population
   variance).  See :func:`pvariance` for arguments and other details.
264 265 266 267 268 269 270

   .. doctest::

      >>> pstdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
      0.986893273527251


271
.. function:: pvariance(data, mu=None)
272

273 274 275 276 277
   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.
278

279 280
   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
281
   automatically calculated.
282

283 284 285 286 287
   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.
288 289 290 291 292 293 294 295 296

   Examples:

   .. doctest::

      >>> data = [0.0, 0.25, 0.25, 1.25, 1.5, 1.75, 2.75, 3.25]
      >>> pvariance(data)
      1.25

297 298
   If you have already calculated the mean of your data, you can pass it as the
   optional second argument *mu* to avoid recalculation:
299 300 301 302 303 304 305

   .. doctest::

      >>> mu = mean(data)
      >>> pvariance(data, mu)
      1.25

306 307 308
   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.
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

   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::

324 325 326
      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.
327

328 329 330 331 332
      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.
333 334


335
.. function:: stdev(data, xbar=None)
336

337 338
   Return the sample standard deviation (the square root of the sample
   variance).  See :func:`variance` for arguments and other details.
339 340 341 342 343 344 345

   .. doctest::

      >>> stdev([1.5, 2.5, 2.5, 2.75, 3.25, 4.75])
      1.0810874155219827


346
.. function:: variance(data, xbar=None)
347

348 349 350 351 352
   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.
353

354 355
   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
356
   automatically calculated.
357

358 359 360 361
   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.
362 363 364 365 366 367 368 369 370

   Examples:

   .. doctest::

      >>> data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]
      >>> variance(data)
      1.3720238095238095

371 372
   If you have already calculated the mean of your data, you can pass it as the
   optional second argument *xbar* to avoid recalculation:
373 374 375 376 377 378 379

   .. doctest::

      >>> m = mean(data)
      >>> variance(data, m)
      1.3720238095238095

380 381
   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
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
   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::

398 399 400 401
      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.
402

403 404 405
      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.
406 407 408 409 410 411

Exceptions
----------

A single exception is defined:

Benjamin Peterson's avatar
Benjamin Peterson committed
412
.. exception:: StatisticsError
413

414
   Subclass of :exc:`ValueError` for statistics-related exceptions.
415 416 417 418

..
   # 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;