Discrete random variables — theory with the class WhatsApp group, the binomial, the geometric and Poisson (and the coffee I lost betting on memes)

Laura wrote in the class group that “one in three messages here is a meme”, and I bet a coffee that among the next ten there would be at least five memes. I lost the coffee. That night, with my notes in front of me, I did the calculation I should have done before betting: if 30% of the messages really are memes, the probability of seeing five or more out of ten is 0.15. Nowhere near as likely as it felt. To do that calculation, the probability from the previous topic is not enough: you need to turn chance into numbers, and that is a random variable.

In topic 2 we calculated probabilities of events: “the email is spam”, “it contains the word free”. Now we take one more step: instead of asking whether something happens, we count how many times it happens. How many memes there are in ten messages, how many useless messages you have to read before finding one with information about the assignment, how many messages arrive in ten minutes the night before a deadline. The whole topic revolves around the WhatsApp group of first-year Data Science and Engineering (GCID), with made-up but realistic data. Each idea appears first in the example, then with its formula and then in R, with how to read every output.

Before you start. For this topic you only need what you saw in topic 2: what an event is, that the probability of two independent things is multiplied, and what the combinations (nk) \binom{n}{k} are. From topic 1, the mean of a frequency table. And from calculus, how to differentiate eat e^{at} and how to read a sum ∑ \sum as “add this up for each value”. The topic goes in this order: first, what a random variable is (a rule that turns outcomes into numbers); then, the two ways of writing its distribution, the mass function (one bar per value) and the distribution function (the staircase that accumulates); next, the numbers that summarise it, mean, variance and the moment generating function; and finally, six models with their own name, how to use them in R and how to recognise them in a problem statement. Almost all the time we use the same example: how many times a day someone asks in the group “is there class tomorrow?”, with probabilities 0.4, 0.3, 0.2 and 0.1. If at some point you get lost, go back to that table and ask yourself what happens to each value.

What a random variable is

Let’s start with a small experiment. You open the group and look at the next three messages. Each one can be a meme (M) or not (N). The sample space has 23=82^3 = 8 outcomes: MMM, MMN, MNM, NMM, MNN, NMN, NNM and NNN.

What you care about is almost never the exact sequence, but a number: how many memes there were. That number is a random variable.

Before giving it its official name, think about what that “how many memes” does. You take each of the 8 outcomes and count its M’s: MMN gets a 2; NNN, a 0. That’s all. A random variable is not a number that “comes out”, but a rule that, when you give it an outcome, gives you back a number.

That rule has three features worth having clear before the definition:

  • Each outcome gets exactly one number. MMN has two memes, not one or three. No doubt about it.
  • Several outcomes can share a number. MMN, MNM and NMM are different, but all three give 2.
  • The probability comes from the outcomes. The number 2 has no probability of its own: it inherits it from the outcomes that end up at it.

The third feature is what turns the whole topic into calculations: to find P(X=2) P(X = 2) , you look for the outcomes that give 2 and add up their probabilities. Now, the definition.

A random variable XX is a function that assigns a real number to each outcome of a random experiment: X:Ω→RX: \Omega \to \mathbb{R}. It is written with a capital letter (XX) and its specific values with lower case (xx).

A random variable is a functionThe eight outcomes of looking at three messages (from MMM to NNN), each with an arrow to the number of memes: 3, 2, 1 or 0. Three outcomes go to 2 and three to 1, so those values have probability 3/8; 3 and 0 have 1/8.Ω: the next 3 messagesX = number of memesMMMMMNMNMNMMMNNNMNNNMNNN3P(X = 3) = 1/82P(X = 2) = 3/81P(X = 1) = 3/80P(X = 0) = 1/8M = meme, N = not a meme
Each outcome goes to exactly one number; several outcomes can go to the same one. Counting arrows gives the probability of each value.

In the diagram, each outcome on the left has an arrow to a single number on the right. Several outcomes can go to the same number (MMN, MNM and NMM all go to 2), but no outcome goes to two numbers at once: that is what it means for XX to be a function. And why call it random if it is an ordinary function? Because the outcome of the experiment is random, so the number it produces is random too. That is why each value of XX has its own probability.

If memes and non-memes really were equally likely (50%, like a coin), each sequence would have probability 1/81/8 and we could count cases:

xxOutcomes giving that valueP(X=x)P(X = x)
0NNN1/8
1MNN, NMN, NNM3/8
2MMN, MNM, NMM3/8
3MMM1/8

That table is already the most important thing in the topic: the distribution of the variable, which values it takes and with what probability.

With the same experiment you can define other variables. For example, YY = number of messages that are not memes, or ZZ = memes minus non-memes. All three live on the same Ω\Omega; what changes is the rule that puts a number on each outcome.

Check you’ve got it: with memes at 50%, which values does Z Z = memes minus non-memes take, and with what probability?

You apply the rule to each outcome. MMM has 3 memes and 0 non-memes, so Z=3 Z = 3 . The three outcomes with two memes (MMN, MNM, NMM) give 2−1=1 2 – 1 = 1 . The three with one meme give 1−2=−1 1 – 2 = -1 . And NNN gives 0−3=−3 0 – 3 = -3 . Counting outcomes as in the table for X X : P(Z=−3)=1/8 P(Z = -3) = 1/8 , P(Z=−1)=3/8 P(Z = -1) = 3/8 , P(Z=1)=3/8 P(Z = 1) = 3/8 and P(Z=3)=1/8 P(Z = 3) = 1/8 . The same probabilities as X X , but placed on other values. That’s no coincidence: since non-memes =3−X = 3 – X , it turns out that Z=X−(3−X)=2X−3 Z = X – (3 – X) = 2X – 3 , and each value of X X goes to one of Z Z .

With memes at 30%: the same calculation, without a coin

The table above uses a coin, but Laura said memes are one in three, roughly 30%. With p=0.3 p = 0.3 counting cases no longer works, because the 8 sequences are not equally likely: NNN is much easier than MMM. What you do is work out the probability of each sequence and add up the ones that give the same value:

x x SequencesProbability of each oneP(X=x) P(X = x) Why
0NNN0.73=0.343 0.7^3 = 0.343 0.343 0.343 Three non-memes; the messages are independent, so you multiply
1MNN, NMN, NNM0.3⋅0.72=0.147 0.3 \cdot 0.7^2 = 0.147 3⋅0.147=0.441 3 \cdot 0.147 = 0.441 All three have one meme and two non-memes: same probability, and they add up
2MMN, MNM, NMM0.32⋅0.7=0.063 0.3^2 \cdot 0.7 = 0.063 3⋅0.063=0.189 3 \cdot 0.063 = 0.189 Same as the row above, with the roles swapped
3MMM0.33=0.027 0.3^3 = 0.027 0.027 0.027 Three memes in a row: very rare

