Example usage for org.apache.commons.math3.distribution EnumeratedDistribution EnumeratedDistribution

List of usage examples for org.apache.commons.math3.distribution EnumeratedDistribution EnumeratedDistribution

Introduction

In this page you can find the example usage for org.apache.commons.math3.distribution EnumeratedDistribution EnumeratedDistribution.

Prototype

public EnumeratedDistribution(final RandomGenerator rng, final List<Pair<T, Double>> pmf)
        throws NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException 

Source Link

Document

Create an enumerated distribution using the given random number generator and probability mass function enumeration.

Usage

From source file:cc.kave.commons.pointsto.evaluation.UsagePruning.java

/**
 * @return The number of pruned usages//w ww  .jav a  2s .  c  o  m
 */
public int prune(final int maxUsages, Map<ProjectIdentifier, List<Usage>> usages) {
    final int initialNumUsages = usages.values().stream().mapToInt(Collection::size).sum();
    int numUsages = initialNumUsages;

    if (numUsages > maxUsages) {
        Random rnd = new Random(rndGenerator.nextLong());
        List<Pair<ProjectIdentifier, Double>> projectUsageCounts = new ArrayList<>(usages.size());
        for (Map.Entry<ProjectIdentifier, List<Usage>> entry : usages.entrySet()) {
            projectUsageCounts.add(Pair.create(entry.getKey(), (double) entry.getValue().size()));
            Collections.shuffle(entry.getValue(), rnd);
        }
        EnumeratedDistribution<ProjectIdentifier> distribution = new EnumeratedDistribution<>(rndGenerator,
                projectUsageCounts);

        while (numUsages > maxUsages) {
            ProjectIdentifier project = distribution.sample();
            List<Usage> projectUsages = usages.get(project);
            if (!projectUsages.isEmpty()) {
                projectUsages.remove(projectUsages.size() - 1);
                --numUsages;
            }
        }
    }

    return initialNumUsages - numUsages;
}

From source file:cc.kave.commons.pointsto.evaluation.cv.StratifiedProjectsCVFoldBuilder.java

@Override
public List<List<Usage>> createFolds(Map<ProjectIdentifier, List<Usage>> projectUsages) {
    EnumeratedDistribution<ProjectIdentifier> projectDistribution;
    List<Pair<ProjectIdentifier, Double>> projectProbabilities = new ArrayList<>(projectUsages.size());

    // initialize distributions
    long totalUsages = 0;
    for (Map.Entry<ProjectIdentifier, List<Usage>> project : projectUsages.entrySet()) {
        projectProbabilities//w w  w .  j ava 2 s  .  c o  m
                .add(new Pair<ProjectIdentifier, Double>(project.getKey(), (double) project.getValue().size()));
        totalUsages += project.getValue().size();
    }
    // normalization of the probability mass is done by the constructor
    projectDistribution = new EnumeratedDistribution<>(rndGenerator, projectProbabilities);

    shuffleUsages(projectUsages.values());

    List<List<Usage>> folds = new ArrayList<>(numFolds);
    int avgFoldSize = (int) (totalUsages / numFolds);
    for (int f = 0; f < numFolds - 1; ++f) {
        List<Usage> fold = new ArrayList<>(avgFoldSize);
        folds.add(fold);

        for (int i = 0; i < avgFoldSize; ++i) {
            ProjectIdentifier project;
            List<Usage> usages;
            do {
                project = projectDistribution.sample();
                usages = projectUsages.get(project);
            } while (usages.isEmpty());

            int lastIndex = usages.size() - 1;
            Usage usage = usages.remove(lastIndex);
            fold.add(usage);
        }
    }
    // add remaining usages to the last fold
    List<Usage> lastFold = new ArrayList<>(avgFoldSize);
    folds.add(lastFold);
    for (List<Usage> usages : projectUsages.values()) {
        lastFold.addAll(usages);
        usages.clear();
    }
    Collections.shuffle(lastFold, new Random(rndGenerator.nextLong()));

    return folds;
}

From source file:cc.kave.commons.pointsto.evaluation.cv.StratifiedMethodsCVFoldBuilder.java

private EnumeratedDistribution<ICoReMethodName> buildDistribution(Map<ICoReMethodName, Integer> methods) {
    List<Pair<ICoReMethodName, Double>> methodProbabilities = new ArrayList<>(methods.size());

    for (Map.Entry<ICoReMethodName, Integer> methodEntry : methods.entrySet()) {
        methodProbabilities.add(Pair.create(methodEntry.getKey(), (double) methodEntry.getValue()));
    }//  w w w  .  ja  v  a2s. com

    // probabilities are normalized by the constructor
    return new EnumeratedDistribution<>(rndGenerator, methodProbabilities);
}

From source file:io.coala.random.impl.RandomDistributionFactoryImpl.java

@Override
public <T> RandomDistribution<T> getEnumerated(final RandomNumberStream rng,
        final List<ProbabilityMass<T, Number>> probabilities) {
    final List<Pair<T, Double>> pmf = new ArrayList<>();
    if (probabilities != null)
        for (ProbabilityMass<T, Number> p : probabilities)
            pmf.add(Pair.create(p.getValue(), p.getMass().doubleValue()));
    final EnumeratedDistribution<T> dist = new EnumeratedDistribution<T>(
            RandomNumberStream.Util.asCommonsRandomGenerator(rng), pmf);
    return new RandomDistribution<T>() {
        @Override//from w  ww.j  a  v  a2 s .com
        public T draw() {
            return dist.sample();
        }
    };
}