{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Expectation by Conditioning ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Expectation is a long-run average, so properties of expectation are properties of averages. The method of calculating expectation by conditioning, developed in the previous section, is equivalent to the calculation of the average of a group based on information about subgroups.\n",
"\n",
"As an example, suppose a class of 60 students is split into two sections of sizes 40 and 20. After the midterm, the average scores in the two sections are recorded in a table.\n",
"\n",
"|Section| A | B |\n",
"|------:|:---:|:---:|\n",
"|Average| 80 | 70 |\n",
"|Size | 40 | 20 |\n",
"\n",
"Then the class average can be calculated as\n",
"\n",
"$$\n",
"\\text{class average} ~ = ~ 80\\cdot \\frac{40}{60} ~ + ~ 70 \\cdot \\frac{20}{60} ~ \\approx ~ 76.7\n",
"$$\n",
"\n",
"The overall average is the weighted average of the section averages, where the weights are the proportions in the sections.\n",
"\n",
"Analogously, if $X$ and $Y$ are two random variables then the the expectation of $X$ can be found by taking the weighted average of the conditional expectations $E(X \\mid Y=y)$, where the weights are the probabilities $P(Y = y)$. \n",
"\n",
"This can be written as a formula for finding expectation by conditioning:\n",
"\n",
"$$\n",
"E(X) ~ = ~ \\sum_{\\text{all }y} E(X \\mid Y = y)P(Y = y)\n",
"$$\n",
"\n",
"The proof is routine after writing $E(X)$ in terms of all the cells of the joint distribution table of $X$ and $Y$. We won't do the details.\n",
"\n",
"Instead, we will just focus on the natural idea that just as we can find an average as a weighted average of group averages, we can also find an expectation as a weighted average of conditional expectations.\n",
"\n",
"Here are some examples where we can do such calculations without even writing out a joint distribution table."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Time to Reach Campus ###\n",
"A student has two routes to campus. Each route has a random duration. The student prefers Route A because its expected duration is 15 minutes compared to 20 minutes for Route B. However, on 10% of her trips she is forced to take Route B because of road work on Route A. On the remaining 90% of the days she takes Route A.\n",
"\n",
"What is the expected duration of her trip on a random day?\n",
"\n",
"The answer is the weighted average of the two conditional expectations given the different routes:\n",
"\n",
"$$\n",
"15(0.9) + 20(0.1) ~ = ~ 15.5 \\text{ minutes}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Tosses of a Random Coin ###\n",
"In Bayesian statistics, parameters that we have thus far considered constant can be thought of as random variables. Let's consider 10 tosses of a coin picked at random from a bag that contains one fair coin, one coin that lands heads with chance 0.2, and two that land heads with chance 0.9. What is the expected number of heads?\n",
"\n",
"If we knew which coin we were tossing, we would know the expected number of heads. \n",
"\n",
"- If we were tossing the coin that lands heads with chance 0.2, then the expected number of heads would be $10 \\times 0.2 = 2$. \n",
"- If we were tossing the fair coin then the expected number of heads would be $5$.\n",
"- If we were tossing a coin that lands heads with chance 0.9 then the expected number of heads would be $9$. \n",
"\n",
"This is our signal to condition on which coin we got. The expected number of heads is the weighted average of the conditional expectations above.\n",
"\n",
"$$\n",
"2(1/4) + 5(1/4) + 9(2/4) ~ = ~ 6.25\n",
"$$\n",
"\n",
"This is the weighted average of the conditional expectations given which coin was selected, where the weights are the chances of selecting the coins."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Catching Misprints ###\n",
"The number of misprints in a document about to be printed is a random variable $N$ that has a Poisson $(5)$ distribution. Each misprint is caught before printing with chance $0.95$ independently of all other misprints. What is the expected number of misprints that are caught before printing?\n",
"\n",
"Let $M$ be the number of misprints that are caught before printing.\n",
"\n",
"As a starting point, suppose we happen to know that the number of misprints $N$ has the value 8. Then the number of misprints that are caught has the binomial distribution with parameters $n = 8$ and $p = 0.95$. So $E(M \\mid N = 8) = 8 \\times 0.95$.\n",
"\n",
"In general, suppose we are given that the total number of misprints is $N = n$ for some fixed positive number $n$. Conditional on the event $N=n$, the distribution of $M$ is binomial $(n, 0.95)$, and therefore $E(M \\mid N = n) = n \\times 0.95$.\n",
"\n",
"The result is true also for $n = 0$. If there are no misprints, then no misprints will be caught.\n",
"\n",
"So for each possible value $n$ of the number of misprints $N$, we have $E(M \\mid N=n) = 0.95n$. By our rule for finding expectations by conditioning,\n",
"\n",
"$$\n",
"E(M) ~ = ~ \\sum_{n=0}^\\infty 0.95n P(N=n) ~ = ~ 0.95 \\sum_{n=0}^\\infty nP(N=n) ~ = ~ 0.95E(N) ~ = ~ 0.95 \\times 5\n",
"$$\n",
"\n",
"because $N$ has the Poisson $(5)$ distribution and hence $E(N) = 5$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Expectation of a Geometric Waiting Time ###\n",
"\n",
"We will now use the method of finding expectation by conditioning to find the expectation of a waiting time that has the geometric $(p)$ distribution on $1, 2, 3, \\ldots $.\n",
"\n",
"Remember that a random variable $X$ has this distribution if $X$ is the number of trials till the first success in independent, identically distributed Bernoulli $(p)$ trials.\n",
"\n",
"Set $x$ equal to the as yet unknown value of $E(X)$. We will develop an equation for $x$ and then solve it.\n",
"\n",
"The equation is based on conditioning on the result of the first trial, as follows.\n",
"\n",
"If the first trial is a success, then $X$ is 1.\n",
"\n",
"If the first trial is a failure, then one trial has been used up and we still have to wait for the first success. The crucial observation is that at this point *the game starts over*. The excess amount we have to wait after the failure on the first trial *has the same distribution as $X$, and hence the same expectation as $X$*.\n",
"\n",
"The equation for $x = E(X)$ is thus\n",
"\n",
"$$\n",
"x ~ = ~ p \\cdot 1 ~ + ~ q(1 + x)\n",
"$$\n",
"\n",
"That is,\n",
"\n",
"$$\n",
"(1 - q)x ~ = ~ p + q = 1\n",
"$$\n",
"\n",
"That's the same as\n",
"\n",
"$$\n",
"px ~ = ~ 1, ~~~~~~ \\text{so } x ~ = ~ \\frac{1}{p}\n",
"$$\n",
"\n",
"We have shown that if $X$ has the geometric $(p)$ distribution on $1, 2, 3, \\ldots$ then $E(X) = 1/p$.\n",
"\n",
"This result agrees with our intuition. For example,\n",
"\n",
"- The expected number of tosses till a coin lands heads is $1/0.5 = 2$.\n",
"- The expected number of rolls till a die shows the face with six spots is $1/(1/6) = 6$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Waiting Till 10 Sixes ###\n",
"Let $T_{10}$ be the number of rolls of a die till a total of 10 sixes have appeared. Find $E(T_{10})$.\n",
"\n",
"To get 10 sixes, you have to wait for one six, then for the second one, then for the third one, and so on, up to the tenth one. Thus\n",
"\n",
"$$\n",
"T_{10} ~ = ~ X_1 + X_2 + \\ldots + X_{10}\n",
"$$\n",
"\n",
"where \n",
"\n",
"- $X_1$ is the number of rolls till the first six appears\n",
"- $X_2$ is the number of rolls following $X_1$ till the second six appears,\n",
"- and so on, till\n",
"- $X_{10}$ is the number of rolls following $X_9$ till the tenth six appears.\n",
"\n",
"Each of $X_1, X_2, \\ldots, X_n$ has the geometric $(1/6)$ distribution on $1, 2, 3, \\ldots $. By additivity of expectation,\n",
"\n",
"$$\n",
"E(T_{10}) ~ = ~ E(X_1) + E(X_2) + \\ldots + E(X_{10}) ~ = ~ 6 + 6 + \\cdots + 6 ~ = ~ 60\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
}