Skip to main content

module util::Sampling

rascal-Not specified

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","b","e","f","h","i","j","k"}
rascal>sample({"a","b","c","e","f","g","h","i","j","k"}, 4)
set[str]: {"a","b","e","f","h","i","j","k"}
rascal>sample({"a","b","c","e","f","g","h","i","j","k"}, 4)
set[str]: {"a","c","e","h","j"}

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]: [98,106,122,134,170,177,178,180,251,259,282,293,309,382,422,425,451,460,475,503,619,623,638,649,760,823,837,841,850,949,975,978,992]
rascal>sample([1..1000], 30)
list[int]: [16,23,96,101,107,175,220,260,288,355,431,440,455,642,650,654,705,730,829,852,879,946,991]
rascal>sample([1..1000], 30)
list[int]: [76,81,105,106,161,180,193,276,287,362,389,423,435,483,486,524,610,652,665,690,758,858,901,908,947,951,995]

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.