@Test public void testRandomPartition() { final Map<String, Double> partitionWeights = Maps.newHashMap(); partitionWeights.put("A", 1.); partitionWeights.put("B", 1.); partitionWeights.put("C", 1.); final Map<Integer, Double> elementWeights = Maps.newHashMap(); for (int i = 0; i < 300; i++) { elementWeights.put(i, 1.); } assertEquals(elementWeights.keySet().size(), 300); Multimap<String, Integer> partition = SampleUtils.randomPartition( elementWeights, partitionWeights); assertEquals(partition.get("A").size(), 100); assertEquals(partition.get("B").size(), 100); assertEquals(partition.get("C").size(), 100); partitionWeights.put("A", 1.); partitionWeights.put("B", 5.); partitionWeights.put("C", 4.); partition = SampleUtils.randomPartition(elementWeights, partitionWeights); assertEquals(partition.get("A").size(), 30); assertEquals(partition.get("B").size(), 150); assertEquals(partition.get("C").size(), 120); partitionWeights.put("A", 100.); partitionWeights.put("B", 0.); partitionWeights.put("C", 200.); partition = SampleUtils.randomPartition(elementWeights, partitionWeights); assertEquals(partition.get("A").size(), 100); assertEquals(partition.get("B").size(), 0); assertEquals(partition.get("C").size(), 200); }
public static <K, T> Multimap<K, T> randomPartition( final Map<T, Double> elementWeights, final Map<K, Double> partitionWeights) { final List<Entry<T, Double>> elements = Lists .newArrayList(elementWeights.entrySet()); Collections.shuffle(elements); return partitionGivenOrder(elementWeights, partitionWeights, elements); }
SampleUtils { public static <K, T> Multimap<K, T> randomPartition( final Map<T, Double> elementWeights, final Map<K, Double> partitionWeights) { final List<Entry<T, Double>> elements = Lists .newArrayList(elementWeights.entrySet()); Collections.shuffle(elements); return partitionGivenOrder(elementWeights, partitionWeights, elements); } }
SampleUtils { public static <K, T> Multimap<K, T> randomPartition( final Map<T, Double> elementWeights, final Map<K, Double> partitionWeights) { final List<Entry<T, Double>> elements = Lists .newArrayList(elementWeights.entrySet()); Collections.shuffle(elements); return partitionGivenOrder(elementWeights, partitionWeights, elements); } private  SampleUtils(); }
SampleUtils { public static <K, T> Multimap<K, T> randomPartition( final Map<T, Double> elementWeights, final Map<K, Double> partitionWeights) { final List<Entry<T, Double>> elements = Lists .newArrayList(elementWeights.entrySet()); Collections.shuffle(elements); return partitionGivenOrder(elementWeights, partitionWeights, elements); } private  SampleUtils(); static T getRandomElement(final Collection<T> collection); static T getRandomElement(final Multiset<T> set); static int getRandomIndex(final double[] log2ProbWeights); static T getRandomKey(final Map<T, Double> log2ProbWeights); static Multimap<K, T> partitionGivenOrder(
			final Map<T, Double> elementWeights,
			final Map<K, Double> partitionWeights,
			final List<Entry<T, Double>> orderedElements); static Multimap<K, T> randomPartition(
			final Map<T, Double> elementWeights,
			final Map<K, Double> partitionWeights); }
SampleUtils { public static <K, T> Multimap<K, T> randomPartition( final Map<T, Double> elementWeights, final Map<K, Double> partitionWeights) { final List<Entry<T, Double>> elements = Lists .newArrayList(elementWeights.entrySet()); Collections.shuffle(elements); return partitionGivenOrder(elementWeights, partitionWeights, elements); } private  SampleUtils(); static T getRandomElement(final Collection<T> collection); static T getRandomElement(final Multiset<T> set); static int getRandomIndex(final double[] log2ProbWeights); static T getRandomKey(final Map<T, Double> log2ProbWeights); static Multimap<K, T> partitionGivenOrder(
			final Map<T, Double> elementWeights,
			final Map<K, Double> partitionWeights,
			final List<Entry<T, Double>> orderedElements); static Multimap<K, T> randomPartition(
			final Map<T, Double> elementWeights,
			final Map<K, Double> partitionWeights);  }