Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | |
| 15 | #ifndef ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ |
| 16 | #define ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ |
| 17 | |
| 18 | // The chi-square statistic. |
| 19 | // |
| 20 | // Useful for evaluating if `D` independent random variables are behaving as |
| 21 | // expected, or if two distributions are similar. (`D` is the degrees of |
| 22 | // freedom). |
| 23 | // |
| 24 | // Each bucket should have an expected count of 10 or more for the chi square to |
| 25 | // be meaningful. |
| 26 | |
| 27 | #include <cassert> |
| 28 | |
| 29 | namespace absl { |
| 30 | namespace random_internal { |
| 31 | |
| 32 | constexpr const char kChiSquared[] = "chi-squared"; |
| 33 | |
| 34 | // Returns the measured chi square value, using a single expected value. This |
| 35 | // assumes that the values in [begin, end) are uniformly distributed. |
| 36 | template <typename Iterator> |
| 37 | double ChiSquareWithExpected(Iterator begin, Iterator end, double expected) { |
| 38 | // Compute the sum and the number of buckets. |
| 39 | assert(expected >= 10); // require at least 10 samples per bucket. |
| 40 | double chi_square = 0; |
| 41 | for (auto it = begin; it != end; it++) { |
| 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 | |
| 49 | // Returns the measured chi square value, taking the actual value of each bucket |
| 50 | // from the first set of iterators, and the expected value of each bucket from |
| 51 | // the second set of iterators. |
| 52 | template <typename Iterator, typename Expected> |
| 53 | double ChiSquare(Iterator it, Iterator end, Expected eit, Expected eend) { |
| 54 | double chi_square = 0; |
| 55 | for (; it != end && eit != eend; ++it, ++eit) { |
| 56 | if (*it > 0) { |
| 57 | assert(*eit > 0); |
| 58 | } |
| 59 | double e = static_cast<double>(*eit); |
| 60 | double d = static_cast<double>(*it - *eit); |
| 61 | if (d != 0) { |
| 62 | assert(e > 0); |
| 63 | chi_square += (d * d) / e; |
| 64 | } |
| 65 | } |
| 66 | assert(it == end && eit == eend); |
| 67 | return chi_square; |
| 68 | } |
| 69 | |
| 70 | // ====================================================================== |
| 71 | // The following methods can be used for an arbitrary significance level. |
| 72 | // |
| 73 | |
| 74 | // Calculates critical chi-square values to produce the given p-value using a |
| 75 | // bisection search for a value within epsilon, relying on the monotonicity of |
| 76 | // ChiSquarePValue(). |
| 77 | double ChiSquareValue(int dof, double p); |
| 78 | |
| 79 | // Calculates the p-value (probability) of a given chi-square value. |
| 80 | double ChiSquarePValue(double chi_square, int dof); |
| 81 | |
| 82 | } // namespace random_internal |
| 83 | } // namespace absl |
| 84 | |
| 85 | #endif // ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ |