@Test public void testRandomPartitionAvgBehavior() { for (int i = 0; i < 100; i++) { final Map<Integer, Double> partitionWeights = Maps.newHashMap(); final Map<Integer, Double> elementWeights = Maps.newHashMap(); final int nPartitions = RandomUtils.nextInt(99) + 1; final int nElements = RandomUtils.nextInt(1000) + 5000; for (int partition = 0; partition < nPartitions; partition++) { partitionWeights.put(partition, RandomUtils.nextDouble()); } for (int element = 0; element < nElements; element++) { elementWeights.put(element, RandomUtils.nextDouble()); } final Multimap<Integer, Integer> partitions = SampleUtils .randomPartition(elementWeights, partitionWeights); final double partitionsTotalSum = StatsUtil.sum(partitionWeights .values()); double sumDifference = 0; for (final int partitionId : partitions.keySet()) { final int partitionSize = partitions.get(partitionId).size(); final double expectedPartitionPct = partitionWeights .get(partitionId) / partitionsTotalSum; final double actualPartitionPct = ((double) partitionSize) / nElements; sumDifference += Math.abs(expectedPartitionPct - actualPartitionPct); } assertEquals(sumDifference / partitions.keySet().size(), 0, 1E-2); } }
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);  }