The four values add up to 0.343+0.441+0.189+0.027=1 0.343 + 0.441 + 0.189 + 0.027 = 1 , as they must. Look at the two pieces in each row: the probability of one sequence and how many sequences there are. Keep them in mind, because they are exactly the two pieces of the binomial, further down: this is a binomial with n=3 n = 3 , and dbinom(0:3, 3, 0.3) returns these same four numbers.

Discrete and continuous

A random variable is discrete when it takes a finite set of values (0, 1, 2 or 3 memes) or a countably infinite one (0, 1, 2, … messages in ten minutes, with no clear upper limit). The values can be lined up and counted, even if the line never ends. A variable is continuous when it can take any value in an interval, like the exact time someone takes to reply. Continuous variables are topic 4; here everything is a count.

A quick test for discrete: is the answer given with “how many”? How many memes, how many messages, how many attempts. If the answer is “how much” (how much time, how much it weighs), it is usually continuous.

The mass function and the distribution function

Let’s switch variables to get less round numbers. During one semester I noted how many times a day someone asks in the group “is there class tomorrow?”. Let’s call that number XX. Experience says this:

xx0123
P(X=x)P(X = x)0.40.30.20.1

The mass function

The probability mass function (or probability function) of a discrete variable is

p(xi)=P(X=xi) p(x_i) = P(X = x_i)

for each value xix_i the variable takes. It has to satisfy two conditions, and the exam will ask you to check them many times:

  1. P(X=xi)≥0P(X = x_i) \geq 0 for every xix_i: no probability is negative.
  2. ∑iP(X=xi)=1\sum_i P(X = x_i) = 1: between them, the values share out all the probability.

In our table, 0.4+0.3+0.2+0.1=10.4 + 0.3 + 0.2 + 0.1 = 1. Correct. Notice that the mass function is only defined at the values the variable takes: it makes no sense to ask for P(X=1.5)P(X = 1.5); it is 0 because nobody asks “is there class tomorrow?” half a time.

Check you’ve got it: can the table 0.5, 0.3, 0.3 and −0.1 for the values 0, 1, 2 and 3 be a mass function?

No. It adds up to 0.5+0.3+0.3−0.1=1 0.5 + 0.3 + 0.3 – 0.1 = 1 , so the second condition holds, but the first does not: there is a negative probability, and something happening “less than never” makes no sense. That’s why in the exam you check both conditions, not just the sum. If you stop at the sum, this table would look correct.

The distribution function

The distribution function accumulates probability from left to right:

F(x)=P(X≤x)=∑xi≤xP(X=xi) F(x) = P(X \leq x) = \sum_{x_i \leq x} P(X = x_i)

Unlike the mass function, F(x)F(x) is defined for any real number xx, not just for 0, 1, 2 and 3. Let’s go stretch by stretch:

  • If x<0x < 0: no value of XX is less than or equal to xx, so F(x)=0F(x) = 0.
  • If 0≤x<10 \leq x < 1: only 0 counts, F(x)=0.4F(x) = 0.4.
  • If 1≤x<21 \leq x < 2: 0 and 1 count, F(x)=0.4+0.3=0.7F(x) = 0.4 + 0.3 = 0.7.
  • If 2≤x<32 \leq x < 3: F(x)=0.7+0.2=0.9F(x) = 0.7 + 0.2 = 0.9.
  • If x≥3x \geq 3: everything counts, F(x)=1F(x) = 1.
Stretchx<0x < 00≤x<10 \leq x < 11≤x<21 \leq x < 22≤x<32 \leq x < 3x≥3x \geq 3
F(x)F(x)00.40.70.91
Probability mass function and distribution functionTop, bars of height 0.4, 0.3, 0.2 and 0.1 at x equal to 0, 1, 2 and 3. Bottom, the step distribution function: 0 before 0, then 0.4, 0.7, 0.9 and 1, with a filled dot at the top of each jump and a hollow one at the bottom.0.000.100.200.300.400.500123P(X = x)0.40.30.20.10.000.250.500.751.00-101234"Is there class tomorrow?" questions per dayF(x)0.4+0.40.7+0.30.9+0.21.0+0.1
Each step of F is as tall as the bar above it. The filled dot means F(x) already includes x itself.

In the diagram, the bars of the mass function are at the top and the staircase of FF is underneath. Each step is exactly as tall as the bar above it: at x=1x = 1 the staircase rises by 0.3, which is P(X=1)P(X = 1). And look at the dots: the filled one is on the left of each stretch and the hollow one on the right. That means that at x=1x = 1 the function is already 0.7 (the upper value), because F(1)=P(X≤1)F(1) = P(X \leq 1) includes 1 itself. We say that FF is right-continuous.

The properties of any distribution function:

  1. 0≤F(x)≤10 \leq F(x) \leq 1.
  2. lim⁡x→−∞F(x)=0\lim_{x \to -\infty} F(x) = 0 and lim⁡x→∞F(x)=1\lim_{x \to \infty} F(x) = 1.
  3. FF is non-decreasing: it never goes down, it only stays level or goes up.
  4. FF is right-continuous.

The properties, on the staircase

Put like that they sound like a list to memorise, but each one can be seen in the drawing of the staircase:

  • Between 0 and 1. F(x) F(x) is a probability, that of X≤x X \leq x , so it cannot go outside that range.
  • Starts at 0 and ends at 1. Far to the left (at x=−5 x = -5 , for example) there is no value of X X below: 0. Far to the right they are all there: 1.
  • Never goes down. As you move right you can only add values, and each one brings a probability that is not negative. On our staircase: 0, 0.4, 0.7, 0.9 and 1, always upwards.
  • Right-continuous. It is the filled dot in the drawing: exactly at a value that X X takes, the step has already gone up.

If the exam gives you a “distribution function” and asks whether it is one, check these four things in this order. The one that fails most is never going down: one stretch where the value is lower than in the previous one is enough.

Check you’ve got it: what are F(1.5) F(1.5) , F(2) F(2) and F(−1) F(-1) in the “is there class tomorrow?” example?

F(1.5)=0.7 F(1.5) = 0.7 : X≤1.5 X \leq 1.5 includes 0 and 1, but not 2, so 0.4+0.3=0.7 0.4 + 0.3 = 0.7 . You are in the stretch 1≤x<2 1 \leq x < 2 . F(2)=0.9 F(2) = 0.9 : exactly at 2 the step has already gone up, because X≤2 X \leq 2 includes 2; on the other hand, F(1.99) F(1.99) is still 0.7. And F(−1)=0 F(-1) = 0 : no day has someone asking a negative number of times.

Going from one to the other

Topic 3 of the exam is full of back-and-forth between mass and distribution. These are the four calculations you need to do without thinking:

