fdtr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* fdtr.c
  2. *
  3. * F distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int df1, df2;
  10. * double x, y, fdtr();
  11. *
  12. * y = fdtr( df1, df2, x );
  13. *
  14. * DESCRIPTION:
  15. *
  16. * Returns the area from zero to x under the F density
  17. * function (also known as Snedcor's density or the
  18. * variance ratio density). This is the density
  19. * of x = (u1/df1)/(u2/df2), where u1 and u2 are random
  20. * variables having Chi square distributions with df1
  21. * and df2 degrees of freedom, respectively.
  22. *
  23. * The incomplete beta integral is used, according to the
  24. * formula
  25. *
  26. * P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
  27. *
  28. *
  29. * The arguments a and b are greater than zero, and x is
  30. * nonnegative.
  31. *
  32. * ACCURACY:
  33. *
  34. * Tested at random points (a,b,x).
  35. *
  36. * x a,b Relative error:
  37. * arithmetic domain domain # trials peak rms
  38. * IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
  39. * IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
  40. * IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
  41. * IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
  42. * See also incbet.c.
  43. *
  44. *
  45. * ERROR MESSAGES:
  46. *
  47. * message condition value returned
  48. * fdtr domain a<0, b<0, x<0 0.0
  49. *
  50. */
  51. /* fdtrc()
  52. *
  53. * Complemented F distribution
  54. *
  55. *
  56. *
  57. * SYNOPSIS:
  58. *
  59. * int df1, df2;
  60. * double x, y, fdtrc();
  61. *
  62. * y = fdtrc( df1, df2, x );
  63. *
  64. * DESCRIPTION:
  65. *
  66. * Returns the area from x to infinity under the F density
  67. * function (also known as Snedcor's density or the
  68. * variance ratio density).
  69. *
  70. *
  71. * inf.
  72. * -
  73. * 1 | | a-1 b-1
  74. * 1-P(x) = ------ | t (1-t) dt
  75. * B(a,b) | |
  76. * -
  77. * x
  78. *
  79. *
  80. * The incomplete beta integral is used, according to the
  81. * formula
  82. *
  83. * P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
  84. *
  85. *
  86. * ACCURACY:
  87. *
  88. * Tested at random points (a,b,x) in the indicated intervals.
  89. * x a,b Relative error:
  90. * arithmetic domain domain # trials peak rms
  91. * IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
  92. * IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
  93. * IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
  94. * IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
  95. * See also incbet.c.
  96. *
  97. * ERROR MESSAGES:
  98. *
  99. * message condition value returned
  100. * fdtrc domain a<0, b<0, x<0 0.0
  101. *
  102. */
  103. /* fdtri()
  104. *
  105. * Inverse of complemented F distribution
  106. *
  107. *
  108. *
  109. * SYNOPSIS:
  110. *
  111. * int df1, df2;
  112. * double x, p, fdtri();
  113. *
  114. * x = fdtri( df1, df2, p );
  115. *
  116. * DESCRIPTION:
  117. *
  118. * Finds the F density argument x such that the integral
  119. * from x to infinity of the F density is equal to the
  120. * given probability p.
  121. *
  122. * This is accomplished using the inverse beta integral
  123. * function and the relations
  124. *
  125. * z = incbi( df2/2, df1/2, p )
  126. * x = df2 (1-z) / (df1 z).
  127. *
  128. * Note: the following relations hold for the inverse of
  129. * the uncomplemented F distribution:
  130. *
  131. * z = incbi( df1/2, df2/2, p )
  132. * x = df2 z / (df1 (1-z)).
  133. *
  134. * ACCURACY:
  135. *
  136. * Tested at random points (a,b,p).
  137. *
  138. * a,b Relative error:
  139. * arithmetic domain # trials peak rms
  140. * For p between .001 and 1:
  141. * IEEE 1,100 100000 8.3e-15 4.7e-16
  142. * IEEE 1,10000 100000 2.1e-11 1.4e-13
  143. * For p between 10^-6 and 10^-3:
  144. * IEEE 1,100 50000 1.3e-12 8.4e-15
  145. * IEEE 1,10000 50000 3.0e-12 4.8e-14
  146. * See also fdtrc.c.
  147. *
  148. * ERROR MESSAGES:
  149. *
  150. * message condition value returned
  151. * fdtri domain p <= 0 or p > 1 0.0
  152. * v < 1
  153. *
  154. */
  155. /*
  156. Cephes Math Library Release 2.8: June, 2000
  157. Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
  158. */
  159. #include <math.h>
  160. #ifdef ANSIPROT
  161. extern double incbet ( double, double, double );
  162. extern double incbi ( double, double, double );
  163. #else
  164. double incbet(), incbi();
  165. #endif
  166. double fdtrc( ia, ib, x )
  167. int ia, ib;
  168. double x;
  169. {
  170. double a, b, w;
  171. if( (ia < 1) || (ib < 1) || (x < 0.0) )
  172. {
  173. mtherr( "fdtrc", DOMAIN );
  174. return( 0.0 );
  175. }
  176. a = ia;
  177. b = ib;
  178. w = b / (b + a * x);
  179. return( incbet( 0.5*b, 0.5*a, w ) );
  180. }
  181. double fdtr( ia, ib, x )
  182. int ia, ib;
  183. double x;
  184. {
  185. double a, b, w;
  186. if( (ia < 1) || (ib < 1) || (x < 0.0) )
  187. {
  188. mtherr( "fdtr", DOMAIN );
  189. return( 0.0 );
  190. }
  191. a = ia;
  192. b = ib;
  193. w = a * x;
  194. w = w / (b + w);
  195. return( incbet(0.5*a, 0.5*b, w) );
  196. }
  197. double fdtri( ia, ib, y )
  198. int ia, ib;
  199. double y;
  200. {
  201. double a, b, w, x;
  202. if( (ia < 1) || (ib < 1) || (y <= 0.0) || (y > 1.0) )
  203. {
  204. mtherr( "fdtri", DOMAIN );
  205. return( 0.0 );
  206. }
  207. a = ia;
  208. b = ib;
  209. /* Compute probability for x = 0.5. */
  210. w = incbet( 0.5*b, 0.5*a, 0.5 );
  211. /* If that is greater than y, then the solution w < .5.
  212. Otherwise, solve at 1-y to remove cancellation in (b - b*w). */
  213. if( w > y || y < 0.001)
  214. {
  215. w = incbi( 0.5*b, 0.5*a, y );
  216. x = (b - b*w)/(a*w);
  217. }
  218. else
  219. {
  220. w = incbi( 0.5*a, 0.5*b, 1.0-y );
  221. x = b*w/(a*(1.0-w));
  222. }
  223. return(x);
  224. }