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 2021/05/30 13:03:31 UTC

[commons-math] branch master updated (665ae9c -> f9a7c56)

This is an automated email from the ASF dual-hosted git repository.

erans pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git.


    from 665ae9c  Draft of release notes.
     new 8a756d7  Removed dependency on "GaussianRandomGenerator" (unit test).
     new c93520a  MATH-1593: Remove duplicate functionality (provided in "Commons RNG").
     new f9a7c56  MATH-1596: Remove dependency on "RandomVectorGenerator".

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:
 .../optim/BaseMultiStartMultivariateOptimizer.java | 14 ++--
 .../scalar/MultiStartMultivariateOptimizer.java    |  8 +-
 .../random/CorrelatedRandomVectorGenerator.java    |  4 +-
 .../legacy/random/GaussianRandomGenerator.java     | 51 ------------
 .../random/UncorrelatedRandomVectorGenerator.java  | 92 ----------------------
 .../MultiStartMultivariateOptimizerTest.java       | 24 +++---
 .../CorrelatedRandomVectorGeneratorTest.java       | 38 ++++++---
 .../legacy/random/GaussianRandomGeneratorTest.java | 40 ----------
 .../UncorrelatedRandomVectorGeneratorTest.java     | 65 ---------------
 .../GLSMultipleLinearRegressionTest.java           | 28 +++++--
 10 files changed, 74 insertions(+), 290 deletions(-)
 delete mode 100644 commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/GaussianRandomGenerator.java
 delete mode 100644 commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGenerator.java
 delete mode 100644 commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/GaussianRandomGeneratorTest.java
 delete mode 100644 commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGeneratorTest.java

[commons-math] 02/03: MATH-1593: Remove duplicate functionality (provided in "Commons RNG").

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit c93520a02ff74328871c22b287892dcdc3c08d21
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sun May 30 14:58:49 2021 +0200

    MATH-1593: Remove duplicate functionality (provided in "Commons RNG").
---
 .../legacy/random/GaussianRandomGenerator.java     | 51 ------------
 .../random/UncorrelatedRandomVectorGenerator.java  | 92 ----------------------
 .../legacy/random/GaussianRandomGeneratorTest.java | 40 ----------
 .../UncorrelatedRandomVectorGeneratorTest.java     | 65 ---------------
 4 files changed, 248 deletions(-)

diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/GaussianRandomGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/GaussianRandomGenerator.java
deleted file mode 100644
index 069b276..0000000
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/GaussianRandomGenerator.java
+++ /dev/null
@@ -1,51 +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.math4.legacy.random;
-
-import org.apache.commons.rng.UniformRandomProvider;
-import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler;
-import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
-
-/**
- * Random generator that generates normally distributed samples.
- *
- * @since 1.2
- */
-public class GaussianRandomGenerator implements NormalizedRandomGenerator {
-    /** Gaussian distribution sampler. */
-    private final NormalizedGaussianSampler sampler;
-
-    /**
-     * Creates a new generator.
-     *
-     * @param generator Underlying random generator.
-     */
-    public GaussianRandomGenerator(final UniformRandomProvider generator) {
-        sampler = new MarsagliaNormalizedGaussianSampler(generator);
-    }
-
-    /**
-     * Generates a random scalar with zero mean and unit standard deviation.
-     *
-     * @return a random value sampled from a normal distribution.
-     */
-    @Override
-    public double nextNormalizedDouble() {
-        return sampler.sample();
-    }
-}
diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGenerator.java
deleted file mode 100644
index 9df5fef..0000000
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGenerator.java
+++ /dev/null
@@ -1,92 +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.math4.legacy.random;
-
-import java.util.Arrays;
-
-import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
-
-/**
- * A {@link RandomVectorGenerator} that generates vectors with uncorrelated
- * components. Components of generated vectors follow (independent) Gaussian
- * distributions, with parameters supplied in the constructor.
- *
- * @since 1.2
- */
-public class UncorrelatedRandomVectorGenerator
-  implements RandomVectorGenerator {
-
-    /** Underlying scalar generator. */
-    private final NormalizedRandomGenerator generator;
-
-    /** Mean vector. */
-    private final double[] mean;
-
-    /** Standard deviation vector. */
-    private final double[] standardDeviation;
-
-  /** Simple constructor.
-   * <p>Build an uncorrelated random vector generator from
-   * its mean and standard deviation vectors.</p>
-   * @param mean expected mean values for each component
-   * @param standardDeviation standard deviation for each component
-   * @param generator underlying generator for uncorrelated normalized
-   * components
-   */
-  public UncorrelatedRandomVectorGenerator(double[] mean,
-                                           double[] standardDeviation,
-                                           NormalizedRandomGenerator generator) {
-    if (mean.length != standardDeviation.length) {
-        throw new DimensionMismatchException(mean.length, standardDeviation.length);
-    }
-    this.mean              = mean.clone();
-    this.standardDeviation = standardDeviation.clone();
-    this.generator = generator;
-  }
-
-  /** Simple constructor.
-   * <p>Build a null mean random and unit standard deviation
-   * uncorrelated vector generator</p>
-   * @param dimension dimension of the vectors to generate
-   * @param generator underlying generator for uncorrelated normalized
-   * components
-   */
-  public UncorrelatedRandomVectorGenerator(int dimension,
-                                           NormalizedRandomGenerator generator) {
-    mean              = new double[dimension];
-    standardDeviation = new double[dimension];
-    Arrays.fill(standardDeviation, 1.0);
-    this.generator = generator;
-  }
-
-  /** Generate an uncorrelated random vector.
-   * @return a random vector as a newly built array of double
-   */
-  @Override
-public double[] nextVector() {
-
-    double[] random = new double[mean.length];
-    for (int i = 0; i < random.length; ++i) {
-      random[i] = mean[i] + standardDeviation[i] * generator.nextNormalizedDouble();
-    }
-
-    return random;
-
-  }
-
-}
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/GaussianRandomGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/GaussianRandomGeneratorTest.java
deleted file mode 100644
index f31a8fa..0000000
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/GaussianRandomGeneratorTest.java
+++ /dev/null
@@ -1,40 +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.math4.legacy.random;
-
-import org.apache.commons.rng.simple.RandomSource;
-import org.apache.commons.math4.legacy.stat.StatUtils;
-import org.junit.Assert;
-import org.junit.Test;
-
-
-public class GaussianRandomGeneratorTest {
-
-    @Test
-    public void testMeanAndStandardDeviation() {
-        final GaussianRandomGenerator generator = new GaussianRandomGenerator(RandomSource.create(RandomSource.MT));
-        final double[] sample = new double[10000];
-        for (int i = 0; i < sample.length; ++i) {
-            sample[i] = generator.nextNormalizedDouble();
-        }
-        final double mean = StatUtils.mean(sample);
-        Assert.assertEquals("mean=" + mean, 0, mean, 1e-2);
-        final double variance = StatUtils.variance(sample);
-        Assert.assertEquals("variance=" + variance, 1, variance, 1e-2);
-    }
-}
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGeneratorTest.java
deleted file mode 100644
index 593449c..0000000
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/UncorrelatedRandomVectorGeneratorTest.java
+++ /dev/null
@@ -1,65 +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.math4.legacy.random;
-
-import org.apache.commons.math4.legacy.linear.RealMatrix;
-import org.apache.commons.rng.simple.RandomSource;
-import org.apache.commons.math4.legacy.stat.descriptive.moment.VectorialCovariance;
-import org.apache.commons.math4.legacy.stat.descriptive.moment.VectorialMean;
-import org.junit.Test;
-import org.junit.Assert;
-
-public class UncorrelatedRandomVectorGeneratorTest {
-    private double[] mean;
-    private double[] standardDeviation;
-    private UncorrelatedRandomVectorGenerator generator;
-
-    public UncorrelatedRandomVectorGeneratorTest() {
-        mean              = new double[] {0.0, 1.0, -3.0, 2.3};
-        standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
-        generator =
-            new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
-                                                  new GaussianRandomGenerator(RandomSource.create(RandomSource.MT,
-                                                                                                  17399225433L)));
-    }
-
-    @Test
-    public void testMeanAndCorrelation() {
-        // The test is extremely sensitive to the seed (cf. constructor).
-        VectorialMean meanStat = new VectorialMean(mean.length);
-        VectorialCovariance covStat = new VectorialCovariance(mean.length, true);
-        for (int i = 0; i < 10000; ++i) {
-            double[] v = generator.nextVector();
-            meanStat.increment(v);
-            covStat.increment(v);
-        }
-
-        double[] estimatedMean = meanStat.getResult();
-        double scale;
-        RealMatrix estimatedCorrelation = covStat.getResult();
-        for (int i = 0; i < estimatedMean.length; ++i) {
-            Assert.assertEquals(mean[i], estimatedMean[i], 0.07);
-            for (int j = 0; j < i; ++j) {
-                scale = standardDeviation[i] * standardDeviation[j];
-                Assert.assertEquals(0, estimatedCorrelation.getEntry(i, j) / scale, 0.03);
-            }
-            scale = standardDeviation[i] * standardDeviation[i];
-            Assert.assertEquals(1, estimatedCorrelation.getEntry(i, i) / scale, 0.025);
-        }
-    }
-}

