Skip to content

Commit e689394

Browse files
authored
Minor improvement to statistics.pdf() (gh-148500)
1 parent afaf58b commit e689394

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Lib/statistics.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
from collections import Counter, namedtuple, defaultdict
146146

147147
_SQRT2 = sqrt(2.0)
148+
_SQRT2PI = sqrt(tau)
148149
_random = random
149150

150151
## Exceptions ##############################################################
@@ -1257,11 +1258,11 @@ def samples(self, n, *, seed=None):
12571258

12581259
def pdf(self, x):
12591260
"Probability density function. P(x <= X < x+dx) / dx"
1260-
variance = self._sigma * self._sigma
1261-
if not variance:
1261+
sigma = self._sigma
1262+
if not sigma:
12621263
raise StatisticsError('pdf() not defined when sigma is zero')
1263-
diff = x - self._mu
1264-
return exp(diff * diff / (-2.0 * variance)) / sqrt(tau * variance)
1264+
z = (x - self._mu) / sigma
1265+
return exp(-0.5 * z * z) / (_SQRT2PI * sigma)
12651266

12661267
def cdf(self, x):
12671268
"Cumulative distribution function. P(X <= x)"

0 commit comments

Comments
 (0)