       Re: and sampling a distribution

• To: mathgroup at smc.vnet.net
• Subject: [mg110595] Re: and sampling a distribution
• From: Bill Rowe <readnews at sbcglobal.net>
• Date: Sun, 27 Jun 2010 04:55:43 -0400 (EDT)

```On 6/26/10 at 3:09 AM, readnews at sbcglobal.net (Bill Rowe) wrote:

>On 6/25/10 at 7:27 AM, stone at geology.washington.edu (John Stone)
>wrote:
>
>>I am trying to use RandomReal[ ] to sample from bins of different
>>widths that span the interval 0 - 1.  The bin widths represent the
>>weights I'm assigning to a family of trial solutions in an
>>optimization problem.  The aim is to sample the solutions in
>>proportion to their weights using a uniform distribution of random
>>numbers generated by RandomReal[ ].

>>For a simple example, however, suppose there are 10 equally
>>weighted solutions.  My selection process would use some code that
>>looks like:

>weights = Table[0.1, {10}]; bins = Accumulate[weights]; Select[bins,
>(# >= RandomReal[] &)][]

>Rather than RandomReal you should be using RandomChoice.
>Specifically,

>RandomChoice[weights->bins,10]

>will return a list of 10 values with the desired distribution. This
>can be seen by doing:

>Histogram[RandomChoice[weights -> bins, 1000]]

>and note with equal weights and equally spaced bins of size 0.1, the
>following is equivalent

>RandomInteger[{1,10}]/10//N

Up to this point my response was fine. RandomChoice is the thing
to use when you want random selection from a pre-defined list of
things with various weights.

But the explanation I gave for why the code didn't work as
expected is simply wrong. Peter Pain correctly pointed out
something I should have immediately realized. RandomReal
generate a new random value for each comparison made. And it is
this characteristic that causes the distribution to differ from
uniform. A simple demonstration that this is the case is to look
at the length of the lists returned that start with 0.1. That is:

In:= Union[
Length /@
Cases[Table[Select[bins, (# >= RandomReal[] &)], {1000}],
{0.1, __}]]

Out= {3,4,5,6,7,8,9}

If there were only one random value selected whenever the
selection was done, clearly the length of the lists with a given
starting value would be constant. The idea of using Select to
create the distribution can be made to work as follows:

With[a = RandomReal[], Select[bins, (# >= a) &]][]

Repeating the demonstration above using this code yields:

In:= Union[
Length /@
Cases[Table[
With[{a = RandomReal[]},
Select[bins, (# >= a) &]], {1000}], {0.1, __}]]

Out= {10}

showing every list returned that starts with the value 0.1
contains all ten values.

But while this corrects the issue, this code will execute slower
than code using RandomChoice will.

```

• Prev by Date: Re: precedence for ReplaceAll?
• Next by Date: Re: Image processing Questions
• Previous by thread: Re: and sampling a distribution
• Next by thread: function local/private to another function?