{
"cells": [
{
"cell_type": "markdown",
"id": "8a85b839-4c7e-43a4-8f09-f11f4161a035",
"metadata": {},
"source": [
"# Distribution and density functions\n",
"\n",
"Random variables are functions from $\\Omega$ to $\\mathbb{R}$ which need not assume a countable set of values, unlike discrete random variables.\n",
"Continuous random variables are a special case of random variables, whose distribution function can be written as the integral of another function, the probability density function. Some example continuous random variables are presented, as well as some basic results."
]
},
{
"cell_type": "markdown",
"id": "6b7128b3-0a3e-4cab-a332-35ac60612d70",
"metadata": {},
"source": [
"## Distribution functions\n",
"\n",
"Extending the definition of a discrete random variable allows us to deal with probability spaces with uncountably many outcomes.\n",
"\n",
"\n",
":::{prf:definition} Random variable\n",
"\n",
"A discrete random variable $X$ is a function $X : \\Omega \\to \\mathbb{R}$ such that for every $x \\in \\mathbb{R}$ we have $\\{\\omega \\in \\Omega : X(\\omega) = x\\} \\in \\mathcal{F}$.\n",
":::\n",
"\n",
"Whereas discrete random variables are described by a pmf, general random variables are described by distribution functions.\n",
"\n",
"\n",
":::{prf:definition} Distribution function\n",
"\n",
"If $X$ is a random variable on $(\\Omega, \\mathcal{F}, \\mathbb{P})$, then its distribution function of $X$ is the function $F_X : \\mathbb{R} \\to [0, 1]$ defined as\n",
" \n",
"$$\\begin{align}\n",
"F_X(x) = \\mathbb{P}(X \\leq x).\n",
"\\end{align}$$\n",
":::\n",
"\n",
"The following useful properties are true of distribution functions:\n",
"\n",
"1. If $x \\leq y$, then $F_X(x) \\leq F_X(y)$ if $x \\leq y$, since $\\{X \\leq x\\} \\subseteq \\{Y \\leq y\\}.$\n",
"2. $F_X(-\\infty) = 0$ and $F_X(\\infty) = 1$, by the continuity of $\\mathbb{P}$ and considering $\\mathbb{P}(\\emptyset) = 0, \\mathbb{P}(\\Omega) = 1.$\n",
"3. $F_X$ is continuous from the right, by the continuity of $\\mathbb{P}.$\n",
"4. $\\mathbb{P}(a < X \\leq b) = \\mathbb{P}(X \\leq b) - \\mathbb{P}(X \\leq a).$"
]
},
{
"cell_type": "markdown",
"id": "5b12348c-6dae-4118-8f0e-e1b2b9cb810c",
"metadata": {},
"source": [
"## Continuous random variables\n",
"\n",
"Just like discrete random variables are special case of random variables, continuous random variables are another special case of interest.\n",
"\n",
":::{prf:definition} Continuous random variable\n",
"\n",
"A random variable $X$ is continuous if and only if its distribution function $F_X$ can be written in the form\n",
" \n",
"$$\\begin{align}\n",
"F_X(x) = \\mathbb{P}(X \\leq x) = \\int^x_{-\\infty} f_X(z) dz,\n",
"\\end{align}$$\n",
" \n",
"for some non-negative function $f_X$, called the probability density function (pdf) of $X$.\n",
":::\n",
"\n",
"Pdfs are a rough counterpart [^pdfandpmf] of pmfs for continuous random variables. Given a continuous random variable $X$ and its distribution function $F_X$ we can write its pmf as:\n",
" \n",
"$$\\begin{align}\n",
"f_X(x) = \\begin{cases}\n",
"\\frac{d}{dx} F_X(x) & \\text{ if this exists at } x,\\\\\n",
"0 & \\text{ otherwise.}\n",
"\\end{cases}\n",
"\\end{align}$$\n",
" \n",
"Further, the pmf $f_X$ satisfies the following properties.\n",
"\n",
":::{prf:theorem} Probability density function properties\n",
"\n",
"If $X$ is a continuous random variable with density function $f_X$, then\n",
" \n",
"$$\\begin{align}\n",
"\\mathbb{P}(X = x) = 0 & \\text{ for } x \\in \\mathbb{R},\\\\\n",
"\\mathbb{P}(a \\leq X \\leq b) = \\int^b_a f_X(z) dz & \\text{ for } a, b \\in\n",
"\\mathbb{R} \\text{ with } a \\leq b\n",
"\\end{align}$$\n",
":::\n",
"\n",
"### Uniform\n",
"\n",
"$X$ is uniformly distributed in the interval $[a, b]$ if its density function is\n",
"\n",
"$$\\begin{align}\n",
"f_X(x) = \\begin{cases}\n",
"\\frac{1}{b - a} & \\text{ if } x \\in [a, b]\\\\\n",
"0 & \\text{otherwise}.\n",
"\\end{cases}\n",
"\\end{align}$$\n",
"\n",
"If $X$ is a uniformly distributed random variable and $F$ is a distribution function, then the random variable $Y(\\omega) = F^{-1}(X(\\omega))$ where\n",
" \n",
"$$\\begin{align}\n",
"F^{-1}(y) = \\inf\\{x : y \\leq F(x)\\},\n",
"\\end{align}$$\n",
" \n",
"has distribution function $F$. This holds because\n",
" \n",
"$$\\begin{align}\n",
"\\mathbb{P}(Y \\leq y) &= \\mathbb{P}(F^{-1}(X) \\leq y) \\\\\n",
" &= \\mathbb{P}(X \\leq F(y)) \\\\\n",
" &= F(y),\n",
"\\end{align}$$\n",
" \n",
"where we used $F(F^{-1}(x)) = x$, which holds because $F$ is continuous from the right. This property can be used to generate random samples from any distribution, given its distribution function $F$.\n",
"\n",
"\n",
"(prob-intro-exponential)=\n",
"### Exponential\n",
"\n",
"$X$ is exponentially distributed with parameter $\\lambda$ if its density function is\n",
" \n",
"$$\\begin{align}\n",
"f_X(x) = \\begin{cases}\n",
"\\lambda e^{-\\lambda x} & \\text{ if } x > 0,\\\\\n",
"0 & \\text{ if } x \\leq 0.\n",
"\\end{cases}\n",
"\\end{align}$$\n",
"\n",
"### Normal\n",
"\n",
"$X$ is normally distributed with parameters $\\mu$ and $\\sigma^2$, if its density function is\n",
" \n",
"$$\\begin{align}\n",
"f_X(x) = \\frac{1}{\\sqrt{2\\pi\\sigma^2}} \\exp\\left(- \\frac{1}{2\\sigma^2}(x - \\mu)^2\\right) & \\text{ for } x \\in \\mathbb{R}.\n",
"\\end{align}$$\n",
" \n",
"### Cauchy\n",
"\n",
"$X$ is Cauchy distributed if its density function is\n",
"\n",
"$$\\begin{align}\n",
"f_X(x) = \\frac{1}{\\pi(1 + x^2)} & \\text{ for } x \\in \\mathbb{R}.\n",
"\\end{align}$$\n",
"\n",
"### Gamma\n",
"\n",
"$X$ is gamma distributed with parameters $w > 0$ and $\\lambda > 0$ if its density function is\n",
"\n",
"$$\\begin{align}\n",
"f_X(x) = \\begin{cases}\n",
"\\frac{1}{\\Gamma(w)}\\lambda^w x^{w-1}e^{-\\lambda x} & \\text{ if } x > 0,\\\\\n",
"0 & \\text{ if } x \\leq 0.\n",
"\\end{cases}\n",
"\\end{align}$$\n",
"\n",
"where $\\Gamma(w)$ is the gamma function defined as\n",
"\n",
"$$\\begin{align}\n",
"\\Gamma(w) = \\int_0^\\infty x^{w-1} e^{-x} dx.\n",
"\\end{align}$$\n",
"\n",
"Note that the exponential distribution is a special case of the gamma distribution, when $w = 1$. More generally, if $X_1, X_2, ..., X_w$ are exponentially distributed with parameter $\\lambda$, their sum $X = X_1 + X_2 + ... + X_w$ is gamma distributed with parameters $w$ and $\\lambda.$\n",
"This can be proved most easily using moment generating functions, but can also be shown using continuous convolutions.\n",
"Alternatively, $f_X$ can be obtained up to a normalisation constant, by considering that it is equal to the product of $f_{X_1}, f_{X_2}, ..., f_{X_w}$ and a volume term which scales as $x^{w-1}.$\n",
"\n",
"\n",
"### Beta\n",
"\n",
"$X$ is beta distributed with parameters $a, b > 0$ if it has density function\n",
"\n",
"$$\\begin{align}\n",
"\\frac{1}{B(s, t)} x^{s - 1} (1 - x)^{t - 1} & \\text{ for } 0 \\leq x \\leq 1,\n",
"\\end{align}$$\n",
"\n",
"where $B(s, t)$ is the beta function defined as\n",
"\n",
"$$\\begin{align}\n",
"B(s, t) = \\int^1_0 x^{s - 1} (1 - x)^{t - 1} dx = \\frac{\\Gamma(s)\\Gamma(t\n",
")}{\\Gamma(s + t)}.\n",
"\\end{align}$$\n",
"\n",
"The last equality above can be shown by computing the convolution of two gamma distributions with parameters $(s, \\lambda)$ and $(t, \\lambda)$, to obtain a third gamma distribution with parameters $(s + t, \\lambda)$ and then comparing the normalisation constants of the convolution expression, and the standard form of the gamma distribution.\n",
" \n",
"### Chi-squared\n",
"\n",
"$X$ is chi-squared distributed with $n$ degrees of freedom if it has density function\n",
" \n",
"$$\\begin{align}\n",
"f_X(x) = \\begin{cases}\n",
"\\frac{1}{2\\Gamma\\left(\\frac{n}{2}\\right)} \\left(\\frac{x}{2}\\right)^{\\frac{n}{2} - 1} e ^{-\\frac{x}{2}} & \\text{ if } x > 0,\\\\\n",
"0 & \\text{ if } x \\leq 0.\n",
"\\end{cases}\n",
"\\end{align}$$\n",
"\n",
"The chi-squared distribution is a special version of the gamma distribution, where $w = n / 2$ and $\\lambda = 1/2$, which is of interest on its own.\n",
"For example, if $Z$ is normally distributed with parameters $(\\mu, \\sigma^2)$, then $X = (Z - \\mu)^2 / \\sigma^2$ is chi-squared distributed with $n = 1$ degrees of freedom."
]
},
{
"cell_type": "markdown",
"id": "a08be2a3-d710-4737-94ba-51ecc612b689",
"metadata": {},
"source": [
"## Functions of random variables\n",
"\n",
"We are often interested in functions of random variables, and the distributions of values these take.\n",
"Strictly speaking, if $X$ is a random variable on $(\\Omega, \\mathcal{F}, \\mathbb{P})$ and $g : \\mathbb{R} \\to \\mathbb{R}$ is any function, then $Y(\\omega) = g[X(\\omega)]$ is not a variable in general, because $\\{\\omega \\in \\Omega : Y(\\omega) = y\\}$ may not be in $\\mathcal{F}.$\n",
"Modulo this detail, the following result gives us a rule for determining $f_Y.$\n",
" \n",
":::{prf:theorem} Pdf of a function of a random variable\n",
"\n",
"Let $X$ be a continuous random variable on $(\\Omega, \\mathcal{F}, \\mathbb{P})$ and $g : \\mathbb{R} \\to \\mathbb{R}$ be a continuous increasing function. Then $Y(\\omega) = g[X(\\omega)]$ has pmf\n",
"\n",
"$$f_Y(y) = f_X(g^{-1}(y)) \\frac{d}{dy} g^{-1}(y).$$\n",
"\n",
":::\n",
"\n",
"\n",
"A similar relation holds for continuous and decreasing $g$, where a $-$ sign is added in front of the right hand side of the above result. Cases where $g$ is not strictly increasing or decreasing, are best treated on their own.\n",
" \n",
"## Expectations\n",
"\n",
"The expectation of a continuous random variable is the sensible analogue of the expectation of a continuous random variable.\n",
" \n",
":::{prf:definition} Expectation of continuous random variable\n",
"\n",
"If $X$ is a continuous random variable with pdf $f_X$, the expectation of $X$ is defined by\n",
" \n",
"$$\\mathbb{E}(X) = \\int^\\infty_{-\\infty} xf_X(x) dx,$$\n",
" \n",
"whenever the integral converges absolutely.\n",
":::\n",
"\n",
"Finally, as with discrete random variables, an analogous law of the subconscious statistician states that we can compute expectations of functions of random variables in the straightforward way.\n",
"\n",
"\n",
":::{prf:theorem}\n",
"\n",
"If $X$ is a continuous random variable with density function $f_X$, and $g: \\mathbb{R} \\to \\mathbb{R}$, then\n",
" \n",
"$$\\begin{align}\n",
"\\mathbb{E}(g(X)) = \\int^\\infty_{-\\infty} g(x)f_X(x) dx,\n",
"\\end{align}$$\n",
" \n",
"whenever the integral converges absolutely.\n",
":::"
]
},
{
"cell_type": "markdown",
"id": "7486c0de-3900-4456-a56d-f06027d094ae",
"metadata": {},
"source": [
"[^pdfandpmf]: More accurately, the analogy is precise between $f_X(x)\\delta x$\n",
" and $p_X(x)$, where $f_X$ and $p_X$ are a pdf and a pmf respectively."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv-rw",
"language": "python",
"name": "venv-rw"
},
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}