# How to extend the Inclusion Exclusion Principle to an XOR situation

For calculating the probability of the union of independent events, $P(A_1\cup A_2\cup A_3\cup A_4)$ one can use the Inclusion Exclusion principle:

\eqalign{ P\Bigl(\bigcup_{i=1}^n A_i\Bigr) = \sum_{i\le n} P(A_i) - &\sum_{i_1<i_2} P(A_{i_1}\cap A_{i_2}) +\sum_{i_1<i_2<i_3} P(A_{i_1}\cap A_{i_2}\cap A_{i_3}) - \cr &\cdots+ (-1)^{n}\sum_{i_1<i_2<\cdots<i_{n-1} } P(A_{i_1}\cap\cdots\cap A_{i_{n-1}} )\cr&\qquad\qquad + (-1)^{n+1}P(A_1\cap A_2\cap\cdots\cap A_n)}.

How do we extend this to the case where we want the probability of one and only one of the events occurring (an XOR scenario)?

For a little clarification on my specific application, I am trying to use a recursive formula in Python to calculate P( A XOR B XOR C XOR ...):

def inclusionExclusion(P,n):
if n < 1:
return 0 #error state
elif n == 1:
return P
else:
temp = inclusionExclusion(P,n-1)
return temp + P - temp*P


This works (caveat: this is for when all events have the same probability, P, of occuring) for the inclusive or case, but not for the exclusive or case.

Further, if I want the Probability of $(A_1 \& A_2)\ OR\ (A_1 \& A_3)\ OR\ (A_1\&A_4)$, i.e. two and only two at a time, or three and only three at a time, how would I go about it?

• As an aside, my specific case ended up being solvable by using the probability mass function for a binomial random variable. The end result was a binomial distribution. – TraxusIV Feb 19 '15 at 19:01
• Just a remark/question: the inclusion-exclusion principle you mentioned holds even when the events aren't independent, right? – Quinn Culver Jul 2 '16 at 14:49

## 1 Answer

Firstly, $XOR$ for $2$ or more operands is defined as (Source: Wikipedia)

XOR is true whenever an odd number of inputs is true. A chain of XORs—a XOR b XOR c XOR d (and so on)—is true whenever an odd number of the inputs are true and is false whenever an even number of inputs are true.

This differs to your use if it. I'll assume you mean "exactly one of a set of events". Then,

\begin{eqnarray*} P(\text{Exactly one of $\{A_1,\ldots,A_n\}$}) &=& P\left(\bigcup_{i=1}^{n}{\left(A_i \bigcap_{j=1,j\neq i}^{n}{A_j^c} \right)} \right) \\ && \\ &=& \sum_{i=1}^{n}{P\left(A_i \bigcap_{j=1,j\neq i}^{n}{A_j^c} \right)} - \sum_{i\lt k}{P\left(\left(A_i \bigcap_{j=1,j\neq i}^{n}{A_j^c} \right) \bigcap \left(A_k \bigcap_{l=1,l\neq k}^{n}{A_l^c} \right)\right)} \\ && + \cdots \qquad\qquad\qquad\qquad\qquad\text{by Inclusion-Exclusion} \\ && \\ &=& \sum_{i=1}^{n}{P\left(A_i \bigcap_{j=1,j\neq i}^{n}{A_j^c} \right)} \\ && \qquad\text{since in all but the first term there is some $m$} \\ && \qquad\text{for which $A_m \cap A_m^c$ appears.} \\ \end{eqnarray*}

This result is fairly obvious from the event definition without this Inclusion-Exclusion derivation.

The corresponding result for "Exactly $2/3/\cdots\;$ of $\{A_1,\ldots,A_n\}$" is similar. E.g.

$$P(\text{Exactly two of \{A_1,\ldots,A_n\}}) = \sum_{i\lt j}^{n}{P\left(A_i \bigcap A_j \bigcap_{k=1,k\neq i,j}^{n}{A_k^c} \right)}.$$

Note that these results simplify in certain cases: (a) if the events $A_i$ are independent, (b) the events $A_i$ have the same probability $p$. E.g. if both (a) and (b) are true then

$$P(\text{Exactly two of \{A_1,\ldots,A_n\}}) = \binom{n}{2}p^2(1-p)^{n-2}.$$

• Good assumption. Thanks for the quick solution! – TraxusIV Feb 19 '15 at 18:54
• @TraxusIV No problem. Glad to help. – Mick A Feb 19 '15 at 18:55
{"ft_lang_label":"__label__en","ft_lang_prob":0.68977714,"math_prob":0.99897397,"size":1223,"snap":"2021-21-2021-25","text_gpt3_token_len":452,"char_repetition_ratio":0.11730927,"word_repetition_ratio":0.0,"special_character_ratio":0.364677,"punctuation_ratio":0.10037175,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99995375,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-15T05:41:08Z\",\"WARC-Record-ID\":\"<urn:uuid:2a77aa8f-ded7-40bb-9e70-b556dbc6839e>\",\"Content-Length\":\"164864\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4569df9a-769a-4b40-95e3-0423ae1c195d>\",\"WARC-Concurrent-To\":\"<urn:uuid:d3df7c3f-435c-4f94-acad-a2d2326912e4>\",\"WARC-IP-Address\":\"151.101.65.69\",\"WARC-Target-URI\":\"https://math.stackexchange.com/questions/1154985/how-to-extend-the-inclusion-exclusion-principle-to-an-xor-situation\",\"WARC-Payload-Digest\":\"sha1:SPULFQEGPNSKVIYQWKWVGJLHEBGFDPX7\",\"WARC-Block-Digest\":\"sha1:62CUIVJB6SIMUPEKC2V55VCKYFWW5QWH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487617599.15_warc_CC-MAIN-20210615053457-20210615083457-00098.warc.gz\"}"}