Draw samples from a set
use Algorithm::Numerical::Sample qw /sample/; @sample = sample (-set => [1 .. 10000], -sample_size => 100); $sampler = Algorithm::Numerical::Sample::Stream -> new; while (<>) {$sampler -> data ($_)} $random_line = $sampler -> extract;
This package gives two methods to draw fair, random samples from a set. There is a procedural interface for the case the entire set is known, and an object oriented interface when the a set with unknown size has to be processed. The \*(C`sample\*(C' function takes a set and a sample size as arguments. If the sample size is omitted, a sample of 1 is taken. The keywords \*(C`set\*(C' and \*(C`sample_size\*(C' may be preceeded with an optional \*(C`-\*(C'. The function returns the sample list, or a reference to the sample list, depending on the context. The class \*(C`Algorithm::Numerical::Sample::Stream\*(C' has the following methods: This function returns an object of the \*(C`Algorithm::Numerical::Sample::Stream\*(C' class. It will take an optional argument of the form \*(C`sample_size => EXPR\*(C', where \*(C`EXPR\*(C' evaluates to the sample size to be taken. If this argument is missing, a sample of size 1 will be taken. The keyword \*(C`sample_size\*(C' may be preceeded by an optional dash. The method \*(C`data\*(C' takes a list of parameters which are elements of the set we are sampling. Any number of arguments can be given. This method will extract the sample from the object, and reset it to a fresh state, such that a sample of the same size but from a different set, can be taken. \*(C`extract\*(C' will return a list in list context, or the first element of the sample in scalar context.
Crucial to see that the \*(C`sample\*(C' algorithm is correct is the fact that when we sample \*(C`n\*(C' elements from a set of size \*(C`N\*(C' that the \*(C`t + 1\*(C'st element is choosen with probability \*(C`(n - m)/(N - t)\*(C', when already \*(C`m\*(C' elements have been choosen. We can immediately see that we will never pick too many elements (as the probability is 0 as soon as \*(C`n == m\*(C'), nor too few, as the probability will be 1 if we have \*(C`k\*(C' elements to choose from the remaining \*(C`k\*(C' elements, for some \*(C`k\*(C'. For the proof that the sampling is unbiased, we refer to [3]. (Section 3.4.2, Exercise 3).
It is easy to see that the second algorithm returns the correct number of elements. For a sample of size \*(C`n\*(C', the first \*(C`n\*(C' elements go into the reservoir, and after that, the reservoir never grows or shrinks in size; elements only get replaced. A detailed proof of the fairness of the algorithm appears in [3]. (Section 3.4.2, Exercise 7).
Both algorithms are discussed by Knuth [3] (Section 3.4.2). The first algoritm, Selection sampling technique, was discovered by Fan, Muller and Rezucha [1], and independently by Jones [2]. The second algorithm, Reservoir sampling, is due to Waterman.
C. T. Fan, M. E. Muller and I. Rezucha, J. Amer. Stat. Assoc. 57 (1962), pp 387 - 402.
T. G. Jones, \s-1CACM\s0 5 (1962), pp 343.
D. E. Knuth: The Art of Computer Programming, Volume 2, Third edition. Reading: Addison-Wesley, 1997. \s-1ISBN:\s0 0-201-89684-2.
The current sources of this module are found on github, <git://github.com/Abigail/algorithm\*(--numerical--sample.git>.
This package was written by Abigail, [email protected].
Copyright (C) 1998, 1999, 2009, Abigail.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \*(L"Software\*(R"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
\s-1THE\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1PROVIDED\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R", \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EXPRESS\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0 \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0 \s-1THE\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTABILITY\s0, \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0 \s-1AND\s0 \s-1NONINFRINGEMENT\s0. \s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1SHALL\s0 \s-1THE\s0 \s-1AUTHORS\s0 \s-1OR\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1BE\s0 \s-1LIABLE\s0 \s-1FOR\s0 \s-1ANY\s0 \s-1CLAIM\s0, \s-1DAMAGES\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1LIABILITY\s0, \s-1WHETHER\s0 \s-1IN\s0 \s-1AN\s0 \s-1ACTION\s0 \s-1OF\s0 \s-1CONTRACT\s0, \s-1TORT\s0 \s-1OR\s0 \s-1OTHERWISE\s0, \s-1ARISING\s0 \s-1FROM\s0, \s-1OUT\s0 \s-1OF\s0 \s-1OR\s0 \s-1IN\s0 \s-1CONNECTION\s0 \s-1WITH\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1OR\s0 \s-1THE\s0 \s-1USE\s0 \s-1OR\s0 \s-1OTHER\s0 \s-1DEALINGS\s0 \s-1IN\s0 \s-1THE\s0 \s-1SOFTWARE\s0.