Class ProbabilityDistribution<T extends Comparable<T>>
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.)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
Counts the number of occurrences of an element in the ProbabilityDistributionExposes the internal representation of theProbabilityDistribution
as aMap
for testing purposes.int
getTotal()
Total number of instances that have been added via record().int
Returns the index of the element such that pick(index) will return the elementkeySet()
pick
(int index) Picks an element out of the Probability Distribution non-randomly according to the provided index.pick
(NumberGenerator generator) Picks an instance of the ProbabilityDistribution according to the provided NumberGenerator.void
Add an instance to the ProbabilityDistribution.toString()
Print the probability distribution
-
Constructor Details
-
ProbabilityDistribution
public ProbabilityDistribution()Constructs an empty distribution.
-
-
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.
-
getRecords
Exposes the internal representation of theProbabilityDistribution
as aMap
for testing purposes.- Returns:
- a copy of the ProbabilityDistribution's internal Map
-
pick
Picks an instance of the ProbabilityDistribution according to the provided NumberGenerator. If 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 received from the generator is less than zero or greater than the total number of records in the PDNoSuchElementException
- if the generator can't generate numbers less than the number of records in the ProbabilityDistribution
-
pick
Picks an element out of the Probability Distribution non-randomly according to the provided index. To determine an element's index, we treat the distribution as an array where each element occurs a number of times according to its value.For example, given the distribution printed as:
{ "banana": 2 "chair":1 "table":1 }
We think of it as an array of length 4, containing the elements at the indices given:{ 0 , 1 , 2 , 3 } {"banana", "banana", "chair", "table"}
- 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- Throws:
IllegalArgumentException
- when t is null
-
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 occurrences of the provided element in the ProbabilityDistribution
- Throws:
IllegalArgumentException
- when t is not in the distribution (i.e, it has not been previously recorded)
-
keySet
- Returns:
- a set containing all 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
-