Skip to content
Tools for sampling from collections.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
bench
lib/Numeric
src
test
.ghci
.gitignore
.travis.yml
CHANGELOG
LICENSE
README.md
Setup.hs
sampling.cabal
stack-travis.yaml
stack.yaml

README.md

sampling

Build Status Hackage Version MIT License

Basic sampling functionality.

Exports variations on two simple functions for sampling from arbitrary 'Foldable' collections:

  • sample, for sampling without replacement
  • resample, for sampling with replacement (i.e. a bootstrap)

Each variation can be prefixed with 'p' to sample from a container of values weighted by probability.

Usage

sampling uses the PRNG provided by mwc-random for randomness. You can either provide a generator for functions that require one, e.g.:

> import Numeric.Sampling
> gen <- createSystemRandom
> resample 100 [1..1000] gen

Or simply use the IO-specialized versions that will use the system's source of randomness:

> resampleIO 100 [1..1000]

The non-IO specialized functions can be used with any PrimMonad.

Examples

Sample ten elements from a list, without replacement:

> sampleIO 10 ['a'..'z']
Just "azctumlhwj"

Sample five elements from a Map, with replacement:

> import qualified Data.Map.Strict as Map
> resampleIO 5 (Map.fromList [(1, "apple"), (2, "orange"), (3, "pear")])
["apple", "apple", "pear", "orange", "pear"]

Etc.

PRs and issues welcome.

You can’t perform that action at this time.