@Test
  public void testAggregateSpecialValues() {
    double[] totalSample = {Double.POSITIVE_INFINITY, 2, 3, Double.NaN, 5};
    double[][] subSamples = {{Double.POSITIVE_INFINITY, 2}, {3}, {Double.NaN}, {5}};

    // Compute combined stats directly
    SummaryStatistics totalStats = new SummaryStatistics();
    for (int i = 0; i < totalSample.length; i++) {
      totalStats.addValue(totalSample[i]);
    }

    // Now compute subsample stats individually and aggregate
    SummaryStatistics[] subSampleStats = new SummaryStatistics[5];
    for (int i = 0; i < 4; i++) {
      subSampleStats[i] = new SummaryStatistics();
    }
    Collection<SummaryStatistics> aggregate = new ArrayList<SummaryStatistics>();
    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < subSamples[i].length; j++) {
        subSampleStats[i].addValue(subSamples[i][j]);
      }
      aggregate.add(subSampleStats[i]);
    }

    // Compare values
    StatisticalSummaryValues aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
    assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
  }
  /** Similar to {@link #testAggregate()} but operating on {@link StatisticalSummary} instead. */
  @Test
  public void testAggregateStatisticalSummary() {

    // Generate a random sample and random partition
    double[] totalSample = generateSample();
    double[][] subSamples = generatePartition(totalSample);
    int nSamples = subSamples.length;

    // Compute combined stats directly
    SummaryStatistics totalStats = new SummaryStatistics();
    for (int i = 0; i < totalSample.length; i++) {
      totalStats.addValue(totalSample[i]);
    }

    // Now compute subsample stats individually and aggregate
    SummaryStatistics[] subSampleStats = new SummaryStatistics[nSamples];
    for (int i = 0; i < nSamples; i++) {
      subSampleStats[i] = new SummaryStatistics();
    }
    Collection<StatisticalSummary> aggregate = new ArrayList<StatisticalSummary>();
    for (int i = 0; i < nSamples; i++) {
      for (int j = 0; j < subSamples[i].length; j++) {
        subSampleStats[i].addValue(subSamples[i][j]);
      }
      aggregate.add(subSampleStats[i].getSummary());
    }

    // Compare values
    StatisticalSummary aggregatedStats = AggregateSummaryStatistics.aggregate(aggregate);
    assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
  }