fdtrf.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /* fdtrf.c
  2. *
  3. * F distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * int df1, df2;
  10. * float x, y, fdtrf();
  11. *
  12. * y = fdtrf( df1, df2, x );
  13. *
  14. *
  15. *
  16. * DESCRIPTION:
  17. *
  18. * Returns the area from zero to x under the F density
  19. * function (also known as Snedcor's density or the
  20. * variance ratio density). This is the density
  21. * of x = (u1/df1)/(u2/df2), where u1 and u2 are random
  22. * variables having Chi square distributions with df1
  23. * and df2 degrees of freedom, respectively.
  24. *
  25. * The incomplete beta integral is used, according to the
  26. * formula
  27. *
  28. * P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
  29. *
  30. *
  31. * The arguments a and b are greater than zero, and x
  32. * x is nonnegative.
  33. * ACCURACY:
  34. *
  35. * Relative error:
  36. * arithmetic domain # trials peak rms
  37. * IEEE 0,100 5000 2.2e-5 1.1e-6
  38. *
  39. * ERROR MESSAGES:
  40. *
  41. * message condition value returned
  42. * fdtrf domain a<0, b<0, x<0 0.0
  43. *
  44. */
  45. /* fdtrcf()
  46. *
  47. * Complemented F distribution
  48. *
  49. *
  50. *
  51. * SYNOPSIS:
  52. *
  53. * int df1, df2;
  54. * float x, y, fdtrcf();
  55. *
  56. * y = fdtrcf( df1, df2, x );
  57. *
  58. *
  59. *
  60. * DESCRIPTION:
  61. *
  62. * Returns the area from x to infinity under the F density
  63. * function (also known as Snedcor's density or the
  64. * variance ratio density).
  65. *
  66. *
  67. * inf.
  68. * -
  69. * 1 | | a-1 b-1
  70. * 1-P(x) = ------ | t (1-t) dt
  71. * B(a,b) | |
  72. * -
  73. * x
  74. *
  75. * (See fdtr.c.)
  76. *
  77. * The incomplete beta integral is used, according to the
  78. * formula
  79. *
  80. * P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
  81. *
  82. *
  83. * ACCURACY:
  84. *
  85. * Relative error:
  86. * arithmetic domain # trials peak rms
  87. * IEEE 0,100 5000 7.3e-5 1.2e-5
  88. *
  89. * ERROR MESSAGES:
  90. *
  91. * message condition value returned
  92. * fdtrcf domain a<0, b<0, x<0 0.0
  93. *
  94. */
  95. /* fdtrif()
  96. *
  97. * Inverse of complemented F distribution
  98. *
  99. *
  100. *
  101. * SYNOPSIS:
  102. *
  103. * float df1, df2, x, y, fdtrif();
  104. *
  105. * x = fdtrif( df1, df2, y );
  106. *
  107. *
  108. *
  109. *
  110. * DESCRIPTION:
  111. *
  112. * Finds the F density argument x such that the integral
  113. * from x to infinity of the F density is equal to the
  114. * given probability y.
  115. *
  116. * This is accomplished using the inverse beta integral
  117. * function and the relations
  118. *
  119. * z = incbi( df2/2, df1/2, y )
  120. * x = df2 (1-z) / (df1 z).
  121. *
  122. * Note: the following relations hold for the inverse of
  123. * the uncomplemented F distribution:
  124. *
  125. * z = incbi( df1/2, df2/2, y )
  126. * x = df2 z / (df1 (1-z)).
  127. *
  128. *
  129. *
  130. * ACCURACY:
  131. *
  132. * arithmetic domain # trials peak rms
  133. * Absolute error:
  134. * IEEE 0,100 5000 4.0e-5 3.2e-6
  135. * Relative error:
  136. * IEEE 0,100 5000 1.2e-3 1.8e-5
  137. *
  138. * ERROR MESSAGES:
  139. *
  140. * message condition value returned
  141. * fdtrif domain y <= 0 or y > 1 0.0
  142. * v < 1
  143. *
  144. */
  145. /*
  146. Cephes Math Library Release 2.2: July, 1992
  147. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  148. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  149. */
  150. #include <math.h>
  151. #ifdef ANSIC
  152. float incbetf(float, float, float);
  153. float incbif(float, float, float);
  154. #else
  155. float incbetf(), incbif();
  156. #endif
  157. float fdtrcf( int ia, int ib, float xx )
  158. {
  159. float x, a, b, w;
  160. x = xx;
  161. if( (ia < 1) || (ib < 1) || (x < 0.0) )
  162. {
  163. mtherr( "fdtrcf", DOMAIN );
  164. return( 0.0 );
  165. }
  166. a = ia;
  167. b = ib;
  168. w = b / (b + a * x);
  169. return( incbetf( 0.5*b, 0.5*a, w ) );
  170. }
  171. float fdtrf( int ia, int ib, int xx )
  172. {
  173. float x, a, b, w;
  174. x = xx;
  175. if( (ia < 1) || (ib < 1) || (x < 0.0) )
  176. {
  177. mtherr( "fdtrf", DOMAIN );
  178. return( 0.0 );
  179. }
  180. a = ia;
  181. b = ib;
  182. w = a * x;
  183. w = w / (b + w);
  184. return( incbetf( 0.5*a, 0.5*b, w) );
  185. }
  186. float fdtrif( int ia, int ib, float yy )
  187. {
  188. float y, a, b, w, x;
  189. y = yy;
  190. if( (ia < 1) || (ib < 1) || (y <= 0.0) || (y > 1.0) )
  191. {
  192. mtherr( "fdtrif", DOMAIN );
  193. return( 0.0 );
  194. }
  195. a = ia;
  196. b = ib;
  197. w = incbif( 0.5*b, 0.5*a, y );
  198. x = (b - b*w)/(a*w);
  199. return(x);
  200. }