MathGroup Archive 2009

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

Search the Archive

Re: How to calculate a union

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)) +

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.

This is clearly different than the expression you have.
Additionally, P(A,B) is meaningless to me.

>(you can see the idea of the general formula in
> )

This doesn't seem to have anything to do with computing probabilities


>example (n=5): for i=2 {1, 2} {1, 3} {1, 4} {1, 5} {2, 3} {2, 4} {2,
>5} {3, 4} {3, 5} {4, 5}
>i=3 {1, 2, 3} {1, 2, 4} {1, 2, 5} {1, 3, 4} {1, 3, 5} {1, 4, 5} {2,
>3, 4} {2, 3, 5} {2, 4, 5} {3, 4, 5}


>sendToFile[val_, i_] := Module[{str},
>Export[ToString[i + 1] <> ".list", val, "List"];

since the local variable str is never used Module isn't needed.
That is

sendToFile[val_, i_] := Export[ToString[i + 1] <> ".list", val, "List"]

Does exactly the same thing but consumes a bit less memory since
the unneeded local variable is not created.

Also, it appears you are simply storing intermediate results
here. If so using Put instead of Export is more efficient. There
is no need to invoke any of the overhead Export has if you are
not going to use something other than Mathematica to read the
files. That is, I would write sendToFile as:

sendToFile[val_, i_]:=Put[val,ToString[i+1]<>".list"]

The resulting file format won't be the same. So, reading it back
in should be done with Get rather than ReadLists

>u = Range[5];
>sendToFile[{#} & /@ u, 0];

>addGreater[u_, i_] :=
>With[{z = Select[u, # > i[[-1]] &]}, Append[i, #] & /@ z]

More efficient is:

addGreater[u_, {i_}] := {i, #} & /@ Cases[u, _?(# > i &)]

Append tends to be slow and if I remember correctly consumes
more memory. Off hand, I don't know which is better to get the
larger values Cases or Select.

>createList[n_, u_] :=
>Module[{l = ReadList[ToString[n - 1] <> ".list"]},
>With[{k = Flatten[addGreater[u, #] & /@ l, 1]}, sendToFile[k, n -

This can be left as is provided sendToFile is not altered to use
Put. If sendToFile is altered to use Put this should be
re-written as:

createList[n_, u_] :=
  Module[{l = Get[ToString[n - 1] <> ".list"]},
   With[{k = Flatten[addGreater[u, #] & /@ l, 1]}, sendToFile[k,
n -

>For[i = 2, i <= Length[u], i++, createList[i, u]]

Since your code only generated a single file, this isn't going
to work as is without generating error messages. Additionally,
=46or is one of the slower functions in Mathematica. I've yet to
encounter anything where using For worked better than
alternatives. The obvious alternative here would be:

Table[createList[i,u], {i,2,Length@u}];

I don't know if my suggestions here are sufficient to solve your
problem with memory or not. Nor do I see the relationship
between your code and computing a probability.

  • Prev by Date: Re: How do I get Mathematica to Simplify this to 1?
  • Next by Date: Re: Converting Java into J/link runnable code
  • Previous by thread: Re: How to calculate a union
  • Next by thread: Re: How to calculate a union