Algorithm::Numerical::Sample(3pm) | User Contributed Perl Documentation | Algorithm::Numerical::Sample(3pm) |
Algorithm::Numerical::Sample - 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 "sample" function takes a set and a sample size as arguments. If the sample size is omitted, a sample of 1 is taken. The keywords "set" and "sample_size" may be preceeded with an optional "-". The function returns the sample list, or a reference to the sample list, depending on the context.
The class "Algorithm::Numerical::Sample::Stream" has the following methods:
Crucial to see that the "sample" algorithm is correct is the fact that when we sample "n" elements from a set of size "N" that the "t + 1"st element is choosen with probability "(n - m)/(N - t)", when already "m" elements have been choosen. We can immediately see that we will never pick too many elements (as the probability is 0 as soon as "n == m"), nor too few, as the probability will be 1 if we have "k" elements to choose from the remaining "k" elements, for some "k". 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 "n", the first "n" 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.
The current sources of this module are found on github, <git://github.com/Abigail/algorithm--numerical--sample.git>.
This package was written by Abigail, cpan@abigail.be.
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 "Software"), 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.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2010-06-12 | perl v5.10.1 |