1 /* statistics accelerator C extension: _statistics module. */
2
3 // Need limited C API version 3.13 for Py_mod_gil
4 #include "pyconfig.h" // Py_GIL_DISABLED
5 #ifndef Py_GIL_DISABLED
6 # define Py_LIMITED_API 0x030d0000
7 #endif
8
9 #include "Python.h"
10 #include "clinic/_statisticsmodule.c.h"
11
12 /*[clinic input]
13 module _statistics
14
15 [clinic start generated code]*/
16 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=864a6f59b76123b2]*/
17
18 /*
19 * There is no closed-form solution to the inverse CDF for the normal
20 * distribution, so we use a rational approximation instead:
21 * Wichura, M.J. (1988). "Algorithm AS241: The Percentage Points of the
22 * Normal Distribution". Applied Statistics. Blackwell Publishing. 37
23 * (3): 477–484. doi:10.2307/2347330. JSTOR 2347330.
24 */
25
26 /*[clinic input]
27 _statistics._normal_dist_inv_cdf -> double
28 p: double
29 mu: double
30 sigma: double
31 /
32 [clinic start generated code]*/
33
34 static double
_statistics__normal_dist_inv_cdf_impl(PyObject * module,double p,double mu,double sigma)35 _statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
36 double sigma)
37 /*[clinic end generated code: output=02fd19ddaab36602 input=24715a74be15296a]*/
38 {
39 double q, num, den, r, x;
40 if (p <= 0.0 || p >= 1.0) {
41 goto error;
42 }
43
44 q = p - 0.5;
45 if(fabs(q) <= 0.425) {
46 r = 0.180625 - q * q;
47 // Hash sum-55.8831928806149014439
48 num = (((((((2.5090809287301226727e+3 * r +
49 3.3430575583588128105e+4) * r +
50 6.7265770927008700853e+4) * r +
51 4.5921953931549871457e+4) * r +
52 1.3731693765509461125e+4) * r +
53 1.9715909503065514427e+3) * r +
54 1.3314166789178437745e+2) * r +
55 3.3871328727963666080e+0) * q;
56 den = (((((((5.2264952788528545610e+3 * r +
57 2.8729085735721942674e+4) * r +
58 3.9307895800092710610e+4) * r +
59 2.1213794301586595867e+4) * r +
60 5.3941960214247511077e+3) * r +
61 6.8718700749205790830e+2) * r +
62 4.2313330701600911252e+1) * r +
63 1.0);
64 if (den == 0.0) {
65 goto error;
66 }
67 x = num / den;
68 return mu + (x * sigma);
69 }
70 r = (q <= 0.0) ? p : (1.0 - p);
71 if (r <= 0.0 || r >= 1.0) {
72 goto error;
73 }
74 r = sqrt(-log(r));
75 if (r <= 5.0) {
76 r = r - 1.6;
77 // Hash sum-49.33206503301610289036
78 num = (((((((7.74545014278341407640e-4 * r +
79 2.27238449892691845833e-2) * r +
80 2.41780725177450611770e-1) * r +
81 1.27045825245236838258e+0) * r +
82 3.64784832476320460504e+0) * r +
83 5.76949722146069140550e+0) * r +
84 4.63033784615654529590e+0) * r +
85 1.42343711074968357734e+0);
86 den = (((((((1.05075007164441684324e-9 * r +
87 5.47593808499534494600e-4) * r +
88 1.51986665636164571966e-2) * r +
89 1.48103976427480074590e-1) * r +
90 6.89767334985100004550e-1) * r +
91 1.67638483018380384940e+0) * r +
92 2.05319162663775882187e+0) * r +
93 1.0);
94 } else {
95 r -= 5.0;
96 // Hash sum-47.52583317549289671629
97 num = (((((((2.01033439929228813265e-7 * r +
98 2.71155556874348757815e-5) * r +
99 1.24266094738807843860e-3) * r +
100 2.65321895265761230930e-2) * r +
101 2.96560571828504891230e-1) * r +
102 1.78482653991729133580e+0) * r +
103 5.46378491116411436990e+0) * r +
104 6.65790464350110377720e+0);
105 den = (((((((2.04426310338993978564e-15 * r +
106 1.42151175831644588870e-7) * r +
107 1.84631831751005468180e-5) * r +
108 7.86869131145613259100e-4) * r +
109 1.48753612908506148525e-2) * r +
110 1.36929880922735805310e-1) * r +
111 5.99832206555887937690e-1) * r +
112 1.0);
113 }
114 if (den == 0.0) {
115 goto error;
116 }
117 x = num / den;
118 if (q < 0.0) {
119 x = -x;
120 }
121 return mu + (x * sigma);
122
123 error:
124 PyErr_SetString(PyExc_ValueError, "inv_cdf undefined for these parameters");
125 return -1.0;
126 }
127
128
129 static PyMethodDef statistics_methods[] = {
130 _STATISTICS__NORMAL_DIST_INV_CDF_METHODDEF
131 {NULL, NULL, 0, NULL}
132 };
133
134 PyDoc_STRVAR(statistics_doc,
135 "Accelerators for the statistics module.\n");
136
137 static struct PyModuleDef_Slot _statisticsmodule_slots[] = {
138 {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
139 {Py_mod_gil, Py_MOD_GIL_NOT_USED},
140 {0, NULL}
141 };
142
143 static struct PyModuleDef statisticsmodule = {
144 PyModuleDef_HEAD_INIT,
145 "_statistics",
146 statistics_doc,
147 0,
148 statistics_methods,
149 _statisticsmodule_slots,
150 NULL,
151 NULL,
152 NULL
153 };
154
155 PyMODINIT_FUNC
PyInit__statistics(void)156 PyInit__statistics(void)
157 {
158 return PyModuleDef_Init(&statisticsmodule);
159 }
160