8000 Minor whitespace, indentation, and quoting changes to improve interna… · python/cpython@e8b3a2e · GitHub
[go: up one dir, main page]

Skip to content

Commit e8b3a2e

Browse files
Minor whitespace, indentation, and quoting changes to improve internal consistency and appease linters (GH-14888) (GH-14889)
(cherry picked from commit 1c0e9bb) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent 8f50164 commit e8b3a2e

File tree

1 file changed

+74
-53
lines changed

1 file changed

+74
-53
lines changed

Lib/statistics.py

Lines changed: 74 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,25 @@
8080
8181
"""
8282

83-
__all__ = [ 'StatisticsError', 'NormalDist', 'quantiles',
84-
'pstdev', 'pvariance', 'stdev', 'variance',
85-
'median', 'median_low', 'median_high', 'median_grouped',
86-
'mean', 'mode', 'multimode', 'harmonic_mean', 'fmean',
87-
'geometric_mean',
88-
]
83+
__all__ = [
84+
'NormalDist',
85+
'StatisticsError',
86+
'fmean',
87+
'geometric_mean',
88+
'harmonic_mean',
89+
'mean',
90+
'median',
91+
'median_grouped',
92+
'median_high',
93+
'median_low',
94+
'mode',
95+
'multimode',
96+
'pstdev',
97+
'pvariance',
98+
'quantiles',
99+
'stdev',
100+
'variance',
101+
]
89102

90103
import math
91104
import numbers
@@ -304,16 +317,16 @@ def mean(data):
304317
assert count == n
305318
return _convert(total/n, T)
306319

320+
307321
def fmean(data):
308-
""" Convert data to floats and compute the arithmetic mean.
322+
"""Convert data to floats and compute the arithmetic mean.
309323
310324
This runs faster than the mean() function and it always returns a float.
311325
The result is highly accurate but not as perfect as mean().
312326
If the input dataset is empty, it raises a StatisticsError.
313327
314328
>>> fmean([3.5, 4.0, 5.25])
315329
4.25
316-
317330
"""
318331
try:
319332
n = len(data)
@@ -332,6 +345,7 @@ def count(iterable):
332345
except ZeroDivisionError:
333346
raise StatisticsError('fmean requires at least one data point') from None
334347

348+
335349
def geometric_mean(data):
336350
"""Convert data to floats and compute the geometric mean.
337351
@@ -350,6 +364,7 @@ def geometric_mean(data):
350364
raise StatisticsError('geometric mean requires a non-empty dataset '
351365
' containing positive numbers') from None
352366

367+
353368
def harmonic_mean(data):
354369
"""Return the harmonic mean of data.
355370
@@ -547,23 +562,23 @@ def mode(data):
547562

548563

549564
def multimode(data):
550-
""" Return a list of the most frequently occurring values.
551-
552-
Will return more than one result if there are multiple modes
553-
or an empty list if *data* is empty.
565+
"""Return a list of the most frequently occurring values.
554566
555-
>>> multimode('aabbbbbbbbcc')
556-
['b']
557-
>>> multimode('aabbbbccddddeeffffgg')
558-
['b', 'd', 'f']
559-
> 3419 >> multimode('')
560-
[]
567+
Will return more than one result if there are multiple modes
568+
or an empty list if *data* is empty.
561569
570+
>>> multimode('aabbbbbbbbcc')
571+
['b']
572+
>>> multimode('aabbbbccddddeeffffgg')
573+
['b', 'd', 'f']
574+
>>> multimode('')
575+
[]
562576
"""
563577
counts = Counter(iter(data)).most_common()
564578
maxcount, mode_items = next(groupby(counts, key=itemgetter(1)), (0, []))
565579
return list(map(itemgetter(0), mode_items))
566580

581+
567582
# Notes on methods for computing quantiles
568583
# ----------------------------------------
569584
#
@@ -601,7 +616,7 @@ def multimode(data):
601616
# external packages can be used for anything more advanced.
602617

603618
def quantiles(dist, /, *, n=4, method='exclusive'):
604-
'''Divide *dist* into *n* continuous intervals with equal probability.
619+
"""Divide *dist* into *n* continuous intervals with equal probability.
605620
606621
Returns a list of (n - 1) cut points separating the intervals.
607622
@@ -616,7 +631,7 @@ def quantiles(dist, /, *, n=4, method='exclusive'):
616631
If *method* is set to *inclusive*, *dist* is treated as population
617632
data. The minimum value is treated as the 0th percentile and the
618633
maximum value is treated as the 100th percentile.
619-
'''
634+
"""
620635
if n < 1:
621636
raise StatisticsError('n must be at least 1')
622637
if hasattr(dist, 'inv_cdf'):
@@ -646,6 +661,7 @@ def quantiles(dist, /, *, n=4, method='exclusive'):
646661
return result
647662
raise ValueError(f'Unknown method: {method!r}')
648663

664+
649665
# === Measures of spread ===
650666

651667
# See http://mathworld.wolfram.com/Variance.html
@@ -805,59 +821,64 @@ def pstdev(data, mu=None):
805821
except AttributeError:
806822
return math.sqrt(var)
807823

824+
808825
## Normal Distribution #####################################################
809826

810827
class NormalDist:
811-
'Normal distribution of a random variable'
828+
"Normal distribution of a random variable"
812829
# https://en.wikipedia.org/wiki/Normal_distribution
813830
# https://en.wikipedia.org/wiki/Variance#Properties
814831

815-
__slots__ = {'_mu': 'Arithmetic mean of a normal distribution',
816-
'_sigma': 'Standard deviation of a normal distribution'}
832+
__slots__ = {
833+
'_mu': 'Arithmetic mean of a normal distribution',
834+
'_sigma': 'Standard deviation of a normal distribution',
835+
}
817836

818837
def __init__(self, mu=0.0, sigma=1.0):
819-
'NormalDist where mu is the mean and sigma is the standard deviation.'
838+
"NormalDist where mu is the mean and sigma is the standard deviation."
820839
if sigma < 0.0:
821840
raise StatisticsError('sigma must be non-negative')
822841
self._mu = mu
823842
self._sigma = sigma
824843

825844
@classmethod
826845
def from_samples(cls, data):
827-
'Make a normal distribution instance from sample data.'
846+
"Make a normal distribution instance from sample data."
828847
if not isinstance(data, (list, tuple)):
829848
data = list(data)
830849
xbar = fmean(data)
831850
return cls(xbar, stdev(data, xbar))
832851

833852
def samples(self, n, *, seed=None):
834-
'Generate *n* samples for a given mean and standard deviation.'
853+
"Generate *n* samples for a given mean and standard deviation."
835854
gauss = random.gauss if seed is None else random.Random(seed).gauss
836855
mu, sigma = self._mu, self._sigma
837856
return [gauss(mu, sigma) for i in range(n)]
838857

839858
def pdf(self, x):
840-
'Probability density function. P(x <= X < x+dx) / dx'
859+
"Probability density function. P(x <= X < x+dx) / dx"
841860
variance = self._sigma ** 2.0
842861
if not variance:
843862
raise StatisticsError('pdf() not defined when sigma is zero')
844-
return exp((x - self._mu)**2.0 / (-2.0*variance)) / sqrt(tau * variance)
863+
return exp((x - self._mu)**2.0 / (-2.0*variance)) / sqrt(tau*variance)
845864

846865
def cdf(self, x):
847-
'Cumulative distribution function. P(X <= x)'
866+
"Cumulative distribution function. P(X <= x)"
848867
if not self._sigma:
849868
raise StatisticsError('cdf() not defined when sigma is zero')
850869
return 0.5 * (1.0 + erf((x - self._mu) / (self._sigma * sqrt(2.0))))
851870

852871
def inv_cdf(self, p):
853-
'''Inverse cumulative distribution function. x : P(X <= x) = p
872+
"""Inverse cumulative distribution function. x : P(X <= x) = p
854873
855-
Finds the value of the random variable such that the probability of the
856-
variable being less than or equal to that value equals the given probability.
874+
Finds the value of the random variable such that the probability of
875+
the variable being less than or equal to that value equals the given
876+
probability.
857877
858-
This function is also called the percent point function or quantile function.
859-
'''
860-
if (p <= 0.0 or p >= 1.0):
878+
This function is also called the percent point function or quantile
879+
function.
880+
"""
881+
if p <= 0.0 or p >= 1.0:
861882
raise StatisticsError('p must be in the range 0.0 < p < 1.0')
862883
if self._sigma <= 0.0:
863884
raise StatisticsError('cdf() not defined when sigma at or below zero')
@@ -933,7 +954,7 @@ def inv_cdf(self, p):
933954
return self._mu + (x * self._sigma)
934955

935956
def overlap(self, other):
936-
'''Compute the overlapping coefficient (OVL) between two normal distributions.
957+
"""Compute the overlapping coefficient (OVL) between two normal distributions.
937958
938959
Measures the agreement between two normal probability distributions.
939960
Returns a value between 0.0 and 1.0 giving the overlapping area in
@@ -943,7 +964,7 @@ def overlap(self, other):
943964
>>> N2 = NormalDist(3.2, 2.0)
944965
>>> N1.overlap(N2)
945966
0.8035050657330205
946-
'''
967+
"""
947968
# See: "The overlapping coefficient as a measure of agreement between
948969
# probability distributions and point estimation of the overlap of two
949970
# normal densities" -- Henry F. Inman and Edwin L. Bradley Jr
@@ -968,87 +989,87 @@ def overlap(self, other):
968989

969990
@property
970991
def mean(self):
971-
'Arithmetic mean of the normal distribution.'
992+
"Arithmetic mean of the normal distribution."
972993
return self._mu
973994

974995
@property
975996
def stdev(self):
976-
'Standard deviation of the normal distribution.'
997+
"Standard deviation of the normal distribution."
977998
return self._sigma
978999

9791000
@property
9801001
def variance(self):
981-
'Square of the standard deviation.'
1002+
"Square of the standard deviation."
9821003
return self._sigma ** 2.0
9831004

9841005
def __add__(x1, x2):
985-
'''Add a constant or another NormalDist instance.
1006+
"""Add a constant or another NormalDist instance.
9861007
9871008
If *other* is a constant, translate mu by the constant,
9881009
leaving sigma unchanged.
9891010
9901011
If *other* is a NormalDist, add both the means and the variances.
9911012
Mathematically, this works only if the two distributions are
9921013
independent or if they are jointly normally distributed.
993-
'''
1014+
"""
9941015
if isinstance(x2, NormalDist):
9951016
return NormalDist(x1._mu + x2._mu, hypot(x1._sigma, x2._sigma))
9961017
return NormalDist(x1._mu + x2, x1._sigma)
9971018

9981019
def __sub__(x1, x2):
999-
'''Subtract a constant or another NormalDist instance.
1020+
"""Subtract a constant or another NormalDist instance.
10001021
10011022
If *other* is a constant, translate by the constant mu,
10021023
leaving sigma unchanged.
10031024
10041025
If *other* is a NormalDist, subtract the means and add the variances.
10051026
Mathematically, this works only if the two distributions are
10061027
independent or if they are jointly normally distributed.
1007-
'''
1028+
"""
10081029
if isinstance(x2, NormalDist):
10091030
return NormalDist(x1._mu - x2._mu, hypot(x1._sigma, x2._sigma))
10101031
return NormalDist(x1._mu - x2, x1._sigma)
10111032

10121033
def __mul__(x1, x2):
1013-
'''Multiply both mu and sigma by a constant.
1034+
"""Multiply both mu and sigma by a constant.
10141035
10151036
Used for rescaling, perhaps to change measurement units.
10161037
Sigma is scaled with the absolute value of the constant.
1017-
'''
1038+
"""
10181039
return NormalDist(x1._mu * x2, x1._sigma * fabs(x2))
10191040

10201041
def __truediv__(x1, x2):
1021-
'''Divide both mu and sigma by a constant.
1042+
"""Divide both mu and sigma by a constant.
10221043
10231044
Used for rescaling, perhaps to change measurement units.
10241045
Sigma is scaled with the absolute value of the constant.
1025-
'''
1046+
"""
10261047
return NormalDist(x1._mu / x2, x1._sigma / fabs(x2))
10271048

10281049
def __pos__(x1):
1029-
'Return a copy of the instance.'
1050+
"Return a copy of the instance."
10301051
return NormalDist(x1._mu, x1._sigma)
10311052

10321053
def __neg__(x1):
1033-
'Negates mu while keeping sigma the same.'
1054+
"Negates mu while keeping sigma the same."
10341055
return NormalDist(-x1._mu, x1._sigma)
10351056

10361057
__radd__ = __add__
10371058

10381059
def __rsub__(x1, x2):
1039-
'Subtract a NormalDist from a constant or another NormalDist.'
1060+
"Subtract a NormalDist from a constant or another NormalDist."
10401061
return -(x1 - x2)
10411062

10421063
__rmul__ = __mul__
10431064

10441065
def __eq__(x1, x2):
1045-
'Two NormalDist objects are equal if their mu and sigma are both equal.'
1066+
"Two NormalDist objects are equal if their mu and sigma are both equal."
10461067
if not isinstance(x2, NormalDist):
10471068
return NotImplemented
10481069
return (x1._mu, x2._sigma) == (x2._mu, x2._sigma)
10491070

10501071
def __hash__(self):
1051-
'NormalDist objects hash equal if their mu and sigma are both equal.'
1072+
"NormalDist objects hash equal if their mu and sigma are both equal."
10521073
return hash((self._mu, self._sigma))
10531074

10541075
def __repr__(self):

0 commit comments

Comments
 (0)
0