Ñò
IåHc @ s¥ d Z d d k Z d „ Z d „ Z e Z d „ Z e d „ Z d e f d „ ƒ YZ d e f d
„ ƒ YZ
d d d
„ Z e d j o d d k
Z
e
i ƒ n d S( s% Number formatting and numeric helpersiÿÿÿÿNc C s t | d ƒ | S( ss What percent of ``whole`` is ``part``?
>>> percent_of(5, 100)
5.0
>>> percent_of(13, 26)
50.0
id ( t float( t partt whole( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt
percent_of s c C sD y t t | ƒ ƒ t | ƒ SWn t j
o t d ƒ ‚ n Xd S( s‚ Return the mean of a sequence of numbers.
The mean is the average of all the numbers.
>>> mean([5, 10])
7.5
s( can't calculate mean of empty collectionN( R t sumt lent ZeroDivisionErrort
ValueError( t r( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt mean s c C s t | ƒ } t | ƒ } | d j o t d ƒ ‚ n | i ƒ | d } | d } | o | | S| | d } | | d } t | | g ƒ S( s÷ Return the median of an iterable of numbers.
The median is the point at which half the numbers are lower than it and
half the numbers are higher. This gives a better sense of the majority
level than the mean (average) does, because the mean can be skewed by a few
extreme numbers at either end. For instance, say you want to calculate
the typical household income in a community and you've sampled four
households:
>>> incomes = [18000] # Fast food crew
>>> incomes.append(24000) # Janitor
>>> incomes.append(32000) # Journeyman
>>> incomes.append(44000) # Experienced journeyman
>>> incomes.append(67000) # Manager
>>> incomes.append(9999999) # Bill Gates
>>> median(incomes)
49500.0
>>> mean(incomes)
1697499.8333333333
The median here is somewhat close to the majority of incomes, while the
mean is far from anybody's income.
[20 000,
40 000,
60 000,
9 999 999]
The median would be around 50 000, which is close to what the majority of
respondents make. The average would be in the millions, which is far from
what any of the respondents make.
This implementation makes a temporary list of all numbers in memory.
i s( can't calculate mean of empty collectioni i ( t listR R t sortR ( R t st s_lent centert is_oddt lowt high( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt median s #
c C su t | ƒ } t g } | D] } | | | d q ~ ƒ } | o t | ƒ d p d } n
t | ƒ } | | d S( sö Standard deviation, `from the Python Cookbook
`_
Population mode contributed by Lorenzo Catucci.
Standard deviation shows the variability within a sequence of numbers.
A small standard deviation shows the numbers are close to the same. A
large standard deviation shows they are widely different. In fact it
shows how far the numbers tend to deviate from the average. This can be
used to detect whether the average has been skewed by a few extremely high
or extremely low values.
By default the helper computes the unbiased estimate
for the population standard deviation, by applying an unbiasing
factor of sqrt(N/(N-1)).
If you'd rather have the function compute the population standard
deviation, pass ``sample=False``.
The following examples are taken from Wikipedia.
http://en.wikipedia.org/wiki/Standard_deviation
>>> standard_deviation([0, 0, 14, 14]) # doctest: +ELLIPSIS
8.082903768654761...
>>> standard_deviation([0, 6, 8, 14]) # doctest: +ELLIPSIS
5.773502691896258...
>>> standard_deviation([6, 6, 8, 8])
1.1547005383792515
>>> standard_deviation([0, 0, 14, 14], sample=False)
7.0
>>> standard_deviation([0, 6, 8, 14], sample=False)
5.0
>>> standard_deviation([6, 6, 8, 8], sample=False)
1.0
(The results reported in Wikipedia are those expected for whole
population statistics and therefore are equal to the ones we get
by setting ``sample=False`` in the later tests.)
.. code-block:: pycon
# Fictitious average monthly temperatures in Southern California.
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
>>> standard_deviation([70, 70, 70, 75, 80, 85, 90, 95, 90, 80, 75, 70]) # doctest: +ELLIPSIS
9.003366373785...
>>> standard_deviation([70, 70, 70, 75, 80, 85, 90, 95, 90, 80, 75, 70], sample=False) # doctest: +ELLIPSIS
8.620067027323...
# Fictitious average monthly temperatures in Montana.
# Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
>>> standard_deviation([-32, -10, 20, 30, 60, 90, 100, 80, 60, 30, 10, -32]) # doctest: +ELLIPSIS
45.1378360405574...
>>> standard_deviation([-32, -10, 20, 30, 60, 90, 100, 80, 60, 30, 10, -32], sample=False) # doctest: +ELLIPSIS
43.2161878106906...
Most natural and random phenomena follow the normal distribution (aka the
bell curve), which says that most values are close to average but a few are
extreme. E.g., most people are close to 5'9" tall but a few are very tall
or very short. If the data does follow the bell curve, 68% of the values
will be within 1 standard deviation (stdev) of the average, and 95% will be
within 2 standard deviations. So a university professor grading exams on a
curve might give a "C" (mediocre) grade to students within 1 stdev of the
average score, "B" (better than average) to those within 2 stdevs above,
and "A" (perfect) to the 0.25% higher than 2 stdevs. Those between 1 and 2
stdevs below get a "D" (poor), and those below 2 stdevs... we won't talk
about them.
i i g à?( t averageR R ( R t samplet avgt _[1]t it sdsqt normal_denom( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt standard_deviationP s C/t SimpleStatsc B sM e Z d Z d Z e d „ Z d „ Z d „ Z d „ Z d „ Z d „ Z
RS( sg Calculate a few simple stats on data.
This class calculates the minimum, maximum, and count of all the values
given to it. The values are not saved in the object. Usage::
>>> stats = SimpleStats()
>>> stats(2) # Add one data value.
>>> stats.extend([6, 4]) # Add several data values at once.
The statistics are available as instance attributes::
>>> stats.count
3
>>> stats.min
2
>>> stats.max
6
Non-numeric data is also allowed:
>>> stats2 = SimpleStats()
>>> stats2("foo")
>>> stats2("bar")
>>> stats2.count
2
>>> stats2.min
'bar'
>>> stats2.max
'foo'
If the ``numeric`` constructor arg is true, only ``int``, ``long``, and
``float`` values will be accepted. This flag is intended to enable
additional numeric statistics, although none are currently implemented.
``.min`` and ``.max`` are ``None`` until the first data value is
registered.
Subclasses can override ``._init_stats`` and ``._update_stats`` to add
additional statistics.
i c C s2 | | _ d | _ d | _ d | _ | i ƒ d S( Ni ( t numerict countt Nonet mint maxt _init_stats( t selfR ( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt __init__Æ s
c C s
t | i ƒ S( s- The instance is true if it has seen any data.( t boolR ( R" ( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt __nonzero__Í s c C s„ | i o | d n | i d j o | | _ | _ n+ t | i | ƒ | _ t | i | ƒ | _ | i d 7_ | i | ƒ d S( s Add a data value.i i N( R R R R t
_update_stats( R" t value( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt __call__Ñ s
c C s x | D] } | | ƒ q Wd S( s9 Add several data values at once, akin to ``list.extend``.N( ( R" t valuesR' ( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyt extendÝ s c C s d S( s2 Initialize state data used by subclass statistics.N( ( R" ( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyR! ã s c C s d S( s' Add a value to the subclass statistics.N( ( R" R' ( ( s5 /usr/lib/python2.6/site-packages/webhelpers/number.pyR&