I want…CalculationIn the example
P(X≤a)P(X \leq a)F(a)F(a)P(X≤1)=0.7P(X \leq 1) = 0.7
P(X>a)P(X > a)1−F(a)1 – F(a)P(X>1)=1−0.7=0.3P(X > 1) = 1 – 0.7 = 0.3
P(X=a)P(X = a) from FFF(a)−F(a−)F(a) – F(a^-): the jump at aaP(X=2)=0.9−0.7=0.2P(X = 2) = 0.9 – 0.7 = 0.2
P(a<X≤b)P(a < X \leq b)F(b)−F(a)F(b) – F(a)P(0<X≤2)=0.9−0.4=0.5P(0 < X \leq 2) = 0.9 – 0.4 = 0.5

Mixing up << and ≤\leq. In a discrete variable it matters: P(X<2)=P(X≤1)=0.7P(X < 2) = P(X \leq 1) = 0.7, but P(X≤2)=0.9P(X \leq 2) = 0.9. Before using FF, always translate the question into “less than or equal to something”.

The four calculations, with the why

You can learn the table above by heart, but it is easier to know where each row comes from. The rule is always the same: write down which values are included in what you are asked and compare it with what F F measures, which is “everything to the left, including the value itself”. With our variable, which takes 0, 1, 2 and 3:

You are askedValues includedCalculationWhy
P(X≤1) P(X \leq 1) 0 and 1F(1)=0.7 F(1) = 0.7 It is exactly what F F accumulates
P(X>1) P(X > 1) 2 and 31−F(1)=0.3 1 – F(1) = 0.3 They are the ones not in X≤1 X \leq 1 : the complement
P(X≥2) P(X \geq 2) 2 and 31−F(1)=0.3 1 – F(1) = 0.3 The same values as the row above, so the same calculation
P(X=2) P(X = 2) only 2F(2)−F(1)=0.2 F(2) – F(1) = 0.2 Everything up to 2, minus everything up to 1: the bar at 2 is left
P(0<X≤2) P(0 < X \leq 2) 1 and 2F(2)−F(0)=0.5 F(2) – F(0) = 0.5 Everything up to 2, minus what there is up to 0 included
P(1≤X≤2) P(1 \leq X \leq 2) 1 and 2F(2)−F(0)=0.5 F(2) – F(0) = 0.5 With ≤ \leq on the left 1 is included, so you subtract up to 0

Look at the second and third rows: “greater than 1” and “greater than or equal to 2” are the same question, because there is no possible value between 1 and 2. The same goes for the fifth and sixth. In a discrete variable with integer values, F(a−) F(a^-) , what there is just before a a , is F(a−1) F(a – 1) : that’s why the jump at 2 is calculated as F(2)−F(1) F(2) – F(1) .

In R

In R the mass function is a vector of values and another of probabilities, and the distribution function is their cumulative sum, cumsum():

x <- 0:3                        # values X takes
p <- c(0.4, 0.3, 0.2, 0.1)      # P(X = x), in the same order
sum(p)                          # check: it must be 1
F <- cumsum(p)                  # F(0), F(1), F(2), F(3)
F
1 - F[2]                        # P(X > 1): F[2] is F(1), because R indices start at 1
F[3] - F[1]                     # P(0 < X <= 2) = F(2) - F(0)

Output:

[1] 1
[1] 0.4 0.7 0.9 1.0
[1] 0.3
[1] 0.5

How to read it

  • [1] 1: the sum of the probabilities. If it is not exactly 1, the table was copied wrong or a value is missing. It is the first check in any exercise.
  • [1] 0.4 0.7 0.9 1.0: the four values of the staircase. Read them like this: “the probability that they ask at most 0 times is 0.4; at most once, 0.7; at most twice, 0.9; at most 3 times, certain”.
  • [1] 0.3: the probability that they ask more than once in a day. Three days in ten there are at least two “is there class tomorrow?”.
  • [1] 0.5: half of the days they ask once or twice.

F[1] is not F(1)F(1). R vectors start at position 1, so F[1] is F(0)F(0), F[2] is F(1)F(1)… When the values of XX start at 0, every index is one ahead of the value.

Mean, variance and moments

The expectation

The expectation, mean or expected value of a discrete variable is the average of its values, each weighted by its probability:

E(X)=μ=∑ixi P(X=xi) E(X) = \mu = \sum_i x_i \, P(X = x_i)

In the example:

E(X)=0⋅0.4+1⋅0.3+2⋅0.2+3⋅0.1=0+0.3+0.4+0.3=1 E(X) = 0 \cdot 0.4 + 1 \cdot 0.3 + 2 \cdot 0.2 + 3 \cdot 0.1 = 0 + 0.3 + 0.4 + 0.3 = 1

What does that 1 mean? That in the long run, if you look at many days, the group asks once a day on average whether there is class tomorrow. It does not mean they ask once every day: there are days with 0 and days with 3. It is the same as the mean of a frequency table from topic 1, with probabilities instead of relative frequencies.

If the formula feels abstract, put days on it. Imagine 100 days of the group that split up exactly as the probabilities say:

Questions in the dayDaysTotal questionsWhy
0400⋅40=0 0 \cdot 40 = 0 0.4 of 100 days
1301⋅30=30 1 \cdot 30 = 30 0.3 of 100 days, one question each
2202⋅20=40 2 \cdot 20 = 40 0.2 of 100 days, two questions each
3103⋅10=30 3 \cdot 10 = 30 0.1 of 100 days, three questions each
Total100100

That’s 100 questions in 100 days: one a day on average, the 1 from the formula. Dividing by 100 at the end is the same as multiplying each value by its probability from the start, and that is exactly ∑ixi P(X=xi) \sum_i x_i \, P(X = x_i) . That’s why the expectation looks so much like the mean from topic 1: it is the mean the data would have if they were split exactly according to the probabilities.

The expectation does not have to be a value the variable can take. If XX is the number of memes in three messages with probability 1/2, E(X)=1.5E(X) = 1.5: you will never see 1.5 memes, but it is the center of the distribution.

In general, the expectation of a function of the variable, g(X)g(X), is calculated in the same way, applying the function to each value:

E[g(X)]=∑ig(xi) P(X=xi) E[g(X)] = \sum_i g(x_i) \, P(X = x_i)

For example, E(X2)=02⋅0.4+12⋅0.3+22⋅0.2+32⋅0.1=0+0.3+0.8+0.9=2E(X^2) = 0^2 \cdot 0.4 + 1^2 \cdot 0.3 + 2^2 \cdot 0.2 + 3^2 \cdot 0.1 = 0 + 0.3 + 0.8 + 0.9 = 2.

The variance

The variance measures how far the values are from the mean, again weighted by their probability:

Var(X)=σ2=E[(X−μ)2]=∑i(xi−μ)2 P(X=xi) \text{Var}(X) = \sigma^2 = E\big[(X – \mu)^2\big] = \sum_i (x_i – \mu)^2 \, P(X = x_i)

Its square root, σ=Var(X)\sigma = \sqrt{\text{Var}(X)}, is the standard deviation, which is back in the units of the variable. To calculate it you almost always use the shortcut formula, which is proved in the proofs article:

