{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"tags": [
"remove_cell"
]
},
"outputs": [],
"source": [
"# HIDDEN\n",
"from datascience import *\n",
"from prob140 import *\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('fivethirtyeight')\n",
"%matplotlib inline\n",
"import math\n",
"from scipy import stats\n",
"\n",
"from matplotlib import patches\n",
"import shapely.geometry as sg\n",
"import descartes"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Inclusion-Exclusion ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While we have bounds on the probability of the union of $n$ events, we don't yet have a formula for the exact chance except in the case $n = 2$. For $n = 2$ we have \n",
"\n",
"$$\n",
"P(A \\cup B) = P(A) + P(B) - P(AB)\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Union of $n$ Events ###\n",
"Let's see if we can guess the formula for larger $n$, by applying what we know about the union of two events. \n",
"\n",
"For $n = 3$, the event $A_1 \\cup A_2 \\cup A_3$ can be written as $B \\cup A_3$ where $B = A_1 \\cup A_2$. So by applying the formula for the case of two events in every line, we have\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"P(A_1 \\cup A_2 \\cup A_3) ~ = ~ P(B \\cup A_3) &= ~ P(B) + P(A_3) - P(BA_3) \\\\\n",
"&= ~ P(A_1) + P(A_2) - P(A_1A_2) + P(A_3) - P(A_1A_3 \\cup A_2A_3)\\\\\n",
"&= ~ \\sum_{i=1}^3 P(A_i) - \\mathop{\\sum \\sum}_{1 \\le i < j \\le 3} P(A_iA_j) + P(A_1A_2A_3)\n",
"\\end{align*}\n",
"$$\n",
"\n",
"A clear pattern is emerging. Writing it out will be easier if we have some rough-and-ready abbreviations of descriptions, so for example let \"all the double-intersections\" mean \"all terms of the form $P(A_iA_j)$ where $i$ and $j$ are different\".\n",
"\n",
"It is important to note that the set \"$1 \\le i < j \\le n$\" specifies all unordered pairs of distinct indices. If the indices are distinct, one of them has to be less than the other, so it is part of the indicated set. If $i$ and $j$ are in the set, then $i < j$, so $i$ and $j$ are distinct.\n",
"\n",
"In the same way, $1 \\le i < j < k \\le n$ specifies all unordered triples of distinct indices. And so on.\n",
"\n",
"**Guess.** Based on what we saw for three events, we will guess that the chance of the union of $n$ events can be calculated by:\n",
"\n",
"- including the probabilities of all the events; then\n",
"- excluding the probabilities of all the double-intersections; then\n",
"- including the probabilities of all the triple-intersections; then\n",
"- excluding the probabilities of all the quadruple intersections; and so on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### General Inclusion-Exclusion Formula ###\n",
"For events $A_1, A_2, \\ldots, A_n$, \n",
"\n",
"$$\n",
"\\begin{align*}\n",
"& P\\big{(}\\bigcup_{i=1}^n A_i \\big{)} \\\\\n",
"&= \\sum_{i=1}^n P(A_i) - \\mathop{\\sum \\sum}_{1 \\le i < j \\le n} P(A_iA_j) + \\mathop{\\sum \\sum \\sum}_{1 \\le i < j < k \\le n} P(A_iA_jA_k) - \\cdots + (-1)^{n+1} P(A_1A_2 \\ldots A_n) \\\\\n",
"\\end{align*}\n",
"$$\n",
"\n",
"You can prove this by induction if you feel inclined; just go through steps analogous to those above for the case $n=3$. We will prove the formula in a later chapter by a different method.\n",
"\n",
"For now, let's accept it and move on."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### The Number of Terms in Each Sum ###\n",
"To end the section we will count the number of terms in each of the sums in the inclusion-exclusion formula, so we know the extent of the work that has to be done to apply it.\n",
"\n",
"Here is the formula again for reference:\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"& P\\big{(}\\bigcup_{i=1}^n A_i \\big{)} \\\\\n",
"&=\n",
"\\sum_{i=1}^n P(A_i) - \\mathop{\\sum \\sum}_{1 \\le i < j \\le n} P(A_iA_j) + \\mathop{\\sum \\sum \\sum}_{1 \\le i < j < k \\le n} P(A_iA_jA_k) - \\cdots + (-1)^{n+1} P(A_1A_2 \\ldots A_n)\n",
"\\end{align*}\n",
"$$\n",
"\n",
"\n",
"Clearly there are $n$ terms in the first sum. For reasons that will become clear in the next step, we will write that as\n",
"\n",
"$$\n",
"\\binom{n}{1} = n\n",
"$$\n",
"\n",
"In the second sum the terms correspond to distinct unordered pairs chosen from the indices 1 through $n$. That number is\n",
"\n",
"$$\n",
"\\binom{n}{2} = \\frac{n(n-1)}{2}\n",
"$$\n",
"\n",
"In the third sum, the number of terms is the number of sets of three:\n",
"\n",
"$$\n",
"\\binom{n}{3} = \\frac{n(n-1)(n-2)}{3!}\n",
"$$\n",
"\n",
"and so on.\n",
"\n",
"This shows that a lot of terms are being added and subtracted in the inclusion-exclusion formula. \n",
"\n",
"But sometimes we get lucky, and many of the terms are equal. Then the sums simplify dramatically. For a beautiful example, keep reading."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}