Re: bug in RandomChoice if weight is zero?
- To: mathgroup at smc.vnet.net
- Subject: [mg106027] Re: [mg105976] bug in RandomChoice if weight is zero?
- From: Leonid Shifrin <lshifr at gmail.com>
- Date: Wed, 30 Dec 2009 04:17:21 -0500 (EST)
- References: <200912290617.BAA02582@smc.vnet.net>
Hi Matthias, presumably this is because RandomSample never samples any given element more than once - in that case it has no other choice, regardless of the weight. Perhaps, the error message might be added for exact symbolic zero appearing somewhere in the wieights for the case where the size of the desired sample is larger than the number of non-zero weights (non-zero means UnsameQ[weight,0]). It is not clear however whether it is worth it given the overhead of such an analysis. Besides, this will be a point solution anyway, since some expressions that might be used for weights may be in fact identically equal to zero but just not simplified to zero - these cases would not be caught by such an analysis. OTOH, it is hard to make a formal (not based on syntax) distinction between zero and some very small weight close to zero (which may be expressed not necessarily with machine precision). This of course depends on the implementation of the underlying algorithm used to do the sampling - whether or not it treats weights smaller than machine epsilon as zero. From the point of view of the abstract interface of RandomSample, there is no reason why it should do that, even if such events are highly unlikely to be sampled. Generally, what you ask for should be possible once you give a specific interpretation of what you mean by zero, but arguably this would add more ambiguities/restrictions than it is worth for a kind of a general-purpose function that RandomSample is. After all, you can always wrap RandomSample in your own wrapper function which will implement this functionality in the way you want it (consistent with your definition of zero), while preserving the syntax of the original RandomSample. Regards, Leonid On Tue, Dec 29, 2009 at 9:17 AM, Matthias Greiff <greiff at mac.com> wrote: > If I use RandomChoice with weights of zero the corresponding elements will > never be selected. > > In[62]:= RandomChoice[{0, 0, 3} -> {3, 2, 1}, 5] > Out[62]= {1, 1, 1, 1, 1} > > Why is it not the same if I use RandomSample? > The following command > > RandomSample[{1, 2, 0} -> Range[3], 2] > > returns either {1,2} or {2,1}. But when I change the command to sample size > 3 I get the following. > > In[60]:= RandomSample[{1, 2, 0} -> Range[3], 3] > Out[60]= {2, 1, 3} > > Why is the third element selected? Shouldn't Mathematica return an error > message because the sample size is larger than the population size? > > Appreciate your answers. > > Merry Christmas! > > Matthias > >
- References:
- bug in RandomChoice if weight is zero?
- From: Matthias Greiff <greiff@mac.com>
- bug in RandomChoice if weight is zero?