1 /* 2 * Copyright (C) 2012 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.math; 18 19 import static com.google.common.math.StatsTesting.ALLOWED_ERROR; 20 import static com.google.common.math.StatsTesting.ALL_MANY_VALUES; 21 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES; 22 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES_COUNT; 23 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES_MAX; 24 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES_MEAN; 25 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES_MIN; 26 import static com.google.common.math.StatsTesting.INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS; 27 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES; 28 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES_COUNT; 29 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES_MAX; 30 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES_MEAN; 31 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES_MIN; 32 import static com.google.common.math.StatsTesting.LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS; 33 import static com.google.common.math.StatsTesting.MANY_VALUES; 34 import static com.google.common.math.StatsTesting.MANY_VALUES_COUNT; 35 import static com.google.common.math.StatsTesting.MANY_VALUES_MAX; 36 import static com.google.common.math.StatsTesting.MANY_VALUES_MEAN; 37 import static com.google.common.math.StatsTesting.MANY_VALUES_MIN; 38 import static com.google.common.math.StatsTesting.MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS; 39 import static com.google.common.math.StatsTesting.ONE_VALUE; 40 import static com.google.common.math.StatsTesting.OTHER_ONE_VALUE; 41 import static com.google.common.math.StatsTesting.TWO_VALUES; 42 import static com.google.common.math.StatsTesting.TWO_VALUES_MAX; 43 import static com.google.common.math.StatsTesting.TWO_VALUES_MEAN; 44 import static com.google.common.math.StatsTesting.TWO_VALUES_MIN; 45 import static com.google.common.math.StatsTesting.TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS; 46 import static com.google.common.truth.Truth.assertThat; 47 import static com.google.common.truth.Truth.assertWithMessage; 48 import static java.lang.Math.sqrt; 49 import static org.junit.Assert.assertThrows; 50 51 import com.google.common.collect.ImmutableList; 52 import com.google.common.math.StatsTesting.ManyValues; 53 import com.google.common.primitives.Doubles; 54 import com.google.common.primitives.Longs; 55 import junit.framework.TestCase; 56 57 /** 58 * Tests for {@link StatsAccumulator}. This tests the stats methods for instances built with {@link 59 * StatsAccumulator#add} and {@link StatsAccumulator#addAll}, and various error cases of the {@link 60 * StatsAccumulator#add} and {@link StatsAccumulator#addAll} methods. For tests of the {@link 61 * StatsAccumulator#snapshot} method which returns {@link Stats} instances, see {@link StatsTest}. 62 * 63 * @author Pete Gillin 64 */ 65 public class StatsAccumulatorTest extends TestCase { 66 67 private StatsAccumulator emptyAccumulator; 68 private StatsAccumulator emptyAccumulatorByAddAllEmptyIterable; 69 private StatsAccumulator emptyAccumulatorByAddAllEmptyStats; 70 private StatsAccumulator oneValueAccumulator; 71 private StatsAccumulator oneValueAccumulatorByAddAllEmptyStats; 72 private StatsAccumulator twoValuesAccumulator; 73 private StatsAccumulator twoValuesAccumulatorByAddAllStats; 74 private StatsAccumulator manyValuesAccumulatorByAddAllIterable; 75 private StatsAccumulator manyValuesAccumulatorByAddAllIterator; 76 private StatsAccumulator manyValuesAccumulatorByAddAllVarargs; 77 private StatsAccumulator manyValuesAccumulatorByRepeatedAdd; 78 private StatsAccumulator manyValuesAccumulatorByAddAndAddAll; 79 private StatsAccumulator manyValuesAccumulatorByAddAllStats; 80 private StatsAccumulator manyValuesAccumulatorByAddAllStatsAccumulator; 81 private StatsAccumulator integerManyValuesAccumulatorByAddAllIterable; 82 private StatsAccumulator longManyValuesAccumulatorByAddAllIterator; 83 private StatsAccumulator longManyValuesAccumulatorByAddAllVarargs; 84 85 @Override setUp()86 protected void setUp() throws Exception { 87 super.setUp(); 88 89 emptyAccumulator = new StatsAccumulator(); 90 91 emptyAccumulatorByAddAllEmptyIterable = new StatsAccumulator(); 92 emptyAccumulatorByAddAllEmptyIterable.addAll(ImmutableList.<Double>of()); 93 94 emptyAccumulatorByAddAllEmptyStats = new StatsAccumulator(); 95 emptyAccumulatorByAddAllEmptyStats.addAll(Stats.of()); 96 97 oneValueAccumulator = new StatsAccumulator(); 98 oneValueAccumulator.add(ONE_VALUE); 99 100 oneValueAccumulatorByAddAllEmptyStats = new StatsAccumulator(); 101 oneValueAccumulatorByAddAllEmptyStats.add(ONE_VALUE); 102 oneValueAccumulatorByAddAllEmptyStats.addAll(Stats.of()); 103 104 twoValuesAccumulator = new StatsAccumulator(); 105 twoValuesAccumulator.addAll(TWO_VALUES); 106 107 twoValuesAccumulatorByAddAllStats = new StatsAccumulator(); 108 twoValuesAccumulatorByAddAllStats.addAll(Stats.of(ONE_VALUE)); 109 twoValuesAccumulatorByAddAllStats.addAll(Stats.of(OTHER_ONE_VALUE)); 110 111 manyValuesAccumulatorByAddAllIterable = new StatsAccumulator(); 112 manyValuesAccumulatorByAddAllIterable.addAll(MANY_VALUES); 113 114 manyValuesAccumulatorByAddAllIterator = new StatsAccumulator(); 115 manyValuesAccumulatorByAddAllIterator.addAll(MANY_VALUES.iterator()); 116 117 manyValuesAccumulatorByAddAllVarargs = new StatsAccumulator(); 118 manyValuesAccumulatorByAddAllVarargs.addAll(Doubles.toArray(MANY_VALUES)); 119 120 manyValuesAccumulatorByRepeatedAdd = new StatsAccumulator(); 121 for (double value : MANY_VALUES) { 122 manyValuesAccumulatorByRepeatedAdd.add(value); 123 } 124 125 manyValuesAccumulatorByAddAndAddAll = new StatsAccumulator(); 126 manyValuesAccumulatorByAddAndAddAll.add(MANY_VALUES.get(0)); 127 manyValuesAccumulatorByAddAndAddAll.addAll(MANY_VALUES.subList(1, MANY_VALUES.size())); 128 129 manyValuesAccumulatorByAddAllStats = new StatsAccumulator(); 130 manyValuesAccumulatorByAddAllStats.addAll( 131 Stats.of(MANY_VALUES.subList(0, MANY_VALUES.size() / 2))); 132 manyValuesAccumulatorByAddAllStats.addAll( 133 Stats.of(MANY_VALUES.subList(MANY_VALUES.size() / 2, MANY_VALUES.size()))); 134 135 manyValuesAccumulatorByAddAllStatsAccumulator = new StatsAccumulator(); 136 manyValuesAccumulatorByAddAllStatsAccumulator.addAll( 137 statsAccumulatorOf(MANY_VALUES.subList(0, MANY_VALUES.size() / 2))); 138 manyValuesAccumulatorByAddAllStatsAccumulator.addAll( 139 statsAccumulatorOf(MANY_VALUES.subList(MANY_VALUES.size() / 2, MANY_VALUES.size()))); 140 141 integerManyValuesAccumulatorByAddAllIterable = new StatsAccumulator(); 142 integerManyValuesAccumulatorByAddAllIterable.addAll(INTEGER_MANY_VALUES); 143 144 longManyValuesAccumulatorByAddAllIterator = new StatsAccumulator(); 145 longManyValuesAccumulatorByAddAllIterator.addAll(LONG_MANY_VALUES.iterator()); 146 147 longManyValuesAccumulatorByAddAllVarargs = new StatsAccumulator(); 148 longManyValuesAccumulatorByAddAllVarargs.addAll(Longs.toArray(LONG_MANY_VALUES)); 149 } 150 statsAccumulatorOf(Iterable<? extends Number> values)151 private static StatsAccumulator statsAccumulatorOf(Iterable<? extends Number> values) { 152 StatsAccumulator accumulator = new StatsAccumulator(); 153 accumulator.addAll(values); 154 return accumulator; 155 } 156 testCount()157 public void testCount() { 158 assertThat(emptyAccumulator.count()).isEqualTo(0); 159 assertThat(emptyAccumulatorByAddAllEmptyIterable.count()).isEqualTo(0); 160 assertThat(emptyAccumulatorByAddAllEmptyStats.count()).isEqualTo(0); 161 assertThat(oneValueAccumulator.count()).isEqualTo(1); 162 assertThat(oneValueAccumulatorByAddAllEmptyStats.count()).isEqualTo(1); 163 assertThat(twoValuesAccumulator.count()).isEqualTo(2); 164 assertThat(twoValuesAccumulatorByAddAllStats.count()).isEqualTo(2); 165 assertThat(manyValuesAccumulatorByAddAllIterable.count()).isEqualTo(MANY_VALUES_COUNT); 166 assertThat(manyValuesAccumulatorByAddAllIterator.count()).isEqualTo(MANY_VALUES_COUNT); 167 assertThat(manyValuesAccumulatorByAddAllVarargs.count()).isEqualTo(MANY_VALUES_COUNT); 168 assertThat(manyValuesAccumulatorByRepeatedAdd.count()).isEqualTo(MANY_VALUES_COUNT); 169 assertThat(manyValuesAccumulatorByAddAndAddAll.count()).isEqualTo(MANY_VALUES_COUNT); 170 assertThat(manyValuesAccumulatorByAddAllStats.count()).isEqualTo(MANY_VALUES_COUNT); 171 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.count()).isEqualTo(MANY_VALUES_COUNT); 172 assertThat(integerManyValuesAccumulatorByAddAllIterable.count()) 173 .isEqualTo(StatsTesting.INTEGER_MANY_VALUES_COUNT); 174 assertThat(longManyValuesAccumulatorByAddAllIterator.count()) 175 .isEqualTo(StatsTesting.LONG_MANY_VALUES_COUNT); 176 assertThat(longManyValuesAccumulatorByAddAllVarargs.count()) 177 .isEqualTo(StatsTesting.LONG_MANY_VALUES_COUNT); 178 } 179 testCountOverflow_doesNotThrow()180 public void testCountOverflow_doesNotThrow() { 181 StatsAccumulator accumulator = new StatsAccumulator(); 182 accumulator.add(ONE_VALUE); 183 for (int power = 1; power < Long.SIZE - 1; power++) { 184 accumulator.addAll(accumulator.snapshot()); 185 } 186 // Should overflow without throwing. 187 accumulator.addAll(accumulator.snapshot()); 188 assertThat(accumulator.count()).isLessThan(0L); 189 } 190 testMean()191 public void testMean() { 192 assertThrows(IllegalStateException.class, () -> emptyAccumulator.mean()); 193 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyIterable.mean()); 194 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.mean()); 195 assertThat(oneValueAccumulator.mean()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); 196 assertThat(oneValueAccumulatorByAddAllEmptyStats.mean()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); 197 assertThat(twoValuesAccumulator.mean()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN); 198 assertThat(twoValuesAccumulatorByAddAllStats.mean()) 199 .isWithin(ALLOWED_ERROR) 200 .of(TWO_VALUES_MEAN); 201 assertThat(manyValuesAccumulatorByAddAllIterable.mean()) 202 .isWithin(ALLOWED_ERROR) 203 .of(MANY_VALUES_MEAN); 204 assertThat(manyValuesAccumulatorByAddAllIterator.mean()) 205 .isWithin(ALLOWED_ERROR) 206 .of(MANY_VALUES_MEAN); 207 assertThat(manyValuesAccumulatorByAddAllVarargs.mean()) 208 .isWithin(ALLOWED_ERROR) 209 .of(MANY_VALUES_MEAN); 210 assertThat(manyValuesAccumulatorByRepeatedAdd.mean()) 211 .isWithin(ALLOWED_ERROR) 212 .of(MANY_VALUES_MEAN); 213 assertThat(manyValuesAccumulatorByAddAndAddAll.mean()) 214 .isWithin(ALLOWED_ERROR) 215 .of(MANY_VALUES_MEAN); 216 assertThat(manyValuesAccumulatorByAddAllStats.mean()) 217 .isWithin(ALLOWED_ERROR) 218 .of(MANY_VALUES_MEAN); 219 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.mean()) 220 .isWithin(ALLOWED_ERROR) 221 .of(MANY_VALUES_MEAN); 222 // For datasets of many double values created from an iterable, we test many combinations of 223 // finite and non-finite values: 224 for (ManyValues values : ALL_MANY_VALUES) { 225 StatsAccumulator accumulator = new StatsAccumulator(); 226 StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator(); 227 accumulator.addAll(values.asIterable()); 228 for (double value : values.asIterable()) { 229 accumulatorByAddAllStats.addAll(Stats.of(value)); 230 } 231 double mean = accumulator.mean(); 232 double meanByAddAllStats = accumulatorByAddAllStats.mean(); 233 if (values.hasAnyNaN()) { 234 assertWithMessage("mean of " + values).that(mean).isNaN(); 235 assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isNaN(); 236 } else if (values.hasAnyPositiveInfinity() && values.hasAnyNegativeInfinity()) { 237 assertWithMessage("mean of " + values).that(mean).isNaN(); 238 assertWithMessage("mean by addAll(Stats) of " + values).that(meanByAddAllStats).isNaN(); 239 } else if (values.hasAnyPositiveInfinity()) { 240 assertWithMessage("mean of " + values).that(mean).isPositiveInfinity(); 241 assertWithMessage("mean by addAll(Stats) of " + values) 242 .that(meanByAddAllStats) 243 .isPositiveInfinity(); 244 } else if (values.hasAnyNegativeInfinity()) { 245 assertWithMessage("mean of " + values).that(mean).isNegativeInfinity(); 246 assertWithMessage("mean by addAll(Stats) of " + values) 247 .that(meanByAddAllStats) 248 .isNegativeInfinity(); 249 } else { 250 assertWithMessage("mean of " + values) 251 .that(mean) 252 .isWithin(ALLOWED_ERROR) 253 .of(MANY_VALUES_MEAN); 254 assertWithMessage("mean by addAll(Stats) of " + values) 255 .that(meanByAddAllStats) 256 .isWithin(ALLOWED_ERROR) 257 .of(MANY_VALUES_MEAN); 258 } 259 } 260 assertThat(integerManyValuesAccumulatorByAddAllIterable.mean()) 261 .isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN) 262 .of(INTEGER_MANY_VALUES_MEAN); 263 assertThat(longManyValuesAccumulatorByAddAllIterator.mean()) 264 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN) 265 .of(LONG_MANY_VALUES_MEAN); 266 assertThat(longManyValuesAccumulatorByAddAllVarargs.mean()) 267 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN) 268 .of(LONG_MANY_VALUES_MEAN); 269 } 270 testSum()271 public void testSum() { 272 assertThat(emptyAccumulator.sum()).isWithin(0.0).of(0.0); 273 assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isWithin(0.0).of(0.0); 274 assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isWithin(0.0).of(0.0); 275 assertThat(oneValueAccumulator.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); 276 assertThat(oneValueAccumulatorByAddAllEmptyStats.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); 277 assertThat(twoValuesAccumulator.sum()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN * 2); 278 assertThat(twoValuesAccumulatorByAddAllStats.sum()) 279 .isWithin(ALLOWED_ERROR) 280 .of(TWO_VALUES_MEAN * 2); 281 assertThat(manyValuesAccumulatorByAddAllIterable.sum()) 282 .isWithin(ALLOWED_ERROR) 283 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 284 assertThat(manyValuesAccumulatorByAddAllIterator.sum()) 285 .isWithin(ALLOWED_ERROR) 286 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 287 assertThat(manyValuesAccumulatorByAddAllVarargs.sum()) 288 .isWithin(ALLOWED_ERROR) 289 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 290 assertThat(manyValuesAccumulatorByRepeatedAdd.sum()) 291 .isWithin(ALLOWED_ERROR) 292 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 293 assertThat(manyValuesAccumulatorByAddAndAddAll.sum()) 294 .isWithin(ALLOWED_ERROR) 295 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 296 assertThat(manyValuesAccumulatorByAddAllStats.sum()) 297 .isWithin(ALLOWED_ERROR) 298 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 299 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.sum()) 300 .isWithin(ALLOWED_ERROR) 301 .of(MANY_VALUES_MEAN * MANY_VALUES_COUNT); 302 assertThat(integerManyValuesAccumulatorByAddAllIterable.sum()) 303 .isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_MEAN) 304 .of(INTEGER_MANY_VALUES_MEAN * INTEGER_MANY_VALUES_COUNT); 305 assertThat(longManyValuesAccumulatorByAddAllIterator.sum()) 306 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN) 307 .of(LONG_MANY_VALUES_MEAN * LONG_MANY_VALUES_COUNT); 308 assertThat(longManyValuesAccumulatorByAddAllVarargs.sum()) 309 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_MEAN) 310 .of(LONG_MANY_VALUES_MEAN * LONG_MANY_VALUES_COUNT); 311 } 312 testPopulationVariance()313 public void testPopulationVariance() { 314 assertThrows(IllegalStateException.class, () -> emptyAccumulator.populationVariance()); 315 assertThrows( 316 IllegalStateException.class, 317 () -> emptyAccumulatorByAddAllEmptyIterable.populationVariance()); 318 assertThrows( 319 IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationVariance()); 320 assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0); 321 assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0); 322 assertThat(twoValuesAccumulator.populationVariance()) 323 .isWithin(ALLOWED_ERROR) 324 .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2); 325 assertThat(twoValuesAccumulatorByAddAllStats.populationVariance()) 326 .isWithin(ALLOWED_ERROR) 327 .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2); 328 assertThat(manyValuesAccumulatorByAddAllIterable.populationVariance()) 329 .isWithin(ALLOWED_ERROR) 330 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 331 assertThat(manyValuesAccumulatorByAddAllIterator.populationVariance()) 332 .isWithin(ALLOWED_ERROR) 333 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 334 assertThat(manyValuesAccumulatorByAddAllVarargs.populationVariance()) 335 .isWithin(ALLOWED_ERROR) 336 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 337 assertThat(manyValuesAccumulatorByRepeatedAdd.populationVariance()) 338 .isWithin(ALLOWED_ERROR) 339 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 340 assertThat(manyValuesAccumulatorByAddAndAddAll.populationVariance()) 341 .isWithin(ALLOWED_ERROR) 342 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 343 assertThat(manyValuesAccumulatorByAddAllStats.populationVariance()) 344 .isWithin(ALLOWED_ERROR) 345 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 346 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.populationVariance()) 347 .isWithin(ALLOWED_ERROR) 348 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 349 // For datasets of many double values created from an iterator, we test many combinations of 350 // finite and non-finite values: 351 for (ManyValues values : ALL_MANY_VALUES) { 352 StatsAccumulator accumulator = new StatsAccumulator(); 353 StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator(); 354 accumulator.addAll(values.asIterable().iterator()); 355 for (double value : values.asIterable()) { 356 accumulatorByAddAllStats.addAll(Stats.of(value)); 357 } 358 double populationVariance = accumulator.populationVariance(); 359 double populationVarianceByAddAllStats = accumulatorByAddAllStats.populationVariance(); 360 if (values.hasAnyNonFinite()) { 361 assertWithMessage("population variance of " + values).that(populationVariance).isNaN(); 362 assertWithMessage("population variance by addAll(Stats) of " + values) 363 .that(populationVarianceByAddAllStats) 364 .isNaN(); 365 } else { 366 assertWithMessage("population variance of " + values) 367 .that(populationVariance) 368 .isWithin(ALLOWED_ERROR) 369 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 370 assertWithMessage("population variance by addAll(Stats) of " + values) 371 .that(populationVarianceByAddAllStats) 372 .isWithin(ALLOWED_ERROR) 373 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT); 374 } 375 } 376 assertThat(integerManyValuesAccumulatorByAddAllIterable.populationVariance()) 377 .isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 378 .of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT); 379 assertThat(longManyValuesAccumulatorByAddAllIterator.populationVariance()) 380 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 381 .of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT); 382 assertThat(longManyValuesAccumulatorByAddAllVarargs.populationVariance()) 383 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 384 .of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT); 385 } 386 testPopulationStandardDeviation()387 public void testPopulationStandardDeviation() { 388 assertThrows(IllegalStateException.class, () -> emptyAccumulator.populationStandardDeviation()); 389 assertThrows( 390 IllegalStateException.class, 391 () -> emptyAccumulatorByAddAllEmptyIterable.populationStandardDeviation()); 392 assertThrows( 393 IllegalStateException.class, 394 () -> emptyAccumulatorByAddAllEmptyStats.populationStandardDeviation()); 395 assertThat(oneValueAccumulator.populationStandardDeviation()).isWithin(0.0).of(0.0); 396 assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()) 397 .isWithin(0.0) 398 .of(0.0); 399 assertThat(twoValuesAccumulator.populationStandardDeviation()) 400 .isWithin(ALLOWED_ERROR) 401 .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); 402 assertThat(twoValuesAccumulatorByAddAllStats.populationStandardDeviation()) 403 .isWithin(ALLOWED_ERROR) 404 .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); 405 assertThat(manyValuesAccumulatorByAddAllIterable.populationStandardDeviation()) 406 .isWithin(ALLOWED_ERROR) 407 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 408 assertThat(manyValuesAccumulatorByAddAllIterator.populationStandardDeviation()) 409 .isWithin(ALLOWED_ERROR) 410 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 411 assertThat(manyValuesAccumulatorByAddAllVarargs.populationStandardDeviation()) 412 .isWithin(ALLOWED_ERROR) 413 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 414 assertThat(manyValuesAccumulatorByRepeatedAdd.populationStandardDeviation()) 415 .isWithin(ALLOWED_ERROR) 416 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 417 assertThat(manyValuesAccumulatorByAddAndAddAll.populationStandardDeviation()) 418 .isWithin(ALLOWED_ERROR) 419 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 420 assertThat(manyValuesAccumulatorByAddAllStats.populationStandardDeviation()) 421 .isWithin(ALLOWED_ERROR) 422 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 423 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.populationStandardDeviation()) 424 .isWithin(ALLOWED_ERROR) 425 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / MANY_VALUES_COUNT)); 426 assertThat(integerManyValuesAccumulatorByAddAllIterable.populationStandardDeviation()) 427 .isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)) 428 .of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / INTEGER_MANY_VALUES_COUNT)); 429 assertThat(longManyValuesAccumulatorByAddAllIterator.populationStandardDeviation()) 430 .isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)) 431 .of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT)); 432 assertThat(longManyValuesAccumulatorByAddAllVarargs.populationStandardDeviation()) 433 .isWithin(ALLOWED_ERROR * sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)) 434 .of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / LONG_MANY_VALUES_COUNT)); 435 } 436 testSampleVariance()437 public void testSampleVariance() { 438 assertThrows(IllegalStateException.class, () -> emptyAccumulator.sampleVariance()); 439 assertThrows( 440 IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyIterable.sampleVariance()); 441 assertThrows( 442 IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.sampleVariance()); 443 assertThrows(IllegalStateException.class, () -> oneValueAccumulator.sampleVariance()); 444 assertThrows( 445 IllegalStateException.class, () -> oneValueAccumulatorByAddAllEmptyStats.sampleVariance()); 446 assertThat(twoValuesAccumulator.sampleVariance()) 447 .isWithin(ALLOWED_ERROR) 448 .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS); 449 assertThat(twoValuesAccumulatorByAddAllStats.sampleVariance()) 450 .isWithin(ALLOWED_ERROR) 451 .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS); 452 assertThat(manyValuesAccumulatorByAddAllIterable.sampleVariance()) 453 .isWithin(ALLOWED_ERROR) 454 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 455 assertThat(manyValuesAccumulatorByAddAllIterator.sampleVariance()) 456 .isWithin(ALLOWED_ERROR) 457 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 458 assertThat(manyValuesAccumulatorByAddAllVarargs.sampleVariance()) 459 .isWithin(ALLOWED_ERROR) 460 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 461 assertThat(manyValuesAccumulatorByRepeatedAdd.sampleVariance()) 462 .isWithin(ALLOWED_ERROR) 463 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 464 assertThat(manyValuesAccumulatorByAddAndAddAll.sampleVariance()) 465 .isWithin(ALLOWED_ERROR) 466 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 467 assertThat(manyValuesAccumulatorByAddAllStats.sampleVariance()) 468 .isWithin(ALLOWED_ERROR) 469 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 470 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.sampleVariance()) 471 .isWithin(ALLOWED_ERROR) 472 .of(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1)); 473 assertThat(integerManyValuesAccumulatorByAddAllIterable.sampleVariance()) 474 .isWithin(ALLOWED_ERROR * INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 475 .of(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1)); 476 assertThat(longManyValuesAccumulatorByAddAllIterator.sampleVariance()) 477 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 478 .of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1)); 479 assertThat(longManyValuesAccumulatorByAddAllVarargs.sampleVariance()) 480 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 481 .of(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1)); 482 } 483 testSampleStandardDeviation()484 public void testSampleStandardDeviation() { 485 assertThrows(IllegalStateException.class, () -> emptyAccumulator.sampleStandardDeviation()); 486 assertThrows( 487 IllegalStateException.class, 488 () -> emptyAccumulatorByAddAllEmptyIterable.sampleStandardDeviation()); 489 assertThrows( 490 IllegalStateException.class, 491 () -> emptyAccumulatorByAddAllEmptyStats.sampleStandardDeviation()); 492 assertThrows(IllegalStateException.class, () -> oneValueAccumulator.sampleStandardDeviation()); 493 assertThrows( 494 IllegalStateException.class, 495 () -> oneValueAccumulatorByAddAllEmptyStats.sampleStandardDeviation()); 496 assertThat(twoValuesAccumulator.sampleStandardDeviation()) 497 .isWithin(ALLOWED_ERROR) 498 .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS)); 499 assertThat(twoValuesAccumulatorByAddAllStats.sampleStandardDeviation()) 500 .isWithin(ALLOWED_ERROR) 501 .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS)); 502 assertThat(manyValuesAccumulatorByAddAllIterable.sampleStandardDeviation()) 503 .isWithin(ALLOWED_ERROR) 504 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 505 assertThat(manyValuesAccumulatorByAddAllIterator.sampleStandardDeviation()) 506 .isWithin(ALLOWED_ERROR) 507 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 508 assertThat(manyValuesAccumulatorByAddAllVarargs.sampleStandardDeviation()) 509 .isWithin(ALLOWED_ERROR) 510 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 511 assertThat(manyValuesAccumulatorByRepeatedAdd.sampleStandardDeviation()) 512 .isWithin(ALLOWED_ERROR) 513 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 514 assertThat(manyValuesAccumulatorByAddAndAddAll.sampleStandardDeviation()) 515 .isWithin(ALLOWED_ERROR) 516 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 517 assertThat(manyValuesAccumulatorByAddAllStats.sampleStandardDeviation()) 518 .isWithin(ALLOWED_ERROR) 519 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 520 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.sampleStandardDeviation()) 521 .isWithin(ALLOWED_ERROR) 522 .of(sqrt(MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (MANY_VALUES_COUNT - 1))); 523 assertThat(integerManyValuesAccumulatorByAddAllIterable.sampleStandardDeviation()) 524 .isWithin(ALLOWED_ERROR * sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS)) 525 .of(sqrt(INTEGER_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (INTEGER_MANY_VALUES_COUNT - 1))); 526 assertThat(longManyValuesAccumulatorByAddAllIterator.sampleStandardDeviation()) 527 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 528 .of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1))); 529 assertThat(longManyValuesAccumulatorByAddAllVarargs.sampleStandardDeviation()) 530 .isWithin(ALLOWED_ERROR * LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS) 531 .of(sqrt(LONG_MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS / (LONG_MANY_VALUES_COUNT - 1))); 532 } 533 testMax()534 public void testMax() { 535 assertThrows(IllegalStateException.class, () -> emptyAccumulator.max()); 536 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyIterable.max()); 537 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.max()); 538 assertThat(oneValueAccumulator.max()).isEqualTo(ONE_VALUE); 539 assertThat(oneValueAccumulatorByAddAllEmptyStats.max()).isEqualTo(ONE_VALUE); 540 assertThat(twoValuesAccumulator.max()).isEqualTo(TWO_VALUES_MAX); 541 assertThat(twoValuesAccumulatorByAddAllStats.max()).isEqualTo(TWO_VALUES_MAX); 542 assertThat(manyValuesAccumulatorByAddAllIterable.max()).isEqualTo(MANY_VALUES_MAX); 543 assertThat(manyValuesAccumulatorByAddAllIterator.max()).isEqualTo(MANY_VALUES_MAX); 544 assertThat(manyValuesAccumulatorByAddAllVarargs.max()).isEqualTo(MANY_VALUES_MAX); 545 assertThat(manyValuesAccumulatorByRepeatedAdd.max()).isEqualTo(MANY_VALUES_MAX); 546 assertThat(manyValuesAccumulatorByAddAndAddAll.max()).isEqualTo(MANY_VALUES_MAX); 547 assertThat(manyValuesAccumulatorByAddAllStats.max()).isEqualTo(MANY_VALUES_MAX); 548 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.max()).isEqualTo(MANY_VALUES_MAX); 549 // For datasets of many double values created from an array, we test many combinations of 550 // finite and non-finite values: 551 for (ManyValues values : ALL_MANY_VALUES) { 552 StatsAccumulator accumulator = new StatsAccumulator(); 553 StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator(); 554 accumulator.addAll(values.asArray()); 555 for (double value : values.asIterable()) { 556 accumulatorByAddAllStats.addAll(Stats.of(value)); 557 } 558 double max = accumulator.max(); 559 double maxByAddAllStats = accumulatorByAddAllStats.max(); 560 if (values.hasAnyNaN()) { 561 assertWithMessage("max of " + values).that(max).isNaN(); 562 assertWithMessage("max by addAll(Stats) of " + values).that(maxByAddAllStats).isNaN(); 563 } else if (values.hasAnyPositiveInfinity()) { 564 assertWithMessage("max of " + values).that(max).isPositiveInfinity(); 565 assertWithMessage("max by addAll(Stats) of " + values) 566 .that(maxByAddAllStats) 567 .isPositiveInfinity(); 568 } else { 569 assertWithMessage("max of " + values).that(max).isEqualTo(MANY_VALUES_MAX); 570 assertWithMessage("max by addAll(Stats) of " + values) 571 .that(maxByAddAllStats) 572 .isEqualTo(MANY_VALUES_MAX); 573 } 574 } 575 assertThat(integerManyValuesAccumulatorByAddAllIterable.max()) 576 .isEqualTo(INTEGER_MANY_VALUES_MAX); 577 assertThat(longManyValuesAccumulatorByAddAllIterator.max()).isEqualTo(LONG_MANY_VALUES_MAX); 578 assertThat(longManyValuesAccumulatorByAddAllVarargs.max()).isEqualTo(LONG_MANY_VALUES_MAX); 579 } 580 testMin()581 public void testMin() { 582 assertThrows(IllegalStateException.class, () -> emptyAccumulator.min()); 583 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyIterable.min()); 584 assertThrows(IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.min()); 585 assertThat(oneValueAccumulator.min()).isEqualTo(ONE_VALUE); 586 assertThat(oneValueAccumulatorByAddAllEmptyStats.min()).isEqualTo(ONE_VALUE); 587 assertThat(twoValuesAccumulator.min()).isEqualTo(TWO_VALUES_MIN); 588 assertThat(twoValuesAccumulatorByAddAllStats.min()).isEqualTo(TWO_VALUES_MIN); 589 assertThat(manyValuesAccumulatorByAddAllIterable.min()).isEqualTo(MANY_VALUES_MIN); 590 assertThat(manyValuesAccumulatorByAddAllIterator.min()).isEqualTo(MANY_VALUES_MIN); 591 assertThat(manyValuesAccumulatorByAddAllVarargs.min()).isEqualTo(MANY_VALUES_MIN); 592 assertThat(manyValuesAccumulatorByRepeatedAdd.min()).isEqualTo(MANY_VALUES_MIN); 593 assertThat(manyValuesAccumulatorByAddAndAddAll.min()).isEqualTo(MANY_VALUES_MIN); 594 assertThat(manyValuesAccumulatorByAddAllStats.min()).isEqualTo(MANY_VALUES_MIN); 595 assertThat(manyValuesAccumulatorByAddAllStatsAccumulator.min()).isEqualTo(MANY_VALUES_MIN); 596 // For datasets of many double values created by adding elements individually, we test many 597 // combinations of finite and non-finite values: 598 for (ManyValues values : ALL_MANY_VALUES) { 599 StatsAccumulator accumulator = new StatsAccumulator(); 600 StatsAccumulator accumulatorByAddAllStats = new StatsAccumulator(); 601 for (double value : values.asIterable()) { 602 accumulator.add(value); 603 accumulatorByAddAllStats.addAll(Stats.of(value)); 604 } 605 double min = accumulator.min(); 606 double minByAddAllStats = accumulatorByAddAllStats.min(); 607 if (values.hasAnyNaN()) { 608 assertWithMessage("min of " + values).that(min).isNaN(); 609 assertWithMessage("min by addAll(Stats) of " + values).that(minByAddAllStats).isNaN(); 610 } else if (values.hasAnyNegativeInfinity()) { 611 assertWithMessage("min of " + values).that(min).isNegativeInfinity(); 612 assertWithMessage("min by addAll(Stats) of " + values) 613 .that(minByAddAllStats) 614 .isNegativeInfinity(); 615 } else { 616 assertWithMessage("min of " + values).that(min).isEqualTo(MANY_VALUES_MIN); 617 assertWithMessage("min by addAll(Stats) of " + values) 618 .that(minByAddAllStats) 619 .isEqualTo(MANY_VALUES_MIN); 620 } 621 } 622 assertThat(integerManyValuesAccumulatorByAddAllIterable.min()) 623 .isEqualTo(INTEGER_MANY_VALUES_MIN); 624 assertThat(longManyValuesAccumulatorByAddAllIterator.min()).isEqualTo(LONG_MANY_VALUES_MIN); 625 assertThat(longManyValuesAccumulatorByAddAllVarargs.min()).isEqualTo(LONG_MANY_VALUES_MIN); 626 } 627 } 628