You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2024/01/05 13:35:10 UTC
(commons-statistics) branch master updated: STATISTICS-84: Create the Mean from array input using a summation
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-statistics.git
The following commit(s) were added to refs/heads/master by this push:
new 5e3d7ae STATISTICS-84: Create the Mean from array input using a summation
5e3d7ae is described below
commit 5e3d7ae503da4493fe9131cf7cddb9cfb328f431
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Jan 5 11:10:58 2024 +0000
STATISTICS-84: Create the Mean from array input using a summation
Revert to the rolling algorithm in the event of overflow.
---
.../statistics/descriptive/FirstMoment.java | 23 ++++++++++++++++++++--
.../commons/statistics/descriptive/Mean.java | 3 ++-
.../commons/statistics/descriptive/MeanTest.java | 15 +++++++-------
3 files changed, 30 insertions(+), 11 deletions(-)
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
index c928169..2ef8ad8 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/FirstMoment.java
@@ -126,8 +126,7 @@ class FirstMoment implements DoubleConsumer {
/**
* Create an instance with the given first moment.
*
- * <p>This constructor is used when creating the moment from integer values. The
- * non-finite value is not required.
+ * <p>This constructor is used when creating the moment from a finite sum of values.
*
* @param m1 First moment.
* @param n Count of values.
@@ -150,6 +149,26 @@ class FirstMoment implements DoubleConsumer {
if (values.length == 0) {
return new FirstMoment();
}
+ // In the typical use-case a sum of values will not overflow and
+ // is faster than the rolling algorithm
+ return create(org.apache.commons.numbers.core.Sum.of(values), values);
+ }
+
+ /**
+ * Creates the first moment.
+ *
+ * <p>Uses the provided {@code sum} if finite; otherwise reverts to using the rolling moment
+ * to protect from overflow and adds a second pass correction term.
+ *
+ * @param sum Sum of the values.
+ * @param values Values.
+ * @return {@code FirstMoment} instance.
+ */
+ private static FirstMoment create(org.apache.commons.numbers.core.Sum sum, double... values) {
+ final double s = sum.getAsDouble();
+ if (Double.isFinite(s)) {
+ return new FirstMoment(s / values.length, values.length);
+ }
// "Corrected two-pass algorithm"
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Mean.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Mean.java
index 147d631..0eb3a8c 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Mean.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/Mean.java
@@ -40,7 +40,8 @@ package org.apache.commons.statistics.descriptive;
* \( m_{i+1} = m_i + (x - m_i) / (i + 1) \)</li>
* </ol>
*
- * <p>The {@link #of(double...)} method uses a corrected two-pass algorithm, starting with
+ * <p>The {@link #of(double...)} method uses an extended precision sum if the sum is finite.
+ * Otherwise uses a corrected two-pass algorithm, starting with
* the recursive updating algorithm mentioned above, and then correcting this by adding the
* mean deviation of the data values from the one-pass mean (see Ling (1974)).
*
diff --git a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/MeanTest.java b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/MeanTest.java
index cf07075..5094773 100644
--- a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/MeanTest.java
+++ b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/MeanTest.java
@@ -70,7 +70,7 @@ final class MeanTest extends BaseDoubleStatisticTest<Mean> {
@Override
protected DoubleTolerance getToleranceArray() {
- return DoubleTolerances.ulps(8);
+ return DoubleTolerances.ulps(3);
}
@Override
@@ -80,7 +80,7 @@ final class MeanTest extends BaseDoubleStatisticTest<Mean> {
@Override
protected DoubleTolerance getToleranceArrayAndCombine() {
- return DoubleTolerances.ulps(5);
+ return DoubleTolerances.ulps(3);
}
@Override
@@ -97,13 +97,12 @@ final class MeanTest extends BaseDoubleStatisticTest<Mean> {
// Actual mean is 1.1 / 2 = 0.55; numpy computes 0.5500000000000007
// Use the actual mean on this difficult case.
// This case is difficult for the accept method or any combine when the data is
- // not randomised. The dual-pass array method is good on a single array, but not
- // the array and combine.
+ // not randomised.
builder.accept(addReference(0.55,
- createRelTolerance(1e-13), // accept
- createRelTolerance(5e-14), // array
- createRelTolerance(1e-13), // accept and combine
- createRelTolerance(3e-13), // array and combine
+ createRelTolerance(2e-13), // accept
+ createRelTolerance(5e-16), // array
+ createRelTolerance(2e-13), // accept and combine
+ createRelTolerance(1e-15), // array and combine
a));
// R v4.3.1: mean(x)
builder.accept(addReference(5.5, DoubleTolerances.ulps(1), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10));