{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Random Counts #"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The number of people who will vote for a Presidential candidate, the number of treated patients who recover, the number of correct predictions made by a sports journalist – all of these are example of *counts*. Random counts occur commonly in data science. Understanding them not only helps answer questions about the situations in which they arose but also helps us understand more complicated random quantities.\n",
"\n",
"In this chapter we will start out with random counts that seem unlikely. We will then develop some general terminology and notation for dealing with random quantities that have numerical values. Our main examples will be random counts when sampling with and without replacement."
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"### Arranging in a Line

\n",
"\n",
"It will help to review some of the math involved in counting. Let $n$ be a positive integer and suppose you have a set of $n$ elements. \n",
"\n",
"Let $k$ be a fixed integer such that $1 \\le k \\le n$. \n",
"\n",
"By the product rule of counting, the number of ways in which you can arrange $k$ of the $n$ elements in a line is $n(n-1)(n-2) \\cdots (n-k+1)$.\n",
"\n",
"If $k = n$, this is the same as arranging all $n$ elements in a line. Plug $k = n$ into the expression above to see that the number of ways of arranging $n$ elements in a line is $n!$. These are called *permutations* of the $n$ elements."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Choosing Subsets

\n",
"\n",
"Suppose now that you want to select $k$ elements from among the $n$ but you are not interested in the order in which they appear. This happens for example when you deal a hand of cards: you hand is the *subset* or group of cards that you got. Subsets are not ordered.\n",
"\n",
"The number of different subset of $k$ elements that you can create out of $n$ elements is the number $\\binom{n}{k}$, read as \"$n$ choose $k$\".\n",
"\n",
"For $k \\ge 1$, a good way to remember this number is\n",
"\n",
"$$\n",
"\\binom{n}{k} ~ = ~ \\frac{n(n-1)(n-2) \\ldots (n-k+1)}{k!}\n",
"$$\n",
"\n",
"Notice that $\\binom{n}{n} = 1$. There is only one way to choose a set of $n$ elements out of $n$: choose them all.\n",
"\n",
"The calculation says that you can start by pulling out $k$ elements and arranging them in a line; the number of ways to do that is the numerator above. But you can't just stop there. All $k!$ ways of permuting those same $k$ elements give you the same subset, but the numerator counts them all as different arrangements. In other words, the numerator counts every subset consisting of $k$ elements not once but $k!$ times. You only want to count each subset once. Dividing by $k!$ solves this problem.\n",
"\n",
"There is another way to remember $\\binom{n}{k}$. This way is not great for numerical computation because factorials get very large. But it's a clean formula and sometimes helpful in algebraic calculations.\n",
"\n",
"$$\n",
"\\binom{n}{k} ~ = ~ \\frac{n!}{k!(n-k)!}\n",
"$$\n",
"\n",
"It is easy to see by algebra that the two expressions for $\\binom{n}{k}$ give the same answer. \n",
"\n",
"One nice consequence of the second representation is that it can also be used when $k=0$ and gives the natural answer $\\binom{n}{0} = 1$. There is only one way to choose 0 elements out of $n$: do nothing. \n",
"\n",
"Recall that $0!$ is defined to be 1. That's to make the formula above work out.\n",
"\n",
"The numbers $\\binom{n}{k}$ appear in many calculations.\n",
"\n",
"- They are the elements of [Pascal's Triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle).\n",
"- They are the coefficients in the [expansion](https://en.wikipedia.org/wiki/Binomial_theorem) of $(a + b)^n$. Try it out for $n = 2$ and see!\n",
"\n",
"For our purposes, the main property of $\\binom{n}{k}$ is that it is the number of ways you can choose a subset of $k$ elements out of a set of $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
}