chi_square.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Y_ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_
  15. #define Y_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 "y_absl/base/config.h"
  26. namespace y_absl {
  27. Y_ABSL_NAMESPACE_BEGIN
  28. namespace random_internal {
  29. constexpr const char kChiSquared[] = "chi-squared";
  30. // Returns the measured chi square value, using a single expected value. This
  31. // assumes that the values in [begin, end) are uniformly distributed.
  32. template <typename Iterator>
  33. double ChiSquareWithExpected(Iterator begin, Iterator end, double expected) {
  34. // Compute the sum and the number of buckets.
  35. assert(expected >= 10); // require at least 10 samples per bucket.
  36. double chi_square = 0;
  37. for (auto it = begin; it != end; it++) {
  38. double d = static_cast<double>(*it) - expected;
  39. chi_square += d * d;
  40. }
  41. chi_square = chi_square / expected;
  42. return chi_square;
  43. }
  44. // Returns the measured chi square value, taking the actual value of each bucket
  45. // from the first set of iterators, and the expected value of each bucket from
  46. // the second set of iterators.
  47. template <typename Iterator, typename Expected>
  48. double ChiSquare(Iterator it, Iterator end, Expected eit, Expected eend) {
  49. double chi_square = 0;
  50. for (; it != end && eit != eend; ++it, ++eit) {
  51. if (*it > 0) {
  52. assert(*eit > 0);
  53. }
  54. double e = static_cast<double>(*eit);
  55. double d = static_cast<double>(*it - *eit);
  56. if (d != 0) {
  57. assert(e > 0);
  58. chi_square += (d * d) / e;
  59. }
  60. }
  61. assert(it == end && eit == eend);
  62. return chi_square;
  63. }
  64. // ======================================================================
  65. // The following methods can be used for an arbitrary significance level.
  66. //
  67. // Calculates critical chi-square values to produce the given p-value using a
  68. // bisection search for a value within epsilon, relying on the monotonicity of
  69. // ChiSquarePValue().
  70. double ChiSquareValue(int dof, double p);
  71. // Calculates the p-value (probability) of a given chi-square value.
  72. double ChiSquarePValue(double chi_square, int dof);
  73. } // namespace random_internal
  74. Y_ABSL_NAMESPACE_END
  75. } // namespace y_absl
  76. #endif // Y_ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_