Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

bug in RandomChoice if weight is zero?

4 views
Skip to first unread message

Matthias Greiff

unread,
Dec 29, 2009, 1:20:01 AM12/29/09
to
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

dh

unread,
Dec 30, 2009, 4:19:22 AM12/30/09
to
Hi Mathias,
from the manual:
"RandomSample[{Subscript[e, 1],Subscript[e, 2],\[Ellipsis]},n] never
samples any of the Subscript[e, i] more than once."
Daniel

Leonid Shifrin

unread,
Dec 30, 2009, 4:19:43 AM12/30/09
to
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

Daniel Lichtblau

unread,
Dec 31, 2009, 3:13:51 AM12/31/09
to
Matthias Greiff 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

I see your bug, and raise you another.

In[25]:= TimeConstrained[
RandomSample[UnitVector[100,1]->Range[100], 2], 10] (* hangs *)

Actually that's two more bugs, because it is both in some infinite
cycling and also not responding to aborts.

I'll look into repair today. Alternatively, I might go home and have a
good sob.

Daniel Lichtblau
Wolfram Research

Leonid Shifrin

unread,
Dec 31, 2009, 3:17:22 AM12/31/09
to
Nothing, but as I said, issuing diagnostic message for this particular case
seems to me like a point solution, given that there may be extremely small
non-zero weights expressed analytically (as a rational for example). I may
be wrong of course.

On Wed, Dec 30, 2009 at 8:29 AM, DrMajorBob <btr...@austin.rr.com> wrote:

> Generally, what you ask for should be possible once you give a specific
>> interpretation of what you mean by zero
>>
>

> What's ambiguous about the Integer 0?
>
> Bobby

>> On Tue, Dec 29, 2009 at 9:17 AM, Matthias Greiff <gre...@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
>>>
>>>
>>>
>

> --
> DrMaj...@yahoo.com
>

DrMajorBob

unread,
Dec 31, 2009, 3:17:55 AM12/31/09
to
If a probability is very small or algebraically zero but not easily
verified as such, you have a point.

But the OP's example had an explicit integer 0. Mathematica should have
thrown an error.

Bobby

On Wed, 30 Dec 2009 14:47:42 -0600, Leonid Shifrin <lsh...@gmail.com>
wrote:


--
DrMaj...@yahoo.com

DrMajorBob

unread,
Dec 31, 2009, 3:20:30 AM12/31/09
to
Yes, but even so. zero-probability events shouldn't occur in samples.

Hence, the documentation simply contradicts itself.

Bobby

On Wed, 30 Dec 2009 03:16:58 -0600, dh <d...@metrohm.com> wrote:

> Hi Mathias,
> from the manual:
> "RandomSample[{Subscript[e, 1],Subscript[e, 2],\[Ellipsis]},n] never
> samples any of the Subscript[e, i] more than once."
> Daniel
>
> On 29 Dez., 07:20, Matthias Greiff <gre...@mac.com> wrote:


--
DrMaj...@yahoo.com

DrMajorBob

unread,
Dec 31, 2009, 3:20:41 AM12/31/09
to
> Generally, what you ask for should be possible once you give a specific
> interpretation of what you mean by zero

What's ambiguous about the Integer 0?

Bobby

On Wed, 30 Dec 2009 03:17:21 -0600, Leonid Shifrin <lsh...@gmail.com>
wrote:

Matthias

unread,
Jan 5, 2010, 1:42:30 AM1/5/10
to
Another interesting thing about RandomSample is the following:

In[171]:= RandomSample[{0, 0, 0, 1, 0} -> {1, 2, 3, 4, 5}, 1]
RandomSample[{0, 0, 0, 1, 0} -> {1, 2, 3, 4, 5}, 2]
RandomSample[{0, 0, 0, 1, 0} -> {1, 2, 3, 4, 5}, 3]
RandomSample[{0, 0, 0, 1, 0} -> {1, 2, 3, 4, 5}, 5]

Out[171]= {4}
Out[172]= {4, 5}
Out[173]= {4, 5, 5}
Out[174]= {4, 5, 5, 5, 5}

Although the fifth element has weight zero it is selected multiple
times? Is that only in my version of Mathematica (Version 7.0.1.0 on
Max OS X 10.6.2)?

Of course there are ways to work around that but, as DrMajorBob,
already pointed out, this contradicts the documentation.

0 new messages