stdtr.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* stdtr.c
  2. *
  3. * Student's t distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * double t, stdtr();
  10. * short k;
  11. *
  12. * y = stdtr( 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 < -2, 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. * Tested at random 1 <= k <= 25. The "domain" refers to t.
  47. * Relative error:
  48. * arithmetic domain # trials peak rms
  49. * IEEE -100,-2 50000 5.9e-15 1.4e-15
  50. * IEEE -2,100 500000 2.7e-15 4.9e-17
  51. */
  52. /* stdtri.c
  53. *
  54. * Functional inverse of Student's t distribution
  55. *
  56. *
  57. *
  58. * SYNOPSIS:
  59. *
  60. * double p, t, stdtri();
  61. * int k;
  62. *
  63. * t = stdtri( k, p );
  64. *
  65. *
  66. * DESCRIPTION:
  67. *
  68. * Given probability p, finds the argument t such that stdtr(k,t)
  69. * is equal to p.
  70. *
  71. * ACCURACY:
  72. *
  73. * Tested at random 1 <= k <= 100. The "domain" refers to p:
  74. * Relative error:
  75. * arithmetic domain # trials peak rms
  76. * IEEE .001,.999 25000 5.7e-15 8.0e-16
  77. * IEEE 10^-6,.001 25000 2.0e-12 2.9e-14
  78. */
  79. /*
  80. Cephes Math Library Release 2.8: June, 2000
  81. Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
  82. */
  83. #include <math.h>
  84. extern double PI, MACHEP, MAXNUM;
  85. #ifdef ANSIPROT
  86. extern double sqrt ( double );
  87. extern double atan ( double );
  88. extern double incbet ( double, double, double );
  89. extern double incbi ( double, double, double );
  90. extern double fabs ( double );
  91. #else
  92. double sqrt(), atan(), incbet(), incbi(), fabs();
  93. #endif
  94. double stdtr( k, t )
  95. int k;
  96. double t;
  97. {
  98. double x, rk, z, f, tz, p, xsqk;
  99. int j;
  100. if( k <= 0 )
  101. {
  102. mtherr( "stdtr", DOMAIN );
  103. return(0.0);
  104. }
  105. if( t == 0 )
  106. return( 0.5 );
  107. if( t < -2.0 )
  108. {
  109. rk = k;
  110. z = rk / (rk + t * t);
  111. p = 0.5 * incbet( 0.5*rk, 0.5, z );
  112. return( p );
  113. }
  114. /* compute integral from -t to + t */
  115. if( t < 0 )
  116. x = -t;
  117. else
  118. x = t;
  119. rk = k; /* degrees of freedom */
  120. z = 1.0 + ( x * x )/rk;
  121. /* test if k is odd or even */
  122. if( (k & 1) != 0)
  123. {
  124. /* computation for odd k */
  125. xsqk = x/sqrt(rk);
  126. p = atan( xsqk );
  127. if( k > 1 )
  128. {
  129. f = 1.0;
  130. tz = 1.0;
  131. j = 3;
  132. while( (j<=(k-2)) && ( (tz/f) > MACHEP ) )
  133. {
  134. tz *= (j-1)/( z * j );
  135. f += tz;
  136. j += 2;
  137. }
  138. p += f * xsqk/z;
  139. }
  140. p *= 2.0/PI;
  141. }
  142. else
  143. {
  144. /* computation for even k */
  145. f = 1.0;
  146. tz = 1.0;
  147. j = 2;
  148. while( ( j <= (k-2) ) && ( (tz/f) > MACHEP ) )
  149. {
  150. tz *= (j - 1)/( z * j );
  151. f += tz;
  152. j += 2;
  153. }
  154. p = f * x/sqrt(z*rk);
  155. }
  156. /* common exit */
  157. if( t < 0 )
  158. p = -p; /* note destruction of relative accuracy */
  159. p = 0.5 + 0.5 * p;
  160. return(p);
  161. }
  162. double stdtri( k, p )
  163. int k;
  164. double p;
  165. {
  166. double t, rk, z;
  167. int rflg;
  168. if( k <= 0 || p <= 0.0 || p >= 1.0 )
  169. {
  170. mtherr( "stdtri", DOMAIN );
  171. return(0.0);
  172. }
  173. rk = k;
  174. if( p > 0.25 && p < 0.75 )
  175. {
  176. if( p == 0.5 )
  177. return( 0.0 );
  178. z = 1.0 - 2.0 * p;
  179. z = incbi( 0.5, 0.5*rk, fabs(z) );
  180. t = sqrt( rk*z/(1.0-z) );
  181. if( p < 0.5 )
  182. t = -t;
  183. return( t );
  184. }
  185. rflg = -1;
  186. if( p >= 0.5)
  187. {
  188. p = 1.0 - p;
  189. rflg = 1;
  190. }
  191. z = incbi( 0.5*rk, 0.5, 2.0*p );
  192. if( MAXNUM * z < rk )
  193. return(rflg* MAXNUM);
  194. t = sqrt( rk/z - rk );
  195. return( rflg * t );
  196. }