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:33 UTC
[commons-math] 02/03: MATH-1593: Remove duplicate functionality
(provided in "Commons RNG").
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);
- }
- }
-}