\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"

\n",
"

"
],
"text/plain": [
" X=0 X=1 X=2\n",
"Y=2 0.000 0.125 0.125\n",
"Y=1 0.125 0.250 0.125\n",
"Y=0 0.125 0.125 0.000"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joint_dist = Table().values('X', k, 'Y', k).probability_function(joint_probability)\n",
"joint_dist"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This display of the joint distribution object `joint_dist` is called a *joint distribution table* for $X$ and $Y$. \n",
"\n",
"Each cell corresponds to a pair $(x, y)$, where $x$ is a value of $X$ and $y$ a value of $Y$. In the cell you see $P(X = x, Y = y)$, the probability of the pair $(x, y)$. \n",
"\n",
"Joint distribution tables are analogous to the contingency tables you saw in Data 8 when you were analyzing the relation between two categorical variables. In contingency tables, each cell contains the number of individuals in one particular pair of categories. In joint distribution tables, such as the one above, each cell contains the probability of one particular pair of values."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To check that we do indeed have a distribution over all the possible values of the pair $(X, Y)$, we can add up all the probabilities. The sum is 1, as it should be for a distribution."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joint_dist.total_probability()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In fact this is a double check, as the the method for constructing the joint distribution object returns an error if all the probabilities don't sum to 1."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Finding Probabilities ###\n",
"The table contains complete information about the relation between $X$ and $Y$. To find the probabiilty of any event determined by $X$ and $Y$, simply identify the cells that make the event happen, and add up their chances. This is an application of the fundamental method of finding probabilities by partitioning an event."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For example, consider the event $\\{ X = Y \\}$ for $X$ and $Y$ as defined in our example above. The event can happen in three ways: if $X$ and $Y$ are both 0, or both 1, or both 2. So\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"P(X = Y) &= P(X = 0, Y = 0) + P(X = 1, Y = 1) + P(X = 2 , Y = 2) \\\\\n",
"&= 0.125 + 0.25 + 0.125 \\\\\n",
"&= 0.5\n",
"\\end{align*}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's visualize this using the joint distribution table of $X$ and $Y$. To do this, we need a way of specifying the event $\\{ X = Y \\}$. \n",
"\n",
"We will use a method that is of fundamental importance to everything that follows in this course: we will define a function called the *indicator of the event*. The function just returns a Boolean: 1 if the event occurs, and 0 otherwise. In this example, for any pair $(i, j)$ it returns 1 if $i = j$ and 0 otherwise. "
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"def indicator_equal(i, j):\n",
" return i == j # Note the == sign. This is a comparison that results in a Boolean."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `event` method applied to a joint distribution object allows us to visualize the event and also displays the probability of the event. The arguments are: \n",
"\n",
"- The name of a function that is the indicator of the event; it takes two arguments, say `a` and `b`, and returns the Boolean corresponding to whether or not the pair (`a`, `b`) is in the event\n",
"- The name of the random variable whose value is the first co-ordinate `a`\n",
"- The name of the random variable whose value is the second co-ordinate `b`"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P(Event) = 0.5\n"
]
},
{
"data": {
"text/html": [
"X=0 | X=1 | X=2 | |
---|---|---|---|

Y=2 | 0.000 | 0.125 | 0.125 |

Y=1 | 0.125 | 0.250 | 0.125 |

Y=0 | 0.125 | 0.125 | 0.000 |

\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"

\n",
"

"
],
"text/plain": [
" X=0 X=1 X=2\n",
"Y=2 0.125\n",
"Y=1 0.25 \n",
"Y=0 0.125 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joint_dist.event(indicator_equal, 'X', 'Y')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The display `P(event) = 0.5` is consistent with our earlier answer $P(X = Y) = 0.5$. In the table, probabilities are displayed only in the cells that are in the event, that is, only the cells $(x, y)$ for which $x = y$. \n",
"\n",
"You can see that these cells all lie along the $x = y$ diagonal line. The coordinate geometry will be visible again when we calculate $P(Y \\ge X)$, for example."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"P(Event) = 0.75\n"
]
},
{
"data": {
"text/html": [
"X=0 | X=1 | X=2 | |
---|---|---|---|

Y=2 | 0.125 | ||

Y=1 | 0.25 | ||

Y=0 | 0.125 |

\n",
"\n",
"\n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
" \n",
"

\n",
"

"
],
"text/plain": [
" X=0 X=1 X=2\n",
"Y=2 0.000 0.125 0.125\n",
"Y=1 0.125 0.25 \n",
"Y=0 0.125 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def indicator_y_at_least_x(i, j):\n",
" return j >= i\n",
"\n",
"joint_dist.event(indicator_y_at_least_x, 'X', 'Y')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The visible cells form the upper triangle corresponding to points whose coordinates $(x, y)$ satisfy the the inequality $y \\ge x$. The chance of the event is $P(Y \\ge X) = 0.75$."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"### The General Calculation ###\n",
"As we have seen in these examples, saying that random variables $X$ and $Y$ satisfy a specified condition is the same as saying that the random point $(X, Y)$ falls in a particular region of the plane. In general, an *event determined by $X$ and $Y$* has the form \"the point $(X, Y)$ is in a specified region $B$ of the plane\". \n",
"\n",
"In the case of the event $\\{ X = Y \\}$ the region $B$ is the diagonal formed by points whose coordinates satisfy $x = y$.\n",
"\n",
"In the case of the event $\\{ Y \\ge X \\}$ the region $B$ is the upper triangle formed by the points whose coordinates satisfy $y \\ge x$.\n",
"\n",
"The probability of the event is $P((X,Y) \\in B)$, which is obtained in two steps:\n",
"\n",
"- Identify all pairs of possible values $(x, y)$ such that $(x, y) \\in B$.\n",
"- Add the probabilities $P(X = x, Y = y)$ of all those pairs.\n",
"\n",
"Expressed more compactly,\n",
"\n",
"$$\n",
"P((X, Y) \\in B) ~ = ~ \\mathop{\\sum \\sum}_{(x,y) \\in B} P(X = x, Y = y)\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"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
}
X=0 | X=1 | X=2 | |
---|---|---|---|

Y=2 | 0.000 | 0.125 | 0.125 |

Y=1 | 0.125 | 0.25 | |

Y=0 | 0.125 |