[commons-math] 03/03: MATH-1596: Remove dependency on "RandomVectorGenerator".

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit f9a7c568bea5e17d1d1197ef330d185be21785a9
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sun May 30 15:02:13 2021 +0200

    MATH-1596: Remove dependency on "RandomVectorGenerator".
---
 .../optim/BaseMultiStartMultivariateOptimizer.java | 14 ++++++-------
 .../scalar/MultiStartMultivariateOptimizer.java    |  8 +++-----
 .../MultiStartMultivariateOptimizerTest.java       | 24 +++++++++++-----------
 3 files changed, 22 insertions(+), 24 deletions(-)

diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/BaseMultiStartMultivariateOptimizer.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/BaseMultiStartMultivariateOptimizer.java
index 8530341..f4e57f3 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/BaseMultiStartMultivariateOptimizer.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/BaseMultiStartMultivariateOptimizer.java
@@ -16,10 +16,10 @@
  */
 package org.apache.commons.math4.legacy.optim;
 
+import java.util.function.Supplier;
 import org.apache.commons.math4.legacy.exception.MathIllegalStateException;
 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
-import org.apache.commons.math4.legacy.random.RandomVectorGenerator;
 
 /**
  * Base class multi-start optimizer for a multivariate function.
@@ -41,9 +41,9 @@ public abstract class BaseMultiStartMultivariateOptimizer<PAIR>
     /** Number of evaluations already performed for all starts. */
     private int totalEvaluations;
     /** Number of starts to go. */
