You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

chi_square.h 3.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_
  15. #define ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_
  16. // The chi-square statistic.
  17. //
  18. // Useful for evaluating if `D` independent random variables are behaving as
  19. // expected, or if two distributions are similar. (`D` is the degrees of
  20. // freedom).
  21. //
  22. // Each bucket should have an expected count of 10 or more for the chi square to
  23. // be meaningful.
  24. #include <cassert>
  25. #include "absl/base/config.h"
  26. namespace absl
  27. {
  28. ABSL_NAMESPACE_BEGIN
  29. namespace random_internal
  30. {
  31. constexpr const char kChiSquared[] = "chi-squared";
  32. // Returns the measured chi square value, using a single expected value. This
  33. // assumes that the values in [begin, end) are uniformly distributed.
  34. template<typename Iterator>
  35. double ChiSquareWithExpected(Iterator begin, Iterator end, double expected)
  36. {
  37. // Compute the sum and the number of buckets.
  38. assert(expected >= 10); // require at least 10 samples per bucket.
  39. double chi_square = 0;
  40. for (auto it = begin; it != end; it++)
  41. {
  42. double d = static_cast<double>(*it) - expected;
  43. chi_square += d * d;
  44. }
  45. chi_square = chi_square / expected;
  46. return chi_square;
  47. }
  48. // Returns the measured chi square value, taking the actual value of each bucket
  49. // from the first set of iterators, and the expected value of each bucket from
  50. // the second set of iterators.
  51. template<typename Iterator, typename Expected>
  52. double ChiSquare(Iterator it, Iterator end, Expected eit, Expected eend)
  53. {
  54. double chi_square = 0;
  55. for (; it != end && eit != eend; ++it, ++eit)
  56. {
  57. if (*it > 0)
  58. {
  59. assert(*eit > 0);
  60. }
  61. double e = static_cast<double>(*eit);
  62. double d = static_cast<double>(*it - *eit);
  63. if (d != 0)
  64. {
  65. assert(e > 0);
  66. chi_square += (d * d) / e;
  67. }
  68. }
  69. assert(it == end && eit == eend);
  70. return chi_square;
  71. }
  72. // ======================================================================
  73. // The following methods can be used for an arbitrary significance level.
  74. //
  75. // Calculates critical chi-square values to produce the given p-value using a
  76. // bisection search for a value within epsilon, relying on the monotonicity of
  77. // ChiSquarePValue().
  78. double ChiSquareValue(int dof, double p);
  79. // Calculates the p-value (probability) of a given chi-square value.
  80. double ChiSquarePValue(double chi_square, int dof);
  81. } // namespace random_internal
  82. ABSL_NAMESPACE_END
  83. } // namespace absl
  84. #endif // ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_