_statisticsmodule.c 4.9 KB

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