module util::Sampling
Utilities to randomly select smaller datasets from larger datasets
Usage
import util::Sampling;
Dependencies
import util::Math;
import Map;
import List;
import Set;
Description
Sampling is important when the analysis algorithms do not scale to the size of the original corpus, or when you need to train an analysis on a representative set without overfitting on the entire corpus. These sampling functions all assume that a uniformly random selection is required.
function sample
Reduce the arity of a set by selecting a uniformly distributed sample.
set[&T] sample(set[&T] corpus, int target)
A uniform subset is computed by iterating over the set and skipping every element
with a probability of 1/(size(corpus) / target). This rapidly generates a new set of
expected target size, but most probably a little smaller or larger.
Examples
rascal>import util::Sampling;
ok
rascal>sample({"a","b","c","e","f","g","h","i","j","k"}, 4)
set[str]: {"a","c","g","h","i","j"}
rascal>sample({"a","b","c","e","f","g","h","i","j","k"}, 4)
set[str]: {"a","c","e","h","i"}
rascal>sample({"a","b","c","e","f","g","h","i","j","k"}, 4)
set[str]: {"c","e","g","h","k"}
function sample
Reduce the length of a list by selecting a uniformly distributed sample.
list[&T] sample(list[&T] corpus, int target)
The random selection of elements does not change their initial order in the list.
A uniform sublist is computed by iterating over the list and skipping every element
with a probability of 1/(size(corpus) / target). This rapidly generates a new list of
expected target size, but most probably a little smaller or larger.
Examples
rascal>import util::Sampling;
ok
rascal>sample([1..1000], 30)
list[int]: [11,25,74,104,159,252,267,273,275,295,331,352,364,376,429,430,453,468,566,590,624,694,699,700,732,784,787,809,869,919,931,956,985,991]
rascal>sample([1..1000], 30)
list[int]: [10,19,24,45,54,108,198,236,285,294,309,325,331,368,381,395,407,438,457,459,488,569,597,625,674,679,685,706,778,791,796,827,894,896,913,917,924,977]
rascal>sample([1..1000], 30)
list[int]: [6,51,59,97,114,145,156,178,183,203,243,265,282,327,362,385,403,418,484,485,503,557,594,630,646,693,711,721,727,739,756,768,826,840,856,867,940,945]
function sample
Reduce the size of a map by selecting a uniformly distributed sample.
map[&T,&U] sample(map[&T,&U] corpus, int target)
A uniform submap is computed by iterating over the map's keys and skipping every key
with a probability of 1/(size(corpus) / target). This rapidly generates a new map of
expected target size, but most probably a little smaller or larger.