chdtrl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* chdtrl.c
  2. *
  3. * Chi-square distribution
  4. *
  5. *
  6. *
  7. * SYNOPSIS:
  8. *
  9. * long double df, x, y, chdtrl();
  10. *
  11. * y = chdtrl( 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. * See igam().
  45. *
  46. * ERROR MESSAGES:
  47. *
  48. * message condition value returned
  49. * chdtr domain x < 0 or v < 1 0.0
  50. */
  51. /* chdtrcl()
  52. *
  53. * Complemented Chi-square distribution
  54. *
  55. *
  56. *
  57. * SYNOPSIS:
  58. *
  59. * long double v, x, y, chdtrcl();
  60. *
  61. * y = chdtrcl( v, x );
  62. *
  63. *
  64. *
  65. * DESCRIPTION:
  66. *
  67. * Returns the area under the right hand tail (from x to
  68. * infinity) of the Chi square probability density function
  69. * with v degrees of freedom:
  70. *
  71. *
  72. * inf.
  73. * -
  74. * 1 | | v/2-1 -t/2
  75. * P( x | v ) = ----------- | t e dt
  76. * v/2 - | |
  77. * 2 | (v/2) -
  78. * x
  79. *
  80. * where x is the Chi-square variable.
  81. *
  82. * The incomplete gamma integral is used, according to the
  83. * formula
  84. *
  85. * y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
  86. *
  87. *
  88. * The arguments must both be positive.
  89. *
  90. *
  91. *
  92. * ACCURACY:
  93. *
  94. * See igamc().
  95. *
  96. * ERROR MESSAGES:
  97. *
  98. * message condition value returned
  99. * chdtrc domain x < 0 or v < 1 0.0
  100. */
  101. /* chdtril()
  102. *
  103. * Inverse of complemented Chi-square distribution
  104. *
  105. *
  106. *
  107. * SYNOPSIS:
  108. *
  109. * long double df, x, y, chdtril();
  110. *
  111. * x = chdtril( df, y );
  112. *
  113. *
  114. *
  115. *
  116. * DESCRIPTION:
  117. *
  118. * Finds the Chi-square argument x such that the integral
  119. * from x to infinity of the Chi-square density is equal
  120. * to the given cumulative probability y.
  121. *
  122. * This is accomplished using the inverse gamma integral
  123. * function and the relation
  124. *
  125. * x/2 = igami( df/2, y );
  126. *
  127. *
  128. *
  129. *
  130. * ACCURACY:
  131. *
  132. * See igami.c.
  133. *
  134. * ERROR MESSAGES:
  135. *
  136. * message condition value returned
  137. * chdtri domain y < 0 or y > 1 0.0
  138. * v < 1
  139. *
  140. */
  141. /* chdtr() */
  142. /*
  143. Cephes Math Library Release 2.3: March, 1995
  144. Copyright 1984, 1995 by Stephen L. Moshier
  145. */
  146. #include <math.h>
  147. #ifdef ANSIPROT
  148. extern long double igamcl ( long double, long double );
  149. extern long double igaml ( long double, long double );
  150. extern long double igamil ( long double, long double );
  151. #else
  152. long double igamcl(), igaml(), igamil();
  153. #endif
  154. long double chdtrcl(df,x)
  155. long double df, x;
  156. {
  157. if( (x < 0.0L) || (df < 1.0L) )
  158. {
  159. mtherr( "chdtrcl", DOMAIN );
  160. return(0.0L);
  161. }
  162. return( igamcl( 0.5L*df, 0.5L*x ) );
  163. }
  164. long double chdtrl(df,x)
  165. long double df, x;
  166. {
  167. if( (x < 0.0L) || (df < 1.0L) )
  168. {
  169. mtherr( "chdtrl", DOMAIN );
  170. return(0.0L);
  171. }
  172. return( igaml( 0.5L*df, 0.5L*x ) );
  173. }
  174. long double chdtril( df, y )
  175. long double df, y;
  176. {
  177. long double x;
  178. if( (y < 0.0L) || (y > 1.0L) || (df < 1.0L) )
  179. {
  180. mtherr( "chdtril", DOMAIN );
  181. return(0.0L);
  182. }
  183. x = igamil( 0.5L * df, y );
  184. return( 2.0L * x );
  185. }