Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: How to calculate a union

  • To: mathgroup at
  • Subject: [mg104551] Re: [mg104536] Re: How to calculate a union
  • From: Daniel Lichtblau <danl at>
  • Date: Wed, 4 Nov 2009 01:30:59 -0500 (EST)
  • References: <hcjjdv$jte$> <hcl3rn$buv$> <>

rat wrote:
> Helen Read:
>> Bill Rowe wrote:
>>> On 10/31/09 at 1:54 AM, ramiro.barrantes at (Ramiro) wrote:
>>>> I am trying to compute the probability of a union, the formula
>>>> requires to sum and subtract all possible combinations of
>>>> intersections for all pairs, triplets, etc.  When n is big, getting
>>>> all the terms is a challenge.  Otherwise, I can easily compute any
>>>> given intersection.
>>>> ex. P(AUBUC) = P(A) + P(B) + P(C) - (P(A,B)+P(A,C)+P(B,C)) +
>>>> P(A,B,C)
>>> Hmm... You say you are computing a probability. So, I would read
>>> P(AUBUC) as being the probability of event A or event B or event
>>> C occurring. Using that interpretation then
>>> P(AUBUC) = P(A) + P(B) + P(C)
>>> where I've assumed events A,B and C are independent events.
>> The size union is only equal to the sum of the sizes of the individual 
>> sets if the sets are disjoint, which one cannot assume.
>>> This is clearly different than the expression you have.
>>> Additionally, P(A,B) is meaningless to me.
>> He meant P(A intersect B) by that, and has applied the 
>> inclusion/exclusion principle correctly.
> Hi Helen.
> I have a similar problem.
> I'm new to Mathematica and I'd like to program the inclusion-exclusion 
> principle to calculate P(AUBUCUD), for different probabilities 
> P(A),P(B),P(C),P(D) asuming these events are indepedent.
> What functions would be useful? I need something to start investigating...

If the individual probabilities are known, and the events are 
independent, then you can do this iteratively as below. Works because 
the intersection probabilities of any event combination are simply the 
products of the individual probabilities.

probabilityOfUnion[a_, b_] := prob[a] + prob[b] - prob[a]*prob[b]

probabilityOfUnion[a_, b_, c__] := Module[
   {ab, res},
   prob[ab] = probabilityOfUnion[a, b];
   res = probabilityOfUnion[ab, c];

Here is an example. I randomly assign probabilities less than .2 to 15 
independent events.

len = 15;
aa = Array[a, len];
Do[prob[a[j]] = RandomReal[.2], {j, len}]

We compute the union probability:

In[88]:= Apply[probabilityOfUnion, aa]
Out[88]= 0.8317

If you are willing to work directly with the probabilities of the 
individual events, it can be code much more succinctly as below.

prob[ll : {_, _}] := Total[ll] - Apply[Times, ll]

prob[{a_, b_, c__}] := prob[{prob[{a, b}], c}]

len = 15;
aa = RandomReal[.2, len];

In[10]:= prob[aa]
Out[10]= 0.833538

Daniel Lichtblau
Wolfram Research

  • Prev by Date: Re: Set::setps error? Twitter.m's OOP-like approach?
  • Next by Date: Re: Mathematica and desktop search
  • Previous by thread: Re: How to calculate a union
  • Next by thread: Re: How to calculate a union