You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2017/04/07 16:26:41 UTC

[1/4] commons-rng git commit: RNG-36: Class renamed.

Repository: commons-rng
Updated Branches:
  refs/heads/master 805ac0425 -> 2964c6ae6


RNG-36: Class renamed.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/4ef78f53
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/4ef78f53
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/4ef78f53

Branch: refs/heads/master
Commit: 4ef78f53b61eb5df545751da8d3895a3206c1b5a
Parents: 805ac04
Author: Gilles <er...@apache.org>
Authored: Fri Apr 7 15:58:47 2017 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Apr 7 15:58:47 2017 +0200

----------------------------------------------------------------------
 .../AhrensDieterMarsagliaTsangGammaSampler.java |  2 +-
 .../distribution/BoxMullerLogNormalSampler.java |  2 +-
 ...rWithRejectionNormalizedGaussianSampler.java | 88 --------------------
 .../MarsagliaNormalizedGaussianSampler.java     | 87 +++++++++++++++++++
 .../sampling/distribution/PoissonSampler.java   |  2 +-
 .../distribution/ContinuousSamplersList.java    |  2 +-
 6 files changed, 91 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
index baf3b17..7883362 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/AhrensDieterMarsagliaTsangGammaSampler.java
@@ -60,7 +60,7 @@ public class AhrensDieterMarsagliaTsangGammaSampler
         super(rng);
         this.alpha = alpha;
         this.theta = theta;
-        gaussian = new BoxMullerWithRejectionNormalizedGaussianSampler(rng);
+        gaussian = new MarsagliaNormalizedGaussianSampler(rng);
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
index 5e4ba22..e793e38 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerLogNormalSampler.java
@@ -43,7 +43,7 @@ public class BoxMullerLogNormalSampler
         super(null); // Not used.
         this.scale = scale;
         this.shape = shape;
-        gaussian = new BoxMullerWithRejectionNormalizedGaussianSampler(rng);
+        gaussian = new MarsagliaNormalizedGaussianSampler(rng);
     }
 
     /** {@inheritDoc} */

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerWithRejectionNormalizedGaussianSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerWithRejectionNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerWithRejectionNormalizedGaussianSampler.java
deleted file mode 100644
index ae77ce6..0000000
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/BoxMullerWithRejectionNormalizedGaussianSampler.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.commons.rng.sampling.distribution;
-
-import org.apache.commons.rng.UniformRandomProvider;
-
-/**
- * <a href="https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform">
- * Box-Muller algorithm</a> for sampling from Gaussian distribution with
- * mean 0 and standard deviation 1.
- * This is a variation, suggested in <a href="http://haifux.org/lectures/79/random.pdf">
- * this presentation</a> (page 39), of the algorithm implemented in
- * {@link BoxMullerNormalizedGaussianSampler}.
- *
- * @since 1.1
- */
-public class BoxMullerWithRejectionNormalizedGaussianSampler
-    extends SamplerBase
-    implements NormalizedGaussianSampler {
-    /** Next gaussian. */
-    private double nextGaussian = Double.NaN;
-
-    /**
-     * @param rng Generator of uniformly distributed random numbers.
-     */
-    public BoxMullerWithRejectionNormalizedGaussianSampler(UniformRandomProvider rng) {
-        super(rng);
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public double sample() {
-        final double random;
-        if (Double.isNaN(nextGaussian)) {
-            // Rejection scheme for selecting a pair that lies within the unit circle.
-            SAMPLE: while (true) {
-                // Generate a pair of numbers within [-1 , 1).
-                final double x = 2 * nextDouble() - 1;
-                final double y = 2 * nextDouble() - 1;
-                final double r2 = x * x + y * y;
-
-                if (r2 < 1) {
-                    // Pair (x, y) is within unit circle.
-
-                    final double r = Math.sqrt(r2);
-                    final double alpha = 2 * Math.sqrt(-Math.log(r)) / r;
-
-                    // Return the first element of the generated pair.
-                    random = alpha * x;
-
-                    // Keep second element of the pair for next invocation.
-                    nextGaussian = alpha * y;
-                    break SAMPLE;
-                }
-                // Pair is not within the unit circle: Generate another one.
-            }
-        } else {
-            // Use the second element of the pair (generated at the
-            // previous invocation).
-            random = nextGaussian;
-
-            // Both elements of the pair have been used.
-            nextGaussian = Double.NaN;
-        }
-
-        return random;
-    }
-
-    /** {@inheritDoc} */
-    @Override
-    public String toString() {
-        return "Box-Muller (with rejection) normalized Gaussian deviate [" + super.toString() + "]";
-    }
-}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
new file mode 100644
index 0000000..6dc9f2a
--- /dev/null
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rng.sampling.distribution;
+
+import org.apache.commons.rng.UniformRandomProvider;
+
+/**
+ * <a href="https://en.wikipedia.org/wiki/Marsaglia_polar_method">
+ * Marsaglia polar method</a> for sampling from a Gaussian distribution
+ * with mean 0 and standard deviation 1.
+ * This is a variation of the algorithm implemented in
+ * {@link BoxMullerNormalizedGaussianSampler}.
+ *
+ * @since 1.1
+ */
+public class MarsagliaNormalizedGaussianSampler
+    extends SamplerBase
+    implements NormalizedGaussianSampler {
+    /** Next gaussian. */
+    private double nextGaussian = Double.NaN;
+
+    /**
+     * @param rng Generator of uniformly distributed random numbers.
+     */
+    public MarsagliaNormalizedGaussianSampler(UniformRandomProvider rng) {
+        super(rng);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public double sample() {
+        final double random;
+        if (Double.isNaN(nextGaussian)) {
+            // Rejection scheme for selecting a pair that lies within the unit circle.
+            SAMPLE: while (true) {
+                // Generate a pair of numbers within [-1 , 1).
+                final double x = 2 * nextDouble() - 1;
+                final double y = 2 * nextDouble() - 1;
+                final double r2 = x * x + y * y;
+
+                if (r2 < 1) {
+                    // Pair (x, y) is within unit circle.
+
+                    final double r = Math.sqrt(r2);
+                    final double alpha = 2 * Math.sqrt(-Math.log(r)) / r;
+
+                    // Return the first element of the generated pair.
+                    random = alpha * x;
+
+                    // Keep second element of the pair for next invocation.
+                    nextGaussian = alpha * y;
+                    break SAMPLE;
+                }
+                // Pair is not within the unit circle: Generate another one.
+            }
+        } else {
+            // Use the second element of the pair (generated at the
+            // previous invocation).
+            random = nextGaussian;
+
+            // Both elements of the pair have been used.
+            nextGaussian = Double.NaN;
+        }
+
+        return random;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String toString() {
+        return "Box-Muller (with rejection) normalized Gaussian deviate [" + super.toString() + "]";
+    }
+}

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
index ea40f81..50e696e 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSampler.java
@@ -64,7 +64,7 @@ public class PoissonSampler
 
         this.mean = mean;
 
-        gaussian = new BoxMullerWithRejectionNormalizedGaussianSampler(rng);
+        gaussian = new MarsagliaNormalizedGaussianSampler(rng);
         exponential = new AhrensDieterExponentialSampler(rng, 1);
         factorialLog = mean < PIVOT ?
             null : // Not used.

http://git-wip-us.apache.org/repos/asf/commons-rng/blob/4ef78f53/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
index aa0e458..0383928 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/ContinuousSamplersList.java
@@ -50,7 +50,7 @@ public class ContinuousSamplersList {
                                     meanNormal, sigmaNormal));
             // Gaussian ("Box-Muller" with rejection).
             add(LIST, new org.apache.commons.math3.distribution.NormalDistribution(meanNormal, sigmaNormal),
-                new GaussianSampler(new BoxMullerWithRejectionNormalizedGaussianSampler(RandomSource.create(RandomSource.MT)),
+                new GaussianSampler(new MarsagliaNormalizedGaussianSampler(RandomSource.create(RandomSource.MT)),
                                     meanNormal, sigmaNormal));
 
             // Beta ("inverse method").


[3/4] commons-rng git commit: RNG-36: Ensure precondition.

Posted by er...@apache.org.
RNG-36: Ensure precondition.

Not both elements of the generated pair can be zero.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/b1b44a5c
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/b1b44a5c
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/b1b44a5c

Branch: refs/heads/master
Commit: b1b44a5ce686c4b19b6c642fe38c24b9f33d5805
Parents: e591a65
Author: Gilles <er...@apache.org>
Authored: Fri Apr 7 16:21:13 2017 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Apr 7 16:21:13 2017 +0200

----------------------------------------------------------------------
 .../MarsagliaNormalizedGaussianSampler.java     | 27 ++++++++++----------
 1 file changed, 13 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/b1b44a5c/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
index 6c86452..0ef6880 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
@@ -43,7 +43,6 @@ public class MarsagliaNormalizedGaussianSampler
     /** {@inheritDoc} */
     @Override
     public double sample() {
-        final double random;
         if (Double.isNaN(nextGaussian)) {
             // Rejection scheme for selecting a pair that lies within the unit circle.
             SAMPLE: while (true) {
@@ -52,30 +51,30 @@ public class MarsagliaNormalizedGaussianSampler
                 final double y = 2 * nextDouble() - 1;
                 final double r2 = x * x + y * y;
 
-                if (r2 < 1) {
-                    // Pair (x, y) is within unit circle.
+                if (r2 > 1 || r2 == 0) {
+                    // Pair is not within the unit circle: Generate another one.
+                    continue SAMPLE;
+                }
 
-                    final double alpha = Math.sqrt(-2 * Math.log(r2) / r2);
+                // Pair (x, y) is within unit circle.
+                final double alpha = Math.sqrt(-2 * Math.log(r2) / r2);
 
-                    // Return the first element of the generated pair.
-                    random = alpha * x;
+                // Keep second element of the pair for next invocation.
+                nextGaussian = alpha * y;
 
-                    // Keep second element of the pair for next invocation.
-                    nextGaussian = alpha * y;
-                    break SAMPLE;
-                }
-                // Pair is not within the unit circle: Generate another one.
+                // Return the first element of the generated pair.
+                return alpha * x;
             }
         } else {
             // Use the second element of the pair (generated at the
             // previous invocation).
-            random = nextGaussian;
+            final double r = nextGaussian;
 
             // Both elements of the pair have been used.
             nextGaussian = Double.NaN;
-        }
 
-        return random;
+            return r;
+        }
     }
 
     /** {@inheritDoc} */


[4/4] commons-rng git commit: Class was renamed.

Posted by er...@apache.org.
Class was renamed.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/2964c6ae
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/2964c6ae
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/2964c6ae

Branch: refs/heads/master
Commit: 2964c6ae66314994d2c1452b378661b5995f7fcb
Parents: b1b44a5
Author: Gilles <er...@apache.org>
Authored: Fri Apr 7 18:25:02 2017 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Apr 7 18:25:02 2017 +0200

----------------------------------------------------------------------
 .../commons/rng/jmh/distribution/SamplersPerformance.java      | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/2964c6ae/commons-rng-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java
----------------------------------------------------------------------
diff --git a/commons-rng-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java b/commons-rng-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java
index b05bfb1..4d338e6 100644
--- a/commons-rng-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java
+++ b/commons-rng-jmh/src/main/java/org/apache/commons/rng/jmh/distribution/SamplersPerformance.java
@@ -37,7 +37,7 @@ import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
 import org.apache.commons.rng.sampling.distribution.DiscreteSampler;
 import org.apache.commons.rng.sampling.distribution.BoxMullerGaussianSampler;
 import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
-import org.apache.commons.rng.sampling.distribution.BoxMullerWithRejectionNormalizedGaussianSampler;
+import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
 import org.apache.commons.rng.sampling.distribution.AhrensDieterExponentialSampler;
 import org.apache.commons.rng.sampling.distribution.AhrensDieterMarsagliaTsangGammaSampler;
 import org.apache.commons.rng.sampling.distribution.BoxMullerLogNormalSampler;
@@ -157,9 +157,9 @@ public class SamplersPerformance {
      * @param bh Data sink.
      */
     @Benchmark
-    public void runBoxMullerWithRejectionNormalizedGaussianSampler(Sources sources,
+    public void runMarsagliaNormalizedGaussianSampler(Sources sources,
                                                                    Blackhole bh) {
-        runSample(new BoxMullerWithRejectionNormalizedGaussianSampler(sources.getGenerator()), bh);
+        runSample(new MarsagliaNormalizedGaussianSampler(sources.getGenerator()), bh);
     }
 
     /**


[2/4] commons-rng git commit: RNG-36: Performance improvement.

Posted by er...@apache.org.
RNG-36: Performance improvement.


Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/e591a656
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/e591a656
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/e591a656

Branch: refs/heads/master
Commit: e591a65643c1383fd72033df6725de3ae7e2b27c
Parents: 4ef78f5
Author: Gilles <er...@apache.org>
Authored: Fri Apr 7 16:03:50 2017 +0200
Committer: Gilles <er...@apache.org>
Committed: Fri Apr 7 16:03:50 2017 +0200

----------------------------------------------------------------------
 .../sampling/distribution/MarsagliaNormalizedGaussianSampler.java | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-rng/blob/e591a656/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
index 6dc9f2a..6c86452 100644
--- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
+++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/MarsagliaNormalizedGaussianSampler.java
@@ -55,8 +55,7 @@ public class MarsagliaNormalizedGaussianSampler
                 if (r2 < 1) {
                     // Pair (x, y) is within unit circle.
 
-                    final double r = Math.sqrt(r2);
-                    final double alpha = 2 * Math.sqrt(-Math.log(r)) / r;
+                    final double alpha = Math.sqrt(-2 * Math.log(r2) / r2);
 
                     // Return the first element of the generated pair.
                     random = alpha * x;