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/27 23:01:27 UTC
(commons-statistics) branch master updated (868dd70 -> dec2278)
This is an automated email from the ASF dual-hosted git repository.
aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-statistics.git
from 868dd70 STATISTICS-81: Add integer standard deviation implementation
new 60adc60 Add Skewness/Kurtosis to performance test
new 5c729a6 Use the double value of n
new dec2278 Refactor division for the integer mean
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../commons/statistics/descriptive/IntMath.java | 20 +++++++++
.../commons/statistics/descriptive/IntMean.java | 5 +--
.../commons/statistics/descriptive/LongMean.java | 5 +--
.../descriptive/SumOfFourthDeviations.java | 2 +-
.../statistics/descriptive/IntMathTest.java | 31 +++++++++++++
.../commons/statistics/descriptive/TestHelper.java | 2 +
.../jmh/descriptive/IntMomentPerformance.java | 52 ++++++++++++++++++++--
src/conf/checkstyle/checkstyle-suppressions.xml | 2 +-
8 files changed, 107 insertions(+), 12 deletions(-)
(commons-statistics) 02/03: Use the double value of n
Posted by ah...@apache.org.
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 5c729a6dbe3a8917189af345c8bcce839ea9f57b
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Dec 27 22:05:59 2023 +0000
Use the double value of n
---
.../apache/commons/statistics/descriptive/SumOfFourthDeviations.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java
index 522aa34..8b4081f 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/SumOfFourthDeviations.java
@@ -158,7 +158,7 @@ class SumOfFourthDeviations extends SumOfCubedDeviations {
sumFourthDev = sumFourthDev -
sc * nDev * 8 +
ss * nDev * nDev * 24 +
- np * (np1 * np1 - 3 * np) * nDev * nDev * nDev * nDev * n * 16;
+ np * (np1 * np1 - 3 * np) * nDev * nDev * nDev * nDev * np1 * 16;
}
/**
(commons-statistics) 03/03: Refactor division for the integer mean
Posted by ah...@apache.org.
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 dec22784a51aeb0899750a69f81736f2bde0f001
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Dec 27 22:59:42 2023 +0000
Refactor division for the integer mean
---
.../commons/statistics/descriptive/IntMath.java | 20 ++++++++++++++
.../commons/statistics/descriptive/IntMean.java | 5 ++--
.../commons/statistics/descriptive/LongMean.java | 5 ++--
.../statistics/descriptive/IntMathTest.java | 31 ++++++++++++++++++++++
.../commons/statistics/descriptive/TestHelper.java | 2 ++
src/conf/checkstyle/checkstyle-suppressions.xml | 2 +-
6 files changed, 58 insertions(+), 7 deletions(-)
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMath.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMath.java
index 2309750..fd5c164 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMath.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMath.java
@@ -18,6 +18,7 @@ package org.apache.commons.statistics.descriptive;
import java.math.BigDecimal;
import java.math.BigInteger;
+import org.apache.commons.numbers.core.DD;
/**
* Support class for integer math.
@@ -35,6 +36,8 @@ final class IntMath {
private static final int EXP_SHIFT = 52;
/** 0.5. */
private static final double HALF = 0.5;
+ /** 2^53. */
+ private static final long TWO_POW_53 = 1L << 53;
/** No instances. */
private IntMath() {}
@@ -388,4 +391,21 @@ final class IntMath {
}
return y;
}
+
+ /**
+ * Divide value {@code x} by the count {@code n}.
+ *
+ * @param x Value.
+ * @param n Count.
+ * @return the quotient
+ */
+ static double divide(Int128 x, long n) {
+ final DD a = x.toDD();
+ if (n < TWO_POW_53) {
+ // n is a representable double
+ return a.divide(n).doubleValue();
+ }
+ // Extended precision divide when n > 2^53
+ return a.divide(DD.of(n)).doubleValue();
+ }
}
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMean.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMean.java
index 0713047..9bd384d 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMean.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/IntMean.java
@@ -130,9 +130,8 @@ public final class IntMean implements IntStatistic, StatisticAccumulator<IntMean
if (n < SMALL_N) {
return (double) sum.lo64() / n;
}
- // Extended precision.
- // Could divide by DD.of(n) when |n| > 2^53.
- return sum.toDD().divide(n).doubleValue();
+ // Extended precision
+ return IntMath.divide(sum, n);
}
@Override
diff --git a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongMean.java b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongMean.java
index 3f84a9e..37b2095 100644
--- a/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongMean.java
+++ b/commons-statistics-descriptive/src/main/java/org/apache/commons/statistics/descriptive/LongMean.java
@@ -127,9 +127,8 @@ public final class LongMean implements LongStatistic, StatisticAccumulator<LongM
if (sum.hi64() == 0 && Math.abs(sum.lo64()) < SMALL_SUM) {
return (double) sum.lo64() / n;
}
- // Extended precision.
- // Could divide by DD.of(n) when |n| > 2^53.
- return sum.toDD().divide(n).doubleValue();
+ // Extended precision
+ return IntMath.divide(sum, n);
}
@Override
diff --git a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/IntMathTest.java b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/IntMathTest.java
index ccc4416..b16baff 100644
--- a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/IntMathTest.java
+++ b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/IntMathTest.java
@@ -17,13 +17,18 @@
package org.apache.commons.statistics.descriptive;
+import java.math.BigDecimal;
import java.math.BigInteger;
+import java.math.MathContext;
+import java.util.Arrays;
import java.util.function.Supplier;
import java.util.stream.DoubleStream;
import java.util.stream.LongStream;
import java.util.stream.LongStream.Builder;
import java.util.stream.Stream;
import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.statistics.distribution.DoubleTolerances;
+import org.apache.commons.statistics.distribution.TestUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
@@ -203,4 +208,30 @@ class IntMathTest {
Assertions.assertEquals(0, Math.toIntExact(Math.round(x)));
Assertions.assertThrowsExactly(ArithmeticException.class, () -> IntMath.toIntExact(x));
}
+
+ @ParameterizedTest
+ @MethodSource
+ void testInt128Divide(long a, long b, long n) {
+ final Int128 x = new Int128(a, b);
+ final BigInteger bi = BigInteger.valueOf(a).shiftLeft(Long.SIZE).add(BigInteger.valueOf(b));
+ final double expected = new BigDecimal(bi).divide(
+ new BigDecimal(n), MathContext.DECIMAL128).doubleValue();
+ // This may require a 1 ulp tolerance; log the seed to allow repeats
+ TestUtils.assertEquals(expected, IntMath.divide(x, n), DoubleTolerances.equals(),
+ () -> String.format("(2^64 * %d + %d) / %d; Seed=%s",
+ a, b, n, Arrays.toString(TestHelper.createRNGSeed())));
+ }
+
+ static Stream<Arguments> testInt128Divide() {
+ final UniformRandomProvider rng = TestHelper.createRNG(TestHelper.createRNGSeed());
+ final Stream.Builder<Arguments> builder = Stream.builder();
+ for (int i = 0; i < 100; i++) {
+ final long a = rng.nextLong();
+ final long b = rng.nextLong();
+ final long n = rng.nextLong();
+ builder.accept(Arguments.of(a, b, n >>> 1));
+ builder.accept(Arguments.of(a, b, n >>> 43));
+ }
+ return builder.build();
+ }
}
diff --git a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/TestHelper.java b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/TestHelper.java
index 22c385d..a1a57e5 100644
--- a/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/TestHelper.java
+++ b/commons-statistics-descriptive/src/test/java/org/apache/commons/statistics/descriptive/TestHelper.java
@@ -218,6 +218,8 @@ final class TestHelper {
/**
* Creates a seed for the RNG.
+ * This is the same seed within an invocation of the JVM; seeds will be different
+ * across JVM instances.
*
* @return the seed
* @see #createRNG(long[])
diff --git a/src/conf/checkstyle/checkstyle-suppressions.xml b/src/conf/checkstyle/checkstyle-suppressions.xml
index c363f46..dd425bc 100644
--- a/src/conf/checkstyle/checkstyle-suppressions.xml
+++ b/src/conf/checkstyle/checkstyle-suppressions.xml
@@ -41,5 +41,5 @@
<suppress checks="MethodLength" files=".*[/\\]WilcoxonSignedRankTestTest.java" />
<suppress checks="IllegalCatch" files=".*[/\\]TestHelper.java" lines="390-450" />
<suppress checks="IllegalCatch" files=".*[/\\]BaseStatisticTest.java" lines="280-400" />
- <suppress checks="IllegalCatch" files=".*[/\\]IntMathTest.java" lines="165-175" />
+ <suppress checks="IllegalCatch" files=".*[/\\]IntMathTest.java" lines="170-185" />
</suppressions>
(commons-statistics) 01/03: Add Skewness/Kurtosis to performance test
Posted by ah...@apache.org.
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 60adc6084861946cfb2f93b1e464e1dfce38326c
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Wed Dec 27 21:10:02 2023 +0000
Add Skewness/Kurtosis to performance test
---
.../jmh/descriptive/IntMomentPerformance.java | 52 ++++++++++++++++++++--
1 file changed, 48 insertions(+), 4 deletions(-)
diff --git a/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/IntMomentPerformance.java b/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/IntMomentPerformance.java
index cbc4af6..b863e8c 100644
--- a/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/IntMomentPerformance.java
+++ b/commons-statistics-examples/examples-jmh/src/main/java/org/apache/commons/statistics/examples/jmh/descriptive/IntMomentPerformance.java
@@ -39,10 +39,12 @@ import org.apache.commons.statistics.descriptive.DoubleStatistic;
import org.apache.commons.statistics.descriptive.IntMean;
import org.apache.commons.statistics.descriptive.IntStatistic;
import org.apache.commons.statistics.descriptive.IntVariance;
+import org.apache.commons.statistics.descriptive.Kurtosis;
import org.apache.commons.statistics.descriptive.LongMean;
import org.apache.commons.statistics.descriptive.LongStatistic;
import org.apache.commons.statistics.descriptive.LongVariance;
import org.apache.commons.statistics.descriptive.Mean;
+import org.apache.commons.statistics.descriptive.Skewness;
import org.apache.commons.statistics.descriptive.Variance;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -89,6 +91,10 @@ public class IntMomentPerformance {
private static final String LONG_VAR = "LongVariance";
/** Long variance implementation using Math.multiplyHigh. */
private static final String LONG_VAR2 = "LongVariance2";
+ /** Commons Statistics Skewness implementation. */
+ private static final String DOUBLE_SKEWNESS = "DoubleSkewness";
+ /** Commons Statistics Kurtosis implementation. */
+ private static final String DOUBLE_KURTOSIS = "DoubleKurtosis";
/**
* Source of array data.
@@ -150,7 +156,8 @@ public class IntMomentPerformance {
@Param({DOUBLE_MEAN, INT_MEAN,
// Disabled: Run-time ~ IntMean
// LONG_SUM_MEAN
- DOUBLE_VAR, INT_VAR})
+ DOUBLE_VAR, INT_VAR,
+ DOUBLE_SKEWNESS, DOUBLE_KURTOSIS})
private String name;
/** The action. */
@@ -193,6 +200,16 @@ public class IntMomentPerformance {
final IntVariance m = IntVariance.create();
return createIntStatistic(m, m);
};
+ } else if (DOUBLE_SKEWNESS.equals(name)) {
+ action = () -> {
+ final Skewness m = Skewness.create();
+ return createIntStatistic(m, m);
+ };
+ } else if (DOUBLE_KURTOSIS.equals(name)) {
+ action = () -> {
+ final Kurtosis m = Kurtosis.create();
+ return createIntStatistic(m, m);
+ };
} else {
throw new IllegalStateException("Unknown int action: " + name);
}
@@ -245,7 +262,8 @@ public class IntMomentPerformance {
@State(Scope.Benchmark)
public static class DoubleActionSource {
/** Name of the source. */
- @Param({DOUBLE_MEAN, DOUBLE_VAR})
+ @Param({DOUBLE_MEAN, DOUBLE_VAR,
+ DOUBLE_SKEWNESS, DOUBLE_KURTOSIS})
private String name;
/** The action. */
@@ -273,6 +291,16 @@ public class IntMomentPerformance {
final Variance m = Variance.create();
return createDoubleStatistic(m, m);
};
+ } else if (DOUBLE_SKEWNESS.equals(name)) {
+ action = () -> {
+ final Skewness m = Skewness.create();
+ return createDoubleStatistic(m, m);
+ };
+ } else if (DOUBLE_KURTOSIS.equals(name)) {
+ action = () -> {
+ final Kurtosis m = Kurtosis.create();
+ return createDoubleStatistic(m, m);
+ };
} else {
throw new IllegalStateException("Unknown double action: " + name);
}
@@ -310,7 +338,8 @@ public class IntMomentPerformance {
public static class LongActionSource {
/** Name of the source. */
@Param({DOUBLE_MEAN, LONG_MEAN, BIG_INTEGER_SUM_MEAN,
- DOUBLE_VAR, LONG_VAR, LONG_VAR2})
+ DOUBLE_VAR, LONG_VAR, LONG_VAR2,
+ DOUBLE_SKEWNESS, DOUBLE_KURTOSIS})
private String name;
/** The action. */
@@ -358,6 +387,16 @@ public class IntMomentPerformance {
final LongVariance2 m = LongVariance2.create();
return createLongStatistic(m, m);
};
+ } else if (DOUBLE_SKEWNESS.equals(name)) {
+ action = () -> {
+ final Skewness m = Skewness.create();
+ return createLongStatistic(m, m);
+ };
+ } else if (DOUBLE_KURTOSIS.equals(name)) {
+ action = () -> {
+ final Kurtosis m = Kurtosis.create();
+ return createLongStatistic(m, m);
+ };
} else {
throw new IllegalStateException("Unknown long action: " + name);
}
@@ -451,7 +490,8 @@ public class IntMomentPerformance {
@State(Scope.Benchmark)
public static class DoubleFunctionSource {
/** Name of the source. */
- @Param({DOUBLE_MEAN, DOUBLE_VAR})
+ @Param({DOUBLE_MEAN, DOUBLE_VAR,
+ DOUBLE_SKEWNESS, DOUBLE_KURTOSIS})
private String name;
/** The action. */
@@ -473,6 +513,10 @@ public class IntMomentPerformance {
function = x -> Mean.of(x).getAsDouble();
} else if (DOUBLE_VAR.equals(name)) {
function = x -> Variance.of(x).getAsDouble();
+ } else if (DOUBLE_SKEWNESS.equals(name)) {
+ function = x -> Skewness.of(x).getAsDouble();
+ } else if (DOUBLE_KURTOSIS.equals(name)) {
+ function = x -> Kurtosis.of(x).getAsDouble();
} else {
throw new IllegalStateException("Unknown double function: " + name);
}