Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
6463ba30
Commit
6463ba30
authored
Apr 07, 2019
by
Raymond Hettinger
Committed by
GitHub
Apr 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-27181: Add statistics.geometric_mean() (GH-12638)
parent
9d7b2c09
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
132 additions
and
1 deletion
+132
-1
Doc/library/statistics.rst
Doc/library/statistics.rst
+19
-0
Doc/whatsnew/3.8.rst
Doc/whatsnew/3.8.rst
+3
-0
Lib/statistics.py
Lib/statistics.py
+21
-1
Lib/test/test_statistics.py
Lib/test/test_statistics.py
+88
-0
Misc/NEWS.d/next/Library/2019-03-31-01-18-52.bpo-27181.LVUWcc.rst
...S.d/next/Library/2019-03-31-01-18-52.bpo-27181.LVUWcc.rst
+1
-0
No files found.
Doc/library/statistics.rst
View file @
6463ba30
...
...
@@ -40,6 +40,7 @@ or sample.
======================= ===============================================================
:func:`mean` Arithmetic mean ("average") of data.
:func:`fmean` Fast, floating point arithmetic mean.
:func:`geometric_mean` Geometric mean of data.
:func:`harmonic_mean` Harmonic mean of data.
:func:`median` Median (middle value) of data.
:func:`median_low` Low median of data.
...
...
@@ -130,6 +131,24 @@ However, for reading convenience, most of the examples show sorted sequences.
.. versionadded:: 3.8
.. function:: geometric_mean(data)
Convert *data* to floats and compute the geometric mean.
Raises a :exc:`StatisticsError` if the input dataset is empty,
if it contains a zero, or if it contains a negative value.
No special efforts are made to achieve exact results.
(However, this may change in the future.)
.. doctest::
>>> round(geometric_mean([54, 24, 36]), 9)
36.0
.. versionadded:: 3.8
.. function:: harmonic_mean(data)
Return the harmonic mean of *data*, a sequence or iterator of
...
...
Doc/whatsnew/3.8.rst
View file @
6463ba30
...
...
@@ -322,6 +322,9 @@ Added :func:`statistics.fmean` as a faster, floating point variant of
:func:`statistics.mean()`. (Contributed by Raymond Hettinger and
Steven D'Aprano in :issue:`35904`.)
Added :func:`statistics.geometric_mean()`
(Contributed by Raymond Hettinger in :issue:`27181`.)
Added :func:`statistics.multimode` that returns a list of the most
common values. (Contributed by Raymond Hettinger in :issue:`35892`.)
...
...
Lib/statistics.py
View file @
6463ba30
...
...
@@ -11,13 +11,14 @@ Calculating averages
Function Description
================== =============================================
mean Arithmetic mean (average) of data.
geometric_mean Geometric mean of data.
harmonic_mean Harmonic mean of data.
median Median (middle value) of data.
median_low Low median of data.
median_high High median of data.
median_grouped Median, or 50th percentile, of grouped data.
mode Mode (most common value) of data.
multimode List of modes (most common values of data)
multimode List of modes (most common values of data)
.
================== =============================================
Calculate the arithmetic mean ("the average") of data:
...
...
@@ -81,6 +82,7 @@ __all__ = [ 'StatisticsError', 'NormalDist',
'pstdev'
,
'pvariance'
,
'stdev'
,
'variance'
,
'median'
,
'median_low'
,
'median_high'
,
'median_grouped'
,
'mean'
,
'mode'
,
'multimode'
,
'harmonic_mean'
,
'fmean'
,
'geometric_mean'
,
]
import
math
...
...
@@ -328,6 +330,24 @@ def fmean(data):
except
ZeroDivisionError
:
raise
StatisticsError
(
'fmean requires at least one data point'
)
from
None
def
geometric_mean
(
data
):
"""Convert data to floats and compute the geometric mean.
Raises a StatisticsError if the input dataset is empty,
if it contains a zero, or if it contains a negative value.
No special efforts are made to achieve exact results.
(However, this may change in the future.)
>>> round(geometric_mean([54, 24, 36]), 9)
36.0
"""
try
:
return
exp
(
fmean
(
map
(
log
,
data
)))
except
ValueError
:
raise
StatisticsError
(
'geometric mean requires a non-empty dataset '
' containing positive numbers'
)
from
None
def
harmonic_mean
(
data
):
"""Return the harmonic mean of data.
...
...
Lib/test/test_statistics.py
View file @
6463ba30
...
...
@@ -2038,6 +2038,94 @@ class TestStdev(VarianceStdevMixin, NumericTestCase):
expected
=
math
.
sqrt
(
statistics
.
variance
(
data
))
self
.
assertEqual
(
self
.
func
(
data
),
expected
)
class
TestGeometricMean
(
unittest
.
TestCase
):
def
test_basics
(
self
):
geometric_mean
=
statistics
.
geometric_mean
self
.
assertAlmostEqual
(
geometric_mean
([
54
,
24
,
36
]),
36.0
)
self
.
assertAlmostEqual
(
geometric_mean
([
4.0
,
9.0
]),
6.0
)
self
.
assertAlmostEqual
(
geometric_mean
([
17.625
]),
17.625
)
random
.
seed
(
86753095551212
)
for
rng
in
[
range
(
1
,
100
),
range
(
1
,
1_000
),
range
(
1
,
10_000
),
range
(
500
,
10_000
,
3
),
range
(
10_000
,
500
,
-
3
),
[
12
,
17
,
13
,
5
,
120
,
7
],
[
random
.
expovariate
(
50.0
)
for
i
in
range
(
1_000
)],
[
random
.
lognormvariate
(
20.0
,
3.0
)
for
i
in
range
(
2_000
)],
[
random
.
triangular
(
2000
,
3000
,
2200
)
for
i
in
range
(
3_000
)],
]:
gm_decimal
=
math
.
prod
(
map
(
Decimal
,
rng
))
**
(
Decimal
(
1
)
/
len
(
rng
))
gm_float
=
geometric_mean
(
rng
)
self
.
assertTrue
(
math
.
isclose
(
gm_float
,
float
(
gm_decimal
)))
def
test_various_input_types
(
self
):
geometric_mean
=
statistics
.
geometric_mean
D
=
Decimal
F
=
Fraction
# https://www.wolframalpha.com/input/?i=geometric+mean+3.5,+4.0,+5.25
expected_mean
=
4.18886
for
data
,
kind
in
[
([
3.5
,
4.0
,
5.25
],
'floats'
),
([
D
(
'3.5'
),
D
(
'4.0'
),
D
(
'5.25'
)],
'decimals'
),
([
F
(
7
,
2
),
F
(
4
,
1
),
F
(
21
,
4
)],
'fractions'
),
([
3.5
,
4
,
F
(
21
,
4
)],
'mixed types'
),
((
3.5
,
4.0
,
5.25
),
'tuple'
),
(
iter
([
3.5
,
4.0
,
5.25
]),
'iterator'
),
]:
actual_mean
=
geometric_mean
(
data
)
self
.
assertIs
(
type
(
actual_mean
),
float
,
kind
)
self
.
assertAlmostEqual
(
actual_mean
,
expected_mean
,
places
=
5
)
def
test_big_and_small
(
self
):
geometric_mean
=
statistics
.
geometric_mean
# Avoid overflow to infinity
large
=
2.0
**
1000
big_gm
=
geometric_mean
([
54.0
*
large
,
24.0
*
large
,
36.0
*
large
])
self
.
assertTrue
(
math
.
isclose
(
big_gm
,
36.0
*
large
))
self
.
assertFalse
(
math
.
isinf
(
big_gm
))
# Avoid underflow to zero
small
=
2.0
**
-
1000
small_gm
=
geometric_mean
([
54.0
*
small
,
24.0
*
small
,
36.0
*
small
])
self
.
assertTrue
(
math
.
isclose
(
small_gm
,
36.0
*
small
))
self
.
assertNotEqual
(
small_gm
,
0.0
)
def
test_error_cases
(
self
):
geometric_mean
=
statistics
.
geometric_mean
StatisticsError
=
statistics
.
StatisticsError
with
self
.
assertRaises
(
StatisticsError
):
geometric_mean
([])
# empty input
with
self
.
assertRaises
(
StatisticsError
):
geometric_mean
([
3.5
,
0.0
,
5.25
])
# zero input
with
self
.
assertRaises
(
StatisticsError
):
geometric_mean
([
3.5
,
-
4.0
,
5.25
])
# negative input
with
self
.
assertRaises
(
StatisticsError
):
geometric_mean
(
iter
([]))
# empty iterator
with
self
.
assertRaises
(
TypeError
):
geometric_mean
(
None
)
# non-iterable input
with
self
.
assertRaises
(
TypeError
):
geometric_mean
([
10
,
None
,
20
])
# non-numeric input
with
self
.
assertRaises
(
TypeError
):
geometric_mean
()
# missing data argument
with
self
.
assertRaises
(
TypeError
):
geometric_mean
([
10
,
20
,
60
],
70
)
# too many arguments
def
test_special_values
(
self
):
# Rules for special values are inherited from math.fsum()
geometric_mean
=
statistics
.
geometric_mean
NaN
=
float
(
'Nan'
)
Inf
=
float
(
'Inf'
)
self
.
assertTrue
(
math
.
isnan
(
geometric_mean
([
10
,
NaN
])),
'nan'
)
self
.
assertTrue
(
math
.
isnan
(
geometric_mean
([
NaN
,
Inf
])),
'nan and infinity'
)
self
.
assertTrue
(
math
.
isinf
(
geometric_mean
([
10
,
Inf
])),
'infinity'
)
with
self
.
assertRaises
(
ValueError
):
geometric_mean
([
Inf
,
-
Inf
])
class
TestNormalDist
(
unittest
.
TestCase
):
# General note on precision: The pdf(), cdf(), and overlap() methods
...
...
Misc/NEWS.d/next/Library/2019-03-31-01-18-52.bpo-27181.LVUWcc.rst
0 → 100644
View file @
6463ba30
Add statistics.geometric_mean().
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment