{
"cells": [
{
"cell_type": "markdown",
"id": "4b9817d9-d693-42f3-9358-95f0d2408aa5",
"metadata": {},
"source": [
"# Discrete random variables\n",
"\n",
"Discrete random variables are random variables whose images are countable sets.\n",
"We define discrete random variables and probability mass functions and present some examples.\n",
"The (conditional) expectation and variance are important summary statistics of random variables."
]
},
{
"cell_type": "markdown",
"id": "d5bf38b4-3c3d-4c71-9239-57497e7af9fb",
"metadata": {},
"source": [
"## Discrete random variables\n",
"\n",
"We are often interested in the value of a function of elementary events. For example we might be interested in the profit of a gambling game as a function of the elementary outcomes of the game.\n",
"\n",
"\n",
":::{prf:definition} Discrete random variable\n",
"\n",
"A discrete random variable $X$ is a function $X : \\Omega \\to \\mathbb{R}$ such that:\n",
"\n",
"1. The image $X(\\Omega)$ is a countable subset of $\\mathbb{R}.$\n",
"2. For every $x \\in \\mathbb{R}$ we have $\\{\\omega \\in \\Omega : X(\\omega) = x\\} \\in \\mathcal{F}.$\n",
"\n",
":::\n",
"\n",
"By defining discrete random variables as functions of elementary events, we separate the ideas of experimental outcomes and of functions of these outcomes.\n",
"\n",
"Condition (1) specifies that a discrete random variable can only take countably many values. Condition (2) specifies that every set that is mapped to a certain value $x \\in \\mathbb{R}$ is contained in the event space $\\mathcal{F}$ and is therefore assigned a probability by any probability measure defined on $\\mathcal{F}$."
]
},
{
"cell_type": "markdown",
"id": "7d11bda6-7735-4112-b946-59b1c94fecdd",
"metadata": {},
"source": [
"## Probability mass functions\n",
"\n",
"Once we have defined the continuous random variable, we can make statemets about the probability that it will take a certain value, using its probability mass function.\n",
"\n",
":::{prf:definition} Probability mass function\n",
"\n",
"Let $X : \\Omega \\to \\mathbb{R}$ be a discrete random variable. The probability mass function (or pmf) is the function $p_X : \\mathbb{R} \\to [0, 1]$ defined by\n",
"\n",
"$$p_X(x) = \\mathbb{P}(X = x)$$\n",
"\n",
"where $\\mathbb{P}(X = x)$ is shorthand for $\\mathbb{P}(\\{\\omega \\in \\Omega : X(\\omega) = x\\})$.\n",
"\n",
":::\n",
"\n",
"\n",
"Reviewing the definitions leading up to the pmf, we defined: elementary events, event spaces and probability measures, followed by random variables and the probability mass function.\n",
"This was a rigorous build-up of what a discrete random variable is, however in many cases we need not consider this construction at all, instead declaring that \"$X$ is a random variable with pmf $p_X(\\cdot)$\" and proceed to give an expression for $p_X(\\cdot)$ directly.\n",
"\n",
"\n",
":::{prf:theorem} pmf $\\implies$ probability space and random variable\n",
"\n",
"Let $S = \\{s_i : i \\in I\\}$ be a countable set of real numbers and let $\\{\\pi_i : i \\in I\\}$ be a collectioin of real numbers satisfying\n",
"\n",
"$$\\pi_i \\geq 0 \\text{ for } i \\in I, \\text{ and } \\sum_{i \\in I} \\pi_i = 1.$$\n",
"\n",
"There exists a probability space $(\\Omega, \\mathcal{F}, \\mathbb{P})$ and a discrete random variable $X$ on $(\\Omega, \\mathcal{F}, \\mathbb{P})$ such that the pmf $p_X(\\cdot)$ is given by\n",
"\n",
"$$\\begin{align}\n",
"p_X(s_i) &= \\pi_i && \\text{ for } i \\in I\\\\\n",
"p_X(s) &= 0 && \\text{ if } s \\not \\in S\n",
"\\end{align}$$\n",
":::\n",
"\n",
"The proof for this theorem is that we can take $\\Omega = S$, $\\mathcal{F}$ to be the powerset of $\\Omega$ and $\\mathbb{P}$ defined by\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{P}(A) = \\sum_{i : s_i \\in A} \\pi_i & \\text{ for } A \\in \\mathcal{F}.\n",
"\\end{align}$$\n",
"\n",
"Lastly defining $X : \\Omega \\to \\mathbb{R}$ to be any one-to-one function, we arrive at the result because we can verify that:\n",
"\n",
"1. $\\mathcal{F}$ is a valid event space.\n",
"2. $\\mathbb{P}$ is a valid probability measure.\n",
"3. $X$ is a valid discrete random variable and has the pmf from the theorem statement."
]
},
{
"cell_type": "markdown",
"id": "133f01af-d3a7-4850-aebb-598fb6eb3240",
"metadata": {},
"source": [
"## Fundamental discrete distributions\n",
"\n",
"Here are some examples of fundamental discrete distributions.\n",
"Appealing to the theorem above, we can forget about probability spaces and consider only the values taken by the pmf of the random variable in question.\n",
"\n",
"### Bernoulli\n",
"\n",
"Also called the coin toss, the Bernoulli distribution is the simplest discrete distribution.\n",
"A random variable $X$ is Bernoulli-distributed with paramter $p \\in [0, 1]$ if $X$ can take the values 0 or 1:\n",
"\n",
"$$ \\begin{align}\n",
"\\mathbb{P}(X = 0) &= 1 - p, \\\\\n",
"\\mathbb{P}(X = 1) &= p.\n",
"\\end{align}$$\n",
"\n",
"### Binomial\n",
"\n",
"A random variable $X$ is binomially distributed if\n",
"\n",
"$$ \\mathbb{P}(X = k) = {n \\choose k} p^k (1 - p)^{n - k} ~~\\text{for}~~k = 0, 1, 2, ..., n.$$\n",
"\n",
"The sum of $n$ independent and identically distributed coin tosses is Bernoulli distributed.\n",
"\n",
"### Poisson\n",
"\n",
"A random variable $X$ is said to be Poisson-distributed with parameter $\\lambda > 0$ if\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{P}(X = k) = \\frac{1}{k!} \\lambda^k e^{-\\lambda} & \\text{ for } k = 0, 1, 2 ...\n",
"\\end{align}$$\n",
"\n",
"The Poisson distribution is an appropriate model for *point data*. For example, if buses arrive at a local stop such that\n",
"\n",
"1. Each bus arrival occurs at a single point in time.\n",
"2. Arrivals are independent of each other.\n",
"3. The number $N_{t, t + dt}$ of buses arriving within an infinitesimal time interval $[t, t + dt]$ follows\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{P}(N_{t, t+ dt} = 0) &= 1 - \\lambda dt + o(dt)\\\\\n",
"\\mathbb{P}(N_{t, t+ dt} = 1) &= \\lambda dt + o(dt),\n",
"\\end{align}$$\n",
"\n",
"then the number of events occuring within any time interval are Poisson-distributed.\n",
"\n",
"### Geometric\n",
"\n",
"A random variable $X$ has the geometric distribution with parameter $p$ if\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{P}(X = k) = pq^k & \\text{ for } k = 0, 1, 2 ...\n",
"\\end{align}$$\n",
"\n",
"The geometric distribution naturally models random variables such as \"the number of i.i.d. trials up to and including the first occurence of A\". For example, the number of i.i.d. coin tosses required until heads is obtained the first time, is geometrically distributed.\n",
"\n",
"### Negative binomial\n",
"\n",
"A random variable $X$ has the negative binomial distribution with parameters $n$ and $p \\in [0, 1]$ if\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{P}(X = k) = {k - 1 \\choose n - 1} p^n q^{k - n} & \\text{ for } k = n, n + 1, n + 2, ...\n",
"\\end{align}$$\n",
"\n",
"The number of i.i.d. coin tosses up to and including the $n^{th}$ success is distributed according to the negative binomial distribution.\n",
"To see this, consider that a sequence of trials up to the $n^{th}$ success contains $n$ successes and $k - n$ failures, so the probability of obtaining that particular sequence is $p^nq^{k - n}.$\n",
"To count the number of all possible sequences with $k - n$ failures and $n$ successes, consider that the last trial must be a success.\n",
"That leaves us with $k - 1$ choose $n - 1$ ways to rearrange the remaining successes and failures, arriving at the final expression.\n",
" \n",
"Note also that from the definition of this distribution, if $X_1, X_2, ..., X_n$ are geometrically distributed with parameter $p$, then $X = X_1 + X_2 + ... + X_n$ is negative-binomially distributed with parameters $n$ and $p.$"
]
},
{
"cell_type": "markdown",
"id": "8af69778-0a43-41c4-9188-5b4c50a74591",
"metadata": {},
"source": [
"## Expectations\n",
"\n",
"Although random variables are not perfectly determined, we can reason about the values they could attain.\n",
"The expectation of a random variable captures the value that we expect the variable will have on average - as weighted by the probability measure.\n",
"\n",
":::{prf:definition} Expectation\n",
"\n",
"The expectation of a discrete random variable $X$ is denoted by $\\mathbb{E}(X)$ and defined by\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{E}(X) = \\sum_{x : x \\in X(\\Omega)} x \\mathbb{P}(X = x).\n",
"\\end{align}$$\n",
"\n",
"whenever this sum converges absolutely, i.e. $\\sum |x\\mathbb{P}(X = x)| < \\infty.$\n",
"\n",
":::\n",
"\n",
"The need for the last statement in this definition is that the expectation of a discrete random variable may not always exist. For example, if $X$ has pmf\n",
"\n",
"$$\\mathbb{P}(X = x) = \\frac{A}{x^{3/2}} \\text{ for } x = 1, 2, ...$$\n",
"\n",
"where $A$ is a normalising constant, then the sum in the definition of $\\mathbb{E}(X)$ does not converge. When taking expectations of functions of discrete random variables, the following result holds, that we often take for granted.\n",
"\n",
"\n",
":::{prf:theorem} Law of the subconscious statistician\n",
"\n",
"If $X$ is a discrete random variable and $g : \\mathbb{R} \\to \\mathbb{R}$, then\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{E}\\left(g(x)\\right) = \\sum_{x \\in \\text{Im}X} g(x) \\mathbb{P}(X = x)\n",
"\\end{align}$$\n",
"\n",
"whenever this sum converges absolutely.\n",
":::\n",
"\n",
"\n",
"The above can be shown by defining $Y = g(X)$ and considering the expectation of $Y$, showing this is equal to $\\mathbb{E}\\left(g(X)\\right)$. Two useful results about discrete random variables are:\n",
"\n",
":::{prf:theorem} Two results for discrete random variables\n",
"\n",
"Let $X$ be a discrete random variable and $a, b \\in \\mathbb{R}$, then\n",
"1. If $\\mathbb{P}(X \\geq 0) = 1$ and $\\mathbb{E}(X)$, then $\\mathbb{P}(X = 0\n",
" ) = 1$.\n",
"2. $\\mathbb{E}(a X + b) = a\\mathbb{E}(X) + b$.\n",
"\n",
":::\n",
"\n",
"Appart from the expectation, the variance is another central quantity that captures information about a random variable, particularly about its spread.\n",
"\n",
"\n",
":::{prf:definition} Variance\n",
"\n",
"The variance of a discrete random variable $X$ is denoted by $\\text{Var}(X)$ and defined by\n",
"\n",
"$$\\begin{align}\n",
"\\text{Var}(X) &= \\mathbb{E}\\left([X - \\mathbb{E}(X)]^2\\right) \\\\\n",
"&= \\mathbb{E}(X^2) - \\mathbb{E}(X)^2\n",
"\\end{align}$$\n",
":::"
]
},
{
"cell_type": "markdown",
"id": "6581d318-52ac-4ae7-9266-92d6b635ff30",
"metadata": {},
"source": [
"## Conditional expectations\n",
"\n",
"We are often interested in the expectation of a random variable conditioned on some event. For example, the event we are conditioning on could be some observed data, based on which we would like to update our beliefs about the random variable, and compute its expectation.\n",
"\n",
":::{prf:definition} Conditional expectation\n",
"\n",
"If $X$ is a random variable and $\\mathbb{P}(B) > 0$, then the expectation of $X$ conditioned on $B$ is written $\\mathbb{E}[X | B]$ and defined by\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{E}[X | B] &= \\sum_{x \\in \\text{Im} X} x\\mathbb{P}(X = x | B)\n",
"\\end{align}$$\n",
"\n",
"whenever this sum converges absolutely.\n",
":::\n",
"\n",
"As with conditional probabilities, there is a partition theorem associated with conditional expectations, also known as the law of total expectation.\n",
"\n",
"\n",
":::{prf:theorem} Partition theorem for conditional expectations\n",
"\n",
"If $X$ is a discrete random variable and $\\{B_1, B_2, ...\\}$ is a partition of $\\Omega$ with $\\mathbb{P}(B_k) > 0$, we have\n",
"\n",
"$$\\begin{align}\n",
"\\mathbb{E}(X) = \\sum_k \\mathbb{E}(X | B_k)\\mathbb{P}(B_k)\n",
"\\end{align}$$\n",
"\n",
"whenever this sum converges absolutely.\n",
":::"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "rw",
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}