Package org.plumelib.util
Class MultiRandSelector<T extends @Nullable Object>
java.lang.Object
org.plumelib.util.MultiRandSelector<T>
- Type Parameters:
T- the type of elements to be selected among
Performs uniform random selection over an iterator, where the objects in the iterator may be
partitioned so that the random selection chooses the same number from each group.
For example, given data about incomes by state, it may be desirable to select 1000 people from each state rather than 50,000 from the nation. As another example, for selecting invocations in a Daikon trace file, it may be desirable to select an equal number of samples per program point.
The performance is the same as running a set of RandomSelector Objects, one for each bucket, plus some overhead for determining which bucket to assign to each Object in the iteration.
To use this class, call accept(T) on every Object in the iteration to be sampled. Then,
call valuesIter() to receive an iteration of all the values selected by the random
selection.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanWhether to toss a coin or select a given number of elements.private Partitioner<T,T> Partioner that determines how to partition the objects.private doubleLikelihood to select each element.private HashMap<T,RandomSelector<T>> Maps from partition representatives to the RandomSelector to use on that partition.private intNumber of elements to select.private RandomThe Random instance to use. -
Constructor Summary
ConstructorsModifierConstructorDescriptionMultiRandSelector(double keepProbability, Random r, Partitioner<T, T> eq) Create a MultiRandSelector that chooses each element with probabilitykeepProbability., using the given Random.MultiRandSelector(double keepProbability, Partitioner<T, T> eq) Create a MultiRandSelector that chooses each element with probabilitykeepProbability.MultiRandSelector(int numElts, Random r, Partitioner<T, T> eq) Create a MultiRandSelector that choosesnumEltsfrom each partition, using the given Random.MultiRandSelector(int numElts, Partitioner<T, T> eq) Create a MultiRandSelector that choosesnumEltselements from each bucket.privateMultiRandSelector(Random r, Partitioner<T, T> eq) Helper constructor to create a not-fully-initialized MultiRandSelector. -
Method Summary
Modifier and TypeMethodDescriptionvoidUse the given value as one of the objects in the pool to select from.voidacceptIter(Iterator<T> iter) Use all the iterator's elements in the pool to select from.Returns an iterator of all objects selected.
-
Field Details
-
coinTossMode
private boolean coinTossModeWhether to toss a coin or select a given number of elements. -
numElts
private int numEltsNumber of elements to select. -1 if coinTossMode==true. -
keepProbability
private double keepProbabilityLikelihood to select each element. -1.0 if coinTossMode=false. -
r
The Random instance to use. Is not a seed. Gets side-effected. -
eq
Partioner that determines how to partition the objects. -
map
Maps from partition representatives to the RandomSelector to use on that partition.
-
-
Constructor Details
-
MultiRandSelector
Create a MultiRandSelector that choosesnumEltselements from each bucket.- Parameters:
numElts- the number of elements to select from each bucketeq- partioner that determines how to partition the objects
-
MultiRandSelector
Create a MultiRandSelector that chooses each element with probabilitykeepProbability.- Parameters:
keepProbability- the likelihood to select each elementeq- partioner that determines how to partition the objects
-
MultiRandSelector
Create a MultiRandSelector that choosesnumEltsfrom each partition, using the given Random.- Parameters:
numElts- the number of elements to select from each bucketr- the Random instance to use for making random choiceseq- partioner that determines how to partition the objects
-
MultiRandSelector
Create a MultiRandSelector that chooses each element with probabilitykeepProbability., using the given Random.- Parameters:
keepProbability- likelihood to select each elementr- the Random instance to use for making random choiceseq- partioner that determines how to partition the objects
-
MultiRandSelector
Helper constructor to create a not-fully-initialized MultiRandSelector.- Parameters:
r- the Random instance to use for making random choiceseq- partioner that determines how to partition the objects
-
-
Method Details
-
acceptIter
Use all the iterator's elements in the pool to select from.- Parameters:
iter- contains elements that are added to the pool to select from
-
accept
Use the given value as one of the objects in the pool to select from.- Parameters:
next- element that is added to the pool to select from
-
valuesIter
Returns an iterator of all objects selected.- Returns:
- an iterator of all objects selected
-