sampling

Basic sampling functionality for Haskell.
git clone git://git.jtobin.io/sampling.git
Log | Files | Refs | README | LICENSE

README.md (1565B)


      1 # sampling
      2 
      3 [![Build Status](https://secure.travis-ci.org/jtobin/sampling.png)](http://travis-ci.org/jtobin/sampling)
      4 [![Hackage Version](https://img.shields.io/hackage/v/sampling.svg)](http://hackage.haskell.org/package/sampling)
      5 [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/jtobin/sampling/blob/master/LICENSE)
      6 
      7 
      8 Basic sampling functionality.
      9 
     10 Exports variations on two simple functions for sampling from arbitrary
     11 'Foldable' collections:
     12 
     13 * *sample*, for sampling without replacement
     14 * *resample*, for sampling with replacement (i.e. a bootstrap)
     15 
     16 Each variation can be prefixed with 'p' to sample from a container of values
     17 weighted by probability.
     18 
     19 ## Usage
     20 
     21 *sampling* uses the PRNG provided by
     22 [mwc-random](https://hackage.haskell.org/package/mwc-random) for randomness.
     23 You can either provide a generator for functions that require one, e.g.:
     24 
     25     > import Numeric.Sampling
     26     > gen <- createSystemRandom
     27     > resample 100 [1..1000] gen
     28 
     29 Or simply use the `IO`-specialized versions that will use the system's source
     30 of randomness:
     31 
     32     > resampleIO 100 [1..1000]
     33 
     34 The non-`IO` specialized functions can be used with any `PrimMonad`.
     35 
     36 ## Examples
     37 
     38 Sample ten elements from a list, without replacement:
     39 
     40     > sampleIO 10 ['a'..'z']
     41     Just "azctumlhwj"
     42 
     43 Sample five elements from a Map, with replacement:
     44 
     45     > import qualified Data.Map.Strict as Map
     46     > resampleIO 5 (Map.fromList [(1, "apple"), (2, "orange"), (3, "pear")])
     47     ["apple", "apple", "pear", "orange", "pear"]
     48 
     49 ## Etc.
     50 
     51 PRs and issues welcome.
     52