No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NormalDistributionHelper.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using static System.Math;
  3. namespace Unity.PerformanceTesting.Statistics
  4. {
  5. static class NormalDistributionHelper
  6. {
  7. /// <summary>
  8. /// ACM Algorithm 209: Gauss
  9. ///
  10. /// Calculates $(1/\sqrt{2\pi}) \int_{-\infty}^x e^{-u^2 / 2} du$
  11. /// by means of polynomial approximations due to A. M. Murray of Aberdeen University;
  12. ///
  13. /// See: http://dl.acm.org/citation.cfm?id=367664
  14. /// </summary>
  15. /// <param name="x">-infinity..+infinity</param>
  16. /// <returns>Area under the Standard Normal Curve from -infinity to x</returns>
  17. public static double Gauss(double x)
  18. {
  19. double z;
  20. if (Abs(x) < 1e-9)
  21. z = 0.0;
  22. else
  23. {
  24. var y = Abs(x) / 2;
  25. if (y >= 3.0)
  26. z = 1.0;
  27. else if (y < 1.0)
  28. {
  29. var w = y * y;
  30. z = ((((((((0.000124818987 * w - 0.001075204047) * w
  31. + 0.005198775019) * w - 0.019198292004) * w
  32. + 0.059054035642) * w - 0.151968751364) * w
  33. + 0.319152932694) * w - 0.531923007300) * w
  34. + 0.797884560593) * y * 2.0;
  35. }
  36. else
  37. {
  38. y = y - 2.0;
  39. z = (((((((((((((-0.000045255659 * y + 0.000152529290) * y
  40. - 0.000019538132) * y - 0.000676904986) * y
  41. + 0.001390604284) * y - 0.000794620820) * y
  42. - 0.002034254874) * y + 0.006549791214) * y
  43. - 0.010557625006) * y + 0.011630447319) * y
  44. - 0.009279453341) * y + 0.005353579108) * y
  45. - 0.002141268741) * y + 0.000535310849) * y
  46. + 0.999936657524;
  47. }
  48. }
  49. return x > 0.0 ? (z + 1.0) / 2 : (1.0 - z) / 2;
  50. }
  51. }
  52. }