Class ProbabilityDistribution<T extends Comparable<T>>

java.lang.Object
ProbabilityDistribution<T>

class ProbabilityDistribution<T extends Comparable<T>>
extends Object
This class represents a probability distribution over a type T as a map from T values to integers. We can think of this map as a "histogram" of the frequency of occurrences observed for T values.

We can build a probability distribution by "recording" new occurrences of a value. For instance, if we want to build a probability distribution over String values, we might record the following occurrences: record("a"); record("b"); record("a"); record("a"); record("c")

The resulting distribution would map "a" to a frequency count of 3, and "b" and "c" both to 1, since we recorded only one of each.

We can sample (a.k.a. "pick") an element from the probability distribution at random, based on the frequency information in the records.

For instance, given the distribution above, we would pick "a" with 3/5 probability and each of "b" and "c" with 1/5 probability. Of the 5 total observations, 3 were "a". (Assuming that the number generator is truly random -- a non-random number generator, which is useful for testing, might yield different outcomes.)

  • Field Details

  • Constructor Details

    • ProbabilityDistribution

      public ProbabilityDistribution()
  • Method Details

    • getTotal

      public int getTotal()
      Total number of instances that have been added via record().
      Returns:
      an int representing the number of records in the ProbabilityDistribution.
    • getEntrySet

      public Set<Map.Entry<T,​Integer>> getEntrySet()
      Returns:
      an EntrySet representation of the internal Map.
    • getRecords

      public Map<T,​Integer> getRecords()
      Returns:
      a copy of the ProbabilityDistribution's internal Map
    • pick

      public T pick​(NumberGenerator generator)
      Picks an instance of the ProbabilityDistribution according to the provided NumberGenerator. In the provided NumberGenerator is random, then the resulting T values are chosen with probability proportional to the frequency that they have been recorded.
      Parameters:
      generator - - uses the generator to pick a particular element in the ProbabilityDistribution.
      Returns:
      the chosen element of the ProbabilityDistribution
      Throws:
      IllegalArgumentException - if a number recieved from the generator is less than zero or greater than the total number of records in the PD
    • pick

      public T pick​(int index)
      Picks an instance of the ProbabilityDistribution non-randomly according to the provided index.
      Parameters:
      index - - use this to pick a particular element in the ProbabilityDistribution. Must not be more than the number of elements in the ProbabilityDistribution.
      Returns:
      the chosen element of the ProbabilityDistribution
      Throws:
      IllegalArgumentException - if index is less than zero or greater than the total number of records in the PD
    • record

      public void record​(T t)
      Add an instance to the ProbabilityDistribution. If the element already exists in the ProbabilityDistribution, it will increment the number of occurrences of that element.
      Parameters:
      t - - an element to add to the distribution
    • count

      public int count​(T t)
      Counts the number of occurrences of an element in the ProbabilityDistribution
      Parameters:
      t - - the element you want to get the count of
      Returns:
      the number of occurences of the provided element in the ProbabilityDistribution
    • keySet

      public Set<T> keySet()
      Returns:
      a set containing all of the elements in the ProbabilityDistribution
    • index

      public int index​(T element)
      returns the index of the element such that pick(index) will return the element
      Parameters:
      element - - the element to find the index of
      Returns:
      the index of the specified element
      Throws:
      IllegalArgumentException - if the element is not in the distribution
    • toString

      public String toString()
      Print the probability distribution
      Overrides:
      toString in class Object