# Generalization of the probability of union

hi

How can I do for mathematics to expand this equation for any value of the variables i, j, k

• Possible duplicate: mathematica.stackexchange.com/questions/56015/…. – JimB Nov 20 '17 at 4:11
• I need mahematica to develop the formula, not calculate! – susy diaz Nov 20 '17 at 22:22
• I'm not understanding how you want to provide the input and obtain an output. For example, do you want union[{a1, a2, a3}] to produce pr[a1] + pr[a2] + pr[a3] - pr[{a1, a2}] - pr[{a1, a3}] - pr[{a2, a3}] + pr[{a1, a2, a3}] (assuming you have a function pr that provides the associated probabilities of the events) ? – JimB Nov 20 '17 at 22:44
• if I need that given an entry for some i = 4 develop the formula for P (A1 + A2 + A3 + A4) for example, now if you calculate it better – susy diaz Nov 22 '17 at 22:20

Try this exercise in pattern matching. Effectively this a recursive definition of the inclusion-exclusion formula.

p[un[x_, y__]] := p[un[x]] + p[un[y]] - p[in[un@x, un@y]];
un[un[x__]] := un[x];
un[in[x__]] := in[x];
in[un[x__]] := un[x];
in[x_, un[z_, y__]] := un[in[un@x, un@z], in[un@x, un@y]];
in[in[x__]] := in[x];
in[in[x__], in[y__]] := in @@ Intersection[{x, y}]

result = p[un[x, y, z, t, q, w, r]] /. un[x_] :> x;
patterns = Cases[result, p[x___], Infinity];
arglength[p[in[x__]]] := Length[{x}] -> p[x];
arglength[p[x_]] := 1 -> p[x]
counts=Map[Length]@GatherBy[#, First] &@Map[arglength]@patterns

{7, 21, 35, 35, 21, 7, 1}

counts == Table[Binomial[7, i], {i, 1, 7}]
True


If you want all the steps use Trace

Trace[p[un[x, y, z]] /. un[x_] :> x]