chi_squared.qbk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. [section:chi_squared_dist Chi Squared Distribution]
  2. ``#include <boost/math/distributions/chi_squared.hpp>``
  3. namespace boost{ namespace math{
  4. template <class RealType = double,
  5. class ``__Policy`` = ``__policy_class`` >
  6. class chi_squared_distribution;
  7. typedef chi_squared_distribution<> chi_squared;
  8. template <class RealType, class ``__Policy``>
  9. class chi_squared_distribution
  10. {
  11. public:
  12. typedef RealType value_type;
  13. typedef Policy policy_type;
  14. // Constructor:
  15. chi_squared_distribution(RealType i);
  16. // Accessor to parameter:
  17. RealType degrees_of_freedom()const;
  18. // Parameter estimation:
  19. static RealType find_degrees_of_freedom(
  20. RealType difference_from_mean,
  21. RealType alpha,
  22. RealType beta,
  23. RealType sd,
  24. RealType hint = 100);
  25. };
  26. }} // namespaces
  27. The Chi-Squared distribution is one of the most widely used distributions
  28. in statistical tests. If [chi][sub i] are [nu]
  29. independent, normally distributed
  30. random variables with means [mu][sub i] and variances [sigma][sub i][super 2],
  31. then the random variable:
  32. [equation chi_squ_ref1]
  33. is distributed according to the Chi-Squared distribution.
  34. The Chi-Squared distribution is a special case of the gamma distribution
  35. and has a single parameter [nu] that specifies the number of degrees of
  36. freedom. The following graph illustrates how the distribution changes
  37. for different values of [nu]:
  38. [graph chi_squared_pdf]
  39. [h4 Member Functions]
  40. chi_squared_distribution(RealType v);
  41. Constructs a Chi-Squared distribution with /v/ degrees of freedom.
  42. Requires v > 0, otherwise calls __domain_error.
  43. RealType degrees_of_freedom()const;
  44. Returns the parameter /v/ from which this object was constructed.
  45. static RealType find_degrees_of_freedom(
  46. RealType difference_from_variance,
  47. RealType alpha,
  48. RealType beta,
  49. RealType variance,
  50. RealType hint = 100);
  51. Estimates the sample size required to detect a difference from a nominal
  52. variance in a Chi-Squared test for equal standard deviations.
  53. [variablelist
  54. [[difference_from_variance][The difference from the assumed nominal variance
  55. that is to be detected: Note that the sign of this value is critical, see below.]]
  56. [[alpha][The maximum acceptable risk of rejecting the null hypothesis when it is
  57. in fact true.]]
  58. [[beta][The maximum acceptable risk of falsely failing to reject the null hypothesis.]]
  59. [[variance][The nominal variance being tested against.]]
  60. [[hint][An optional hint on where to start looking for a result: the current sample
  61. size would be a good choice.]]
  62. ]
  63. Note that this calculation works with /variances/ and not /standard deviations/.
  64. The sign of the parameter /difference_from_variance/ is important: the Chi
  65. Squared distribution is asymmetric, and the caller must decide in advance
  66. whether they are testing for a variance greater than a nominal value (positive
  67. /difference_from_variance/) or testing for a variance less than a nominal value
  68. (negative /difference_from_variance/). If the latter, then obviously it is
  69. a requirement that `variance + difference_from_variance > 0`, since no sample
  70. can have a negative variance!
  71. This procedure uses the method in Diamond, W. J. (1989).
  72. Practical Experiment Designs, Van-Nostrand Reinhold, New York.
  73. See also section on Sample sizes required in
  74. [@http://www.itl.nist.gov/div898/handbook/prc/section2/prc232.htm the NIST Engineering Statistics Handbook, Section 7.2.3.2].
  75. [h4 Non-member Accessors]
  76. All the [link math_toolkit.dist_ref.nmp usual non-member accessor functions]
  77. that are generic to all distributions are supported: __usual_accessors.
  78. (We have followed the usual restriction of the mode to degrees of freedom >= 2,
  79. but note that the maximum of the pdf is actually zero for degrees of freedom from 2 down to 0,
  80. and provide an extended definition that would avoid a discontinuity in the mode
  81. as alternative code in a comment).
  82. The domain of the random variable is \[0, +[infin]\].
  83. [h4 Examples]
  84. Various [link math_toolkit.stat_tut.weg.cs_eg worked examples]
  85. are available illustrating the use of the Chi Squared Distribution.
  86. [h4 Accuracy]
  87. The Chi-Squared distribution is implemented in terms of the
  88. [link math_toolkit.sf_gamma.igamma incomplete gamma functions]:
  89. please refer to the accuracy data for those functions.
  90. [h4 Implementation]
  91. In the following table /v/ is the number of degrees of freedom of the distribution,
  92. /x/ is the random variate, /p/ is the probability, and /q = 1-p/.
  93. [table
  94. [[Function][Implementation Notes]]
  95. [[pdf][Using the relation: pdf = __gamma_p_derivative(v / 2, x / 2) / 2 ]]
  96. [[cdf][Using the relation: p = __gamma_p(v / 2, x / 2) ]]
  97. [[cdf complement][Using the relation: q = __gamma_q(v / 2, x / 2) ]]
  98. [[quantile][Using the relation: x = 2 * __gamma_p_inv(v / 2, p) ]]
  99. [[quantile from the complement][Using the relation: x = 2 * __gamma_q_inv(v / 2, p) ]]
  100. [[mean][v]]
  101. [[variance][2v]]
  102. [[mode][v - 2 (if v >= 2)]]
  103. [[skewness][2 * sqrt(2 / v) == sqrt(8 / v)]]
  104. [[kurtosis][3 + 12 / v]]
  105. [[kurtosis excess][12 / v]]
  106. ]
  107. [h4 References]
  108. * [@http://www.itl.nist.gov/div898/handbook/eda/section3/eda3666.htm NIST Exploratory Data Analysis]
  109. * [@http://en.wikipedia.org/wiki/Chi-square_distribution Chi-square distribution]
  110. * [@http://mathworld.wolfram.com/Chi-SquaredDistribution.html Weisstein, Eric W. "Chi-Squared Distribution." From MathWorld--A Wolfram Web Resource.]
  111. [endsect] [/section:chi_squared_dist Chi Squared]
  112. [/ chi_squared.qbk
  113. Copyright 2006 John Maddock and Paul A. Bristow.
  114. Distributed under the Boost Software License, Version 1.0.
  115. (See accompanying file LICENSE_1_0.txt or copy at
  116. http://www.boost.org/LICENSE_1_0.txt).
  117. ]