common
Class Histogram

java.lang.Object
  extended by common.Histogram
All Implemented Interfaces:
SetWithDistrib

public class Histogram
extends java.lang.Object
implements SetWithDistrib

A mapping from objects to weights, which are real numbers. A Histogram maintains an internal map from objects to Doubles, and also keeps track of the sum of the weights. If the sorted flag is specified at construction, then the Histogram uses a sorted map so that sampling results are reproducible.

A histogram conceptually stores a weight for every object, but most objects have zero weight. Only objects with non-zero weight are explicitly represented. The iterator method only allows you to iterate over objects with non-zero weight; if you want to iterate over other objects, you need to enumerate those objects in some other way, and call getWeight for each one.

Note: This class does not allow null elements to be added.


Nested Class Summary
static class Histogram.Entry
          Nested class for the entries in a histogram.
 
Constructor Summary
Histogram()
          Creates an empty histogram.
Histogram(boolean sorted)
          Creates an empty histogram.
 
Method Summary
 void clear()
          Resets the weights of all objects to zero.
 java.util.Set elementSet()
          Returns an unmodifiable view of the set of objects that have non-zero weight in this histogram.
 java.util.Set entrySet()
          Returns an unmodifiable view of the set of Histogram.Entry objects corresponding to the non-zero weight objects in this histogram.
 double getLogProb(java.lang.Object o)
          Returns the log probability of the given object being sampled, according to its weight.
 double getProb(java.lang.Object o)
          Returns the probability of the given object being sampled, according to its weight.
 double getTotalWeight()
          Returns the sum of the weights of all objects in this histogram.
 double getWeight(java.lang.Object obj)
          Returns the weight of the given object in this histogram.
 void increaseWeight(java.lang.Object obj, double delta)
          Increases the weight for the given object by the given amount.
 void print(java.io.PrintStream s)
          Prints this histogram to the given stream.
 java.lang.Object sample()
          Returns an object sampled according to this distribution.
 int size()
          Returns the number of objects that have non-zero weight in this histogram.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Histogram

public Histogram()
Creates an empty histogram.


Histogram

public Histogram(boolean sorted)
Creates an empty histogram.

Parameters:
sorted - if true, use a sorted map from elements to weights
Method Detail

size

public int size()
Returns the number of objects that have non-zero weight in this histogram.


getWeight

public double getWeight(java.lang.Object obj)
Returns the weight of the given object in this histogram. If the object is not explicitly represented in the histogram, its weight is zero.


getTotalWeight

public double getTotalWeight()
Returns the sum of the weights of all objects in this histogram.


getProb

public double getProb(java.lang.Object o)
Returns the probability of the given object being sampled, according to its weight.

Specified by:
getProb in interface SetWithDistrib

getLogProb

public double getLogProb(java.lang.Object o)
Returns the log probability of the given object being sampled, according to its weight.

Specified by:
getLogProb in interface SetWithDistrib

increaseWeight

public void increaseWeight(java.lang.Object obj,
                           double delta)
Increases the weight for the given object by the given amount. If the object was not explicitly represented in the histogram, its old weight is considered to be zero.


clear

public void clear()
Resets the weights of all objects to zero.


elementSet

public java.util.Set elementSet()
Returns an unmodifiable view of the set of objects that have non-zero weight in this histogram.


entrySet

public java.util.Set entrySet()
Returns an unmodifiable view of the set of Histogram.Entry objects corresponding to the non-zero weight objects in this histogram.


sample

public java.lang.Object sample()
Description copied from interface: SetWithDistrib
Returns an object sampled according to this distribution. Returns null if this set is empty.

Specified by:
sample in interface SetWithDistrib

print

public void print(java.io.PrintStream s)
Prints this histogram to the given stream. Each entry is printed out on its own line: first the key, then the value. If this histogram was constructed with the sorted flag set to true, then the entries are sorted by key.