{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Functions of Random Variables ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we work with random variables, we often want to consider functions of them. For example, if $X$ is a random length in inches then $Y = 2.54X$ is the length in centimeters, and $W = \\vert X - 12 \\vert $ measures how far $X$ is from 12 inches.\n",
"\n",
"If we know the distribution of $X$ we can easily find the expectation of any function of $X$, as in the example below.\n",
"\n",
"Let $X$ have the uniform distribution on $\\{ -1, 0, 1 \\}$, and let $Y = X^2$. Let's find $E(Y)$.\n",
"\n",
"Here is a distribution table for $X$, with values of $Y$ as well.\n",
"\n",
"|$y = x^2$| $~~~~1$ | $~~~~0$ | $~~~~1$ |\n",
"|--------:|:---:|:---:|:---:|\n",
"|$~~~~~~~~~~~~~~~~~~~ x$| $-1$ | $0$ | $1$ |\n",
"|$P(X = x)$ | $1/3$ | $1/3$ | $1/3$ |\n",
"\n",
"At a glance you can see that $Y$ has the value $1$ with chance $2/3$ and the value $0$ with chance $1/3$, so $E(Y) = 2/3$.\n",
"\n",
"But because of the distributive property, we don't even have to collect terms. We can just find $E(Y)$ as the weighted average of the top row, with the weights in the bottom row.\n",
"\n",
"$$\n",
"E(Y) ~ = ~ (-1)^2\\frac{1}{3} + 0^2\\cdot\\frac{1}{3} + 1^2\\cdot\\frac{1}{3} = \\frac{2}{3}\n",
"$$\n",
"\n",
"This method can be expressed in a formula that has an unfortunate way of making the operation seem more complicated than it is.\n",
"\n",
"Let $Y = g(X)$ be a function of the random variable $X$. Then\n",
"\n",
"$$\n",
"E(g(X)) ~ = ~ \\sum_{\\text{all }x} g(x)P(X=x)\n",
"$$\n",
"\n",
"This just expresses what we did in our example for the function $g$ defined by $g(x) = x^2$:\n",
"\n",
"- Take a value $x$ of the original random variable $X$.\n",
"- Apply the function $g$ to create $g(x)$, the corresponding value of $Y$.\n",
"- Weight this by $P(X=x)$, to get the product $g(x)P(X=x)$.\n",
"- Add these products over all possible values $x$ of $X$.\n",
"\n",
"For example, if $W = \\min(X, 0.5)$ is the smaller of $X$ and 0.5 in our example above, then\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"E(W) ~ &= ~ \\min(-1, 0.5)\\frac{1}{3} + \\min(0, 0.5)\\frac{1}{3} + \\min(1, 0.5)\\frac{1}{3} \\\\\n",
"&= ~ (-1)\\frac{1}{3} + 0\\cdot\\frac{1}{3} + 0.5\\cdot\\frac{1}{3} ~ \\approx ~ -0.167\n",
"\\end{align*}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Functions on the Outcome Space ###\n",
"\n",
"The method above extends to finding the expectation of a function of any number of random variables. That's important because data scientists often want to use functions of random samples to make inferences about unknown quantities.\n",
"\n",
"Recall that our construction of random variables started with the space $\\Omega$ consisting of outcomes of our experiment. We defined a random variable as a numerical function on $\\Omega$. For example, if $X$ is the number of heads in two tosses of coin then we can set $\\Omega = \\{ HH, HT, TH, TT\\}$ and \n",
"\n",
"$$\n",
"X(HH) = 2, ~~ X(HT) = 1 = X(TH), ~~ X(TT) = 0\n",
"$$\n",
"\n",
"In general, for an outcome $\\omega$ in $\\Omega$, the value of $X$ is $X(\\omega)$.\n",
"\n",
"This helps us make precise the idea that a function of a random variable is itself a random variable. For example, let $g$ be the function defined by $g(x) = x^2$. Then $g(X)$ is defined by composing two functions as follows:\n",
"$ g(X(\\omega)) ~ = ~ (X(\\omega))^2$ for every $\\omega \\in \\Omega$. Thus $g(X)$ is also a function on $\\Omega$ and hence is a random variable.\n",
"\n",
"Most commonly, we work with multiple random variables defined on the same space. Here is an example.\n",
"\n",
"Suppose two draws are made at random without replacement from a population that has five elements labeled $ 1, 2, 2, 3, 3 $. Define the following random variables:\n",
"\n",
"- $X_1$ is the number on the first draw\n",
"- $X_2$ is the number on the second draw\n",
"- $S = X_1 + X_2$ is sum of the numbers on the two draws\n",
"\n",
"We will now create a table of all possible values of the pair $(X_1, X_2)$ and the probabilities of all the pairs. We will then use the table to find probabilities of events involving $X_1$, $X_2$, and $S$."
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Joint Distribution ###\n",
"We know how to make a distribution table for a single random variable. We will now extend this to a table that displays the *joint distribution* of two random variables. \n",
"\n",
"The joint distribution of random variables $X$ and $Y$ consists of all pairs $(x, y)$ that are possible values of the pair $(X, Y)$, along with all the corresponding probabilities $P(X=x, Y=y)$.\n",
"\n",
"Here is the *joint distribution table* of the random variables $X_1$ and $X_2$ defined above.\n",
"\n",
"|$~~~~~~~~~~~~~~~$|$X_2=1$|$X_2=2$|$X_2=3$|\n",
"|----------------:|:---:|:---:|:---:|\n",
"|$X_1=1$| $~~~~~ 0 ~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|\n",
"|$X_1=2$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{4}{20}~~~~~$|\n",
"|$X_1=3$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{4} {20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|\n",
"\n",
"Each cell of the table represents an intersection: an event of the form $(X_1 = i, X_2 = j)$ for $i$ and $j$ in the range 1, 2, 3. The quantity displayed in the cell is $P(X_1 = i, X_2 = j)$.\n",
"\n",
"For example, the cell $X_1 = 2, X_2 = 3$ displays $P(X_1 = 2, X_2 = 3) = \\frac{2}{5} \\cdot \\frac{2}{4} = \\frac{4}{20}$.\n",
"\n",
"The table partitions the whole outcome space according to the pairs of values of $X_1$ and $X_2$. So the sum of all the probabilities in the table is equal to 1.\n",
"\n",
"The chance of any event determined by $X_1$ and $X_2$ can be calculated cell by cell using the addition rule:\n",
"\n",
"- If the cell is in the event, include its probability.\n",
"- If the cell is not in the event, skip it.\n",
"\n",
"For example, \n",
"\n",
"$$\n",
"\\begin{align*}\n",
"&P(X_1 = X_2) \\\\\n",
"&= ~ P(X_1 = 1, X_2 = 1) + P(X_1 = 2, X_2 = 2) + P(X_1 = 3, X_2 = 3) \\\\\n",
"&= ~ 0 + \\frac{2}{20} + \\frac{2}{20} ~ = ~ \\frac{1}{5}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"For $S = X_1 + X_2$,\n",
"\n",
"$$\n",
"P(S = 5) ~ = ~ P(X_1 = 2, X_2 = 3) + P(X_1 = 3, X_2 = 2) ~ = ~ \\frac{4}{20} + \\frac{4}{20} ~ = ~ \\frac{2}{5}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"### \"Marginal\" Distribution ###\n",
"If you have the joint distribution of two random variables, then you can find the distribution of each one.\n",
"\n",
"For example, the event $X_1 = 1$ consists of the cells in the top row of the joint distribution table above, so\n",
"\n",
"$$\n",
"P(X_1 = 1) ~ = ~ 0 + \\frac{2}{20} + \\frac{2}{20} ~ = ~ \\frac{4}{20} ~ = ~ \\frac{1}{5}\n",
"$$\n",
"\n",
"This confirms what you already knew from the definition of $X_1$.\n",
"\n",
"To get each probability in the distribution of $X_1$, we have to add all the probabilities in the corresponding row of the table above. Since the sums are often written in the margins of the table, as shown below, the distribution of $X_1$ is also called the *marginal* distribution of $X_1$.\n",
"\n",
"\n",
"|$~~~~~~~~~~~~~~~$|$X_2=1$|$X_2=2$|$X_2=3$|Dist of $X_1$|\n",
"|----------------:|:---:|:---:|:---:|:----------------|\n",
"|$X_1=1$| $~~~~~ 0 ~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{4}{20} = \\frac{1}{5}~~~~~$ |\n",
"|$X_1=2$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{4}{20}~~~~~$|$~~~~~\\frac{8}{20} = \\frac{2}{5}~~~~~$|\n",
"|$X_1=3$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{4} {20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{8}{20} = \\frac{2}{5}~~~~~$|\n",
"\n",
"You can get the distribution of $X_2$ by summing along columns."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Expectations of Functions ###\n",
"We can find the expectation of any function $g$ of two random variables $X$ and $Y$ by extending our method for finding the expectation of a function of $X$.\n",
"\n",
"- Take a cell of the joint distribution table of $X$ and $Y$. This corresponds to one possible value $(x, y)$ of the pair $(X, Y)$.\n",
"- Apply the function $g$ to get $g(x,y)$.\n",
"- Weight this by the probability in the cell, to get the product $g(x,y)P(X=x, Y=y)$\n",
"- Add these products over all the cells of the table.\n",
"\n",
"This is a bit laborious but straightforward. Here for reference is the joint distribution table of $X_1$ and $X_2$ from the examples above. Let's use it to find $E(\\vert X_1 - X_2 \\vert)$, the expected value of how far apart the the two draws are.\n",
"\n",
"|$~~~~~~~~~~~~~~~$|$X_2=1$|$X_2=2$|$X_2=3$|\n",
"|----------------:|:---:|:---:|:---:|\n",
"|$X_1=1$| $~~~~~ 0 ~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|\n",
"|$X_1=2$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{2}{20}~~~~~$|$~~~~~\\frac{4}{20}~~~~~$|\n",
"|$X_1=3$| $~~~~~\\frac{2}{20}~~~~~$ |$~~~~~\\frac{4} {20}~~~~~$|$~~~~~\\frac{2}{20}~~~~~$|\n",
"\n",
"We will find $E(\\vert X_1 - X_2 \\vert)$ by executing the plan described above.\n",
"\n",
"- The contribution of the cell $X_1 = 1, X_2 =1$ is clearly 0.\n",
"- The contribution of the cell $X_1 = 1, X_2 =2$ is $\\vert 1 - 2 \\vert \\frac{2}{20} = \\frac{2}{20}$.\n",
"- The contribution of the cell $X_1 = 1, X_2 =3$ is $\\vert 1 - 3 \\vert \\frac{2}{20} = \\frac{4}{20}$.\n",
"- And so on.\n",
"\n",
"It's a bit of pain, but you can go through the process for all nine cells of the table and add up all the contributions to get $E(\\vert X_1 - X_2 \\vert) = \\frac{20}{20} = 1$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Additivity of Expectation ###\n",
"It will come as a relief that you can find $E(X+Y)$ without going through the long process outlined above. Because of the distributive property, the long calculation simplifies and we get\n",
"\n",
"$$\n",
"E(X+Y) ~ = ~ E(X) + E(Y)\n",
"$$\n",
"\n",
"no matter what the joint distribution of $X$ and $Y$.\n",
"\n",
"This *additivity* is one of the most important properties of expectation, because it is true whether the random variables are dependent or independent. \n",
"\n",
"One way to prove the formula is to apply our long-hand method for finding $E(S)$ and then do the algebraic simplification. Another is to make an equivalent definition of expectation on the outcome space $\\Omega$ and use it instead. But we won't take the time for that. We will just focus on applications.\n",
"\n",
"The rest of the chapter consists of powerful uses of additivity. For now, let's find $E(S)$ in our example above.\n",
"\n",
"- $E(X_1) = 1\\cdot\\frac{1}{5} + 2\\cdot\\frac{2}{5} + 3\\cdot\\frac{2}{5} = 2.2$.\n",
"- By the symmetry of draws without replacement, the distribution of $X_2$ is the same as that of $X_1$ and hence $E(X_2) = 2.2$. You can also check this by finding the marginal distribution of $X_2$ and calculating its expectation.\n",
"\n",
"By additivity, the expectation of $S = X_1 + X_2$ is\n",
"\n",
"$$\n",
"E(S) ~ = ~ E(X_1) + E(X_2) ~ = ~ 2.2 + 2.2 ~ = ~ 4.4\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.7.11"
}
},
"nbformat": 4,
"nbformat_minor": 4
}