Var(X)=E(X2)−[E(X)]2 \text{Var}(X) = E(X^2) – [E(X)]^2

In the example: Var(X)=2−12=1\text{Var}(X) = 2 – 1^2 = 1 and σ=1\sigma = 1. Check with the definition: (0−1)2⋅0.4+(1−1)2⋅0.3+(2−1)2⋅0.2+(3−1)2⋅0.1=0.4+0+0.2+0.4=1(0-1)^2 \cdot 0.4 + (1-1)^2 \cdot 0.3 + (2-1)^2 \cdot 0.2 + (3-1)^2 \cdot 0.1 = 0.4 + 0 + 0.2 + 0.4 = 1. It matches.

Calculating E(X2)E(X^2) and forgetting to subtract [E(X)]2[E(X)]^2. If in the exam you get a variance bigger than E(X2)E(X^2) or a negative variance, you have mixed up the pieces: the variance is never negative and never exceeds E(X2)E(X^2).

The two variance calculations, column by column

In the exam the variance is done in a table, and that way nothing gets lost along the way. With μ=1 \mu = 1 :

x x P(X=x) P(X = x) (x−μ)2 (x – \mu)^2 (x−μ)2 P (x – \mu)^2 \, P x2 P x^2 \, P Why
00.4(0−1)2=1 (0 – 1)^2 = 1 0.400 is one unit away from the mean
10.3(1−1)2=0 (1 – 1)^2 = 0 00.31 is the mean: it adds nothing to the variance
20.2(2−1)2=1 (2 – 1)^2 = 1 0.20.8One unit away, like 0, but with less weight
30.1(3−1)2=4 (3 – 1)^2 = 4 0.40.9Two units away: the square turns it into 4
Sum112

The fourth column adds up to the variance by the definition, 1. The fifth adds up to E(X2)=2 E(X^2) = 2 , and with the shortcut formula 2−12=1 2 – 1^2 = 1 . Look at the row for 3: it is the least likely value, but it contributes as much as 0, because squaring punishes the values far from the mean a lot.

Check you’ve got it: in the three messages with memes at 50%, E(X)=1.5 E(X) = 1.5 . What is Var(X) \text{Var}(X) ?

With the shortcut formula. First E(X2)=02⋅18+12⋅38+22⋅38+32⋅18=0+3+12+98=3 E(X^2) = 0^2 \cdot \frac{1}{8} + 1^2 \cdot \frac{3}{8} + 2^2 \cdot \frac{3}{8} + 3^2 \cdot \frac{1}{8} = \frac{0 + 3 + 12 + 9}{8} = 3 . Then you subtract the square of the mean: Var(X)=3−1.52=3−2.25=0.75 \text{Var}(X) = 3 – 1.5^2 = 3 – 2.25 = 0.75 . If you get 1.5, you subtracted the mean without squaring it.

The properties used most

If aa and bb are constants:

PropertyWhy it makes sense
E(b)=bE(b) = b, Var(b)=0\text{Var}(b) = 0A constant does not vary
E(aX+b)=a E(X)+bE(aX + b) = a\,E(X) + bIf you multiply all the values by aa and add bb, the mean does the same
Var(aX+b)=a2 Var(X)\text{Var}(aX + b) = a^2\,\text{Var}(X)Adding bb shifts everything without spreading it; multiplying by aa spreads by aa, and the variance is squared
E[g1(X)+g2(X)]=E[g1(X)]+E[g2(X)]E[g_1(X) + g_2(X)] = E[g_1(X)] + E[g_2(X)]The expectation of a sum is the sum of the expectations

An example: if each “is there class tomorrow?” generates 4 replies and on top of that there are 2 fixed messages a day from the group’s bot, the number of messages for that reason is W=4X+2W = 4X + 2. Its mean is 4⋅1+2=64 \cdot 1 + 2 = 6 and its variance 42⋅1=164^2 \cdot 1 = 16. The 2 does not appear in the variance: shifting all the values does not spread them.

Writing Var(4X+2)=4 Var(X)+2\text{Var}(4X + 2) = 4\,\text{Var}(X) + 2. Both parts are wrong: the constant that multiplies comes out squared and the one that adds disappears.

Moments, skewness and kurtosis

E(Xk)E(X^k) is called the moment of order kk (about the origin) and E[(X−μ)k]E[(X – \mu)^k], the moment of order kk about the mean. The mean is the first moment and the variance, the second moment about the mean. Moments of order 3 and 4 measure shape:

α3=E[(X−μ)3]σ3α4=E[(X−μ)4]σ4 \alpha_3 = \frac{E[(X – \mu)^3]}{\sigma^3} \qquad \alpha_4 = \frac{E[(X – \mu)^4]}{\sigma^4}

The skewness coefficient α3\alpha_3 is positive if the distribution has a long tail to the right, negative if it has it on the left and 0 if it is symmetric. The kurtosis α4\alpha_4 is compared with 3 (the value for the bell curve, which you will see in topic 5): more than 3, heavier tails; less than 3, flatter. Sometimes α4−3\alpha_4 – 3 is given instead, and then it is compared with 0.

In the “is there class tomorrow?” example, α3=0.6\alpha_3 = 0.6: the distribution has its tail to the right (days with 3 questions are rare and pull it). We calculate it in R in the practice.

The moment generating function

There is a tool that always comes up in the exam: the moment generating function (mgf). For a discrete variable,

φX(t)=E(etX)=∑ietxi P(X=xi) \varphi_X(t) = E\big(e^{tX}\big) = \sum_i e^{t x_i} \, P(X = x_i)

defined for the tt in an interval (−h,h)(-h, h) around 0 where that sum exists. In the example:

φX(t)=0.4+0.3 et+0.2 e2t+0.1 e3t \varphi_X(t) = 0.4 + 0.3\,e^{t} + 0.2\,e^{2t} + 0.1\,e^{3t}

What is such a strange function for? It is in its name: it generates the moments. If you differentiate it kk times and evaluate it at t=0t = 0, you get E(Xk)E(X^k):

φX(k)(0)=E(Xk) \varphi_X^{(k)}(0) = E(X^k)

Let’s check it in the example:

  • φX′(t)=0.3 et+0.4 e2t+0.3 e3t\varphi_X'(t) = 0.3\,e^{t} + 0.4\,e^{2t} + 0.3\,e^{3t}, so φX′(0)=0.3+0.4+0.3=1=E(X)\varphi_X'(0) = 0.3 + 0.4 + 0.3 = 1 = E(X).
  • φX′′(t)=0.3 et+0.8 e2t+0.9 e3t\varphi_X''(t) = 0.3\,e^{t} + 0.8\,e^{2t} + 0.9\,e^{3t}, so φX′′(0)=0.3+0.8+0.9=2=E(X2)\varphi_X''(0) = 0.3 + 0.8 + 0.9 = 2 = E(X^2).

