{
"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",
"from scipy import stats"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Examples ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This section is a workout in finding expectation and variance by conditioning. As before, if you are trying to find a probability, expectation, or variance, and you think, \"If only I knew the value of this other random variable, I'd have the answer,\" then that's a sign that you should consider conditioning on that other random variable."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"tags": [
"remove-input",
"hide-output"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" VIDEO"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# VIDEO: A Mixture\n",
"from IPython.display import YouTubeVideo\n",
"\n",
"YouTubeVideo('-uYGQrQCXzM')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mixture of Two Distributions ###\n",
"Let $X$ have mean $\\mu_X$ and SD $\\sigma_X$. Let $Y$ have mean $\\mu_Y$ and SD $\\sigma_Y$. Now let $p$ be a number between 0 and 1, and define the random variable $M$ as follows.\n",
"\n",
"$$\n",
"M = \n",
"\\begin{cases}\n",
"X ~~ \\text{with probability } p \\\\\n",
"Y ~~ \\text{with probability } q = 1 - p \\\\\n",
"\\end{cases}\n",
"$$\n",
"\n",
"The distribution of $M$ is called a *mixture* of the distributions of $X$ and $Y$.\n",
"\n",
"One way to express the definition of $M$ compactly is to let $I_H$ be the indicator of heads in one toss of a $p$-coin; then\n",
"\n",
"$$\n",
"M = XI_H + Y(1 - I_H)\n",
"$$\n",
"\n",
"To find the expectation of $M$ we can use the expression above, but here we will condition on $I_H$ because we can continue with that method to find $Var(M)$. \n",
"\n",
"The distribution table of the random variable $E(M \\mid I_H)$ is\n",
"\n",
"|**Value**| $\\mu_X$ | $\\mu_Y$|\n",
"|---------|---------|--------|\n",
"|**Probability**| $p$ | $q$ |\n",
"\n",
"The distribution table of the random variable $Var(M \\mid I_H)$ is\n",
"\n",
"|**Value**| $\\sigma_X^2$ | $\\sigma_Y^2$|\n",
"|---------|---------|--------|\n",
"|**Probability**| $p$ | $q$ |\n",
"\n",
"So\n",
"\n",
"$$\n",
"E(M) ~ = ~ E(E(M \\mid I_H)) ~ = ~ \\mu_Xp + \\mu_Yq\n",
"$$\n",
"\n",
"and\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"Var(M) ~ &= ~ E(Var(M \\mid I_H)) + Var(E(M \\mid I_H)) \\\\\n",
"&= ~ \\big{(} \\sigma_X^2p + \\sigma_Y^2q \\big{)} + \\big{(} \\mu_X^2p + \\mu_Y^2q - (E(M))^2 \\big{)}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"This is true no matter what the distributions of $X$ and $Y$ are."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Variance of the Geometric Distribution ###\n",
"We have managed to come quite far into the course without deriving the variance of the geometric distribution. Let's find it now by using the results about mixtures derived above.\n",
"\n",
"Toss a coin that lands heads with probability $p$ and stop when you see a head. The number of tosses $X$ has the geometric $(p)$ distribution on $\\{ 1, 2, \\ldots \\}$. Let $E(X) = \\mu$ and $Var(X) = \\sigma^2$. We will use conditioning to confirm that $E(X) = 1/p$ and also to find $Var(X)$.\n",
"\n",
"Now\n",
"\n",
"$$\n",
"X = \n",
"\\begin{cases} \n",
"1 ~~~ \\text{with probability } p \\\\\n",
"1 + X^* ~~~ \\text{with probability } q = 1-p\n",
"\\end{cases}\n",
"$$\n",
"\n",
"where $X^*$ is an independent copy of $X$. By the previous example,\n",
"\n",
"$$\n",
"\\mu ~ = ~ E(X) ~ = ~ 1p + (1+\\mu)q\n",
"$$\n",
"\n",
"So $\\mu = 1/p$ as we have known for some time.\n",
"\n",
"By the variance formula of the previous example,\n",
"\n",
"$$\n",
"\\sigma^2 = Var(X) = \\big{(} 0^2p + \\sigma^2q \\big{)} + \\big{(}1^2p + (1+\\frac{1}{p})^2q - \\frac{1}{p^2}\\big{)}\n",
"$$\n",
"\n",
"So\n",
"\n",
"$$\n",
"\\sigma^2p ~ = ~ \\frac{p^3 + (p+1)^2q - 1}{p^2} ~ = ~ \\frac{p^3 + (1+p)(1-p^2) - 1}{p^2}\n",
"~ = ~ \\frac{p(1-p)}{p^2}\n",
"$$\n",
"\n",
"and so $Var(X) = \\sigma^2 = q/p^2$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Normal with a Normal Mean ###\n",
"Let $M$ be normal $(\\mu, \\sigma_M^2)$, and given $M = m$, let $X$ be normal $(m, \\sigma_X^2)$.\n",
"\n",
"Then\n",
"\n",
"$$\n",
"E(X \\mid M) ~ = ~ M, ~~~~~~ Var(X \\mid M) ~ = ~ \\sigma_X^2\n",
"$$\n",
"\n",
"Notice that the conditional variance is a constant: it is the same no matter what the value of $M$ turns out to be.\n",
"\n",
"So $E(X) = E(E(X \\mid M)) = E(M) = \\mu$ and \n",
"\n",
"$$\n",
"Var(X) ~ = ~ E(Var(X \\mid M)) + Var(E(X \\mid M)) ~ = ~ \\sigma_X^2 + Var(M) ~ = ~ \\sigma_X^2 + \\sigma_M^2\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Random Sum ###\n",
"Let $N$ be a random variable with values $0, 1, 2, \\ldots$, mean $\\mu_N$, and SD $\\sigma_N$. Let $X_1, X_2, \\ldots $ be i.i.d. with mean $\\mu_X$ and SD $\\sigma_X$, independent of $N$. \n",
"\n",
"Define the *random sum* $S_N$ as\n",
"\n",
"$$\n",
"S_N = \n",
"\\begin{cases}\n",
"0 ~~ \\text{if } N = 0 \\\\\n",
"X_1 + X_2 + \\cdots + X_n ~~ \\text{if } N = n > 0\n",
"\\end{cases}\n",
"$$\n",
"\n",
"Then as we have [seen before](http://prob140.org/textbook/content/Chapter_09/02_Expectation_by_Conditioning.html#example-random-sums), $E(S_N \\mid N = n) = n\\mu_X$ for all $n$ (including $n = 0$). So\n",
"\n",
"$$\n",
"E(S_N \\mid N) ~ = ~ N\\mu_X\n",
"$$\n",
"\n",
"and hence\n",
"\n",
"$$\n",
"E(S_N) ~ = ~ E(N\\mu_X) ~ = ~ \\mu_XE(N) ~ = ~ \\mu_N\\mu_X\n",
"$$\n",
"\n",
"This is consistent with intuition: you expect to be adding $\\mu_N$ i.i.d. random variables, each with mean $\\mu_X$. For the variance, intuition needs some guidance, which is provided by our variance decomposition formula.\n",
"\n",
"First note that because we are adding i.i.d. random variables, $Var(S_N \\mid N = n) = n\\sigma_X^2$ for all $n$ (including $n = 0$). That is,\n",
"\n",
"$$\n",
"Var(S_N \\mid N) ~ = ~ N\\sigma_X^2\n",
"$$\n",
"\n",
"By the variance decomposition formula,\n",
"\n",
"$$\n",
"Var(S_N) ~ = ~ E(N\\sigma_X^2) + Var(N\\mu_X) ~ = ~ \\mu_N\\sigma_X^2 + \\mu_X^2\\sigma_N^2\n",
"$$"
]
}
],
"metadata": {
"anaconda-cloud": {},
"celltoolbar": "Tags",
"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.8.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}