This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git


The following commit(s) were added to refs/heads/master by this push:
     new 8acd0e71b Skip empty summaries in AggregateSummaryStatistics.aggregate 
(#354)
8acd0e71b is described below

commit 8acd0e71b51d2b1e87b15cf9bede56b677703428
Author: insaf021 <[email protected]>
AuthorDate: Sat Nov 14 19:59:03 2026 +0530

    Skip empty summaries in AggregateSummaryStatistics.aggregate (#354)
    
    Summaries with getN() == 0 are now skipped avoiding NaN statistics when 
aggregating a set of non-empty summaries with at least one empty summary.
---
 .../descriptive/AggregateSummaryStatistics.java    | 10 +++++++
 .../AggregateSummaryStatisticsTest.java            | 32 ++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatistics.java
 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatistics.java
index 058e7ebff..af1c70f0a 100644
--- 
a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatistics.java
+++ 
b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatistics.java
@@ -246,6 +246,9 @@ public class AggregateSummaryStatistics implements 
StatisticalSummary {
      * should contain the same values that would have been obtained by 
computing a single
      * StatisticalSummary over the combined dataset.
      * <p>
+     * Summaries with no values ({@code n = 0}) do not contribute to the 
result.
+     * </p>
+     * <p>
      * Returns null if the collection is empty or null.
      * </p>
      *
@@ -261,6 +264,10 @@ public class AggregateSummaryStatistics implements 
StatisticalSummary {
             return null;
         }
         StatisticalSummary current = iterator.next();
+        // An empty summary reports NaN for its statistics; skip it
+        while (current.getN() == 0 && iterator.hasNext()) {
+            current = iterator.next();
+        }
         long n = current.getN();
         double min = current.getMin();
         double sum = current.getSum();
@@ -270,6 +277,9 @@ public class AggregateSummaryStatistics implements 
StatisticalSummary {
         double mean = current.getMean();
         while (iterator.hasNext()) {
             current = iterator.next();
+            if (current.getN() == 0) {
+                continue;
+            }
             min = JdkMath.min(min, current.getMin());
             max = JdkMath.max(max, current.getMax());
             sum += current.getSum();
diff --git 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatisticsTest.java
 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatisticsTest.java
index 4e971947f..3e5af409b 100644
--- 
a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatisticsTest.java
+++ 
b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/descriptive/AggregateSummaryStatisticsTest.java
@@ -270,6 +270,38 @@ public class AggregateSummaryStatisticsTest {
         assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
     }
 
+    @Test
+    public void testAggregateEmptySummaries() {
+        double[] totalSample = {1, 2, 3, 4, 5};
+        double[][] subSamples = {{}, {1, 2}, {}, {3}, {4, 5}, {}};
+
+        // Compute combined stats directly
+        SummaryStatistics totalStats = new SummaryStatistics();
+        for (int i = 0; i < totalSample.length; i++) {
+            totalStats.addValue(totalSample[i]);
+        }
+
+        // Empty partitions contribute no values
+        Collection<SummaryStatistics> aggregate = new ArrayList<>();
+        for (int i = 0; i < subSamples.length; i++) {
+            SummaryStatistics subSampleStats = new SummaryStatistics();
+            for (int j = 0; j < subSamples[i].length; j++) {
+                subSampleStats.addValue(subSamples[i][j]);
+            }
+            aggregate.add(subSampleStats);
+        }
+
+        // Compare values
+        StatisticalSummaryValues aggregatedStats = 
AggregateSummaryStatistics.aggregate(aggregate);
+        assertEquals(totalStats.getSummary(), aggregatedStats, 10E-12);
+
+        // All partitions empty
+        Collection<SummaryStatistics> empty = new ArrayList<>();
+        empty.add(new SummaryStatistics());
+        empty.add(new SummaryStatistics());
+        assertEquals(new SummaryStatistics().getSummary(), 
AggregateSummaryStatistics.aggregate(empty), 0);
+    }
+
     /**
      * Verifies that a StatisticalSummary and a StatisticalSummaryValues are 
equal up
      * to delta, with NaNs, infinities returned in the same spots. For max, 
min, n, values

Reply via email to