暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StudentDistributionHelper.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using static System.Math;
  3. namespace Unity.PerformanceTesting.Statistics
  4. {
  5. static class StudentDistributionHelper
  6. {
  7. public static double InverseTwoTailedStudent(double p, double n)
  8. {
  9. var lower = 0.0;
  10. var upper = 1000.0;
  11. while (upper - lower > 1e-9)
  12. {
  13. var t = (lower + upper) / 2;
  14. var p2 = TwoTailedStudent(t, n);
  15. if (p2 < p)
  16. upper = t;
  17. else
  18. lower = t;
  19. }
  20. return (lower + upper) / 2;
  21. }
  22. /// <summary>
  23. /// ACM Algorithm 395: Student's t-distribution
  24. ///
  25. /// Evaluates the two-tail probability P(t|n) that t is exceeded
  26. /// in magnitude for Student's t-distribution with n degrees of freedom.
  27. ///
  28. /// http://dl.acm.org/citation.cfm?id=355599
  29. /// </summary>
  30. /// <param name="t">t-value, t > 0</param>
  31. /// <param name="n">Degree of freedom, n >= 1</param>
  32. /// <returns>2-tail p-value</returns>
  33. static double TwoTailedStudent(double t, double n)
  34. {
  35. if (t < 0)
  36. throw new ArgumentOutOfRangeException(nameof(t), "t should be >= 0");
  37. if (n < 1)
  38. throw new ArgumentOutOfRangeException(nameof(n), "n should be >= 1");
  39. t = t * t;
  40. var y = t / n;
  41. var b = y + 1.0;
  42. var nn = (int)Round(n);
  43. if (Abs(n - nn) > 1e-9 || n >= 20 || t < n && n > 200)
  44. {
  45. if (y > 1.0e-6)
  46. y = Log(b);
  47. var a = n - 0.5;
  48. b = 48.0 * (a * a);
  49. y = a * y;
  50. y = (((((-0.4 * y - 3.3) * y - 24.0) * y - 85.5) / (0.8 * (y * y) + 100.0 + b) + y + 3.0) / b + 1.0) * Sqrt(y);
  51. return 2 * NormalDistributionHelper.Gauss(-y);
  52. }
  53. {
  54. double z = 1;
  55. double a;
  56. if (n < 20 && t < 4.0)
  57. {
  58. y = Sqrt(y);
  59. a = y;
  60. if (nn == 1)
  61. a = 0;
  62. }
  63. else
  64. {
  65. a = Sqrt(b);
  66. y = a * nn;
  67. var j = 0;
  68. while (Abs(a - z) > 0)
  69. {
  70. j += 2;
  71. z = a;
  72. y *= (j - 1) / (b * j);
  73. a += y / (nn + j);
  74. }
  75. nn += 2;
  76. z = 0;
  77. y = 0;
  78. a = -a;
  79. }
  80. while (true)
  81. {
  82. nn -= 2;
  83. if (nn > 1)
  84. a = (nn - 1) / (b * nn) * a + y;
  85. else
  86. break;
  87. }
  88. a = nn == 0 ? a / Sqrt(b) : (Atan(y) + a / b) * 2 / PI;
  89. return z - a;
  90. }
  91. }
  92. }
  93. }