-    private int starts;
-    /** Random generator for multi-start. */
-    private RandomVectorGenerator generator;
+    private final int starts;
+    /** Generator of start points ("multi-start"). */
+    private final Supplier<double[]> generator;
     /** Optimization data. */
     private OptimizationData[] optimData;
     /**
@@ -72,12 +72,12 @@ public abstract class BaseMultiStartMultivariateOptimizer<PAIR>
      * @param starts Number of starts to perform. If {@code starts == 1},
      * the {@link #optimize(OptimizationData[]) optimize} will return the
      * same solution as the given {@code optimizer} would return.
-     * @param generator Random vector generator to use for restarts.
+     * @param generator Generator to use for restarts.
      * @throws NotStrictlyPositiveException if {@code starts < 1}.
      */
     public BaseMultiStartMultivariateOptimizer(final BaseMultivariateOptimizer<PAIR> optimizer,
                                                final int starts,
-                                               final RandomVectorGenerator generator) {
+                                               final Supplier<double[]> generator) {
         super(optimizer.getConvergenceChecker());
 
         if (starts < 1) {
@@ -185,7 +185,7 @@ public abstract class BaseMultiStartMultivariateOptimizer<PAIR>
                         if (attempts++ >= getMaxEvaluations()) {
                             throw new TooManyEvaluationsException(getMaxEvaluations());
                         }
-                        s = generator.nextVector();
+                        s = generator.get();
                         for (int k = 0; s != null && k < s.length; ++k) {
                             if ((min != null && s[k] < min[k]) || (max != null && s[k] > max[k])) {
                                 // reject the vector
diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizer.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizer.java
index 841dc9b..260185b 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizer.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizer.java
@@ -20,12 +20,12 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.function.Supplier;
 
 import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.legacy.exception.NullArgumentException;
 import org.apache.commons.math4.legacy.optim.BaseMultiStartMultivariateOptimizer;
 import org.apache.commons.math4.legacy.optim.PointValuePair;
-import org.apache.commons.math4.legacy.random.RandomVectorGenerator;
 
 /**
  * Multi-start optimizer.
@@ -50,16 +50,14 @@ public class MultiStartMultivariateOptimizer
      * @param starts Number of starts to perform.
      * If {@code starts == 1}, the result will be same as if {@code optimizer}
      * is called directly.
-     * @param generator Random vector generator to use for restarts.
+     * @param generator Generator to use for restarts.
      * @throws NullArgumentException if {@code optimizer} or {@code generator}
      * is {@code null}.
      * @throws NotStrictlyPositiveException if {@code starts < 1}.
      */
     public MultiStartMultivariateOptimizer(final MultivariateOptimizer optimizer,
                                            final int starts,
-                                           final RandomVectorGenerator generator)
-        throws NullArgumentException,
-        NotStrictlyPositiveException {
+                                           final Supplier<double[]> generator) {
         super(optimizer, starts, generator);
         this.optimizer = optimizer;
     }
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
index 96b228e..f7fc08c 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
@@ -16,6 +16,7 @@
  */
 package org.apache.commons.math4.legacy.optim.nonlinear.scalar;
 
+import java.util.function.Supplier;
 import org.apache.commons.geometry.euclidean.twod.Vector2D;
 import org.apache.commons.math4.legacy.analysis.MultivariateFunction;
 import org.apache.commons.math4.legacy.optim.InitialGuess;
@@ -30,7 +31,6 @@ import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.rng.simple.RandomSource;
 import org.apache.commons.rng.sampling.distribution.GaussianSampler;
 import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
-import org.apache.commons.math4.legacy.random.RandomVectorGenerator;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -49,9 +49,9 @@ public class MultiStartMultivariateOptimizerTest {
         GradientMultivariateOptimizer underlying
             = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                       new SimpleValueChecker(1e-10, 1e-10));
-        final RandomVectorGenerator generator = gaussianRandom(new double[] { 50, 50 },
-                                                               new double[] { 10, 10 },
-                                                               RandomSource.create(RandomSource.MT_64));
+        final Supplier<double[]> generator = gaussianRandom(new double[] { 50, 50 },
+                                                            new double[] { 10, 10 },
+                                                            RandomSource.create(RandomSource.MT_64));
         int nbStarts = 10;
         MultiStartMultivariateOptimizer optimizer
             = new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
@@ -88,9 +88,9 @@ public class MultiStartMultivariateOptimizerTest {
                 { 0.9, 1.2 } ,
                 {  3.5, -2.3 }
             });
-        final RandomVectorGenerator generator = gaussianRandom(new double[] { 0, 0 },
-                                                               new double[] { 1, 1 },
-                                                               RandomSource.create(RandomSource.MT_64));
+        final Supplier<double[]> generator = gaussianRandom(new double[] { 0, 0 },
+                                                            new double[] { 1, 1 },
+                                                            RandomSource.create(RandomSource.MT_64));
         int nbStarts = 10;
         MultiStartMultivariateOptimizer optimizer
             = new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
@@ -136,18 +136,18 @@ public class MultiStartMultivariateOptimizerTest {
      * @return a random array generator where each element is a Gaussian
      * sampling with the given mean and standard deviation.
      */
-    private RandomVectorGenerator gaussianRandom(final double[] mean,
-                                                 final double[] stdev,
-                                                 final UniformRandomProvider rng) {
+    private Supplier<double[]> gaussianRandom(final double[] mean,
+                                              final double[] stdev,
+                                              final UniformRandomProvider rng) {
         final ZigguratNormalizedGaussianSampler normalized = new ZigguratNormalizedGaussianSampler(rng);
         final GaussianSampler[] samplers = new GaussianSampler[mean.length];
         for (int i = 0; i < mean.length; i++) {
             samplers[i] = new GaussianSampler(normalized, mean[i], stdev[i]);
         }
 
-        return new RandomVectorGenerator() {
+        return new Supplier<double[]>() {
             @Override
-            public double[] nextVector() {
+            public double[] get() {
                 final double[] s = new double[mean.length];
                 for (int i = 0; i < mean.length; i++) {
                     s[i] = samplers[i].sample();

[commons-math] 01/03: Removed dependency on "GaussianRandomGenerator" (unit test).

Posted by er...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

erans pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-math.git

commit 8a756d763d9e2788f2818ff41084ffce091b7850
Author: Gilles Sadowski <gi...@gmail.com>
AuthorDate: Sun May 30 01:10:18 2021 +0200

    Removed dependency on "GaussianRandomGenerator" (unit test).
---
 .../random/CorrelatedRandomVectorGenerator.java    |  4 +--
 .../CorrelatedRandomVectorGeneratorTest.java       | 38 +++++++++++++++-------
 .../GLSMultipleLinearRegressionTest.java           | 28 +++++++++++++---
 3 files changed, 52 insertions(+), 18 deletions(-)

diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java
index 753f9c6..0338f5f 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGenerator.java
@@ -36,8 +36,8 @@ import org.apache.commons.math4.legacy.linear.RectangularCholeskyDecomposition;
  * Multivariate Normal Distribution</a>. The approach using a Cholesky
  * decomposition is quite usual in this case. However, it can be extended
  * to other cases as long as the underlying random generator provides
- * {@link NormalizedRandomGenerator normalized values} like {@link
- * GaussianRandomGenerator} or {@link UniformRandomGenerator}.</p>
+ * {@link NormalizedRandomGenerator normalized values} like
+ * {@link UniformRandomGenerator}.</p>
  * <p>Sometimes, the covariance matrix for a given simulation is not
  * strictly positive definite. This means that the correlations are
  * not all independent from each other. In this case, however, the non
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java
index b290c79..273e126 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/CorrelatedRandomVectorGeneratorTest.java
@@ -23,7 +23,9 @@ import org.apache.commons.math4.legacy.TestUtils;
 import org.apache.commons.math4.legacy.linear.Array2DRowRealMatrix;
 import org.apache.commons.math4.legacy.linear.MatrixUtils;
 import org.apache.commons.math4.legacy.linear.RealMatrix;
+import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
 import org.apache.commons.math4.legacy.stat.correlation.StorelessCovariance;
 import org.apache.commons.math4.legacy.stat.descriptive.moment.VectorialCovariance;
 import org.apache.commons.math4.legacy.stat.descriptive.moment.VectorialMean;
@@ -57,11 +59,10 @@ public class CorrelatedRandomVectorGeneratorTest {
             }
         }
 
-        GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 17399225432l));
         generator = new CorrelatedRandomVectorGenerator(mean,
                                                         covariance,
-                                                        1.0e-12 * covariance.getNorm(),
-                                                        rawGenerator);
+                                                        1e-12 * covariance.getNorm(),
+                                                        gaussianRandom(RandomSource.create(RandomSource.WELL_1024_A)));
     }
 
     @Test
@@ -79,9 +80,9 @@ public class CorrelatedRandomVectorGeneratorTest {
                 { 6, 2, -1, 197 }
         };
         RealMatrix covRM = MatrixUtils.createRealMatrix(cov);
-        NormalizedRandomGenerator rg = new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 5322145245211l));
         CorrelatedRandomVectorGenerator sg =
-            new CorrelatedRandomVectorGenerator(mean, covRM, 0.00001, rg);
+            new CorrelatedRandomVectorGenerator(mean, covRM, 0.00001,
+                                                gaussianRandom(RandomSource.create(RandomSource.WELL_1024_A)));
 
         double[] min = new double[mean.length];
         Arrays.fill(min, Double.POSITIVE_INFINITY);
@@ -168,12 +169,11 @@ public class CorrelatedRandomVectorGeneratorTest {
 
     private CorrelatedRandomVectorGenerator createSampler(double[][] cov) {
         RealMatrix matrix = new Array2DRowRealMatrix(cov);
-        double small = 10e-12 * matrix.getNorm();
-        return new CorrelatedRandomVectorGenerator(
-                new double[cov.length],
-                matrix,
-                small,
-                new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 0x366a26b94e520f41l)));
+        double small = 1e-12 * matrix.getNorm();
+        return new CorrelatedRandomVectorGenerator(new double[cov.length],
+                                                   matrix,
+                                                   small,
+                                                   gaussianRandom(RandomSource.create(RandomSource.WELL_1024_A)));
     }
 
     private void testSampler(final double[][] covMatrix, int samples, double epsilon) {
@@ -189,4 +189,20 @@ public class CorrelatedRandomVectorGeneratorTest {
             TestUtils.assertEquals(covMatrix[r], sampleCov[r], epsilon);
         }
     }
+
+    /**
+     * @param rng RNG.
+     * @return a N(0,1) sampler.
+     */
+    private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng) {
+        final ZigguratNormalizedGaussianSampler n = new ZigguratNormalizedGaussianSampler(rng);
+
+        return new NormalizedRandomGenerator() {
+            /** {@inheritDoc} */
+            @Override
+            public double nextNormalizedDouble() {
+                return n.sample();
+            }
+        };
+    }
 }
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java
index 6eeab71..f507be2 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/stat/regression/GLSMultipleLinearRegressionTest.java
@@ -25,10 +25,11 @@ import org.apache.commons.math4.legacy.exception.NullArgumentException;
 import org.apache.commons.math4.legacy.linear.MatrixUtils;
 import org.apache.commons.math4.legacy.linear.RealMatrix;
 import org.apache.commons.math4.legacy.linear.RealVector;
+import org.apache.commons.math4.legacy.random.NormalizedRandomGenerator;
 import org.apache.commons.math4.legacy.random.CorrelatedRandomVectorGenerator;
-import org.apache.commons.math4.legacy.random.GaussianRandomGenerator;
 import org.apache.commons.rng.UniformRandomProvider;
 import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
 import org.apache.commons.statistics.distribution.ContinuousDistribution;
 import org.apache.commons.statistics.distribution.NormalDistribution;
 import org.apache.commons.math4.legacy.stat.correlation.Covariance;
@@ -220,7 +221,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
      */
     @Test
     public void testGLSEfficiency() {
-        final UniformRandomProvider rg = RandomSource.create(RandomSource.MT, 123456789L);
+        final UniformRandomProvider rg = RandomSource.create(RandomSource.MT);
         final ContinuousDistribution.Sampler gauss = new NormalDistribution(0, 1).createSampler(rg);
 
         // Assume model has 16 observations (will use Longley data).  Start by generating
@@ -245,10 +246,11 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
         RealMatrix cov = (new Covariance(errorSeeds)).getCovarianceMatrix();
 
         // Create a CorrelatedRandomVectorGenerator to use to generate correlated errors
-        GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
         double[] errorMeans = new double[nObs];  // Counting on init to 0 here
-        CorrelatedRandomVectorGenerator gen = new CorrelatedRandomVectorGenerator(errorMeans, cov,
-         1.0e-12 * cov.getNorm(), rawGenerator);
+        CorrelatedRandomVectorGenerator gen
+            = new CorrelatedRandomVectorGenerator(errorMeans, cov,
+                                                  1e-12 * cov.getNorm(),
+                                                  gaussianRandom(rg));
 
         // Now start generating models.  Use Longley X matrix on LHS
         // and Longley OLS beta vector as "true" beta.  Generate
@@ -297,4 +299,20 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
         assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean());
         assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation());
     }
+
+    /**
+     * @param rng RNG.
+     * @return a N(0,1) sampler.
+     */
+    private NormalizedRandomGenerator gaussianRandom(final UniformRandomProvider rng) {
+        final ZigguratNormalizedGaussianSampler n = new ZigguratNormalizedGaussianSampler(rng);
+
+        return new NormalizedRandomGenerator() {
+            /** {@inheritDoc} */
+            @Override
+            public double nextNormalizedDouble() {
+                return n.sample();
+            }
+        };
+    }
 }