[R] Sum of Bernoullis with varying probabilities

Thomas Lumley tlumley at u.washington.edu
Mon Oct 9 16:44:46 CEST 2006


On Fri, 6 Oct 2006, Deepayan Sarkar wrote:

> On 10/6/06, Ted Harding <Ted.Harding at nessie.mcc.ac.uk> wrote:
>> Hi Folks,
>>
>> Given a series of n independent Bernoulli trials with
>> outcomes Yi (i=1...n) and Prob[Yi = 1] = Pi, I want
>>
>>   P = Prob[sum(Yi) = r]  (r = 0,1,...,n)
>>


>> OK, some abbreviation of the above can be achieved with
>> the 'apply' function (and I've spelt out the details for
>> clarity). But I feel the basis of the approach (i.e. 'combn')
>> is crude (also tending to cause problems if n is large);
>> and I'm wondering if there is a nicer way.
>
> I don't see how. Someone has to evaluate the n-choose-r distinct terms
> to be added, and your code seems to be doing that as directly as
> possible.

Not necessarily. It could be quicker to numerically invert the 
characteristic function, which is easy to compute.

 	-thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list