org.apache.commons.math3.distribution.EnumeratedDistribution Java Examples

The following examples show how to use org.apache.commons.math3.distribution.EnumeratedDistribution. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomCollectionsMerger.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <T> ArrayList<T> mergeCollections(final Collection<? extends Collection<T>> chunks, final long seed) {

        final JDKRandomGenerator jdkRandomGenerator = new JDKRandomGenerator(Long.hashCode(seed));

        final ArrayList<T> mergedResult = new ArrayList<>();

        // create initial weight pairs
        List<Pair<Spliterator<T>, Double>> weightPairs = chunks.stream()
                .map(chunk -> Pair.create(chunk.spliterator(), (double) chunk.size()))
                .collect(Collectors.toList());

        while (!weightPairs.isEmpty()) {

            final EnumeratedDistribution<Spliterator<T>> ed = new EnumeratedDistribution<>(jdkRandomGenerator, weightPairs);

            // take random elements until face too many misses
            int missCounter = 0;
            while (missCounter++ < 3) {
                final Spliterator<T> sample = ed.sample();
                if (sample.tryAdvance(mergedResult::add)) {
                    missCounter = 0;
                }
            }

            // as empty queues leading to misses - rebuild wight pairs without them
            weightPairs = weightPairs.stream()
                    .filter(p -> p.getFirst().estimateSize() > 0)
                    .map(p -> Pair.create(p.getFirst(), (double) p.getFirst().estimateSize()))
                    .collect(Collectors.toList());

//            log.debug("rebuild size {}", weightPairs.size());
        }

        return mergedResult;
    }
 
Example #2
Source File: SampledOpDistributionFactory.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public OpDistribution get(Timing timing, int sampleCount)
{
    PartitionGenerator generator = newGenerator();
    List<Pair<Operation, Double>> operations = new ArrayList<>();
    for (Map.Entry<T, Double> ratio : ratios.entrySet())
        operations.add(new Pair<>(get(timing.newTimer(ratio.getKey().toString(), sampleCount), generator, ratio.getKey()),
                                  ratio.getValue()));
    return new SampledOpDistribution(new EnumeratedDistribution<>(operations), clustering.get());
}
 
Example #3
Source File: SampledOpDistribution.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public SampledOpDistribution(EnumeratedDistribution<Operation> operations, Distribution clustering)
{
    this.operations = operations;
    this.clustering = clustering;
}
 
Example #4
Source File: MathUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static <E> E randomSelect(final List<E> choices, final Function<E, Double> probabilityFunction, final RandomGenerator rng) {
    Utils.nonNull(choices);
    Utils.nonNull(probabilityFunction);
    Utils.nonNull(rng);
    final List<Pair<E, Double>> pmf = choices.stream()
            .map(e -> new Pair<>(e, probabilityFunction.apply(e))).collect(Collectors.toList());
    return new EnumeratedDistribution<>(rng, pmf).sample();
}
 
Example #5
Source File: ExponentialMechanism.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the distribution to sample from.
 * The arrays values and scores have to have the same length.
 * @param values
 * @param scores
 */
public void setDistribution(T[] values, double[] scores) {
    
    // Check arguments
    if (values.length == 0) {
        throw new IllegalStateException("No values supplied");
    }
    if (values.length != scores.length) {
        throw new IllegalStateException("Number of scores and values must be identical");
    }
    
    // The following code calculates the probability distribution which assigns every value
    // a probability proportional to exp(0,5 * epsilon * score)
    
    // Calculate all exponents having the form 0,5 * epsilon * score and remember the maximal value.
    // This value is used during the following calculations in a manner which reduces the magnitude of numbers involved
    // (which can get very large due to the application of the exponential function) while it does not change the result;
    // it is a trick to make the following computations feasible.
    double[] exponents = new double[scores.length];
    double shift = -Double.MAX_VALUE;
    for (int i = 0; i < scores.length; ++i) {
        exponents[i] = 0.5d * epsilon * scores[i];
        shift = Math.max(shift, exponents[i]);
    }

    // Create a probability mass function containing numbers of the form exp(0,5 * epsilon * score - shift)
    // which correspond to non-normalized probabilities multiplied with exp(-shift).
    List<Pair<T, Double>> pmf = new ArrayList<>();
    for (int i = 0; i < scores.length; ++i) {
        double prob = Math.exp(exponents[i] - shift);
        pmf.add(new Pair<T, Double>(values[i], prob));
    }

    // Create the distribution. Note that all probabilities are being normalized by the class
    // EnumeratedDistribution by effectively dividing them through their sum, which cancels
    // the factor exp(-shift)
    this.distribution = new EnumeratedDistribution<T>(this.random, pmf);
}
 
Example #6
Source File: GATKProtectedMathUtils.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static <E> E randomSelect(final List<E> choices, final Function<E, Double> probabilityFunction, final RandomGenerator rng) {
    final List<Pair<E, Double>> pmf = choices.stream()
            .map(e -> new Pair<>(e, probabilityFunction.apply(e))).collect(Collectors.toList());
    return new EnumeratedDistribution<>(rng, pmf).sample();
}
 
Example #7
Source File: DiscreteDistribution.java    From james-project with Apache License 2.0 4 votes vote down vote up
private DiscreteDistribution(List<DistributionEntry<T>> distribution) {
    enumeratedDistribution = new EnumeratedDistribution<>(distribution.stream()
        .map(DistributionEntry::toPair)
        .collect(Guavate.toImmutableList()));
}
 
Example #8
Source File: NHANESSample.java    From synthea with Apache License 2.0 3 votes vote down vote up
/**
 * Load the NHANES samples from resources into an EnumeratedDistribution. The samples,
 * probabilities, which were calculated from the sample weights are used to create the
 * distribution. This means that when sampling values from the distribution, the possibility of
 * getting a particular value will be properly weighted.
 * @return the weighted distribution of NHANES Samples
 */
public static EnumeratedDistribution<NHANESSample> loadDistribution() {
  List<NHANESSample> samples = loadSamples();
  @SuppressWarnings("rawtypes")
  List sampleWeights = samples.stream().map(i -> new Pair(i, i.prob)).collect(toList());
  return new EnumeratedDistribution<NHANESSample>(sampleWeights);
}