{ "cells": [ { "cell_type": "code", "execution_count": 17, "metadata": { "tags": [ "remove_cell" ] }, "outputs": [], "source": [ "# HIDDEN\n", "from datascience import *\n", "from prob140 import *\n", "import numpy as np\n", "from scipy import stats\n", "import matplotlib.pyplot as plt\n", "plt.style.use('fivethirtyeight')\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "tags": [ "remove_cell" ] }, "outputs": [], "source": [ "# HIDDEN\n", "\n", "# X = number of heads in first two tosses; Y = number of heads in first five tosses\n", "def joint_probability(x, y):\n", " if y >= x:\n", " return stats.binom.pmf(x, 2, 1/2) * stats.binom.pmf(y-x, 3, 1/2)\n", " else:\n", " return 0\n", " \n", "k_x = np.arange(3)\n", "k_y = np.arange(6)\n", "\n", "joint_table = Table().values('X', k_x, 'Y', k_y).probability_function(joint_probability)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional Distributions ##" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To understand the relation between two variables we must examine the conditional behavior of each of them given the value of the other. Towards this goal, we will start by examining the example of the previous section and then develop the general theory." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In our example, the joint distribution of $X$ and $Y$ is given by joint_table. Here we also display the marginal distribution of $X$." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X=0X=1X=2
Y=50.000000.00000.03125
Y=40.000000.06250.09375
Y=30.031250.18750.09375
Y=20.093750.18750.03125
Y=10.093750.06250.00000
Y=00.031250.00000.00000
Sum: Marginal of X0.250000.50000.25000
\n", "
" ], "text/plain": [ " X=0 X=1 X=2\n", "Y=5 0.00000 0.0000 0.03125\n", "Y=4 0.00000 0.0625 0.09375\n", "Y=3 0.03125 0.1875 0.09375\n", "Y=2 0.09375 0.1875 0.03125\n", "Y=1 0.09375 0.0625 0.00000\n", "Y=0 0.03125 0.0000 0.00000\n", "Sum: Marginal of X 0.25000 0.5000 0.25000" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "joint_table.marginal('X')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now suppose we know that $Y = 3$. Then the outcome space is reduced to just the cells in the row labeled Y=3." ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "P(Event) = 0.3125\n" ] }, { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
X=0X=1X=2
Y=5
Y=4
Y=30.031250.18750.09375
Y=2
Y=1
Y=0
\n", "
" ], "text/plain": [ " X=0 X=1 X=2\n", "Y=5 \n", "Y=4 \n", "Y=3 0.03125 0.1875 0.09375\n", "Y=2 \n", "Y=1 \n", "Y=0 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def indicator_Y_equals_3(i, j):\n", " return j == 3\n", "\n", "joint_table.event(indicator_Y_equals_3, 'X', 'Y')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Of course, the probabilities along this row don't sum to 1. Their sum is $P(Y = 3) = 0.3125$.\n", "\n", "By the division rule, for each $x = 0, 1, 2$ we have\n", "\n", "$$\n", "P(X = x \\mid Y = 3) ~ = ~ \\frac{P(X = x, Y = 3)}{P(Y = 3)}\n", "$$\n", "\n", "By *normalizing* all the probabilities in the row by their sum, we get the *conditional distribution of $X$ given $Y=3$*.\n", "\n", "\n", "\\begin{align*}\n", "P(X = 0 \\mid Y = 3) ~ &= ~ \\frac{0.03125}{0.3125} = 0.1 \\\\ \\\\\n", "P(X = 1 \\mid Y = 3) ~ &= ~ \\frac{0.1875}{0.3125} = 0.6 \\\\ \\\\\n", "P(X = 2 \\mid Y = 3) ~ &= ~ \\frac{0.09375}{0.3125} = 0.3\n", "\\end{align*}\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare this conditional distribution to the unconditional distribution of $X$:\n", "\n", "$$\n", "P(X = 0) ~ = ~ 0.25, ~~~~~ P(X = 1) ~ = ~ 0.5, ~~~~~ P(X = 2) ~ = ~ 0.25\n", "$$\n", "\n", "The two distributions are different. Given $Y = 3$, the chance that $X$ is large is higher than it is if we don't have that condition. \n", "\n", "This shows that $X$ and $Y$ are *dependent*. We will define dependence and independence formally in the next section." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Conditional Distribution of $X$ given $Y = y$ ###\n", "The conditional_dist method operates on a joint distribution object and displays conditional distributions, as follows." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", " \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", "
X=0X=1X=2Sum
Dist. of X | Y=50.000.01.001.0
Dist. of X | Y=40.000.40.601.0
Dist. of X | Y=30.100.60.301.0
Dist. of X | Y=20.300.60.101.0
Dist. of X | Y=10.600.40.001.0
Dist. of X | Y=01.000.00.001.0
Marginal of X0.250.50.251.0
\n", "
" ], "text/plain": [ " X=0 X=1 X=2 Sum\n", "Dist. of X | Y=5 0.00 0.0 1.00 1.0\n", "Dist. of X | Y=4 0.00 0.4 0.60 1.0\n", "Dist. of X | Y=3 0.10 0.6 0.30 1.0\n", "Dist. of X | Y=2 0.30 0.6 0.10 1.0\n", "Dist. of X | Y=1 0.60 0.4 0.00 1.0\n", "Dist. of X | Y=0 1.00 0.0 0.00 1.0\n", "Marginal of X 0.25 0.5 0.25 1.0" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# conditional distribution of X given each different value of Y\n", "\n", "joint_table.conditional_dist('X', 'Y') " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To understand this table, start with the row labeled Y=3. The entries are the probabilities in the conditional distribution of $X$ given $Y=3$. \n", "\n", "In the row labeled Y=1, the entries are the probabilities in the conditional distribution of $X$ given $Y=1$. Notice that if $Y=1$ then $X$ can't be 2. You can go back and confirm that in the joint distribution table, $P(X = 2, Y = 1) = 0$.\n", "\n", "All the other rows can be understood in the same way. In row $y$, the given condition is $Y=y$, and the entries are the probabilities in the conditional distribution of $X$ given $Y=y$.\n", "\n", "It is easy to see why each row in the table of conditional distributions sums to 1. The value in each cell in the row is obtained from the joint distribution table by taking the corresponding cell in that table and dividing its entry by the sum of the entries in the row." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Theory ###\n", "We can now generalize the calculations we did in the example above.\n", "\n", "Let $X$ and $Y$ be two random variables defined on the same space. If $x$ is a possible value of $X$, and $y$ and possible value of $Y$, then\n", "\n", "$$\n", "P(X = x \\mid Y = y) = \\frac{P(X = x, Y = y)}{P(Y = y)}\n", "$$\n", "\n", "The conditional probability $P(X = x \\mid Y = y)$ is displayed in the $(x, y)$ cell of the table of conditional distributions above.\n", "\n", "For a fixed value $y^\\*$ of $Y$, the *conditional distribution* of $X$ given $Y = y^\\*$ is the collection of probabilities\n", "\n", "$$\n", "P(X = x \\mid Y = y^*) = \\frac{P(X = x, Y = y^*)}{P(Y = y^*)}\n", "$$\n", "\n", "where $x$ ranges over all the values of $X$. Keep in mind that $x$ represents the values of the variable here. The value $y^\\*$ is the particular value of $Y$ that was observed, so it is a constant." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### The Probabilities in a Conditional Distribution Sum to 1 ###\n", "In a distribution, the probabilities have to sum to 1. To see that this is true for the conditional distribution defined above, start by using the fundamental rule. \n", "\n", "Find $P(Y = y^\\*)$ by partitioning the event $\\{ Y = y^\\* \\}$ according to the values of $X$:\n", "\n", "$$\n", "P(Y = y^*) = \\sum_{\\text{all }x} P(X = x, Y = y^*)\n", "$$\n", "\n", "Now sum the probabilities in the conditional distribution of $X$ given $Y = y^\\*$:\n", "\n", "\n", "\\begin{align*}\n", "\\sum_{\\text{all }x} P(X = x \\mid Y = y^*) ~ &= ~\n", "\\sum_{\\text{all }x} \\frac{P(X = x, Y = y^*)}{P(Y = y^*)} \\\\ \\\\\n", "&= ~ \\frac{1}{P(Y = y^*)} \\sum_{\\text{all }x} P(X = x, Y = y^*) \\\\\n", "&= ~ \\frac{1}{P(Y = y^*)} \\cdot P(Y = y^*) \\\\ \\\\\n", "&= ~ 1\n", "\\end{align*}\n", "\n", "\n", "Thus the conditional distribution is just an ordinary probability distribution: a set of values with probabilities that sum to 1. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "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 }