The same numbers as before. The proof of why it works (a Taylor expansion) is step by step in the proofs for this topic.

Why does it work, without waiting for the proof? Look at what happens to a single term. The derivative of etx e^{tx} with respect to t t is x etx x \, e^{tx} : each time you differentiate, an x x comes down as a factor. And at t=0 t = 0 , e0=1 e^{0} = 1 wipes out all the exponentials. Term by term, in the example:

Term of φX(t) \varphi_X(t) DerivativeAt t=0 t = 0 Why
0.4 0.4 0 0 0 0 It is 0.4 e0⋅t 0.4\,e^{0 \cdot t} : the factor that comes down is the value 0
0.3 et 0.3\,e^{t} 1⋅0.3 et 1 \cdot 0.3\,e^{t} 0.3 0.3 The value 1 comes down
0.2 e2t 0.2\,e^{2t} 2⋅0.2 e2t 2 \cdot 0.2\,e^{2t} 0.4 0.4 The value 2 comes down
0.1 e3t 0.1\,e^{3t} 3⋅0.1 e3t 3 \cdot 0.1\,e^{3t} 0.3 0.3 The value 3 comes down

The t=0 t = 0 column is, row by row, xi P(X=xi) x_i \, P(X = x_i) , and its sum is the expectation. If you differentiate again, a second x x comes down and you get xi2 P(X=xi) x_i^2 \, P(X = x_i) : 0, 0.3, 0.8 and 0.9, which add up to E(X2)=2 E(X^2) = 2 . With k k derivatives, xik x_i^k . That is the whole idea: the Taylor expansion in the proof writes this same thing for any variable.

The mgf has a second property used a lot in topic 6: if two variables have the same mgf, they have the same distribution. So if in an exercise you get an mgf you recognise (say, a Poisson’s), you already know which distribution you have.

With variables that have infinitely many values the mgf is a series, and it is almost always summed with one of these two series you need to have at hand:

SeriesValueWhere it appears
∑k=0∞ak\sum_{k=0}^{\infty} a^k11−a\frac{1}{1-a} if ∣a∣<1\lvert a \rvert < 1Geometric and negative binomial
∑k=0∞zkk!\sum_{k=0}^{\infty} \frac{z^k}{k!}eze^zPoisson

The discrete models

Some situations come up so often that they have their own name. Instead of building the table every time, you recognise the model, plug in the parameters and you already have its mass function, mean, variance and mgf. These are the six in this topic.

Discrete uniform

For the group project presentation there are six of you, and someone suggests rolling a die in the group (WhatsApp has no die, so a website is used). XX = the number that comes up. Each value from 1 to nn has the same probability:

P(X=i)=1n,i=1,2,…,nE(X)=n+12Var(X)=n2−112 P(X = i) = \frac{1}{n}, \quad i = 1, 2, \ldots, n \qquad E(X) = \frac{n+1}{2} \qquad \text{Var}(X) = \frac{n^2 – 1}{12}

With n=6n = 6: E(X)=3.5E(X) = 3.5 and Var(X)=35/12≈2.92\text{Var}(X) = 35/12 \approx 2.92.

Bernoulli

A single message: is it a meme or not? X=1X = 1 if it is a meme (success) and X=0X = 0 if not (failure), with P(X=1)=pP(X = 1) = p. It is written X∼Ber(p)X \sim \text{Ber}(p).

P(X=x)=px(1−p)1−x,x∈{0,1}E(X)=pVar(X)=p(1−p)φX(t)=1−p+p et P(X = x) = p^x (1-p)^{1-x}, \quad x \in \{0, 1\} \qquad E(X) = p \qquad \text{Var}(X) = p(1-p) \qquad \varphi_X(t) = 1 – p + p\,e^{t}

With p=0.3p = 0.3: E(X)=0.3E(X) = 0.3 and Var(X)=0.3⋅0.7=0.21\text{Var}(X) = 0.3 \cdot 0.7 = 0.21. The Bernoulli is the brick the others are built from: the binomial counts successes over many Bernoullis; the geometric and the negative binomial count failures until a success.

Binomial

Back to the bet. Ten messages, each a meme with probability p=0.3p = 0.3, independent of each other. XX = number of memes. It is a binomial with parameters n=10n = 10 and p=0.3p = 0.3: X∼Binom(10,0.3)X \sim \text{Binom}(10, 0.3).

P(X=k)=(nk)pk(1−p)n−k,k=0,1,…,n P(X = k) = \binom{n}{k} p^k (1-p)^{n-k}, \quad k = 0, 1, \ldots, n

Where does it come from? A specific sequence with kk memes and n−kn – k non-memes, for example MMMNNNNNNN, has probability pk(1−p)n−kp^k (1-p)^{n-k} (we multiply because they are independent). And there are (nk)\binom{n}{k} sequences with kk memes, as many as ways to choose which kk positions they go in, using the combinations from topic 2.

E(X)=npVar(X)=np(1−p)φX(t)=(1−p+p et)n E(X) = np \qquad \text{Var}(X) = np(1-p) \qquad \varphi_X(t) = \big(1 – p + p\,e^{t}\big)^n

With n=10n = 10 and p=0.3p = 0.3: E(X)=3E(X) = 3 memes on average and Var(X)=2.1\text{Var}(X) = 2.1. And look at the mgf: it is the Bernoulli’s raised to the power nn. That is no coincidence: a binomial is the sum of nn independent Bernoullis, one per message.

R’s four functions for the binomial start with d, p, q and r and end in binom:

n <- 10; p <- 0.3                  # ten messages, 30% memes
dbinom(3, n, p)                    # P(X = 3): exactly three memes
pbinom(4, n, p)                    # P(X <= 4): at most four
1 - pbinom(4, n, p)                # P(X >= 5) = 1 - P(X <= 4): the bet
round(dbinom(0:10, n, p), 4)       # the whole mass function
qbinom(0.9, n, p)                  # the smallest k with P(X <= k) >= 0.9
c(mean = n * p, variance = n * p * (1 - p))

Output:

[1] 0.2668279
[1] 0.8497317
[1] 0.1502683
 [1] 0.0282 0.1211 0.2335 0.2668 0.2001 0.1029 0.0368 0.0090 0.0014
[10] 0.0001 0.0000
[1] 5
    mean variance 
     3.0      2.1 

