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 2022/10/03 11:10:45 UTC
[commons-statistics] branch master updated (9040f25 -> 5da629d)
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 9040f25 STATISTICS-55: TruncatedNormalDistribution sampler
new 5f4e221 Update RNG 1.4 -> 1.5-SNAPSHOT
new 5b67ad1 Update ZipfDistribution to always use RejectionInversionZipfSampler
new 9632942 Use LogNormalSampler from commons RNG
new 5b5717d Use TSampler from commons RNG
new 5da629d Remove dependency on commons-rng-core module
The 5 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-distribution/pom.xml | 5 --
.../distribution/LogNormalDistribution.java | 5 +-
.../statistics/distribution/TDistribution.java | 98 +---------------------
.../statistics/distribution/ZipfDistribution.java | 9 +-
.../distribution/ZipfDistributionTest.java | 2 +-
commons-statistics-regression/pom.xml | 5 --
pom.xml | 17 ++--
7 files changed, 13 insertions(+), 128 deletions(-)
[commons-statistics] 04/05: Use TSampler from commons RNG
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 5b5717d2b8d60eec498750b61153d372e7c83212
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Oct 3 12:02:22 2022 +0100
Use TSampler from commons RNG
---
.../statistics/distribution/TDistribution.java | 98 +---------------------
1 file changed, 3 insertions(+), 95 deletions(-)
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
index cd3c2b2..e828276 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TDistribution.java
@@ -18,7 +18,7 @@ package org.apache.commons.statistics.distribution;
import org.apache.commons.numbers.gamma.RegularizedBeta;
import org.apache.commons.rng.UniformRandomProvider;
-import java.util.function.DoubleUnaryOperator;
+import org.apache.commons.rng.sampling.distribution.TSampler;
import org.apache.commons.numbers.gamma.Beta;
import org.apache.commons.numbers.gamma.LogBeta;
@@ -231,100 +231,8 @@ public abstract class TDistribution extends AbstractContinuousDistribution {
@Override
public Sampler createSampler(UniformRandomProvider rng) {
- // TODO: Move sampler to Commons RNG
- return new TSampler(rng, getDegreesOfFreedom());
- }
-
- /**
- * Sampling from a T-distribution.
- * <blockquote>
- * Bailey, R. W. (1994)
- * Polar Generation of Random Variates with the t-Distribution.
- * Mathematics of Computation 62, 779-781.
- * </blockquote>
- * @see <a href="https://doi.org/10.2307/2153537">Mathematics of Computation, 62, 779-781</a>
- */
- private static class TSampler implements Sampler {
- /** Threshold for large degrees of freedom. */
- private static final double LARGE_DF = 25;
- /** The multiplier to convert the least significant 53-bits of a {@code long} to a
- * uniform {@code double}. */
- private static final double DOUBLE_MULTIPLIER = 0x1.0p-53;
-
- /** Source of randomness. */
- private final UniformRandomProvider rng;
- /** Degrees of freedom. */
- private final double df;
- /** Function to compute pow(x, -2/v) - 1, where v = degrees of freedom. */
- private final DoubleUnaryOperator powm1;
-
- /**
- * @param rng Source of randomness.
- * @param v Degrees of freedom.
- */
- TSampler(UniformRandomProvider rng, double v) {
- this.rng = rng;
- df = v;
-
- // The sampler requires pow(w, -2/v) - 1 with
- // 0 <= w <= 1; Expected(w) = sqrt(0.5).
- // When the exponent is small then pow(x, y) -> 1.
- // This affects large degrees of freedom.
- final double exponent = -2 / v;
- powm1 = v > LARGE_DF ?
- x -> Math.expm1(Math.log(x) * exponent) :
- x -> Math.pow(x, exponent) - 1;
- }
-
- @Override
- public double sample() {
- // Require u and v in [0, 1] and a random sign.
- // Create u in (0, 1] to avoid generating nan
- // from u*u/w (0/0) or r2*c2 (inf*0).
- final double u = makeNonZeroDouble(rng.nextLong());
- final double v = makeSignedDouble(rng.nextLong());
- final double w = u * u + v * v;
- if (w > 1) {
- // Rejection frequency = 1 - pi/4 = 0.215.
- // Recursion will generate stack overflow given a broken RNG
- // and avoids an infinite loop.
- return sample();
- }
- // Sidestep a square-root calculation.
- final double c2 = u * u / w;
- final double r2 = df * powm1.applyAsDouble(w);
- // Choose sign at random from the sign of v.
- return Math.copySign(Math.sqrt(r2 * c2), v);
- }
-
- /**
- * Creates a {@code double} in the interval {@code (0, 1]} from a {@code long} value.
- *
- * @param v Number.
- * @return {@code (0, 1]}.
- */
- private static double makeNonZeroDouble(long v) {
- // This matches the method in o.a.c.rng.core.util.NumberFactory.makeDouble(long)
- // but shifts the range from [0, 1) to (0, 1].
- return ((v >>> 11) + 1L) * DOUBLE_MULTIPLIER;
- }
-
- /**
- * Creates a signed double in the range {@code [-1, 1)}.
- *
- * <p>Note: This method will not return samples for both -0.0 and 0.0.
- *
- * @param bits the bits
- * @return {@code [-1, 1)}.
- */
- private static double makeSignedDouble(long bits) {
- // As per o.a.c.rng.core.utils.NumberFactory.makeDouble(long) but using a signed
- // shift of 10 in place of an unsigned shift of 11.
- // Use the upper 54 bits on the assumption they are more random.
- // The sign bit is maintained by the signed shift.
- // The next 53 bits generates a magnitude in the range [0, 2^53) or [-2^53, 0).
- return (bits >> 10) * DOUBLE_MULTIPLIER;
- }
+ // T distribution sampler.
+ return TSampler.of(rng, getDegreesOfFreedom())::sample;
}
}
[commons-statistics] 01/05: Update RNG 1.4 -> 1.5-SNAPSHOT
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 5f4e221f7d741c1744222e8a47aaadea3f9e4224
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Oct 3 11:30:49 2022 +0100
Update RNG 1.4 -> 1.5-SNAPSHOT
Manage dependencies using the RNG Bill of Materials
---
pom.xml | 17 +++++------------
1 file changed, 5 insertions(+), 12 deletions(-)
diff --git a/pom.xml b/pom.xml
index c20312a..9a92b19 100644
--- a/pom.xml
+++ b/pom.xml
@@ -125,7 +125,8 @@
Temporary workaround?
-->
<commons.release.name>commons-statistics-${project.version}</commons.release.name>
- <statistics.commons.rng.version>1.4</statistics.commons.rng.version>
+ <!-- TODO: Release dependency Commons RNG -->
+ <statistics.commons.rng.version>1.5-SNAPSHOT</statistics.commons.rng.version>
<!-- TODO: Release dependency Commons Numbers -->
<statistics.commons.numbers.version>1.1-SNAPSHOT</statistics.commons.numbers.version>
<statistics.commons.math3.version>3.6.1</statistics.commons.math3.version>
@@ -139,18 +140,10 @@
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
- <artifactId>commons-rng-client-api</artifactId>
- <version>${statistics.commons.rng.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-rng-simple</artifactId>
- <version>${statistics.commons.rng.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-rng-sampling</artifactId>
+ <artifactId>commons-rng-bom</artifactId>
<version>${statistics.commons.rng.version}</version>
+ <scope>import</scope>
+ <type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
[commons-statistics] 05/05: Remove dependency on commons-rng-core module
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 5da629da2073fdeb6518e55df12d3949fc5ffccb
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Oct 3 12:09:59 2022 +0100
Remove dependency on commons-rng-core module
The statistics API requires the commons-rng-client-api. The
implementation requires commons-rng-sampling. The commons-rng-core
module is not required. The dependency is imported transiently for
testing using commons-rng-simple.
---
commons-statistics-distribution/pom.xml | 5 -----
commons-statistics-regression/pom.xml | 5 -----
2 files changed, 10 deletions(-)
diff --git a/commons-statistics-distribution/pom.xml b/commons-statistics-distribution/pom.xml
index 03ba681..e496b1c 100644
--- a/commons-statistics-distribution/pom.xml
+++ b/commons-statistics-distribution/pom.xml
@@ -53,11 +53,6 @@
<artifactId>commons-rng-sampling</artifactId>
</dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-numbers-core</artifactId>
- </dependency>
-
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-numbers-combinatorics</artifactId>
diff --git a/commons-statistics-regression/pom.xml b/commons-statistics-regression/pom.xml
index ed321e6..0f616a6 100644
--- a/commons-statistics-regression/pom.xml
+++ b/commons-statistics-regression/pom.xml
@@ -43,11 +43,6 @@
<dependencies>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-numbers-core</artifactId>
- </dependency>
-
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
[commons-statistics] 02/05: Update ZipfDistribution to always use RejectionInversionZipfSampler
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 5b67ad1dd7ac2c4e1b704fda3a92ad7a4bccc51b
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Oct 3 11:54:54 2022 +0100
Update ZipfDistribution to always use RejectionInversionZipfSampler
The RejectionInversionZipfSampler was modified in release 1.5 to allow
an exponent of 0.
Change fixed seed for ZipfDistributionTest. Seed expansion from
primitive seeds has changed in RNG 1.5.
---
.../apache/commons/statistics/distribution/ZipfDistribution.java | 9 ++-------
.../commons/statistics/distribution/ZipfDistributionTest.java | 2 +-
2 files changed, 3 insertions(+), 8 deletions(-)
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/ZipfDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/ZipfDistribution.java
index b9933d5..82bbdde 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/ZipfDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/ZipfDistribution.java
@@ -18,7 +18,6 @@
package org.apache.commons.statistics.distribution;
import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.sampling.distribution.DiscreteUniformSampler;
import org.apache.commons.rng.sampling.distribution.RejectionInversionZipfSampler;
/**
@@ -269,11 +268,7 @@ public final class ZipfDistribution extends AbstractDiscreteDistribution {
/** {@inheritDoc} */
@Override
public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) {
- if (exponent > 0) {
- // Zipf distribution sampler.
- return RejectionInversionZipfSampler.of(rng, numberOfElements, exponent)::sample;
- }
- // Special case when exponent is zero then the sample is uniform
- return DiscreteUniformSampler.of(rng, 1, numberOfElements)::sample;
+ // Zipf distribution sampler.
+ return RejectionInversionZipfSampler.of(rng, numberOfElements, exponent)::sample;
}
}
diff --git a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ZipfDistributionTest.java b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ZipfDistributionTest.java
index 640e7ed..e3675f8 100644
--- a/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ZipfDistributionTest.java
+++ b/commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/ZipfDistributionTest.java
@@ -114,7 +114,7 @@ class ZipfDistributionTest extends BaseDiscreteDistributionTest {
// that all test cases do not fail is 0.999^(32*22) = 0.49442874426
final DiscreteDistribution.Sampler distribution =
ZipfDistribution.of(numPoints, exponent).createSampler(
- RandomSource.XO_SHI_RO_256_PP.create(6));
+ RandomSource.XO_SHI_RO_256_PP.create(1));
final double[] expectedCounts = new double[numPoints];
final long[] observedCounts = new long[numPoints];
[commons-statistics] 03/05: Use LogNormalSampler from commons RNG
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 9632942c5059448da1a7106531ec65ab281e39e8
Author: aherbert <ah...@apache.org>
AuthorDate: Mon Oct 3 12:01:50 2022 +0100
Use LogNormalSampler from commons RNG
---
.../commons/statistics/distribution/LogNormalDistribution.java | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/LogNormalDistribution.java b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/LogNormalDistribution.java
index 8e667b9..686aa03 100644
--- a/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/LogNormalDistribution.java
+++ b/commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/LogNormalDistribution.java
@@ -21,6 +21,7 @@ import org.apache.commons.numbers.gamma.ErfDifference;
import org.apache.commons.numbers.gamma.Erfc;
import org.apache.commons.numbers.gamma.InverseErfc;
import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.sampling.distribution.LogNormalSampler;
import org.apache.commons.rng.sampling.distribution.ZigguratSampler;
/**
@@ -261,8 +262,6 @@ public final class LogNormalDistribution extends AbstractContinuousDistribution
public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) {
// Log normal distribution sampler.
final ZigguratSampler.NormalizedGaussian gaussian = ZigguratSampler.NormalizedGaussian.of(rng);
- // TODO: Replace with LogNormalSampler in Commons RNG when
- // the version supporting negative mu has been released.
- return () -> Math.exp(mu + sigma * gaussian.sample());
+ return LogNormalSampler.of(gaussian, mu, sigma)::sample;
}
}