Class ProbabilityDistribution<T extends Comparable<T>>
class ProbabilityDistribution<T extends Comparable<T>> extends Object
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 Summary
-
Constructor Summary
Constructors Constructor Description ProbabilityDistribution()
-
Method Summary
Modifier and Type Method Description int
count(T t)
Counts the number of occurrences of an element in the ProbabilityDistributionSet<Map.Entry<T,Integer>>
getEntrySet()
Map<T,Integer>
getRecords()
int
getTotal()
Total number of instances that have been added via record().int
index(T element)
returns the index of the element such that pick(index) will return the elementSet<T>
keySet()
T
pick(int index)
Picks an instance of the ProbabilityDistribution non-randomly according to the provided index.T
pick(NumberGenerator generator)
Picks an instance of the ProbabilityDistribution according to the provided NumberGenerator.void
record(T t)
Add an instance to the ProbabilityDistribution.String
toString()
Print the probability distribution
-
Field Details
-
records
-
total
-
-
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
- Returns:
- an EntrySet representation of the internal Map.
-
getRecords
- Returns:
- a copy of the ProbabilityDistribution's internal Map
-
pick
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
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
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
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
- Returns:
- a set containing all of the elements in the ProbabilityDistribution
-
index
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
Print the probability distribution
-