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.
Table of Contents
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 are. From topic 1, the mean of a frequency table. And from calculus, how to differentiate and how to read a 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 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 , you look for the outcomes that give 2 and add up their probabilities. Now, the definition.
A random variable is a function that assigns a real number to each outcome of a random experiment: . It is written with a capital letter () and its specific values with lower case ().
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 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 has its own probability.
If memes and non-memes really were equally likely (50%, like a coin), each sequence would have probability and we could count cases:
| Outcomes giving that value | ||
|---|---|---|
| 0 | NNN | 1/8 |
| 1 | MNN, NMN, NNM | 3/8 |
| 2 | MMN, MNM, NMM | 3/8 |
| 3 | MMM | 1/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, = number of messages that are not memes, or = memes minus non-memes. All three live on the same ; what changes is the rule that puts a number on each outcome.
Check you’ve got it: with memes at 50%, which values does = 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 . The three outcomes with two memes (MMN, MNM, NMM) give . The three with one meme give . And NNN gives . Counting outcomes as in the table for : , , and . The same probabilities as , but placed on other values. That’s no coincidence: since non-memes , it turns out that , and each value of goes to one of .
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 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:
| Sequences | Probability of each one | Why | ||
|---|---|---|---|---|
| 0 | NNN | Three non-memes; the messages are independent, so you multiply | ||
| 1 | MNN, NMN, NNM | All three have one meme and two non-memes: same probability, and they add up | ||
| 2 | MMN, MNM, NMM | Same as the row above, with the roles swapped | ||
| 3 | MMM | Three memes in a row: very rare |
The four values add up to , 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 , 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 . Experience says this:
| 0 | 1 | 2 | 3 | |
|---|---|---|---|---|
| 0.4 | 0.3 | 0.2 | 0.1 |
The mass function
The probability mass function (or probability function) of a discrete variable is
for each value the variable takes. It has to satisfy two conditions, and the exam will ask you to check them many times:
- for every : no probability is negative.
- : between them, the values share out all the probability.
In our table, . Correct. Notice that the mass function is only defined at the values the variable takes: it makes no sense to ask for ; 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 , 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:
Unlike the mass function, is defined for any real number , not just for 0, 1, 2 and 3. Let’s go stretch by stretch:
- If : no value of is less than or equal to , so .
- If : only 0 counts, .
- If : 0 and 1 count, .
- If : .
- If : everything counts, .
| Stretch | |||||
|---|---|---|---|---|---|
| 0 | 0.4 | 0.7 | 0.9 | 1 |
In the diagram, the bars of the mass function are at the top and the staircase of is underneath. Each step is exactly as tall as the bar above it: at the staircase rises by 0.3, which is . 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 the function is already 0.7 (the upper value), because includes 1 itself. We say that is right-continuous.
The properties of any distribution function:
- .
- and .
- is non-decreasing: it never goes down, it only stays level or goes up.
- 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. is a probability, that of , so it cannot go outside that range.
- Starts at 0 and ends at 1. Far to the left (at , for example) there is no value of 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 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 , and in the “is there class tomorrow?” example?
: includes 0 and 1, but not 2, so . You are in the stretch . : exactly at 2 the step has already gone up, because includes 2; on the other hand, is still 0.7. And : 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… | Calculation | In the example |
|---|---|---|
| from | : the jump at | |
Mixing up and . In a discrete variable it matters: , but . Before using , 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 measures, which is “everything to the left, including the value itself”. With our variable, which takes 0, 1, 2 and 3:
| You are asked | Values included | Calculation | Why |
|---|---|---|---|
| 0 and 1 | It is exactly what accumulates | ||
| 2 and 3 | They are the ones not in : the complement | ||
| 2 and 3 | The same values as the row above, so the same calculation | ||
| only 2 | Everything up to 2, minus everything up to 1: the bar at 2 is left | ||
| 1 and 2 | Everything up to 2, minus what there is up to 0 included | ||
| 1 and 2 | With 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, , what there is just before , is : that’s why the jump at 2 is calculated as .
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 . R vectors start at position 1, so F[1] is , F[2] is … When the values of 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:
In the example:
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 day | Days | Total questions | Why |
|---|---|---|---|
| 0 | 40 | 0.4 of 100 days | |
| 1 | 30 | 0.3 of 100 days, one question each | |
| 2 | 20 | 0.2 of 100 days, two questions each | |
| 3 | 10 | 0.1 of 100 days, three questions each | |
| Total | 100 | 100 |
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 . 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 is the number of memes in three messages with probability 1/2, : 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, , is calculated in the same way, applying the function to each value:
For example, .
The variance
The variance measures how far the values are from the mean, again weighted by their probability:
Its square root, , 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:
In the example: and . Check with the definition: . It matches.
Calculating and forgetting to subtract . If in the exam you get a variance bigger than or a negative variance, you have mixed up the pieces: the variance is never negative and never exceeds .
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 :
| Why | |||||
|---|---|---|---|---|---|
| 0 | 0.4 | 0.4 | 0 | 0 is one unit away from the mean | |
| 1 | 0.3 | 0 | 0.3 | 1 is the mean: it adds nothing to the variance | |
| 2 | 0.2 | 0.2 | 0.8 | One unit away, like 0, but with less weight | |
| 3 | 0.1 | 0.4 | 0.9 | Two units away: the square turns it into 4 | |
| Sum | 1 | 1 | 2 |
The fourth column adds up to the variance by the definition, 1. The fifth adds up to , and with the shortcut formula . 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%, . What is ?
With the shortcut formula. First . Then you subtract the square of the mean: . If you get 1.5, you subtracted the mean without squaring it.
The properties used most
If and are constants:
| Property | Why it makes sense |
|---|---|
| , | A constant does not vary |
| If you multiply all the values by and add , the mean does the same | |
| Adding shifts everything without spreading it; multiplying by spreads by , and the variance is squared | |
| 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 . Its mean is and its variance . The 2 does not appear in the variance: shifting all the values does not spread them.
Writing . Both parts are wrong: the constant that multiplies comes out squared and the one that adds disappears.
Moments, skewness and kurtosis
is called the moment of order (about the origin) and , the moment of order 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:
The skewness coefficient 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 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 is given instead, and then it is compared with 0.
In the “is there class tomorrow?” example, : 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,
defined for the in an interval around 0 where that sum exists. In the example:
What is such a strange function for? It is in its name: it generates the moments. If you differentiate it times and evaluate it at , you get :
Let’s check it in the example:
- , so .
- , so .
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 with respect to is : each time you differentiate, an comes down as a factor. And at , wipes out all the exponentials. Term by term, in the example:
| Term of | Derivative | At | Why |
|---|---|---|---|
| It is : the factor that comes down is the value 0 | |||
| The value 1 comes down | |||
| The value 2 comes down | |||
| The value 3 comes down |
The column is, row by row, , and its sum is the expectation. If you differentiate again, a second comes down and you get : 0, 0.3, 0.8 and 0.9, which add up to . With derivatives, . 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:
| Series | Value | Where it appears |
|---|---|---|
| if | Geometric and negative binomial | |
| Poisson |
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). = the number that comes up. Each value from 1 to has the same probability:
With : and .
Bernoulli
A single message: is it a meme or not? if it is a meme (success) and if not (failure), with . It is written .
With : and . 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 , independent of each other. = number of memes. It is a binomial with parameters and : .
Where does it come from? A specific sequence with memes and non-memes, for example MMMNNNNNNN, has probability (we multiply because they are independent). And there are sequences with memes, as many as ways to choose which positions they go in, using the combinations from topic 2.
With and : memes on average and . And look at the mgf: it is the Bernoulli’s raised to the power . That is no coincidence: a binomial is the sum of 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 insidepbinom: “at least 5” is “not at most 4”.- The row of eleven numbers is the whole mass function, from to . 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 with ( does not reach it; does).mean 3.0,variance 2.1: the values from the formulas and .
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 ; with it would be symmetric and with 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. = number of useless messages you read before the first useful one. It is a geometric with parameter : .
For the first useful one to arrive after useless ones there must be failures in a row, , and then a success, . There are no combinations because there is only one possible order: all the failures first.
With : 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 () and the one that counts trials up to and including the first success (, with and mean ). 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: . 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)? = number of useless messages you read before collecting the third useful one. It is a negative binomial with parameters and : .
Why those combinations? The last message read must be the third useful one (otherwise you would have stopped earlier). Before it there are useless ones and useful ones mixed in any order: ways of arranging them.
With and : 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 independent geometrics (waiting for the first useful one, then the second, then the third). With it is the geometric.
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 (the mean for the interval): .
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 .[1] 0.05113362: eight or more messages in ten minutes happens 5% of the time. The “at least” trick again: .- The mass function rises up to 3 and 4, which are equal (0.1954), and then falls. When is a whole number, the Poisson has two modes: and .
[1] 4 7: the median is 4 messages, and in 90% of the ten-minute slots 7 or fewer arrive.
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.
depends on the interval. If 4 arrive on average in 10 minutes, arrive in an hour and 2 in 5 minutes. Changing the interval means multiplying , 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, , of being a voice note longer than two minutes. The number of long voice notes is . When is large and small, the binomial looks a lot like a Poisson with the same mean, :
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 , is . The textbook rule of thumb is and . Before computers it was the way to avoid calculating by hand; today it mostly helps you understand why the Poisson shows up in so many places: many chances, each with a small probability.
In the diagram, each pair of bars is one value of : violet is the binomial and coral, the Poisson. You can barely tell them apart. If 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 ), 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:
| Letter | What it returns | Binomial | Geometric | Negative binomial | Poisson |
|---|---|---|---|---|---|
d | dbinom(k, n, p) | dgeom(k, p) | dnbinom(k, r, p) | dpois(k, lambda) | |
p | pbinom(k, n, p) | pgeom(k, p) | pnbinom(k, r, p) | ppois(k, lambda) | |
q | smallest with prob | qbinom(prob, n, p) | qgeom(prob, p) | qnbinom(prob, r, p) | qpois(prob, lambda) |
r | random values | rbinom(m, n, p) | rgeom(m, p) | rnbinom(m, r, p) | rpois(m, lambda) |
And the four translations used most:
| You are asked for… | Write |
|---|---|
d…(k, …) | |
p…(k, …) | |
1 - p…(k - 1, …) | |
p…(b, …) - p…(a - 1, …) |
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 .
1 - pbinom(5, 10, 0.3) is , not . 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:
- Is there a fixed number of independent trials, and do you count successes? Binomial (and if it is a single trial, Bernoulli).
- Do you repeat until you get successes, and count the failures before? Up to the first, geometric; up to the -th, negative binomial.
- Do you count events arriving at an average rate in an interval, with no trials? Poisson.
- Are all the values in a range equally likely? Discrete uniform.
| Situation in the group | Variable | Model |
|---|---|---|
| One message, is it a meme? | 1 if meme, 0 if not | |
| Out of 10 messages, how many memes? | Number of memes | |
| How many useless ones before the first useful one? | Failures before the 1st success | |
| How many useless ones before the third useful one? | Failures before the 3rd success | |
| How many messages in 10 minutes? | Arrivals in an interval | |
| Die to choose who presents | Number from 1 to 6 | Uniform on |
Look for the key word in the question. “Out of …” or “in 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:
| Result | Proof |
|---|---|
| That a function is a mass function, its mgf and its mean (the 15-point exam exercise) | Proof 1 |
| and | Proof 2 |
| The binomial adds up to 1, its mgf, mean and variance | Proof 3 |
| The geometric adds up to 1, its closed-form and memorylessness | Proof 4 |
| The Poisson adds up to 1, its mgf and mean equal to variance | Proof 5 |
| Proof 6 |
Common mistakes
Confusing the mass function with the distribution function. is one bar; 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 . If you are given 4 messages every 10 minutes and asked about an hour, .
Using the binomial when the trials are not independent or 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 , independence.
Differentiating the mgf and evaluating at . The moments come out at , where 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:
- In the Environment panel,
x,p,mean_xandvariance: check thatmean_xis 1 andvariancetoo. - The first plot is the staircase of . Change
type = "s"totype = "h"and you will see the cumulative mass function as bars. - In the second plot, change 0.3 to 0.5: the binomial becomes symmetric around 5.
- Type
?dbinomin the console and read the Help tab: the four functions are there together, with the order of their arguments. - Try
rbinom(5, 10, 0.3): five simulated batches of ten messages. Every time you run it you will get different ones.
Summary
| Concept | Formula | In R |
|---|---|---|
| Mass function | , add up to 1 | d… |
| Distribution function | p…, cumsum(p) | |
| Expectation | sum(x * p) | |
| Variance | sum(x^2 * p) - mean^2 | |
| mgf | ; | — |
| Binomial | ; mean | dbinom |
| Geometric | ; mean | dgeom |
| Negative binomial | ; mean | dnbinom |
| Poisson | ; mean = variance = | dpois |
Three ideas to take away:
- 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 are the bars of the mass function.
- Mean and variance are calculated as in descriptive statistics, but with probabilities. The mgf is a shortcut: differentiate and evaluate at 0.
- Before touching R, recognise the model and translate the question into “less than or equal to”. The rest is writing the right letter,
d,porq.
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 →



