@Override public void clear() { super.clear(); this.total = 0.0; }
@Override public DefaultDataDistribution<KeyType> clone() { DefaultDataDistribution<KeyType> clone = (DefaultDataDistribution<KeyType>) super.clone(); // We have to manually reset "total" because super.super.clone // calls "incrementAll", which will, in turn, increment the total // So we'd end up with twice the total. clone.total = this.total; return clone; }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super KeyType> output) { // Compute the cumulative weights final int size = this.getDomainSize(); double[] cumulativeWeights = new double[size]; double cumulativeSum = 0.0; ArrayList<KeyType> domain = new ArrayList<KeyType>(size); int index = 0; for (ScalarMap.Entry<KeyType> entry : this.entrySet()) { domain.add(entry.getKey()); final double value = entry.getValue(); cumulativeSum += value; cumulativeWeights[index] = cumulativeSum; index++; } ProbabilityMassFunctionUtil.sampleMultipleInto( cumulativeWeights, domain, random, sampleCount, output); }
@Override public ArrayList<KeyType> sample( final Random random, final int numSamples) { final ArrayList<KeyType> result = new ArrayList<KeyType>(numSamples); this.sampleInto(random, numSamples, result); return result; }
@Override public Set<KeyType> getDomain() { return this.keySet(); }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super KeyType> output) { // Compute the cumulative weights final int size = this.getDomainSize(); double[] cumulativeWeights = new double[size]; double cumulativeSum = 0.0; ArrayList<KeyType> domain = new ArrayList<KeyType>(size); int index = 0; for (ScalarMap.Entry<KeyType> entry : this.entrySet()) { domain.add(entry.getKey()); final double value = entry.getValue(); cumulativeSum += value; cumulativeWeights[index] = cumulativeSum; index++; } ProbabilityMassFunctionUtil.sampleMultipleInto( cumulativeWeights, domain, random, sampleCount, output); }
@Override public ArrayList<KeyType> sample( final Random random, final int numSamples) { final ArrayList<KeyType> result = new ArrayList<KeyType>(numSamples); this.sampleInto(random, numSamples, result); return result; }
@Override public Set<KeyType> getDomain() { return this.keySet(); }
@Override public double getEntropy() { double entropy = 0.0; final double total = this.getTotal(); final double denom = (total != 0.0) ? total : 1.0; for (ScalarMap.Entry<KeyType> entry : this.entrySet()) { double p = entry.getValue() / denom; if (p != 0.0) { entropy -= p * MathUtil.log2(p); } } return entropy; }
@Override public void sampleInto( final Random random, final int sampleCount, final Collection<? super KeyType> output) { // Compute the cumulative weights final int size = this.getDomainSize(); double[] cumulativeWeights = new double[size]; double cumulativeSum = 0.0; ArrayList<KeyType> domain = new ArrayList<KeyType>(size); int index = 0; for (ScalarMap.Entry<KeyType> entry : this.entrySet()) { domain.add(entry.getKey()); final double value = entry.getValue(); cumulativeSum += value; cumulativeWeights[index] = cumulativeSum; index++; } ProbabilityMassFunctionUtil.sampleMultipleInto( cumulativeWeights, domain, random, sampleCount, output); }