Algori...Sample(3pm)
π Algori...Sample β the Algorithm::Numerical::Sample page, bestie edition π
okay so. this is Algorithm::Numerical::Sample, a Perl module that lives in the User Contributed Perl Documentation section, and it is literally about drawing fair random samples from a set. it's giving "tiny module, huge math energy" and i'm obsessed. let's go. π¦
β¨ NAME β¨
Algorithm::Numerical::Sample β Draw samples from a set πΈ
β¨ SYNOPSIS β¨
look at her. she's so clean:
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;
no cap, that second half is how you pull one random line out of a stream of unknown length. iconic. π
β¨ DESCRIPTION β¨
so this package gives you two whole methods for drawing fair, random samples from a set. two vibes, two situations:
- there's a procedural interface for when you already know the entire set up front.
- there's an object oriented interface for when you've got a set of unknown size that still has to get processed. π«Ά
π A: sample (set => ARRAYREF [,sample_size => EXPR])
the sample function takes a set and a sample size as arguments. if you ghost the sample size entirely, it takes a sample of 1. the keywords set and sample_size may be preceeded with an optional - (yes, the dash is optional, she's flexible like that). and depending on the context, the function hands you back the sample list, or a reference to the sample list. β¨
π B: Algorithm::Numerical::Sample::Stream
the class Algorithm::Numerical::Sample::Stream has the following methods, and they are a whole little friend group:
newβ this function returns an object of theAlgorithm::Numerical::Sample::Streamclass. it'll take an optional argument of the formsample_size => EXPR, whereEXPRevaluates to the sample size you want taken. if that argument goes missing, a sample of size 1 gets taken instead. and yes, the keywordsample_sizemay be preceeded by an optional dash. πdata (LIST)β thedatamethod takes a list of parameters, and those parameters are elements of the set we're sampling from. any number of arguments can be given. feed it, bestie. πextractβ this method extracts the sample from the object, and resets it to a fresh state, so that a sample of the same size but from a different set can be taken.extractwill return a list in list context, or the first element of the sample in scalar context. it's giving reusable. π
β¨ CORRECTNESS PROOFS β¨
we're doing math now, stay with me, it's gonna slay.
π§ Algorithm A.
the crucial thing to see for the sample algorithm being correct is the fact that when we sample n elements from a set of size N, the t + 1st element is choosen with probability (n - m)/(N - t), when already m elements have been choosen. and we can immediately see that we will never pick too many elements (because the probability is 0 as soon as n == m), nor too few, because 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). π
π§ Algorithm B.
it's 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). π₯
β¨ LITERATURE β¨
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. π
β¨ REFERENCES β¨
- C. T. Fan, M. E. Muller and I. Rezucha, J. Amer. Stat. Assoc. 57 (1962), pp 387 - 402.
- T. G. Jones, CACM 5 (1962), pp 343.
- D. E. Knuth: The Art of Computer Programming, Volume 2, Third edition. Reading: Addison-Wesley, 1997. ISBN: 0-201-89684-2.
β¨ DEVELOPMENT β¨
the current sources of this module are found on github, <git://github.com/Abigail/algorithm--numerical--sample.git>. go star her, she deserves it. β
β¨ AUTHOR β¨
this package was written by Abigail, cpan@abigail.be. π
β¨ COPYRIGHT and LICENSE β¨
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.
okay and that's the whole page!! π this one came from the perl v5.30.3 era, dated 2020-07-27, and it's filed under Algori...Sample(3pm) in the User Contributed Perl Documentation. go sample something random, bestie, you've earned it. π
β¨π¦
π¦ where this page comes from
- debian bookworm: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- debian trixie: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- ubuntu jammy: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- ubuntu noble: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- ubuntu resolute: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- ubuntu stonking: libalgorithm-numerical-sample-perl 2010011201-2 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms