1
- /* statistics accelerator C extensor : _statistics module. */
1
+ /* statistics accelerator C extension : _statistics module. */
2
2
3
3
#include "Python.h"
4
4
#include "structmember.h"
@@ -10,11 +10,13 @@ module _statistics
10
10
[clinic start generated code]*/
11
11
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=864a6f59b76123b2]*/
12
12
13
-
14
- static PyMethodDef speedups_methods [] = {
15
- _STATISTICS__NORMAL_DIST_INV_CDF_METHODDEF
16
- {NULL , NULL , 0 , NULL }
17
- };
13
+ /*
14
+ * There is no closed-form solution to the inverse CDF for the normal
15
+ * distribution, so we use a rational approximation instead:
16
+ * Wichura, M.J. (1988). "Algorithm AS241: The Percentage Points of the
17
+ * Normal Distribution". Applied Statistics. Blackwell Publishing. 37
18
+ * (3): 477–484. doi:10.2307/2347330. JSTOR 2347330.
19
+ */
18
20
19
21
/*[clinic input]
20
22
_statistics._normal_dist_inv_cdf -> double
@@ -34,7 +36,7 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
34
36
// Algorithm AS 241: The Percentage Points of the Normal Distribution
35
37
if (fabs (q ) <= 0.425 ) {
36
38
r = 0.180625 - q * q ;
37
- // Hash sum AB: 55.88319 28806 14901 4439
39
+ // Hash sum- 55.8831928806149014439
38
40
num = (((((((2.5090809287301226727e+3 * r +
39
41
3.3430575583588128105e+4 ) * r +
40
42
6.7265770927008700853e+4 ) * r +
@@ -54,11 +56,11 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
54
56
x = num / den ;
55
57
return mu + (x * sigma );
56
58
}
57
- r = q <= 0.0 ? p : 1.0 - p ;
59
+ r = ( q <= 0.0 ) ? p : ( 1.0 - p ) ;
58
60
r = sqrt (- log (r ));
59
61
if (r <= 5.0 ) {
60
62
r = r - 1.6 ;
61
- // Hash sum CD: 49.33206 50330 16102 89036
63
+ // Hash sum- 49.33206503301610289036
62
64
num = (((((((7.74545014278341407640e-4 * r +
63
65
2.27238449892691845833e-2 ) * r +
64
66
2.41780725177450611770e-1 ) * r +
@@ -77,7 +79,7 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
77
79
1.0 );
78
80
} else {
79
81
r -= 5.0 ;
80
- // Hash sum EF: 47.52583 31754 92896 71629
82
+ // Hash sum- 47.52583317549289671629
81
83
num = (((((((2.01033439929228813265e-7 * r +
82
84
2.71155556874348757815e-5 ) * r +
83
85
1.24266094738807843860e-3 ) * r +
@@ -96,23 +98,30 @@ _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
96
98
1.0 );
97
99
}
98
100
x = num / den ;
99
- if (q < 0.0 ) x = - x ;
101
+ if (q < 0.0 ) {
102
+ x = - x ;
103
+ }
100
104
return mu + (x * sigma );
101
105
}
102
106
107
+
108
+ static PyMethodDef statistics_methods [] = {
109
+ _STATISTICS__NORMAL_DIST_INV_CDF_METHODDEF
110
+ {NULL , NULL , 0 , NULL }
111
+ };
112
+
103
113
static struct PyModuleDef statisticsmodule = {
104
114
PyModuleDef_HEAD_INIT ,
105
115
"_statistics" ,
106
116
_statistics__normal_dist_inv_cdf__doc__ ,
107
117
-1 ,
108
- speedups_methods ,
118
+ statistics_methods ,
109
119
NULL ,
110
120
NULL ,
111
121
NULL ,
112
122
NULL
113
123
};
114
124
115
-
116
125
PyMODINIT_FUNC
117
126
PyInit__statistics (void )
118
127
{
0 commit comments