Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit042aa88

Browse files
authored
gh-108322: Optimize statistics.NormalDist.samples() (gh-108324)
1 parent09343db commit042aa88

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

‎Doc/library/statistics.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,11 @@ of applications in statistics.
828828
number generator. This is useful for creating reproducible results,
829829
even in a multi-threading context.
830830

831+
..versionchanged::3.13
832+
833+
Switched to a faster algorithm. To reproduce samples from previous
834+
versions, use:func:`random.seed` and:func:`random.gauss`.
835+
831836
..method::NormalDist.pdf(x)
832837

833838
Using a `probability density function (pdf)

‎Lib/statistics.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ def linear_regression(x, y, /, *, proportional=False):
11351135
>>> noise = NormalDist().samples(5, seed=42)
11361136
>>> y = [3 * x[i] + 2 + noise[i] for i in range(5)]
11371137
>>> linear_regression(x, y) #doctest: +ELLIPSIS
1138-
LinearRegression(slope=3.09078914170..., intercept=1.75684970486...)
1138+
LinearRegression(slope=3.17495..., intercept=1.00925...)
11391139
11401140
If *proportional* is true, the independent variable *x* and the
11411141
dependent variable *y* are assumed to be directly proportional.
@@ -1148,7 +1148,7 @@ def linear_regression(x, y, /, *, proportional=False):
11481148
11491149
>>> y = [3 * x[i] + noise[i] for i in range(5)]
11501150
>>> linear_regression(x, y, proportional=True) #doctest: +ELLIPSIS
1151-
LinearRegression(slope=3.02447542484..., intercept=0.0)
1151+
LinearRegression(slope=2.90475..., intercept=0.0)
11521152
11531153
"""
11541154
n=len(x)
@@ -1279,9 +1279,11 @@ def from_samples(cls, data):
12791279

12801280
defsamples(self,n,*,seed=None):
12811281
"Generate *n* samples for a given mean and standard deviation."
1282-
gauss=random.gaussifseedisNoneelserandom.Random(seed).gauss
1283-
mu,sigma=self._mu,self._sigma
1284-
return [gauss(mu,sigma)for_inrepeat(None,n)]
1282+
rnd=random.randomifseedisNoneelserandom.Random(seed).random
1283+
inv_cdf=_normal_dist_inv_cdf
1284+
mu=self._mu
1285+
sigma=self._sigma
1286+
return [inv_cdf(rnd(),mu,sigma)for_inrepeat(None,n)]
12851287

12861288
defpdf(self,x):
12871289
"Probability density function. P(x <= X < x+dx) / dx"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed-up NormalDist.samples() by using the inverse CDF method instead of
2+
calling random.gauss().

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp