bpo-37798: Add C fastpath for statistics.NormalDist.inv_cdf() (GH-15266) · aaronsgithub/cpython@0a18ee4 · GitHub
Skip to content

Commit 0a18ee4

Browse files
corona10rhettinger
authored andcommitted
bpo-37798: Add C fastpath for statistics.NormalDist.inv_cdf() (pythonGH-15266)
1 parent 5be6660 commit 0a18ee4

9 files changed

Lines changed: 264 additions & 73 deletions

File tree

Lib/statistics.py

Lines changed: 82 additions & 73 deletions
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add C fastpath for statistics.NormalDist.inv_cdf() Patch by Dong-hee Na

Modules/Setup

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ _symtable symtablemodule.c
182182
#_heapq _heapqmodule.c # Heap queue algorithm
183183
#_asyncio _asynciomodule.c # Fast asyncio Future
184184
#_json -I$(srcdir)/Include/internal -DPy_BUILD_CORE_BUILTIN _json.c # _json speedups
185+
#_statistics _statisticsmodule.c # statistics accelerator
185186

186187
#unicodedata unicodedata.c # static Unicode character database
187188

Modules/_statisticsmodule.c

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/* statistics accelerator C extensor: _statistics module. */
2+
3+
#include "Python.h"
4+
#include "structmember.h"
5+
#include "clinic/_statisticsmodule.c.h"
6+
7+
/*[clinic input]
8+
module _statistics
9+
10+
[clinic start generated code]*/
11+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=864a6f59b76123b2]*/
12+
13+
14+
static PyMethodDef speedups_methods[] = {
15+
_STATISTICS__NORMAL_DIST_INV_CDF_METHODDEF
16+
{NULL, NULL, 0, NULL}
17+
};
18+
19+
/*[clinic input]
20+
_statistics._normal_dist_inv_cdf -> double
21+
p: double
22+
mu: double
23+
sigma: double
24+
/
25+
[clinic start generated code]*/
26+
27+
static double
28+
_statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
29+
double sigma)
30+
/*[clinic end generated code: output=02fd19ddaab36602 input=24715a74be15296a]*/
31+
{
32+
double q, num, den, r, x;
33+
q = p - 0.5;
34+
// Algorithm AS 241: The Percentage Points of the Normal Distribution
35+
if(fabs(q) <= 0.425) {
36+
r = 0.180625 - q * q;
37+
// Hash sum AB: 55.88319 28806 14901 4439
38+
num = (((((((2.5090809287301226727e+3 * r +
39+
3.3430575583588128105e+4) * r +
40+
6.7265770927008700853e+4) * r +
41+
4.5921953931549871457e+4) * r +
42+
1.3731693765509461125e+4) * r +
43+
1.9715909503065514427e+3) * r +
44+
1.3314166789178437745e+2) * r +
45+
3.3871328727963666080e+0) * q;
46+
den = (((((((5.2264952788528545610e+3 * r +
47+
2.8729085735721942674e+4) * r +
48+
3.9307895800092710610e+4) * r +
49+
2.1213794301586595867e+4) * r +
50+
5.3941960214247511077e+3) * r +
51+
6.8718700749205790830e+2) * r +
52+
4.2313330701600911252e+1) * r +
53+
1.0);
54+
x = num / den;
55+
return mu + (x * sigma);
56+
}
57+
r = q <= 0.0? p : 1.0-p;
58+
r = sqrt(-log(r));
59+
if (r <= 5.0) {
60+
r = r - 1.6;
61+
// Hash sum CD: 49.33206 50330 16102 89036
62+
num = (((((((7.74545014278341407640e-4 * r +
63+
2.27238449892691845833e-2) * r +
64+
2.41780725177450611770e-1) * r +
65+
1.27045825245236838258e+0) * r +
66+
3.64784832476320460504e+0) * r +
67+
5.76949722146069140550e+0) * r +
68+
4.63033784615654529590e+0) * r +
69+
1.42343711074968357734e+0);
70+
den = (((((((1.05075007164441684324e-9 * r +
71+
5.47593808499534494600e-4) * r +
72+
1.51986665636164571966e-2) * r +
73+
1.48103976427480074590e-1) * r +
74+
6.89767334985100004550e-1) * r +
75+
1.67638483018380384940e+0) * r +
76+
2.05319162663775882187e+0) * r +
77+
1.0);
78+
} else {
79+
r -= 5.0;
80+
// Hash sum EF: 47.52583 31754 92896 71629
81+
num = (((((((2.01033439929228813265e-7 * r +
82+
2.71155556874348757815e-5) * r +
83+
1.24266094738807843860e-3) * r +
84+
2.65321895265761230930e-2) * r +
85+
2.96560571828504891230e-1) * r +
86+
1.78482653991729133580e+0) * r +
87+
5.46378491116411436990e+0) * r +
88+
6.65790464350110377720e+0);
89+
den = (((((((2.04426310338993978564e-15 * r +
90+
1.42151175831644588870e-7) * r +
91+
1.84631831751005468180e-5) * r +
92+
7.86869131145613259100e-4) * r +
93+
1.48753612908506148525e-2) * r +
94+
1.36929880922735805310e-1) * r +
95+
5.99832206555887937690e-1) * r +
96+
1.0);
97+
}
98+
x = num / den;
99+
if (q < 0.0) x = -x;
100+
return mu + (x * sigma);
101+
}
102+
103+
static struct PyModuleDef statisticsmodule = {
104+
PyModuleDef_HEAD_INIT,
105+
"_statistics",
106+
_statistics__normal_dist_inv_cdf__doc__,
107+
-1,
108+
speedups_methods,
109+
NULL,
110+
NULL,
111+
NULL,
112+
NULL
113+
};
114+
115+
116+
PyMODINIT_FUNC
117+
PyInit__statistics(void)
118+
{
119+
PyObject *m = PyModule_Create(&statisticsmodule);
120+
if (!m) return NULL;
121+
return m;
122+
}

Modules/clinic/_statisticsmodule.c.h

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PC/config.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern PyObject* PyInit__sha1(void);
2323
extern PyObject* PyInit__sha256(void);
2424
extern PyObject* PyInit__sha512(void);
2525
extern PyObject* PyInit__sha3(void);
26+
extern PyObject* PyInit__statistics(void);
2627
extern PyObject* PyInit__blake2(void);
2728
extern PyObject* PyInit_time(void);
2829
extern PyObject* PyInit__thread(void);
@@ -103,6 +104,7 @@ struct _inittab _PyImport_Inittab[] = {
103104
{"_blake2", PyInit__blake2},
104105
{"time", PyInit_time},
105106
{"_thread", PyInit__thread},
107+
{"_statistics", PyInit__statistics},
106108
#ifdef WIN32
107109
{"msvcrt", PyInit_msvcrt},
108110
{"_locale", PyInit__locale},

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@
333333
<ClCompile Include="..\Modules\sha256module.c" />
334334
<ClCompile Include="..\Modules\sha512module.c" />
335335
<ClCompile Include="..\Modules\signalmodule.c" />
336+
<ClCompile Include="..\Modules\_statisticsmodule.c" />
336337
<ClCompile Include="..\Modules\symtablemodule.c" />
337338
<ClCompile Include="..\Modules\_threadmodule.c" />
338339
<ClCompile Include="..\Modules\_tracemalloc.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)