Dear community members,
I’m trying to replicate Dave Giles’ permutation test examples using Mathematica but I’m having difficulties randomly selecting 50,000 samples from all the possible permutations of a list of 20 prices.
Mathematica runs out of memory in my laptop (8 GB RAM).
prices = {5.0, 4.8, 4.7, 4.0, 5.3, 4.1, 5.5, 4.7, 3.3, 4.0, 4.0, 4.6,
5.3, 3.0, 3.5, 3.9, 4.7, 5.0, 5.2, 4.6};
In[4]:= RandomSample[Permutations[prices], 50000]
During evaluation of In[4]:= General::nomem: The current computation was aborted because there was insufficient memory available to complete the computation.
During evaluation of In[4]:= Throw::sysexc: Uncaught SystemException returned to top level. Can be caught with Catch[\[Ellipsis], _SystemException].
Out[4]= SystemException["MemoryAllocationFailure"]
Is there an alternative way that I could use to generate those samples?
Just a simple comment: Your method is obviously more efficient than the OP’s. However, the resulting collection of permutations will probably contain duplicates. While Rubens method will not.
Point well taken. However, here’s my (maybe feeble) defense:
The R code used in “Dave Giles’ permutation test examples” allows for potential duplicates. The probability that there will be any duplicates (let alone even a few) with 50,000 samples out of 20! elements is extremely small. (Although to have a proper defense I’ll need to quantify that.)
The number of duplicates in any particular sample using the method I
proposed will almost always be zero. One could execute Length[samples] - Length[DeleteDuplicates[Sort[samples]]] to
provide the number of duplicates.
If one looked at the distribution of the 20! test statistics and the distribution of an infinite number of samples with replacement from those 20! test statistics, those distributions would be identical as each of the 20! test statistics will be represented the same proportion of the time.
There will be no practical difference in the result with or without duplicates for this particular permutation test and dataset. (I am reminded of the Hans Christian Andersen story “The Princess and the Pea”.) Or rather the only practical difference will be the ability to perform the test (allowing duplicates) or not (not allowing duplicates).
But it is apparently desired to have 50,000 permutations with none of those arrangements duplicated. With a sample of size 1 there are by definition no duplicates. What am I missing?
Actually, after rereading what you posted more carefully, I agree with you. You can obviously expand the approach I posted above to include 50000 samples:
Good idea, Neil. If one needed exactly 50,000 samples with no duplicates, just sample somewhat more than 50,000, delete duplicates, and then just use the first 50,000.
Dear all,
thank your for your answers.
I followed Jim’s initial suggestion and I got a result (0.00184) pretty close to Dave’s figures ( 0.0017 & 0.0016).
Regards,
Ruben