What is the module that selects a random sample of size k (without replacement) from a given population of size n, involving the command Subsets.
Asked
Active
Viewed 2,258 times
4
m_goldberg
- 107,779
- 16
- 103
- 257
mais
- 71
- 3
-
I have a problem with " without replacement" – mais Nov 25 '13 at 17:51
-
1Enter "random sample", with or without quotes, in the Documentation Center. Check amongst the top hits and you should find something applicable. – Daniel Lichtblau Nov 25 '13 at 17:53
-
I didn't find anything about without replacement – mais Nov 25 '13 at 18:06
-
(1) What did you find? (2) What did the documentation state about what you found? – Daniel Lichtblau Nov 25 '13 at 18:11
-
I found how to select a random sample... but there's nothing about "without replacement" – mais Nov 25 '13 at 18:22
-
3Click under Details. Always. – Daniel Lichtblau Nov 25 '13 at 18:25
-
1@DanielLichtblau So surely you can answer here! – Dr. belisarius Nov 25 '13 at 19:55
-
@Belisarius Not knowing how to do that, I defer to Shirley. – Daniel Lichtblau Nov 25 '13 at 20:13
-
@DanielLichtblau http://www.whitepages.com/name/Shirley-Defer – Dr. belisarius Nov 25 '13 at 20:19
1 Answers
6
Well, they don't literally use the terminology "without replacement" in the manual, but One might expect you should know that is the same as saying "no elements occur more then once"..
Anyway..in summary there are two related functions:
RandomSample[list,k] -> "without replacement"
RandomChoice[list,k] -> "with replacement"
And to literally answer the question, if you insist on using Subsets,
RandomChoice@Subsets[list,{k}]
will also give a sample "without replacement". Note Subsets approach will be horribly slow for large lists since it generates all combinations then picks one. (Subsets also orders the result which you may or may not want)
Just for fun you get respectable performance out of Subsets if you do this:
Subsets[list,{k},{RandomInteger[ Length[list]! / (Length[list]-k)! / k!]}]
george2079
- 38,913
- 1
- 43
- 110