How to read it

  • [1] 0.2668279: the probability of seeing exactly three memes in ten messages. It is the most likely value, but it does not even reach a third: usually you do not see exactly the mean.
  • [1] 0.8497317: 85% of the time there will be at most four memes.
  • [1] 0.1502683: the probability of five or more memes, the one from my bet. About once in seven. Notice the 4 inside pbinom: “at least 5” is “not at most 4”.
  • The row of eleven numbers is the whole mass function, from P(X=0)=0.0282P(X = 0) = 0.0282 to P(X=10)≈0P(X = 10) \approx 0. Rounded to 4 decimals, the last two come out as 0.0001 and 0: ten memes in a row does happen, but once every 170,000 batches.
  • [1] 5: the 0.9 quantile. In 90% of batches of ten messages there will be five memes or fewer. It is not exactly “the value that leaves 90% below”, because in a discrete variable that usually does not exist: it is the first kk with F(k)≥0.9F(k) \geq 0.9 (F(4)=0.85F(4) = 0.85 does not reach it; F(5)=0.95F(5) = 0.95 does).
  • mean 3.0, variance 2.1: the values from the formulas npnp and np(1−p)np(1-p).
Binomial(10, 0.3): memes in ten messagesBars of the binomial with n 10 and p 0.3: 0.028, 0.121, 0.233, 0.267 and 0.200 from 0 to 4, and those from 5 to 10 in coral, which add up to 0.1503. Dashed line at the mean, 3.0.000.100.200.30012345678910Memes in 10 messagesP(X = k)0.0280.1210.2330.2000.267mean 3P(X ≥ 5) = 0.15031 – pbinom(4, 10, 0.3)
What plot(0:10, dbinom(0:10, 10, 0.3), type = "h") draws: the bet, in coral, only had a 15% chance.

The diagram is what you get with plot(0:10, dbinom(0:10, 10, 0.3), type = "h"), redrawn in the site colours and with the bet’s bars in coral. Read it like this: the distribution piles up around 3 (the mean), falls quickly to the right and the coral zone, from 5 to 10, only adds up to 0.15. That right skew is typical of a binomial with p<0.5p < 0.5; with p=0.5p = 0.5 it would be symmetric and with p>0.5p > 0.5 it would lean the other way.

Geometric

Another change of question. Only 20% of the group’s messages have useful information about the assignment (a date, the brief, a question answered). You start reading from the last one you saw. XX = number of useless messages you read before the first useful one. It is a geometric with parameter p=0.2p = 0.2: X∼geom(0.2)X \sim \text{geom}(0.2).

P(X=k)=(1−p)k p,k=0,1,2,… P(X = k) = (1-p)^k \, p, \quad k = 0, 1, 2, \ldots

For the first useful one to arrive after kk useless ones there must be kk failures in a row, (1−p)k(1-p)^k, and then a success, pp. There are no combinations because there is only one possible order: all the failures first.

E(X)=1−ppVar(X)=1−pp2φX(t)=p1−(1−p)etif (1−p)et<1 E(X) = \frac{1-p}{p} \qquad \text{Var}(X) = \frac{1-p}{p^2} \qquad \varphi_X(t) = \frac{p}{1 – (1-p)e^{t}} \quad \text{if } (1-p)e^t < 1

With p=0.2p = 0.2: on average you read 4 useless messages before the useful one, with variance 20 (standard deviation 4.47: some days you read 15 useless ones).

Books use two geometrics: the one that counts failures before the first success (k=0,1,2,…k = 0, 1, 2, \ldots) and the one that counts trials up to and including the first success (k=1,2,3,…k = 1, 2, 3, \ldots, with P(X=k)=(1−p)k−1pP(X = k) = (1-p)^{k-1}p and mean 1/p1/p). The course notes and R use the first. If the question says “the first useful one is message number 3”, that is 2 useless ones before: dgeom(2, 0.2), not dgeom(3, 0.2).

The geometric has a property no other discrete distribution has: it is memoryless. If you have already read 5 useless messages, the probability of reading at least 3 more before the useful one is the same as if you were starting from scratch: P(X≥5+3∣X≥5)=P(X≥3)P(X \geq 5 + 3 \mid X \geq 5) = P(X \geq 3). The messages do not “remember” that you have been reading for a while; we prove it in the proofs article.

Negative binomial

And what if you need three useful messages (the date, the brief and the submission format)? XX = number of useless messages you read before collecting the third useful one. It is a negative binomial with parameters r=3r = 3 and p=0.2p = 0.2: X∼NBinom(3,0.2)X \sim \text{NBinom}(3, 0.2).

P(X=k)=(k+r−1k)pr(1−p)k,k=0,1,2,… P(X = k) = \binom{k + r – 1}{k} p^r (1-p)^k, \quad k = 0, 1, 2, \ldots

Why those combinations? The last message read must be the third useful one (otherwise you would have stopped earlier). Before it there are kk useless ones and r−1=2r – 1 = 2 useful ones mixed in any order: (k+r−1k)\binom{k + r – 1}{k} ways of arranging them.

E(X)=r(1−p)pVar(X)=r(1−p)p2φX(t)=(p1−(1−p)et)r E(X) = \frac{r(1-p)}{p} \qquad \text{Var}(X) = \frac{r(1-p)}{p^2} \qquad \varphi_X(t) = \left(\frac{p}{1 – (1-p)e^{t}}\right)^r

With r=3r = 3 and p=0.2p = 0.2: 12 useless on average, variance 60. It is exactly three times the geometric, and the mgf is the geometric’s cubed: a negative binomial is the sum of rr independent geometrics (waiting for the first useful one, then the second, then the third). With r=1r = 1 it is the geometric.

What the geometric and negative binomial countFourteen messages in a row: four useless, one useful, three useless, one useful, four useless and one useful. The geometric counts the 4 useless ones before the first useful one; the negative binomial with r equal to 3 counts the 11 useless ones before the third useful one.1234useful5678useful910111213useful14geometric: 4 useless before the 1st usefulnegative binomial (r = 3): 11 useless before the 3rd usefulOne read of the group, message by messageR counts the grey ones (failures), not the green ones
Both variables count useless messages: the geometric up to the first useful one and the negative binomial up to the third.

The diagram shows what each variable counts in the same read of the group. Grey squares are useless messages and green ones, useful. The geometric stops at the first green and counts the greys before it (4 in this read). The negative binomial carries on to the third green and counts all the greys (11). Neither counts the greens: R does not either.

Poisson

Last question for the group: how many messages arrive in ten minutes? The night before a deadline, 4 on average. There is no number of “trials” as in the binomial: messages arrive when they arrive, one at a time, with no clear upper limit. When something happens like that, at a constant average rate and independently, the number of times it happens in an interval is a Poisson with parameter λ\lambda (the mean for the interval): X∼Pois(4)X \sim \text{Pois}(4).

P(X=k)=e−λλkk!,k=0,1,2,…E(X)=λVar(X)=λφX(t)=eλ(et−1) P(X = k) = e^{-\lambda} \frac{\lambda^k}{k!}, \quad k = 0, 1, 2, \ldots \qquad E(X) = \lambda \qquad \text{Var}(X) = \lambda \qquad \varphi_X(t) = e^{\lambda (e^t – 1)}

The Poisson has a giveaway feature: the mean and the variance are equal. If in some count data the variance is much bigger than the mean, the Poisson does not fit them well (we check it with data in the practice).

lambda <- 4                        # average messages in 10 minutes
dpois(0, lambda)                   # P(X = 0): ten minutes of silence
1 - ppois(7, lambda)               # P(X >= 8): an avalanche
round(dpois(0:10, lambda), 4)
qpois(c(0.5, 0.9), lambda)         # median and 0.9 quantile

Output:

[1] 0.01831564
[1] 0.05113362
 [1] 0.0183 0.0733 0.1465 0.1954 0.1954 0.1563 0.1042 0.0595 0.0298
[10] 0.0132 0.0053
[1] 4 7

How to read it

  • [1] 0.01831564: ten minutes without a single message that night are rare, less than 2%. It is e−4e^{-4}.
  • [1] 0.05113362: eight or more messages in ten minutes happens 5% of the time. The “at least” trick again: P(X≥8)=1−P(X≤7)P(X \geq 8) = 1 – P(X \leq 7).
  • The mass function rises up to 3 and 4, which are equal (0.1954), and then falls. When λ\lambda is a whole number, the Poisson has two modes: λ−1\lambda – 1 and λ\lambda.
  • [1] 4 7: the median is 4 messages, and in 90% of the ten-minute slots 7 or fewer arrive.
Poisson(4): messages in ten minutesBars of the Poisson with mean 4 from 0 to 12. The highest, in coral, are 3 and 4 with 0.1954 each. P of 0 is 0.0183 and P of 8 is 0.0298; the tail keeps falling without reaching zero.0.000.050.100.150.200.250123456789101112Messages in 10 minutesP(X = k)0.01830.02980.1954 eachtwo modes: 3 and 4the tail never reaches 0
What plot(0:12, dpois(0:12, 4), type = "h") draws: with an integer λ there are two tallest bars, at λ − 1 and at λ.

In the diagram (the output of plot(0:12, dpois(0:12, 4), type = "h") in the site colours) you can see the two tallest bars, at 3 and at 4, and the long tail to the right that never quite reaches zero: in theory 50 messages could arrive in ten minutes, although with a ridiculous probability.

λ\lambda depends on the interval. If 4 arrive on average in 10 minutes, 4⋅6=244 \cdot 6 = 24 arrive in an hour and 2 in 5 minutes. Changing the interval means multiplying λ\lambda, not the probability.

The Poisson as an approximation to the binomial

There is a connection that comes up in the problem sheets. In an afternoon 200 messages arrive, and each one has a small probability, p=0.01p = 0.01, of being a voice note longer than two minutes. The number of long voice notes is Binom(200,0.01)\text{Binom}(200, 0.01). When nn is large and pp small, the binomial looks a lot like a Poisson with the same mean, λ=np=2\lambda = np = 2:

k <- 0:6
round(cbind(k, binomial = dbinom(k, 200, 0.01), poisson = dpois(k, 2)), 4)

Output:

     k binomial poisson
[1,] 0   0.1340  0.1353
[2,] 1   0.2707  0.2707
[3,] 2   0.2720  0.2707
[4,] 3   0.1814  0.1804
[5,] 4   0.0902  0.0902
[6,] 5   0.0357  0.0361
[7,] 6   0.0117  0.0120

The two columns are hard to tell apart: the biggest difference, at k=2k = 2, is 0.2720−0.2707=0.00130.2720 – 0.2707 = 0.0013. The textbook rule of thumb is n≥50n \geq 50 and np≤5np \leq 5. Before computers it was the way to avoid calculating (200k)\binom{200}{k} by hand; today it mostly helps you understand why the Poisson shows up in so many places: many chances, each with a small probability.

The binomial with large n and small p looks like a PoissonPairs of bars from 0 to 6: binomial with n 200 and p 0.01 in violet and Poisson with mean 2 in coral. The pairs are almost equal; the largest difference is 0.0013 at k equal to 2.0.000.100.200.300123456Long voice notes in 200 messagesP(X = k)Binom(200, 0.01)Pois(2)
Same mean (np = 2) and almost the same shape: that is why the Poisson shows up where there are many chances and each one is rare.

In the diagram, each pair of bars is one value of kk: violet is the binomial and coral, the Poisson. You can barely tell them apart. If pp is large, as in “98% of messages are sent correctly”, you do not approximate the variable directly: you count the rare thing (the ones that fail, with p=0.02p = 0.02), which is Poisson.

R’s d, p, q and r functions

All the models use the same four letters in front of their name in R:

LetterWhat it returnsBinomialGeometricNegative binomialPoisson
dP(X=k)P(X = k)dbinom(k, n, p)dgeom(k, p)dnbinom(k, r, p)dpois(k, lambda)
pP(X≤k)P(X \leq k)pbinom(k, n, p)pgeom(k, p)pnbinom(k, r, p)ppois(k, lambda)
qsmallest kk with F(k)≥F(k) \geq probqbinom(prob, n, p)qgeom(prob, p)qnbinom(prob, r, p)qpois(prob, lambda)
rrandom valuesrbinom(m, n, p)rgeom(m, p)rnbinom(m, r, p)rpois(m, lambda)

And the four translations used most:

You are asked for…Write
P(X=k)P(X = k)d…(k, …)
P(X≤k)P(X \leq k)p…(k, …)
P(X≥k)P(X \geq k)1 - p…(k - 1, …)
P(a≤X≤b)P(a \leq X \leq b)p…(b, …) - p…(a - 1, …)
R's four letters on the same binomialFour panels with the binomial with n 10 and p 0.3. d marks only the bar at 3; p marks the bars from 0 to 4; 1 minus p of 4 marks those from 5 to 10; q is the step function F cut by a line at 0.9, which crosses it at k equal to 5.dbinom(3, 10, 0.3)P(X = 3) = 0.2668012345678910pbinom(4, 10, 0.3)P(X ≤ 4) = 0.84970123456789101 – pbinom(4, 10, 0.3)P(X ≥ 5) = 0.1503012345678910qbinom(0.9, 10, 0.3)first k with F(k) ≥ 0.9: 50123456789100.9
d is one bar; p, everything to the left; 1 − p(k − 1), everything to the right; q goes the other way: from a height to a value.

The diagram puts all four on the same binomial: d is one bar, p is everything to the left including the bar, 1 - p(k − 1) is everything to the right including the bar, and q goes the other way: you give it a height on the staircase and it returns the value of kk.

1 - pbinom(5, 10, 0.3) is P(X>5)=P(X≥6)P(X > 5) = P(X \geq 6), not P(X≥5)P(X \geq 5). In a discrete variable, “greater than or equal to 5” leaves out everything up to 4, and that is why you subtract pbinom(4, …). It is the most repeated mistake of the topic in exams (and the one I made the first time I checked my bet).

How to choose the model

In the exam nobody tells you “this is a negative binomial”. They describe a situation and you have to recognise it. These are the questions, in order:

