Skip to content

bpo-40290: Add zscore() to statistics.NormalDist. #19547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Doc/library/statistics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,16 @@ of applications in statistics.
Set *n* to 100 for percentiles which gives the 99 cuts points that
separate the normal distribution into 100 equal sized groups.

.. method:: NormalDist.zscore(x)

Compute the
`Standard Score <https://www.statisticshowto.com/probability-and-statistics/z-score/>`_
describing *x* in terms of the number of standard deviations
above or below the mean of the normal distribution:
``(x - mean) / stdev``.

.. versionadded:: 3.9

Instances of :class:`NormalDist` support addition, subtraction,
multiplication and division by a constant. These operations
are used for translation and scaling. For example:
Expand Down
11 changes: 11 additions & 0 deletions Lib/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,17 @@ def overlap(self, other):
x2 = (a - b) / dv
return 1.0 - (fabs(Y.cdf(x1) - X.cdf(x1)) + fabs(Y.cdf(x2) - X.cdf(x2)))

def zscore(self, x):
"""Compute the Standard Score. (x - mean) / stdev

Describes *x* in terms of the number of standard deviations
above or below the mean of the normal distribution.
"""
# https://www.statisticshowto.com/probability-and-statistics/z-score/
if not self._sigma:
raise StatisticsError('zscore() not defined when sigma is zero')
return (x - self._mu) / self._sigma

@property
def mean(self):
"Arithmetic mean of the normal distribution."
Expand Down
15 changes: 15 additions & 0 deletions Lib/test/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,21 @@ def overlap_numeric(X, Y, *, steps=8_192, z=5):
with self.assertRaises(self.module.StatisticsError):
NormalDist(1, 0).overlap(X) # left operand sigma is zero

def test_zscore(self):
NormalDist = self.module.NormalDist
X = NormalDist(100, 15)
self.assertEqual(X.zscore(142), 2.8)
self.assertEqual(X.zscore(58), -2.8)
self.assertEqual(X.zscore(100), 0.0)
with self.assertRaises(TypeError):
X.zscore() # too few arguments
with self.assertRaises(TypeError):
X.zscore(1, 1) # too may arguments
with self.assertRaises(TypeError):
X.zscore(None) # non-numeric type
with self.assertRaises(self.module.StatisticsError):
NormalDist(1, 0).zscore(100) # sigma is zero

def test_properties(self):
X = self.module.NormalDist(100, 15)
self.assertEqual(X.mean, 100)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added zscore() to statistics.NormalDist().