MCPcopy Index your code
hub / github.com/python/cpython / NormalDist

Class NormalDist

Lib/statistics.py:1228–1448  ·  view source on GitHub ↗

Normal distribution of a random variable

Source from the content-addressed store, hash-verified

1226## Normal Distribution #####################################################
1227
1228class NormalDist:
1229 "Normal distribution of a random variable"
1230 # https://en.wikipedia.org/wiki/Normal_distribution
1231 # https://en.wikipedia.org/wiki/Variance#Properties
1232
1233 __slots__ = {
1234 '_mu': 'Arithmetic mean of a normal distribution',
1235 '_sigma': 'Standard deviation of a normal distribution',
1236 }
1237
1238 def __init__(self, mu=0.0, sigma=1.0):
1239 "NormalDist where mu is the mean and sigma is the standard deviation."
1240 if sigma < 0.0:
1241 raise StatisticsError('sigma must be non-negative')
1242 self._mu = float(mu)
1243 self._sigma = float(sigma)
1244
1245 @classmethod
1246 def from_samples(cls, data):
1247 "Make a normal distribution instance from sample data."
1248 return cls(*_mean_stdev(data))
1249
1250 def samples(self, n, *, seed=None):
1251 "Generate *n* samples for a given mean and standard deviation."
1252 rnd = random.random if seed is None else random.Random(seed).random
1253 inv_cdf = _normal_dist_inv_cdf
1254 mu = self._mu
1255 sigma = self._sigma
1256 return [inv_cdf(rnd(), mu, sigma) for _ in repeat(None, n)]
1257
1258 def pdf(self, x):
1259 "Probability density function. P(x <= X < x+dx) / dx"
1260 variance = self._sigma * self._sigma
1261 if not variance:
1262 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)
1265
1266 def cdf(self, x):
1267 "Cumulative distribution function. P(X <= x)"
1268 if not self._sigma:
1269 raise StatisticsError('cdf() not defined when sigma is zero')
1270 return 0.5 * erfc((self._mu - x) / (self._sigma * _SQRT2))
1271
1272 def inv_cdf(self, p):
1273 """Inverse cumulative distribution function. x : P(X <= x) = p
1274
1275 Finds the value of the random variable such that the probability of
1276 the variable being less than or equal to that value equals the given
1277 probability.
1278
1279 This function is also called the percent point function or quantile
1280 function.
1281 """
1282 if p <= 0.0 or p >= 1.0:
1283 raise StatisticsError('p must be in the range 0.0 < p < 1.0')
1284 return _normal_dist_inv_cdf(p, self._mu, self._sigma)
1285

Callers 15

__add__Method · 0.85
__sub__Method · 0.85
__mul__Method · 0.85
__truediv__Method · 0.85
__pos__Method · 0.85
__neg__Method · 0.85
test_pdfMethod · 0.85
test_cdfMethod · 0.85
test_inv_cdfMethod · 0.85
test_overlapMethod · 0.85

Calls

no outgoing calls

Tested by 11

test_pdfMethod · 0.68
test_cdfMethod · 0.68
test_inv_cdfMethod · 0.68
test_overlapMethod · 0.68
test_zscoreMethod · 0.68
test_unary_operationsMethod · 0.68
test_equalityMethod · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…