List of usage examples for org.apache.commons.math3.distribution EnumeratedDistribution sample
public T sample()
From source file:io.coala.math3.Math3ProbabilityDistribution.java
@SafeVarargs public static <T, S> Math3ProbabilityDistribution<T> of(final EnumeratedDistribution<T> dist, //final PseudoRandom stream, final S... args) { Objects.requireNonNull(dist); final Math3ProbabilityDistribution<T> result = new Math3ProbabilityDistribution<T>() { @Override//from w w w .jav a 2 s . co m public T draw() { return dist.sample(); } }; // result.stream = stream; // result.params = Arrays.asList( args ); return result; }
From source file:cc.kave.commons.pointsto.evaluation.UsagePruning.java
/** * @return The number of pruned usages// w w w . j a va 2 s .co 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:com.github.tteofili.calabrize.impl.RNN.java
/** * sample a sequence of integers from the model, using current (hPrev) memory state, seedIx is seed letter for first time step *//*from w w w .ja v a 2s . c o m*/ public String sample(int seedIx) { INDArray x = Nd4j.zeros(vocabSize, 1); x.putScalar(seedIx, 1); int sampleSize = 144; INDArray ixes = Nd4j.create(sampleSize); INDArray h = hPrev.dup(); for (int t = 0; t < sampleSize; t++) { h = Transforms.tanh(wxh.mmul(x).add(whh.mmul(h)).add(bh)); INDArray y = (why.mmul(h)).add(by); INDArray pm = Nd4j.getExecutioner().execAndReturn(new SoftMax(y)).ravel(); List<Pair<Integer, Double>> d = new LinkedList<>(); for (int pi = 0; pi < vocabSize; pi++) { d.add(new Pair<>(pi, pm.getDouble(0, pi))); } try { EnumeratedDistribution<Integer> distribution = new EnumeratedDistribution<>(d); int ix = distribution.sample(); x = Nd4j.zeros(vocabSize, 1); x.putScalar(ix, 1); ixes.putScalar(t, ix); } catch (Exception e) { } } return getSampleString(ixes); }
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 w w . j ava2s . c o m public T draw() { return dist.sample(); } }; }
From source file:cc.kave.commons.pointsto.evaluation.cv.StratifiedMethodsCVFoldBuilder.java
@Override public List<List<Usage>> createFolds(Map<ProjectIdentifier, List<Usage>> projectUsages) { shuffleUsages(projectUsages.values()); Map<ICoReMethodName, Integer> methods = countMethods(Iterables.concat(projectUsages.values())); EnumeratedDistribution<ICoReMethodName> distribution = buildDistribution(methods); MethodRegistry registry = new MethodRegistry(Iterables.concat(projectUsages.values()), methods.size()); List<List<Usage>> folds = new ArrayList<>(numFolds); int avgFoldSize = calcAvgFoldSize(projectUsages.values()); for (int f = 0; f < numFolds - 1; ++f) { List<Usage> fold = new ArrayList<>(avgFoldSize); folds.add(fold);//w w w. java 2s . c om for (int i = 0; i < avgFoldSize; ++i) { ICoReMethodName method; Usage usage; do { method = distribution.sample(); usage = registry.next(method); } while (usage == null); fold.add(usage); } } folds.add(registry.remaining()); return folds; }
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/*from w w w. j av a 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; }