chdtrf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* chdtrf.c
  2. *
  3. * Chi-square distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * float df, x, y, chdtrf();
  10. *
  11. * y = chdtrf( df, x );
  12. *
  13. *
  14. *
  15. * DESCRIPTION:
  16. *
  17. * Returns the area under the left hand tail (from 0 to x)
  18. * of the Chi square probability density function with
  19. * v degrees of freedom.
  20. *
  21. *
  22. * inf.
  23. * -
  24. * 1 | | v/2-1 -t/2
  25. * P( x | v ) = ----------- | t e dt
  26. * v/2 - | |
  27. * 2 | (v/2) -
  28. * x
  29. *
  30. * where x is the Chi-square variable.
  31. *
  32. * The incomplete gamma integral is used, according to the
  33. * formula
  34. *
  35. * y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
  36. *
  37. *
  38. * The arguments must both be positive.
  39. *
  40. *
  41. *
  42. * ACCURACY:
  43. *
  44. * Relative error:
  45. * arithmetic domain # trials peak rms
  46. * IEEE 0,100 5000 3.2e-5 5.0e-6
  47. *
  48. * ERROR MESSAGES:
  49. *
  50. * message condition value returned
  51. * chdtrf domain x < 0 or v < 1 0.0
  52. */
  53. /* chdtrcf()
  54. *
  55. * Complemented Chi-square distribution
  56. *
  57. *
  58. *
  59. * SYNOPSIS:
  60. *
  61. * float v, x, y, chdtrcf();
  62. *
  63. * y = chdtrcf( v, x );
  64. *
  65. *
  66. *
  67. * DESCRIPTION:
  68. *
  69. * Returns the area under the right hand tail (from x to
  70. * infinity) of the Chi square probability density function
  71. * with v degrees of freedom:
  72. *
  73. *
  74. * inf.
  75. * -
  76. * 1 | | v/2-1 -t/2
  77. * P( x | v ) = ----------- | t e dt
  78. * v/2 - | |
  79. * 2 | (v/2) -
  80. * x
  81. *
  82. * where x is the Chi-square variable.
  83. *
  84. * The incomplete gamma integral is used, according to the
  85. * formula
  86. *
  87. * y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
  88. *
  89. *
  90. * The arguments must both be positive.
  91. *
  92. *
  93. *
  94. * ACCURACY:
  95. *
  96. * Relative error:
  97. * arithmetic domain # trials peak rms
  98. * IEEE 0,100 5000 2.7e-5 3.2e-6
  99. *
  100. * ERROR MESSAGES:
  101. *
  102. * message condition value returned
  103. * chdtrc domain x < 0 or v < 1 0.0
  104. */
  105. /* chdtrif()
  106. *
  107. * Inverse of complemented Chi-square distribution
  108. *
  109. *
  110. *
  111. * SYNOPSIS:
  112. *
  113. * float df, x, y, chdtrif();
  114. *
  115. * x = chdtrif( df, y );
  116. *
  117. *
  118. *
  119. *
  120. * DESCRIPTION:
  121. *
  122. * Finds the Chi-square argument x such that the integral
  123. * from x to infinity of the Chi-square density is equal
  124. * to the given cumulative probability y.
  125. *
  126. * This is accomplished using the inverse gamma integral
  127. * function and the relation
  128. *
  129. * x/2 = igami( df/2, y );
  130. *
  131. *
  132. *
  133. *
  134. * ACCURACY:
  135. *
  136. * Relative error:
  137. * arithmetic domain # trials peak rms
  138. * IEEE 0,100 10000 2.2e-5 8.5e-7
  139. *
  140. * ERROR MESSAGES:
  141. *
  142. * message condition value returned
  143. * chdtri domain y < 0 or y > 1 0.0
  144. * v < 1
  145. *
  146. */
  147. /* chdtr() */
  148. /*
  149. Cephes Math Library Release 2.2: July, 1992
  150. Copyright 1984, 1987, 1992 by Stephen L. Moshier
  151. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  152. */
  153. #include <math.h>
  154. #ifdef ANSIC
  155. float igamcf(float, float), igamf(float, float), igamif(float, float);
  156. #else
  157. float igamcf(), igamf(), igamif();
  158. #endif
  159. float chdtrcf(float dff, float xx)
  160. {
  161. float df, x;
  162. df = dff;
  163. x = xx;
  164. if( (x < 0.0) || (df < 1.0) )
  165. {
  166. mtherr( "chdtrcf", DOMAIN );
  167. return(0.0);
  168. }
  169. return( igamcf( 0.5*df, 0.5*x ) );
  170. }
  171. float chdtrf(float dff, float xx)
  172. {
  173. float df, x;
  174. df = dff;
  175. x = xx;
  176. if( (x < 0.0) || (df < 1.0) )
  177. {
  178. mtherr( "chdtrf", DOMAIN );
  179. return(0.0);
  180. }
  181. return( igamf( 0.5*df, 0.5*x ) );
  182. }
  183. float chdtrif( float dff, float yy )
  184. {
  185. float y, df, x;
  186. y = yy;
  187. df = dff;
  188. if( (y < 0.0) || (y > 1.0) || (df < 1.0) )
  189. {
  190. mtherr( "chdtrif", DOMAIN );
  191. return(0.0);
  192. }
  193. x = igamif( 0.5 * df, y );
  194. return( 2.0 * x );
  195. }