The product of the first $k$ positive integers (counting numbers) is called $k$ factorial and is denoted $k!$ (Weiss 2010).
$$ k! = k \times (k - 1) \times ...\times 2 \times 1 $$We also define $0! = 1$.
Let us consider an easy example and calculate the factorial of 6. Plugging into the equation from above this gives us:
$$ 6! = 6 \times 5 \times 4 \times 3 \times 2 \times 1 $$In Python we write...
# First, let's import all the needed libraries.
import numpy as np
import math
6 * 5 * 4 * 3 * 2 * 1
720
...or we use the in-built function factorial()
from the math
library.
math.factorial(6)
720
If $n$ is a positive integer and $k$ is a non negative integer less than or equal to $n$, then the binomial coefficient ${n \choose k}$ is defined as:
$$ {n \choose k}= \frac{n!}{k!(n - k)!}$$The binomial coefficients ${n \choose k}$ is often read aloud as "n choose k", because there are ${n \choose k}$ ways to choose $k$ elements, disregarding their order, from a set of $n$ elements. Or in other words, the binomial coefficients refers to the number of combinations of $n$ things taken $k$ at a time without repetition. Please note, that the order of selection does not matter.
Let us try an example to gain some intuition.
Consider a simple word, such as "dog", containing three different letters; d,o,g. How many possibilities exist to draw exactly one letter out those 3 letters? For sure, there are 3 possibilities to draw exactly 1 letter: "d", "o", or "g". Thus we can write all the combinations as ${3 \choose 1}$. What about two letters? How many combinations exist to draw exactly two letters out of 3 letters? The combinations are "do", "dg", and "og" (Please note that for example "og" and "go" are counted only as one combinations, as order does not matter right now). Accordingly, the answer is 3 which can be written as ${3 \choose 2}$. One final question: How many combinations exist to draw exactly three letters out of 3 letters? Let us ask Python!
First, we implement a naive approach, by applying the formula from above.
n = 3 # the word "dog" has 3 letters
k = 3 # we draw exactly three letters
math.factorial(n) / (math.factorial(k) * (math.factorial(n - k)))
1.0
However, we may as well apply the built-in function called comb()
from the math
library to calculate the number of combinations. We do that for $k = 1,2,3$.
n = 3
math.comb(n, 1), math.comb(n, 2), math.comb(n, 3)
(3, 3, 1)
Now, that we are familiar with the concept let us consider a more complex example: The Department of Earth Sciences of FU Berlin asks all graduates to choose their favorite 4 courses of the curriculum. How many different answers could the students give, if the curriculum offers 24 courses to choose from? Any guesses? Let us ask Python.
math.comb(24, 4)
10626
The students could give 10626 different answers.
Repeated trials of an experiment are called Bernoulli trials if the following three conditions are satisfied:
Citation
The E-Learning project SOGA-Py was developed at the Department of Earth Sciences by Annette Rudolph, Joachim Krois and Kai Hartmann. You can reach us via mail by soga[at]zedat.fu-berlin.de.
Please cite as follow: Rudolph, A., Krois, J., Hartmann, K. (2023): Statistics and Geodata Analysis using Python (SOGA-Py). Department of Earth Sciences, Freie Universitaet Berlin.