Repository: commons-math Updated Branches: refs/heads/master 5c753a87c -> 657b1b49d
MATH-1393: Remove code provided in "Commons RNG". Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/d198cc8c Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/d198cc8c Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/d198cc8c Branch: refs/heads/master Commit: d198cc8cae8e42aed25b7ea8af6f1b7960e8a6bb Parents: 5c753a8 Author: Gilles <er...@apache.org> Authored: Wed Dec 14 15:52:02 2016 +0100 Committer: Gilles <er...@apache.org> Committed: Wed Dec 14 17:38:28 2016 +0100 ---------------------------------------------------------------------- .../commons/math4/random/RandomUtils.java | 86 ----------- .../RandomUtilsDataGeneratorAbstractTest.java | 142 ------------------- 2 files changed, 228 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/d198cc8c/src/main/java/org/apache/commons/math4/random/RandomUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/RandomUtils.java b/src/main/java/org/apache/commons/math4/random/RandomUtils.java index 8f9c804..7b2cb9e 100644 --- a/src/main/java/org/apache/commons/math4/random/RandomUtils.java +++ b/src/main/java/org/apache/commons/math4/random/RandomUtils.java @@ -30,7 +30,6 @@ import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.NumberIsTooLargeException; import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.rng.UniformRandomProvider; -import org.apache.commons.math4.util.MathArrays; /** * Factory for creating generators of miscellaneous data. @@ -403,90 +402,5 @@ public class RandomUtils { return u * upper + (1.0 - u) * lower; } - - /** - * Generates an integer array of length {@code k} whose entries are selected - * randomly, without repetition, from the integers {@code 0, ..., n - 1} - * (inclusive). - * <p> - * Generated arrays represent permutations of {@code n} taken {@code k} at a - * time. - * </p> - * <p> - * This method calls {@link MathArrays#shuffle(int[],UniformRandomProvider) - * MathArrays.shuffle} in order to create a random shuffle of the set - * of natural numbers {@code { 0, 1, ..., n - 1 }}. - * </p> - * - * @param n Domain of the permutation. - * @param k Size of the permutation. - * @return a random {@code k}-permutation of {@code n}, as an array of - * integers. - * @throws NumberIsTooLargeException if {@code k > n}. - * @throws NotStrictlyPositiveException if {@code k <= 0}. - */ - public int[] nextPermutation(int n, - int k) - throws NumberIsTooLargeException, NotStrictlyPositiveException { - if (k > n) { - throw new NumberIsTooLargeException(LocalizedFormats.PERMUTATION_EXCEEDS_N, - k, n, true); - } - if (k <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.PERMUTATION_SIZE, - k); - } - - final int[] index = MathArrays.natural(n); - MathArrays.shuffle(index, rng); - - // Return a new array containing the first "k" entries of "index". - return MathArrays.copyOf(index, k); - } - - /** - * Returns a list of {@code k} objects selected randomly from the - * given {@code collection}. - * - * <p> - * Sampling is without replacement; but if {@code collection} contains - * identical objects, the sample may include repeats. If all elements - * are distinct, the resulting object array represents a - * <a href="http://rkb.home.cern.ch/rkb/AN16pp/node250.html#SECTION0002500000000000000000"> - * Simple Random Sample</a> of size {@code k} from the elements of - * the {@code collection}. - * </p> - * <p> - * This method calls {@link #nextPermutation(int,int) nextPermutation(c.size(), k)} - * in order to sample the collection. - * </p> - * - * @param <T> Type of objects held in the {@code collection}. - * @param collection Collection to be sampled. - * @param k Size of the sample. - * @return a random sample of {@code k} elements from the {@code collection}. - * @throws NumberIsTooLargeException if {@code k > collection.size()}. - * @throws NotStrictlyPositiveException if {@code k <= 0}. - */ - public <T> List<T> nextSample(Collection<T> collection, - int k) { - final int len = collection.size(); - if (k > len) { - throw new NumberIsTooLargeException(LocalizedFormats.SAMPLE_SIZE_EXCEEDS_COLLECTION_SIZE, - k, len, true); - } - if (k <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES, k); - } - - final ArrayList<T> objects = new ArrayList<>(collection); - final int[] index = nextPermutation(len, k); - final List<T> result = new ArrayList<>(k); - for (int i = 0; i < k; i++) { - result.add(objects.get(index[i])); - } - - return result; - } } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/d198cc8c/src/test/java/org/apache/commons/math4/random/RandomUtilsDataGeneratorAbstractTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/random/RandomUtilsDataGeneratorAbstractTest.java b/src/test/java/org/apache/commons/math4/random/RandomUtilsDataGeneratorAbstractTest.java index 59170d4..dedf278 100644 --- a/src/test/java/org/apache/commons/math4/random/RandomUtilsDataGeneratorAbstractTest.java +++ b/src/test/java/org/apache/commons/math4/random/RandomUtilsDataGeneratorAbstractTest.java @@ -311,146 +311,4 @@ public abstract class RandomUtilsDataGeneratorAbstractTest { Assert.assertTrue(u > 0.99 && u < 1); } } - - /** Tests for "nextSample" (sampling from Collection). */ - @Test - public void testNextSample() { - Object[][] c = { { "0", "1" }, { "0", "2" }, { "0", "3" }, - { "0", "4" }, { "1", "2" }, { "1", "3" }, { "1", "4" }, - { "2", "3" }, { "2", "4" }, { "3", "4" } }; - long[] observed = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - double[] expected = { 100, 100, 100, 100, 100, 100, 100, 100, 100, 100 }; - - HashSet<Object> cPop = new HashSet<>(); // {0,1,2,3,4} - for (int i = 0; i < 5; i++) { - cPop.add(Integer.toString(i)); - } - - Object[] sets = new Object[10]; // 2-sets from 5 - for (int i = 0; i < 10; i++) { - HashSet<Object> hs = new HashSet<>(); - hs.add(c[i][0]); - hs.add(c[i][1]); - sets[i] = hs; - } - - for (int i = 0; i < 1000; i++) { - List<Object> cSamp = randomData.nextSample(cPop, 2); - observed[findSample(sets, cSamp)]++; - } - - // Use ChiSquare dist with df = 10-1 = 9, alpha = 0.001 - // Change to 21.67 for alpha = 0.01 - Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", - testStatistic.chiSquare(expected, observed) < 27.88); - - // Make sure sample of size = size of collection returns same collection - HashSet<Object> hs = new HashSet<>(); - hs.add("one"); - List<Object> one = randomData.nextSample(hs, 1); - String oneString = (String) one.get(0); - if (one.size() != 1 || - !oneString.equals("one")) { - Assert.fail("bad sample for set size = 1, sample size = 1"); - } - - // Make sure we fail for sample size > collection size. - try { - one = randomData.nextSample(hs, 2); - Assert.fail("sample size > set size, expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - - // Make sure we fail for empty collection. - try { - hs = new HashSet<>(); - one = randomData.nextSample(hs, 0); - Assert.fail("n = k = 0, expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - } - - @SuppressWarnings("unchecked") - private int findSample(Object[] u, List<Object> sampList) { - Object[] samp = sampList.toArray(new Object[sampList.size()]); - for (int i = 0; i < u.length; i++) { - HashSet<Object> set = (HashSet<Object>) u[i]; - HashSet<Object> sampSet = new HashSet<>(); - for (int j = 0; j < samp.length; j++) { - sampSet.add(samp[j]); - } - if (set.equals(sampSet)) { - return i; - } - } - Assert.fail("sample not found:{" + samp[0] + "," + samp[1] + "}"); - return -1; - } - - /** tests for nextPermutation */ - @Test - public void testNextPermutation() { - int[][] p = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 0, 2 }, { 1, 2, 0 }, - { 2, 0, 1 }, { 2, 1, 0 } }; - long[] observed = { 0, 0, 0, 0, 0, 0 }; - double[] expected = { 100, 100, 100, 100, 100, 100 }; - - for (int i = 0; i < 600; i++) { - int[] perm = randomData.nextPermutation(3, 3); - observed[findPerm(p, perm)]++; - } - - String[] labels = {"{0, 1, 2}", "{ 0, 2, 1 }", "{ 1, 0, 2 }", - "{ 1, 2, 0 }", "{ 2, 0, 1 }", "{ 2, 1, 0 }"}; - TestUtils.assertChiSquareAccept(labels, expected, observed, 0.001); - - // Check size = 1 boundary case - int[] perm = randomData.nextPermutation(1, 1); - if ((perm.length != 1) || (perm[0] != 0)) { - Assert.fail("bad permutation for n = 1, sample k = 1"); - - // Make sure we fail for k size > n - try { - perm = randomData.nextPermutation(2, 3); - Assert.fail("permutation k > n, expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - - // Make sure we fail for n = 0 - try { - perm = randomData.nextPermutation(0, 0); - Assert.fail("permutation k = n = 0, expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - - // Make sure we fail for k < n < 0 - try { - perm = randomData.nextPermutation(-1, -3); - Assert.fail("permutation k < n < 0, expecting MathIllegalArgumentException"); - } catch (MathIllegalArgumentException ex) { - // ignored - } - - } - } - - private int findPerm(int[][] p, int[] samp) { - for (int i = 0; i < p.length; i++) { - boolean good = true; - for (int j = 0; j < samp.length; j++) { - if (samp[j] != p[i][j]) { - good = false; - } - } - if (good) { - return i; - } - } - Assert.fail("permutation not found"); - return -1; - } }