How to choose the discrete modelFour questions in order. Fixed number of independent trials counting successes: binomial. Repeat until r successes counting failures: geometric or negative binomial. Events at an average rate in an interval: Poisson. Equally likely values: uniform.Fixed number of independent trials?you count successesyesBinom(n, p)noRepeat until you get r successes?you count failures beforeyesgeom(p) · NBinom(r, p)noEvents at an average rate in an interval?no trialsyesPois(λ)noAll values equally likely?from 1 to nyesUniform
Ask the questions in this order: the first one you answer "yes" gives you the model.
  1. Is there a fixed number of independent trials, and do you count successes? Binomial (and if it is a single trial, Bernoulli).
  2. Do you repeat until you get successes, and count the failures before? Up to the first, geometric; up to the rr-th, negative binomial.
  3. Do you count events arriving at an average rate in an interval, with no trials? Poisson.
  4. Are all the values in a range equally likely? Discrete uniform.
Situation in the groupVariableModel
One message, is it a meme?1 if meme, 0 if notBer(0.3)\text{Ber}(0.3)
Out of 10 messages, how many memes?Number of memesBinom(10,0.3)\text{Binom}(10, 0.3)
How many useless ones before the first useful one?Failures before the 1st successgeom(0.2)\text{geom}(0.2)
How many useless ones before the third useful one?Failures before the 3rd successNBinom(3,0.2)\text{NBinom}(3, 0.2)
How many messages in 10 minutes?Arrivals in an intervalPois(4)\text{Pois}(4)
Die to choose who presentsNumber from 1 to 6Uniform on {1,…,6}\{1, \ldots, 6\}

Look for the key word in the question. “Out of nn…” or “in nn trials” is usually binomial. “Until…” is geometric or negative binomial. “Per hour”, “per minute”, “per page”, “per metre” is Poisson.

What is proved in this topic

Here you have seen the results. In the proofs for this topic there is, step by step, why they are true, with the same WhatsApp group:

ResultProof
That a function is a mass function, its mgf and its mean (the 15-point exam exercise)Proof 1
φ(k)(0)=E(Xk)\varphi^{(k)}(0) = E(X^k) and Var(X)=E(X2)−[E(X)]2\text{Var}(X) = E(X^2) – [E(X)]^2Proof 2
The binomial adds up to 1, its mgf, mean and varianceProof 3
The geometric adds up to 1, its closed-form FF and memorylessnessProof 4
The Poisson adds up to 1, its mgf and mean equal to varianceProof 5
E(X)=∑n≥1P(X≥n)E(X) = \sum_{n \geq 1} P(X \geq n)Proof 6

Common mistakes

Confusing the mass function with the distribution function. P(X=2)P(X = 2) is one bar; F(2)=P(X≤2)F(2) = P(X \leq 2) is everything up to and including that bar. In R, dbinom versus pbinom.

“At least 5” with 1 - pbinom(5, …). It is 1 - pbinom(4, …). In discrete variables the boundary matters.

Counting trials instead of failures in the geometric and the negative binomial. R counts failures: if the first success arrives on trial 3, the value of the variable is 2.

Changing the interval in the Poisson without changing λ\lambda. If you are given 4 messages every 10 minutes and asked about an hour, λ=24\lambda = 24.

Using the binomial when the trials are not independent or pp changes from one to the next (for example, drawing messages from a closed list without putting them back). Before writing dbinom, check: fixed number of trials, two outcomes, constant pp, independence.

Differentiating the mgf and evaluating at t=1t = 1. The moments come out at t=0t = 0, where e0=1e^{0} = 1 makes everything simplify.

Try it in the R playground

Open the site’s R playground (or RStudio) and paste this script. It puts together the “is there class tomorrow?” example and the meme bet:

# A discrete variable by hand
x <- 0:3
p <- c(0.4, 0.3, 0.2, 0.1)
mean_x <- sum(x * p)
variance <- sum(x^2 * p) - mean_x^2
c(mean = mean_x, variance = variance)
plot(x, cumsum(p), type = "s", ylim = c(0, 1),
     xlab = "Questions per day", ylab = "F(x)")

# The bet: at least 5 memes in 10 messages
1 - pbinom(4, 10, 0.3)
plot(0:10, dbinom(0:10, 10, 0.3), type = "h", lwd = 4,
     xlab = "Memes in 10 messages", ylab = "P(X = k)")

What to look at:

  1. In the Environment panel, x, p, mean_x and variance: check that mean_x is 1 and variance too.
  2. The first plot is the staircase of FF. Change type = "s" to type = "h" and you will see the cumulative mass function as bars.
  3. In the second plot, change 0.3 to 0.5: the binomial becomes symmetric around 5.
  4. Type ?dbinom in the console and read the Help tab: the four functions are there together, with the order of their arguments.
  5. Try rbinom(5, 10, 0.3): five simulated batches of ten messages. Every time you run it you will get different ones.

Summary

ConceptFormulaIn R
Mass functionP(X=xi)P(X = x_i), add up to 1d…
Distribution functionF(x)=P(X≤x)F(x) = P(X \leq x)p…, cumsum(p)
Expectation∑xiP(X=xi)\sum x_i P(X = x_i)sum(x * p)
VarianceE(X2)−[E(X)]2E(X^2) – [E(X)]^2sum(x^2 * p) - mean^2
mgfE(etX)E(e^{tX}); φ(k)(0)=E(Xk)\varphi^{(k)}(0) = E(X^k)—
Binomial(nk)pk(1−p)n−k\binom{n}{k}p^k(1-p)^{n-k}; mean npnpdbinom
Geometric(1−p)kp(1-p)^k p; mean (1−p)/p(1-p)/pdgeom
Negative binomial(k+r−1k)pr(1−p)k\binom{k+r-1}{k}p^r(1-p)^k; mean r(1−p)/pr(1-p)/pdnbinom
Poissone−λλk/k!e^{-\lambda}\lambda^k/k!; mean = variance = λ\lambdadpois

Three ideas to take away:

  1. A random variable turns outcomes into numbers; its distribution (values and probabilities) is what you study. Mass and distribution are two sides of the same thing: the jumps of FF are the bars of the mass function.
  2. Mean and variance are calculated as in descriptive statistics, but with probabilities. The mgf is a shortcut: differentiate and evaluate at 0.
  3. Before touching R, recognise the model and translate the question into “less than or equal to”. The rest is writing the right letter, d, p or q.

Next comes seeing why all of this is true in the proofs for this topic. After that, in the R practice, we simulate the WhatsApp group and check the formulas with thousands of batches. And to check you have got it, the discrete random variables exercises go from easy to hard and come with commented solutions, in exam format. In topic 4 we move on to continuous variables, where sums turn into integrals.

Next step: proofsDiscrete random variables — step-by-step proofs with the WhatsApp group, series that add themselves up and the mgf differentiated at zero (and the mean I left half done)

Did this article help you? Here is how you can support SergioLearns →