|
|
|||
Warning, file /include/absl/random/internal/chi_square.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // Copyright 2017 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 0015 #ifndef ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ 0016 #define ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_ 0017 0018 // The chi-square statistic. 0019 // 0020 // Useful for evaluating if `D` independent random variables are behaving as 0021 // expected, or if two distributions are similar. (`D` is the degrees of 0022 // freedom). 0023 // 0024 // Each bucket should have an expected count of 10 or more for the chi square to 0025 // be meaningful. 0026 0027 #include <cassert> 0028 0029 #include "absl/base/config.h" 0030 0031 namespace absl { 0032 ABSL_NAMESPACE_BEGIN 0033 namespace random_internal { 0034 0035 constexpr const char kChiSquared[] = "chi-squared"; 0036 0037 // Returns the measured chi square value, using a single expected value. This 0038 // assumes that the values in [begin, end) are uniformly distributed. 0039 template <typename Iterator> 0040 double ChiSquareWithExpected(Iterator begin, Iterator end, double expected) { 0041 // Compute the sum and the number of buckets. 0042 assert(expected >= 10); // require at least 10 samples per bucket. 0043 double chi_square = 0; 0044 for (auto it = begin; it != end; it++) { 0045 double d = static_cast<double>(*it) - expected; 0046 chi_square += d * d; 0047 } 0048 chi_square = chi_square / expected; 0049 return chi_square; 0050 } 0051 0052 // Returns the measured chi square value, taking the actual value of each bucket 0053 // from the first set of iterators, and the expected value of each bucket from 0054 // the second set of iterators. 0055 template <typename Iterator, typename Expected> 0056 double ChiSquare(Iterator it, Iterator end, Expected eit, Expected eend) { 0057 double chi_square = 0; 0058 for (; it != end && eit != eend; ++it, ++eit) { 0059 if (*it > 0) { 0060 assert(*eit > 0); 0061 } 0062 double e = static_cast<double>(*eit); 0063 double d = static_cast<double>(*it - *eit); 0064 if (d != 0) { 0065 assert(e > 0); 0066 chi_square += (d * d) / e; 0067 } 0068 } 0069 assert(it == end && eit == eend); 0070 return chi_square; 0071 } 0072 0073 // ====================================================================== 0074 // The following methods can be used for an arbitrary significance level. 0075 // 0076 0077 // Calculates critical chi-square values to produce the given p-value using a 0078 // bisection search for a value within epsilon, relying on the monotonicity of 0079 // ChiSquarePValue(). 0080 double ChiSquareValue(int dof, double p); 0081 0082 // Calculates the p-value (probability) of a given chi-square value. 0083 double ChiSquarePValue(double chi_square, int dof); 0084 0085 } // namespace random_internal 0086 ABSL_NAMESPACE_END 0087 } // namespace absl 0088 0089 #endif // ABSL_RANDOM_INTERNAL_CHI_SQUARE_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|