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 2023/12/30 08:32:08 UTC
(commons-statistics) 04/06: Add additional mean computations to benchmark
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
commit 2e8bbc267e88708c7c03fe8b960e3ba5e88092b8
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Dec 29 20:19:49 2023 +0000
Add additional mean computations to benchmark
---
.../jmh/descriptive/MomentPerformance.java | 40 ++++++++++++++++++++++
.../jmh/descriptive/MomentPerformanceTest.java | 2 ++
2 files changed, 42 insertions(+)
diff --git a/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformance.java b/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformance.java
index 7b77433..a071083 100644
--- a/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformance.java
+++ b/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformance.java
@@ -23,6 +23,8 @@ import java.util.function.DoubleConsumer;
import java.util.function.DoubleSupplier;
import java.util.function.Function;
import java.util.function.Supplier;
+import org.apache.commons.numbers.core.DD;
+import org.apache.commons.numbers.core.Sum;
import org.apache.commons.rng.simple.RandomSource;
import org.apache.commons.statistics.descriptive.Mean;
import org.openjdk.jmh.annotations.Benchmark;
@@ -146,6 +148,7 @@ public class MomentPerformance {
/** Name of the source. */
@Param({MEAN, ROLLING_MEAN, SAFE_ROLLING_MEAN, SCALED_ROLLING_MEAN,
INLINE_SAFE_ROLLING_MEAN, INLINE_SAFE_ROLLING_MEAN_EXT,
+ SUM_MEAN, EXTENDED_SUM_MEAN, "DDMean",
// Same speed as the ROLLING_MEAN, i.e. the DoubleConsumer is not an overhead
//INLINE_ROLLING_MEAN
// Higher moments
@@ -183,6 +186,13 @@ public class MomentPerformance {
function = MomentPerformance::arrayInlineSafeRollingFirstMoment;
} else if (INLINE_SAFE_ROLLING_MEAN_EXT.equals(name)) {
function = MomentPerformance::arrayInlineSafeRollingFirstMomentExt;
+ } else if (SUM_MEAN.equals(name)) {
+ function = MomentPerformance::arraySumMean;
+ } else if (EXTENDED_SUM_MEAN.equals(name)) {
+ // This returns the IEEE result for non-finite input, or overflow
+ function = x -> Sum.of(x).getAsDouble() / x.length;
+ } else if ("DDMean".equals(name)) {
+ function = MomentPerformance::arrayDDSumMean;
} else if ("SumOfCubed".equals(name)) {
function = MomentPerformance::arraySumOfCubed;
} else if ("SumOfCubedPow".equals(name)) {
@@ -503,6 +513,36 @@ public class MomentPerformance {
return correctMeanKahan(data, m1);
}
+ /**
+ * Create the mean using a single pass sum.
+ * The mean is not safe against overflow.
+ *
+ * @param data Data.
+ * @return the statistic
+ */
+ static double arraySumMean(double[] data) {
+ double s = 0;
+ for (final double x : data) {
+ s += x;
+ }
+ return s / data.length;
+ }
+
+ /**
+ * Create themean using a single pass sum using double-double precision.
+ * The mean is not safe against overflow or non-finite input.
+ *
+ * @param data Data.
+ * @return the statistic
+ */
+ static double arrayDDSumMean(double[] data) {
+ DD s = DD.ZERO;
+ for (final double x : data) {
+ s = s.add(x);
+ }
+ return s.doubleValue() / data.length;
+ }
+
/**
* Create the sum-of-cubed deviations from the mean.
*
diff --git a/commons-statistics-examples/examples-jmh/src/test/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformanceTest.java b/commons-statistics-examples/examples-jmh/src/test/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformanceTest.java
index 11a320a..3d51247 100644
--- a/commons-statistics-examples/examples-jmh/src/test/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformanceTest.java
+++ b/commons-statistics-examples/examples-jmh/src/test/java/org/apache/commons/statistics/examples/jmh/descriptive/MomentPerformanceTest.java
@@ -47,5 +47,7 @@ class MomentPerformanceTest {
Assertions.assertEquals(expected, MomentPerformance.arrayInlineRollingFirstMoment(values), tolerance);
Assertions.assertEquals(expected, MomentPerformance.arrayInlineSafeRollingFirstMoment(values), tolerance);
Assertions.assertEquals(expected, MomentPerformance.arrayInlineSafeRollingFirstMomentExt(values), tolerance);
+ Assertions.assertEquals(expected, MomentPerformance.arraySumMean(values), tolerance);
+ Assertions.assertEquals(expected, MomentPerformance.arrayDDSumMean(values), tolerance);
}
}