stdtrf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* stdtrf.c
  2. *
  3. * Student's t distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float t, stdtrf();
  10. * short k;
  11. *
  12. * y = stdtrf( k, t );
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Computes the integral from minus infinity to t of the Student
  18. * t distribution with integer k > 0 degrees of freedom:
  19. *
  20. * t
  21. * -
  22. * | |
  23. * - | 2 -(k+1)/2
  24. * | ( (k+1)/2 ) | ( x )
  25. * ---------------------- | ( 1 + --- ) dx
  26. * - | ( k )
  27. * sqrt( k pi ) | ( k/2 ) |
  28. * | |
  29. * -
  30. * -inf.
  31. *
  32. * Relation to incomplete beta integral:
  33. *
  34. * 1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
  35. * where
  36. * z = k/(k + t**2).
  37. *
  38. * For t < -1, this is the method of computation. For higher t,
  39. * a direct method is derived from integration by parts.
  40. * Since the function is symmetric about t=0, the area under the
  41. * right tail of the density is found by calling the function
  42. * with -t instead of t.
  43. *
  44. * ACCURACY:
  45. *
  46. * Relative error:
  47. * arithmetic domain # trials peak rms
  48. * IEEE +/- 100 5000 2.3e-5 2.9e-6
  49. */
  50. /*
  51. Cephes Math Library Release 2.2: July, 1992
  52. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  53. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  54. */
  55. #include <math.h>
  56. extern float PIF, MACHEPF;
  57. #ifdef ANSIC
  58. float sqrtf(float), atanf(float), incbetf(float, float, float);
  59. #else
  60. float sqrtf(), atanf(), incbetf();
  61. #endif
  62. float stdtrf( int k, float tt )
  63. {
  64. float t, x, rk, z, f, tz, p, xsqk;
  65. int j;
  66. t = tt;
  67. if( k <= 0 )
  68. {
  69. mtherr( "stdtrf", DOMAIN );
  70. return(0.0);
  71. }
  72. if( t == 0 )
  73. return( 0.5 );
  74. if( t < -1.0 )
  75. {
  76. rk = k;
  77. z = rk / (rk + t * t);
  78. p = 0.5 * incbetf( 0.5*rk, 0.5, z );
  79. return( p );
  80. }
  81. /* compute integral from -t to + t */
  82. if( t < 0 )
  83. x = -t;
  84. else
  85. x = t;
  86. rk = k; /* degrees of freedom */
  87. z = 1.0 + ( x * x )/rk;
  88. /* test if k is odd or even */
  89. if( (k & 1) != 0)
  90. {
  91. /* computation for odd k */
  92. xsqk = x/sqrtf(rk);
  93. p = atanf( xsqk );
  94. if( k > 1 )
  95. {
  96. f = 1.0;
  97. tz = 1.0;
  98. j = 3;
  99. while( (j<=(k-2)) && ( (tz/f) > MACHEPF ) )
  100. {
  101. tz *= (j-1)/( z * j );
  102. f += tz;
  103. j += 2;
  104. }
  105. p += f * xsqk/z;
  106. }
  107. p *= 2.0/PIF;
  108. }
  109. else
  110. {
  111. /* computation for even k */
  112. f = 1.0;
  113. tz = 1.0;
  114. j = 2;
  115. while( ( j <= (k-2) ) && ( (tz/f) > MACHEPF ) )
  116. {
  117. tz *= (j - 1)/( z * j );
  118. f += tz;
  119. j += 2;
  120. }
  121. p = f * x/sqrtf(z*rk);
  122. }
  123. /* common exit */
  124. if( t < 0 )
  125. p = -p; /* note destruction of relative accuracy */
  126. p = 0.5 + 0.5 * p;
  127. return(p);
  128. }