| 167 | //! Wilcoxon test for sorted by absolute value floating numbers. |
| 168 | template <typename InputIterator> |
| 169 | NStatistics::TStatTestResult WilcoxonTestWithSign(InputIterator begin, InputIterator end) { |
| 170 | typedef typename std::iterator_traits<InputIterator>::value_type ValueType; |
| 171 | |
| 172 | const ValueType size = static_cast<ValueType>(std::distance(begin, end)); |
| 173 | ValueType denominator = size * (size + 1) * (2 * size + 1); |
| 174 | ValueType w = 0; |
| 175 | |
| 176 | InputIterator blockFirst = begin; |
| 177 | InputIterator blockLast = begin; |
| 178 | ValueType blockFirstIndex = 0; |
| 179 | ValueType blockLastIndex = 0; |
| 180 | for (; blockLast != end; ++blockLast, blockLastIndex += 1) { |
| 181 | InputIterator next = blockLast; |
| 182 | ++next; |
| 183 | |
| 184 | if (next == end || !RelativeEqual(*next, *blockFirst)) { |
| 185 | const ValueType rank = ((blockFirstIndex + blockLastIndex + 2) / 2.0); |
| 186 | |
| 187 | for (InputIterator it = blockFirst; it != next; ++it) { |
| 188 | if (*it > 0) { |
| 189 | w += rank; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | const ValueType blockSize = blockLastIndex - blockFirstIndex + 1; |
| 194 | denominator -= blockSize * (blockSize - 1) * (blockSize + 1) * 0.5; |
| 195 | blockFirst = next; |
| 196 | blockFirstIndex = blockLastIndex + 1; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (denominator <= 0) { |
| 201 | ythrow yexception() << "Incorrect denominator: " << denominator << " <= 0"; |
| 202 | } |
| 203 | denominator = sqrt(denominator / 24.0); |
| 204 | |
| 205 | const ValueType x = (w - size * (size + 1) / 4.0) / denominator; |
| 206 | double res = Phi(static_cast<ValueType>(0.0), static_cast<ValueType>(1.0), std::abs(x), false); |
| 207 | |
| 208 | return TStatTestResult((1 - res) * 2, (x > 0) - (x < 0)); |
| 209 | } |
| 210 | // Wilcoxon(...) implementation details END |
| 211 | |
| 212 | //! Save addition function for Kullback–Leibler divergence |
no test coverage detected