Skip to content

Commit 9e456bc

Browse files
rhettingermiss-islington
authored andcommitted
bpo-36018: Add properties for mean and stdev (GH-12022)
Responding to suggestions on the tracker and some off-line suggestions. Davin suggested that english named accessors instead of greek letters would result in more intelligible user code. Steven suggested that the parameters still need to be *mu* and *theta* which are used elsewhere (and I noted those parameter names are used in linked-to resources). Michael suggested proving-out the API by seeing whether it generalized to *Lognormal*. I did so and found that Lognormal distribution parameters *mu* and *sigma* do not represent the mean and standard deviation of the lognormal distribution (instead, they are for the underlying regular normal distribution). Putting these ideas together, we have NormalDist parameterized by *mu* and *sigma* but offering English named properties for accessors. That gives lets us match other API that access mu and sigma, it matches the external resources on the topic, gives us clear english names in user code. The API extends nicely to LogNormal where the parameters and the summary statistic accessors are not the same. https://bugs.python.org/issue36018
1 parent a875ea5 commit 9e456bc

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

Doc/library/statistics.rst

+10-6
Original file line numberDiff line numberDiff line change
@@ -488,13 +488,17 @@ of applications in statistics, including simulations and hypothesis testing.
488488

489489
If *sigma* is negative, raises :exc:`StatisticsError`.
490490

491-
.. attribute:: mu
491+
.. attribute:: mean
492492

493-
The mean of a normal distribution.
493+
A read-only property representing the `arithmetic mean
494+
<https://en.wikipedia.org/wiki/Arithmetic_mean>`_ of a normal
495+
distribution.
494496

495-
.. attribute:: sigma
497+
.. attribute:: stdev
496498

497-
The standard deviation of a normal distribution.
499+
A read-only property representing the `standard deviation
500+
<https://en.wikipedia.org/wiki/Standard_deviation>`_ of a normal
501+
distribution.
498502

499503
.. attribute:: variance
500504

@@ -566,8 +570,8 @@ of applications in statistics, including simulations and hypothesis testing.
566570
>>> birth_weights = NormalDist.from_samples([2.5, 3.1, 2.1, 2.4, 2.7, 3.5])
567571
>>> drug_effects = NormalDist(0.4, 0.15)
568572
>>> combined = birth_weights + drug_effects
569-
>>> f'mu={combined.mu :.1f} sigma={combined.sigma :.1f}'
570-
'mu=3.1 sigma=0.5'
573+
>>> f'mean: {combined.mean :.1f} standard deviation: {combined.stdev :.1f}'
574+
'mean: 3.1 standard deviation: 0.5'
571575

572576
.. versionadded:: 3.8
573577

Lib/statistics.py

+10
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,16 @@ def cdf(self, x):
740740
raise StatisticsError('cdf() not defined when sigma is zero')
741741
return 0.5 * (1.0 + erf((x - self.mu) / (self.sigma * sqrt(2.0))))
742742

743+
@property
744+
def mean(self):
745+
'Arithmetic mean of the normal distribution'
746+
return self.mu
747+
748+
@property
749+
def stdev(self):
750+
'Standard deviation of the normal distribution'
751+
return self.sigma
752+
743753
@property
744754
def variance(self):
745755
'Square of the standard deviation'

Lib/test/test_statistics.py

+6
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,12 @@ def test_cdf(self):
21282128
with self.assertRaises(statistics.StatisticsError):
21292129
Y.cdf(90)
21302130

2131+
def test_properties(self):
2132+
X = statistics.NormalDist(100, 15)
2133+
self.assertEqual(X.mean, 100)
2134+
self.assertEqual(X.stdev, 15)
2135+
self.assertEqual(X.variance, 225)
2136+
21312137
def test_unary_operations(self):
21322138
NormalDist = statistics.NormalDist
21332139
X = NormalDist(100, 12)

0 commit comments

Comments
 (0)