blob: 16a992485237abe7c769ce76bcadf02d8d5355e6 [file] [log] [blame]
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -07001/* statistics accelerator C extension: _statistics module. */
Miss Islington (bot)5779c532019-08-23 15:39:27 -07002
3#include "Python.h"
4#include "structmember.h"
5#include "clinic/_statisticsmodule.c.h"
6
7/*[clinic input]
8module _statistics
9
10[clinic start generated code]*/
11/*[clinic end generated code: output=da39a3ee5e6b4b0d input=864a6f59b76123b2]*/
12
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -070013/*
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 */
Miss Islington (bot)5779c532019-08-23 15:39:27 -070020
21/*[clinic input]
22_statistics._normal_dist_inv_cdf -> double
23 p: double
24 mu: double
25 sigma: double
26 /
27[clinic start generated code]*/
28
29static double
30_statistics__normal_dist_inv_cdf_impl(PyObject *module, double p, double mu,
31 double sigma)
32/*[clinic end generated code: output=02fd19ddaab36602 input=24715a74be15296a]*/
33{
34 double q, num, den, r, x;
35 q = p - 0.5;
36 // Algorithm AS 241: The Percentage Points of the Normal Distribution
37 if(fabs(q) <= 0.425) {
38 r = 0.180625 - q * q;
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -070039 // Hash sum-55.8831928806149014439
Miss Islington (bot)5779c532019-08-23 15:39:27 -070040 num = (((((((2.5090809287301226727e+3 * r +
41 3.3430575583588128105e+4) * r +
42 6.7265770927008700853e+4) * r +
43 4.5921953931549871457e+4) * r +
44 1.3731693765509461125e+4) * r +
45 1.9715909503065514427e+3) * r +
46 1.3314166789178437745e+2) * r +
47 3.3871328727963666080e+0) * q;
48 den = (((((((5.2264952788528545610e+3 * r +
49 2.8729085735721942674e+4) * r +
50 3.9307895800092710610e+4) * r +
51 2.1213794301586595867e+4) * r +
52 5.3941960214247511077e+3) * r +
53 6.8718700749205790830e+2) * r +
54 4.2313330701600911252e+1) * r +
55 1.0);
56 x = num / den;
57 return mu + (x * sigma);
58 }
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -070059 r = (q <= 0.0) ? p : (1.0 - p);
Miss Islington (bot)5779c532019-08-23 15:39:27 -070060 r = sqrt(-log(r));
61 if (r <= 5.0) {
62 r = r - 1.6;
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -070063 // Hash sum-49.33206503301610289036
Miss Islington (bot)5779c532019-08-23 15:39:27 -070064 num = (((((((7.74545014278341407640e-4 * r +
65 2.27238449892691845833e-2) * r +
66 2.41780725177450611770e-1) * r +
67 1.27045825245236838258e+0) * r +
68 3.64784832476320460504e+0) * r +
69 5.76949722146069140550e+0) * r +
70 4.63033784615654529590e+0) * r +
71 1.42343711074968357734e+0);
72 den = (((((((1.05075007164441684324e-9 * r +
73 5.47593808499534494600e-4) * r +
74 1.51986665636164571966e-2) * r +
75 1.48103976427480074590e-1) * r +
76 6.89767334985100004550e-1) * r +
77 1.67638483018380384940e+0) * r +
78 2.05319162663775882187e+0) * r +
79 1.0);
80 } else {
81 r -= 5.0;
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -070082 // Hash sum-47.52583317549289671629
Miss Islington (bot)5779c532019-08-23 15:39:27 -070083 num = (((((((2.01033439929228813265e-7 * r +
84 2.71155556874348757815e-5) * r +
85 1.24266094738807843860e-3) * r +
86 2.65321895265761230930e-2) * r +
87 2.96560571828504891230e-1) * r +
88 1.78482653991729133580e+0) * r +
89 5.46378491116411436990e+0) * r +
90 6.65790464350110377720e+0);
91 den = (((((((2.04426310338993978564e-15 * r +
92 1.42151175831644588870e-7) * r +
93 1.84631831751005468180e-5) * r +
94 7.86869131145613259100e-4) * r +
95 1.48753612908506148525e-2) * r +
96 1.36929880922735805310e-1) * r +
97 5.99832206555887937690e-1) * r +
98 1.0);
99 }
100 x = num / den;
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -0700101 if (q < 0.0) {
102 x = -x;
103 }
Miss Islington (bot)5779c532019-08-23 15:39:27 -0700104 return mu + (x * sigma);
105}
106
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -0700107
108static PyMethodDef statistics_methods[] = {
109 _STATISTICS__NORMAL_DIST_INV_CDF_METHODDEF
110 {NULL, NULL, 0, NULL}
111};
112
Miss Islington (bot)58067d22019-09-03 03:21:59 -0700113PyDoc_STRVAR(statistics_doc,
114"Accelerators for the statistics module.\n");
115
Miss Islington (bot)5779c532019-08-23 15:39:27 -0700116static struct PyModuleDef statisticsmodule = {
117 PyModuleDef_HEAD_INIT,
118 "_statistics",
Miss Islington (bot)58067d22019-09-03 03:21:59 -0700119 statistics_doc,
Miss Islington (bot)5779c532019-08-23 15:39:27 -0700120 -1,
Miss Islington (bot)56c4d2d2019-08-26 12:10:00 -0700121 statistics_methods,
Miss Islington (bot)5779c532019-08-23 15:39:27 -0700122 NULL,
123 NULL,
124 NULL,
125 NULL
126};
127
Miss Islington (bot)5779c532019-08-23 15:39:27 -0700128PyMODINIT_FUNC
129PyInit__statistics(void)
130{
131 PyObject *m = PyModule_Create(&statisticsmodule);
132 if (!m) return NULL;
133 return m;
134}