Discrete random variables — exam-style exercises with the WhatsApp group, models you have to recognise and a mass function with a parameter (and the 1 – pbinom(5) that cost me a point)

In the first mock of the continuous assessment I wrote 1 - pbinom(5, 20, 0.15) for “at least five” and got 0.07. The correct result was 0.17. One number off inside the brackets cost me the whole part, because the following parts used that result. Since then, before writing any p… I ask myself out loud: “which value am I leaving out?”. These exercises are designed so that kind of slip happens to you here and not in the exam.

These are exam-style exercises on the discrete random variables theory, with the marks for each part as in ME1’s first continuous assessment: one context per exercise, parts that build on the previous one, hand calculations, a proof and R. If you get stuck on the proof, review the proofs for this topic; if you get stuck on the code, the R practice.

How to use this article

  1. Do the exercises with the exam in mind. In ME1’s EC1 you have 4 hours for 50 points, with RStudio and the data open. These three exercises add up to 35 points plus a 2.5-point bonus: give them about 2 hours and 45 minutes.
  2. Paper for what you prove, R for what you calculate. Calculations with 2 decimals, as the exam requires. Whatever you do in R, keep it in a script: at the end of the exam you have to upload the .R file with the code for each exercise.
  3. Mark yourself with the criteria. Each solution comes with “How it is marked” and a breakdown of the points. It is a guide of mine, not the official marking scheme.
  4. Finish with the cheat sheet and “For your script”. The cheat sheet only has results (it is what you can take into the exam handwritten: one sheet, both sides, in EC1) and “For your script” is the topic’s code ready for your .R file.

Exercise 1: Marcos’s voice notes (10 points)

Marcos sends voice notes to the group. After a month of counting, the number of voice notes he sends in a day, XX, has this distribution function:

xx0124
F(x)=P(X≤x)F(x) = P(X \leq x)0.350.650.851

(He never sends 3: either two or he gets carried away and sends four.)

(a) Find the probability mass function of XX. (2 points)

(b) Calculate P(X>1)P(X > 1) and P(1≤X<4)P(1 \leq X < 4). (2 points)

(c) With R, calculate the mean, the variance and the standard deviation of XX, and plot its distribution function. (3 points)

(d) Find the moment generating function of XX and, by differentiating it, check E(X)E(X) and calculate E(X2)E(X^2). (3 points)

The mass at each value is the jump of FF at that value. And for (b), translate each probability into specific values: which values of XX satisfy 1≤X<41 \leq X < 4?

Exercise 2: questions about the assignment (10 points)

15% of the group’s messages are questions about the ME1 assignment. We assume each message is a question or not independently of the others.

(a) Let XX be the number of questions among the next 20 messages. Identify its distribution and calculate, with R, P(X=3)P(X = 3) and the probability that there are at least 5 questions. Give its mean and its variance. (3 points)

(b) Let YY be the number of messages that are not questions before the first question. Identify its distribution and calculate the probability that there are more than 6, and its mean. (2 points)

(c) Let ZZ be the number of messages that are not questions before the third question arrives. Calculate P(Z≤10)P(Z \leq 10) and the value that ZZ does not exceed with probability 0.9. (2 points)

(d) On average 6 questions arrive per hour. If the number of questions in an hour, WW, follows a Poisson, calculate P(4≤W≤8)P(4 \leq W \leq 8), plot its mass function with R and say, from the plot, where the mode is. (3 points)

In (b) and (c), R counts the messages that are not questions, which is exactly what is asked. In (d), “from the plot” means the marker wants you to look at the bars and say it, not calculate it with a formula.

Exercise 3: conversations that get started (15 points)

When someone in the group starts a private conversation with someone else, the number of messages exchanged, XX, is at least 1 (if there are no messages, there is no conversation). The following model is proposed:

P(X=x)=θxx! (eθ−1),x=1,2,3,… P(X = x) = \frac{\theta^x}{x!\,(e^\theta – 1)}, \quad x = 1, 2, 3, \ldots

with θ>0\theta > 0.

(a) Prove that P(X=x)P(X = x) defines a probability mass function for every θ>0\theta > 0. (5 points)

(b) Find its moment generating function. (5 points)

(c) Calculate E(X)E(X). With θ=2\theta = 2, give its value and check it with R by summing the series. (5 points)

It looks a lot like the Poisson. Which term of the exponential series is missing when the sum starts at x=1x = 1?

Bonus (2.5 points)

Let X∼Pois(λ)X \sim \text{Pois}(\lambda) be the number of messages in a slot. Using only the moment generating function of XX, calculate E(2X)E(2^X). Give its value with λ=4\lambda = 4.

Solutions

Exercise 1

(a) The mass at each value is the jump of FF:

xx0124
P(X=x)P(X = x)0.350.350.65−0.35=0.300.65 – 0.35 = 0.300.85−0.65=0.200.85 – 0.65 = 0.201−0.85=0.151 – 0.85 = 0.15

Check: 0.35+0.30+0.20+0.15=10.35 + 0.30 + 0.20 + 0.15 = 1.

Why the jumps and not the table as it is. The temptation is to read the table as if it were the mass: “P(X=1)=0.65 P(X = 1) = 0.65 “. But F(1) F(1) is P(X≤1) P(X \leq 1) : it accumulates 0 and 1. What 1 contributes on its own is how much F F rises as it passes it. The check is not a formality: copying F F , you would get a sum of 2.85, and the mistake jumps out.

(b) P(X>1)=1−F(1)=1−0.65=0.35P(X > 1) = 1 – F(1) = 1 – 0.65 = 0.35. For the second, the values with 1≤X<41 \leq X < 4 are 1 and 2 (4 is left out by the << and 3 does not exist): P(1≤X<4)=0.30+0.20=0.50P(1 \leq X < 4) = 0.30 + 0.20 = 0.50. With FF: F(2)−F(0)=0.85−0.35=0.50F(2) – F(0) = 0.85 – 0.35 = 0.50.

The set-up before the calculation. With < < and ≤ \leq mixed, the safe move is not to touch F F until you have said which values are in. The obvious route, F(4)−F(1) F(4) – F(1) , computes P(1<X≤4) P(1 < X \leq 4) : it drops 1, which is in, and adds 4, which is not. It gives 0.35, a number that looks reasonable and is wrong.

Check you’ve got it: what are P(X≥2) P(X \geq 2) and P(X>1.5) P(X > 1.5) ?

Both are 0.35, the same as P(X>1) P(X > 1) . X X only takes 0, 1, 2 and 4, so “greater than 1”, “greater than 1.5” and “at least 2” are the same event, X∈{2,4} X \in \{2, 4\} : 0.20+0.15=0.35 0.20 + 0.15 = 0.35 .

(c) In R:

x <- c(0, 1, 2, 4)
F <- c(0.35, 0.65, 0.85, 1)
p <- diff(c(0, F))                  # jumps of F: the mass function
p
mean_x <- sum(x * p)
variance <- sum(x^2 * p) - mean_x^2
round(c(mean = mean_x, variance = variance, sd = sqrt(variance)), 2)
plot(c(-1, x, 5), c(0, F, 1), type = "s", xlab = "Voice notes per day",
     ylab = "F(x)", xaxt = "n")
axis(1, at = c(-1, x, 5))
points(x, F, pch = 19)

Output:

[1] 0.35 0.30 0.20 0.15
    mean variance       sd 
    1.30     1.81     1.35 

diff(c(0, F)) does the subtractions from (a) in one go: it puts a 0 in front (FF before the first value) and subtracts each term from the next. The mean is 0⋅0.35+1⋅0.30+2⋅0.20+4⋅0.15=1.30 \cdot 0.35 + 1 \cdot 0.30 + 2 \cdot 0.20 + 4 \cdot 0.15 = 1.3 voice notes a day; E(X2)=0.30+0.80+2.40=3.5E(X^2) = 0.30 + 0.80 + 2.40 = 3.5 and Var(X)=3.5−1.69=1.81\text{Var}(X) = 3.5 – 1.69 = 1.81, with standard deviation 1.35.

Why diff(c(0, F)) and not diff(F). diff(F) returns only three numbers (0.30, 0.20 and 0.15): the first value has nothing to be subtracted from, and the mass at 0, the largest one, is lost. The leading 0 plays the role of “F F before the first value”. And the variance as E(X2)−E(X)2 E(X^2) – E(X)^2 is for convenience: ∑(xi−μ)2pi \sum (x_i – \mu)^2 p_i also gives 1.81, but this way you reuse the E(X2) E(X^2) from (d).

plot(type = "s"): the distribution function of Marcos's voice notesStep function F: 0 before 0; 0.35 from 0; 0.65 from 1; 0.85 from 2 up to 4, flat at 3; and 1 from 4. Black dots at the top of each jump.0.000.250.500.751.00-1012345Voice notes per dayF(x)0.350.650.851.00flat: never sends 3
Redrawn in the site colours: one step per value of X, and a flat stretch where there is no probability.

The plot is the staircase drawn by plot(…, type = "s"). Read it like this: there is a step at each value XX takes, as tall as its probability; between 2 and 4 the staircase is flat, because Marcos never sends 3 voice notes; and the step at 4 is the last one, after which F=1F = 1. The black dots mark that the value of FF at each jump is the upper one.

(d) φX(t)=E(etX)=0.35+0.30 et+0.20 e2t+0.15 e4t\varphi_X(t) = E(e^{tX}) = 0.35 + 0.30\,e^{t} + 0.20\,e^{2t} + 0.15\,e^{4t}, which exists for every tt (it is a finite sum).

φX′(t)=0.30 et+0.40 e2t+0.60 e4t⇒E(X)=φX′(0)=0.30+0.40+0.60=1.3 \varphi_X'(t) = 0.30\,e^{t} + 0.40\,e^{2t} + 0.60\,e^{4t} \quad \Rightarrow \quad E(X) = \varphi_X'(0) = 0.30 + 0.40 + 0.60 = 1.3
φX′′(t)=0.30 et+0.80 e2t+2.40 e4t⇒E(X2)=φX′′(0)=3.5 \varphi_X''(t) = 0.30\,e^{t} + 0.80\,e^{2t} + 2.40\,e^{4t} \quad \Rightarrow \quad E(X^2) = \varphi_X''(0) = 3.5

They match (c).

Why differentiating at 0 gives the mean. Each term of the mgf is pi etxi p_i\,e^{t x_i} . Differentiating brings the exponent down, xi pi etxi x_i\,p_i\,e^{t x_i} , and at t=0 t = 0 the exponential is 1: you are left with ∑xipi \sum x_i p_i , the mean from (c). The second derivative brings down another xi x_i and gives ∑xi2pi \sum x_i^2 p_i . That is why they match: it is the same sum written another way.

How it is marked (a guide, not official): (a) 2 points if all four probabilities are there (0.5 each); (b) 1 + 1; (c) 1 for the mean, 1 for variance and standard deviation and 1 for the plot with the jumps in the right place; (d) 1 for the mgf, 1 for E(X)E(X) and 1 for E(X2)E(X^2). A variance without subtracting E(X)2E(X)^2 loses that point even if the code runs.

Exercise 2

(a) Twenty messages, each a question with probability 0.15, independent, and we count questions: X∼Binom(20,0.15)X \sim \text{Binom}(20, 0.15). Mean np=3np = 3 and variance np(1−p)=20⋅0.15⋅0.85=2.55np(1-p) = 20 \cdot 0.15 \cdot 0.85 = 2.55.

(b) We count failures (non-questions) before the first success: Y∼geom(0.15)Y \sim \text{geom}(0.15). “More than 6” is Y≥7Y \geq 7, and by the geometric tail P(Y≥7)=0.857P(Y \geq 7) = 0.85^7. Mean 1−pp=0.850.15=5.67\frac{1-p}{p} = \frac{0.85}{0.15} = 5.67.

(c) Failures before the third success: Z∼NBinom(3,0.15)Z \sim \text{NBinom}(3, 0.15).

(d) W∼Pois(6)W \sim \text{Pois}(6).

How to decide the model before touching R. The four variables share the group and the probability, 0.15. What changes is what is counted and when you stop:

VariableWhat it countsWhen it stopsModel
X X questionsafter 20 messages, fixedBinomial
Y Y messages that are not questionsat the first questionGeometric
Z Z messages that are not questionsat the third questionNegative binomial
W W questionswhen the hour endsPoisson

The Poisson is the only one with no number of trials: the data is a rate, 6 per hour. And in (b), the tail formula reads by itself: “more than 6 messages before the first question” is “the first 7 are not questions”, 0.857=0.32 0.85^7 = 0.32 , without adding up infinitely many terms.

# (a)
round(c(P3 = dbinom(3, 20, 0.15), at_least_5 = 1 - pbinom(4, 20, 0.15),
        mean = 20 * 0.15, var = 20 * 0.15 * 0.85), 2)
# (b)
round(c(more_than_6 = 1 - pgeom(6, 0.15), formula = 0.85^7, mean = 0.85 / 0.15), 2)
# (c)
round(pnbinom(10, 3, 0.15), 2)
qnbinom(0.9, 3, 0.15)
# (d)
round(ppois(8, 6) - ppois(3, 6), 2)
round(dpois(4:8, 6), 4)
plot(0:16, dpois(0:16, 6), type = "h", lwd = 4,
     xlab = "Questions in an hour", ylab = "P(W = k)")

Output:

        P3 at_least_5       mean        var 
      0.24       0.17       3.00       2.55 
more_than_6     formula        mean 
       0.32        0.32        5.67 
[1] 0.31
[1] 31
[1] 0.7
[1] 0.1339 0.1606 0.1606 0.1377 0.1033

Reading each number:

  • (a) P(X=3)=0.24P(X = 3) = 0.24: in one batch of twenty messages in four there are exactly three questions, which is the mean. At least five, 0.170.17. Notice the 4: “at least 5” leaves out everything up to 4.
  • (b) More than 6 messages before the first question: 0.320.32, by the formula and by R. On average there are 5.67 messages before someone asks.
  • (c) P(Z≤10)=0.31P(Z \leq 10) = 0.31: only three times in ten are ten messages about other things enough to collect three questions. And in 90% of cases, Z≤31Z \leq 31.
  • (d) P(4≤W≤8)=P(W≤8)−P(W≤3)=0.70P(4 \leq W \leq 8) = P(W \leq 8) – P(W \leq 3) = 0.70.

The quantile in (c), no magic. qnbinom(0.9, 3, 0.15) gives the first z z with P(Z≤z)≥0.9 P(Z \leq z) \geq 0.9 : with 30 it does not get there (0.89) and with 31 it does (0.90). In a discrete variable there is almost never a value that leaves exactly 90% below it, and R gives the first one that reaches it.

And in (d), why ppois(3, 6). 4 is in, so you only remove what lies below it, P(W≤3) P(W \leq 3) .

Check you’ve got it: what would you write in R for P(4<W≤8) P(4 < W \leq 8) , and what does it give?

ppois(8, 6) - ppois(4, 6), which gives 0.56. Now 4 is out, so it is removed along with everything below it. What you lose compared with (d) is the bar at 4, P(W=4)=0.1339 P(W = 4) = 0.1339 in the dpois output.

plot(type = "h"): Poisson(6), questions in an hourBars of the Poisson with mean 6 from 0 to 16. Those at 5 and 6, in coral, are equal and the highest, 0.1606. From 4 to 8 in violet and coral: they add up to 0.70.0.000.050.100.150.20012345678910111213141516Questions in an hourP(W = k)modes 5 and 6: 0.1606violet and coral: 4 ≤ W ≤ 8, 0.70
Redrawn in the site colours: two bars tied at the top, so the mode is double.

In the plot, the two tallest bars are those for 5 and 6 questions, equal (0.1606 both): the mode is double, 5 and 6. That is what happens in the Poisson when λ\lambda is a whole number: P(W=λ)=P(W=λ−1)P(W = \lambda) = P(W = \lambda – 1). That is what you have to write “from the plot”.

How it is marked (a guide, not official): (a) 1 for the model with its parameters, 1 for the two probabilities and 1 for mean and variance; (b) 1 for the model and 1 for the calculations; (c) 1 + 1; (d) 1 for the probability, 1 for the plot and 1 for the double mode. Giving only one mode loses half a point.

Exercise 3

(a) Two things have to be proved.

Non-negative. For θ>0\theta > 0 and x≥1x \geq 1: θx>0\theta^x > 0, x!>0x! > 0 and eθ−1>0e^\theta – 1 > 0 (because eθ>1e^\theta > 1 if θ>0\theta > 0). A quotient of positives is positive: P(X=x)>0P(X = x) > 0.

The plan before the sum. An infinite series is not added term by term: you take out whatever does not depend on x x and look for a series you know. Here what is left is θx/x! \theta^x / x! , the exponential, as the hint suggested. The only difference is where the sum starts, and that is the whole exercise: the eθ−1 e^\theta – 1 in the denominator makes up for the missing x=0 x = 0 term.

Adds up to 1. Step 1: 1eθ−1\frac{1}{e^\theta – 1} does not depend on xx and comes out:

∑x=1∞θxx! (eθ−1)=1eθ−1∑x=1∞θxx! \sum_{x=1}^{\infty} \frac{\theta^x}{x!\,(e^\theta – 1)} = \frac{1}{e^\theta – 1} \sum_{x=1}^{\infty} \frac{\theta^x}{x!}

Step 2: the exponential series starts at x=0x = 0 and equals eθe^\theta; the x=0x = 0 term is θ00!=1\frac{\theta^0}{0!} = 1. So the sum from 1 is eθ−1e^\theta – 1:

∑x=1∞θxx!=eθ−1 \sum_{x=1}^{\infty} \frac{\theta^x}{x!} = e^\theta – 1

Step 3: 1eθ−1⋅(eθ−1)=1\frac{1}{e^\theta – 1} \cdot (e^\theta – 1) = 1. It is a mass function for every θ>0\theta > 0.

(b) Step 1: definition and etxθx=(θet)xe^{tx}\theta^x = (\theta e^t)^x:

φX(t)=∑x=1∞etxθxx! (eθ−1)=1eθ−1∑x=1∞(θet)xx! \varphi_X(t) = \sum_{x=1}^{\infty} e^{tx} \frac{\theta^x}{x!\,(e^\theta – 1)} = \frac{1}{e^\theta – 1} \sum_{x=1}^{\infty} \frac{(\theta e^t)^x}{x!}

Step 2: the same sum as in (a), now with θet\theta e^t instead of θ\theta:

φX(t)=eθet−1eθ−1,for every t∈R \boxed{\varphi_X(t) = \frac{e^{\theta e^t} – 1}{e^\theta – 1}}, \quad \text{for every } t \in \mathbb{R}

Check: φX(0)=eθ−1eθ−1=1\varphi_X(0) = \frac{e^\theta – 1}{e^\theta – 1} = 1.

Why combine etxθx e^{tx}\theta^x into one power. Written as (θet)x (\theta e^t)^x , there is nothing new to add up: it is the series from (a) with θet \theta e^t instead of θ \theta . And since the exponential series converges for any number, it holds for every t t : that is the domain the marking asks for.

(c) Step 1: the denominator is a constant. The derivative of eθete^{\theta e^t} is eθet⋅θete^{\theta e^t} \cdot \theta e^t (chain rule) and that of the −1-1, zero:

φX′(t)=θet eθeteθ−1 \varphi_X'(t) = \frac{\theta e^t \, e^{\theta e^t}}{e^\theta – 1}

Step 2: at t=0t = 0:

E(X)=φX′(0)=θ eθeθ−1 \boxed{E(X) = \varphi_X'(0) = \frac{\theta\,e^\theta}{e^\theta – 1}}

With θ=2\theta = 2: E(X)=2e2e2−1=14.786.39=2.31E(X) = \frac{2e^2}{e^2 – 1} = \frac{14.78}{6.39} = 2.31 messages per conversation.

Another route, as a check. Differentiating the mgf from (b) is the shortest way, but the mean also comes out directly: in ∑x P(X=x) \sum x\,P(X = x) the x x cancels with the factorial, x/x!=1/(x−1)! x / x! = 1/(x-1)! , leaving

E(X)=θeθ−1∑x=1∞θx−1(x−1)!=θ eθeθ−1 E(X) = \frac{\theta}{e^\theta – 1} \sum_{x=1}^{\infty} \frac{\theta^{x-1}}{(x-1)!} = \frac{\theta\,e^\theta}{e^\theta – 1}

because that series, which now starts at x−1=0 x – 1 = 0 , is the full exponential. Two routes, one result.

theta <- 2
x <- 1:60                                       # the tail beyond 60 is negligible
p <- theta^x / (factorial(x) * (exp(theta) - 1))
round(c(sum = sum(p), series_mean = sum(x * p),
        formula_mean = theta * exp(theta) / (exp(theta) - 1)), 2)

Output:

         sum  series_mean formula_mean 
        1.00         2.31         2.31 

The sum is 1 and the mean from summing the series matches the formula. A detail for those who want more: this distribution is a Poisson with the 0 removed (“zero-truncated Poisson”), and that is why its mean, 2.31, is bigger than θ=2\theta = 2: removing the 0 pushes the mean up.

How it is marked (a guide, not official): (a) 1 for non-negativity with its reason (eθ>1e^\theta > 1) and 4 for the sum, 2 of them for saying where eθ−1e^\theta – 1 comes from; (b) 2 for setting up the series with θet\theta e^t, 2 for summing it and 1 for the domain; (c) 3 for the derivative at 0 and 2 for the value and its check in R.

Bonus

2X=eXln⁡22^X = e^{X \ln 2}, so E(2X)=E(e(ln⁡2)X)=φX(ln⁡2)E(2^X) = E\big(e^{(\ln 2) X}\big) = \varphi_X(\ln 2): the mgf evaluated at t=ln⁡2t = \ln 2. With the Poisson’s, φX(t)=eλ(et−1)\varphi_X(t) = e^{\lambda(e^t – 1)}:

E(2X)=eλ(eln⁡2−1)=eλ(2−1)=eλ E(2^X) = e^{\lambda(e^{\ln 2} – 1)} = e^{\lambda(2 – 1)} = e^{\lambda}

With λ=4\lambda = 4, E(2X)=e4=54.60E(2^X) = e^4 = 54.60. Check in R: sum(2^(0:60) * dpois(0:60, 4)) also gives 54.60.

Why not add up the series. ∑2xe−λλx/x! \sum 2^x e^{-\lambda}\lambda^x/x! also works, but the statement asks for “only the mgf”, and the 1.5 points are for seeing the link. The idea: any cX c^X is etX e^{tX} with t=ln⁡c t = \ln c .

Check you’ve got it: with the same X∼Pois(4) X \sim \text{Pois}(4) , what is E(3X) E(3^X) ?

E(3X)=φX(ln⁡3)=e4(3−1)=e8=2980.96 E(3^X) = \varphi_X(\ln 3) = e^{4(3 – 1)} = e^8 = 2980.96 . In general, E(cX)=eλ(c−1) E(c^X) = e^{\lambda(c – 1)} ; with c=2 c = 2 you get the eλ e^{\lambda} above.

How it is marked (a guide): 1.5 for seeing that E(2X)=φX(ln⁡2)E(2^X) = \varphi_X(\ln 2) and 1 for the result.

What’s wrong here?

Three solutions from classmates to exercise 2 and the bonus. Each one has a mistake. Predict which before reading the explanation.

Solution 1. “At least 5 questions in 20 messages”:

1 - pbinom(5, 20, 0.15)

Output:

[1] 0.06730797

pbinom(5, …) is P(X≤5)P(X \leq 5), so 1−P(X≤5)=P(X≥6)1 – P(X \leq 5) = P(X \geq 6). The 5 has been left out. The right answer is 1 - pbinom(4, 20, 0.15), which gives 0.17: more than double.

Before writing 1 - p…(k, …), say the last value you do not want. For “at least 5”, the last one you do not want is 4.

Solution 2. “The first question arrives at message number 4”:

dgeom(4, 0.15)

Output:

[1] 0.07830094

If the first question is message 4, there were 3 non-question messages before it. dgeom counts failures: the right answer is dgeom(3, 0.15) =0.853⋅0.15=0.092= 0.85^3 \cdot 0.15 = 0.092. The classmate has calculated the probability that the first question is message 5.

Solution 3. “If W∼Pois(6)W \sim \text{Pois}(6), each question generates 3 replies and there are 2 fixed messages from a bot per hour, the variance of the messages, 3W+23W + 2, is”:

Var(3W+2)=3⋅Var(W)+2=3⋅6+2=20 \text{Var}(3W + 2) = 3 \cdot \text{Var}(W) + 2 = 3 \cdot 6 + 2 = 20

The constant that multiplies comes out squared and the one that adds disappears: Var(3W+2)=9⋅6=54\text{Var}(3W + 2) = 9 \cdot 6 = 54. Check with R: x <- 0:100; p <- dpois(x, 6); sum((3*x + 2)^2 * p) - sum((3*x + 2) * p)^2 gives 54.

The 5 that is left outBars of the binomial with n 20 and p 0.15 from 0 to 10. The bar at 5, in red, is 0.1028; those from 6 on, in coral, add up to 0.07. With the one at 5, 0.17.0.000.100.200.30012345678910Questions in 20 messagesP(X = k)left out0.1028coral: 1 – pbinom(5) = 0.07red + coral: 1 – pbinom(4) = 0.17
Subtracting pbinom(5) leaves out the bar at 5, the tallest in the tail: that is why the result drops to less than half.

The diagram shows the mistake in solution 1 on the exercise’s binomial: the bar at 5 (0.10) is the one left out with 1 - pbinom(5, …). It is the tallest in that zone, so the mistake is not small.

Practice quiz

Before moving on to the next topic, check you have got it. This quiz is here to consolidate what this article explains: the ME1 exam is not a multiple-choice test (you solve problems by hand and with R), so no mark here counts. There are 10 questions on discrete random variables: key ideas, typical mistakes and many «what does this R code print?», with the outputs from really running it. In the short answers, a decimal point or a decimal comma both work. Answer without running anything, press Check and read the explanation for each option; you can repeat it as many times as you like.

Question 1Single choice

X ~ Bin(20; 0.15). For «at least 5», a classmate writes 1 - pbinom(5, 20, 0.15). What is wrong?

  1. It computes P(X ≥ 6): the 5 is left out. The right one is 1 - pbinom(4, 20, 0.15)
  2. Nothing, it is right
  3. It should use dbinom
  4. It should be pbinom(5, 20, 0.15) without the 1 −
Solution

Answer: It computes P(X ≥ 6): the 5 is left out. The right one is 1 - pbinom(4, 20, 0.15)

Before writing 1 - p…(k, …), say the last value you do not want. For «at least 5», the last one you do not want is 4.

Question 2Single choice

Each message is a question with probability 0.15. «The first question arrives in message 4.» Which R command is right?

  1. dgeom(3, 0.15)
  2. dgeom(4, 0.15)
  3. pgeom(3, 0.15)
  4. dbinom(4, 4, 0.15)
Solution

Answer: dgeom(3, 0.15)

In R, the geometric counts failures (it starts at 0). Always translate «the success comes on attempt k» into «k − 1 failures».

Question 3Short answer

W ~ Pois(6). Each question generates 3 replies and there are 2 fixed messages: what is Var(3W + 2)?

x <- 0:100; p <- dpois(x, 6)
round(sum((3 * x + 2)^2 * p) - sum((3 * x + 2) * p)^2, 2)
Solution

Answer: 54

The multiplying constant comes out squared and the added one disappears: Var(3W + 2) = 3² · Var(W) = 9 · 6 = 54. The typical mistake is 3 · 6 + 2 = 20. The code checks it by summing over the mass function.

Question 4Short answer

The distribution function of X at the values 0, 1, 2 and 4 is F = 0.35; 0.65; 0.85; 1. What does it print? (The probabilities of each value.)

F <- c(0.35, 0.65, 0.85, 1)
diff(c(0, F))
Solution

Answer: 0.35 0.30 0.20 0.15

The mass is F’s jumps: 0.35 − 0 = 0.35; 0.65 − 0.35 = 0.30; 0.85 − 0.65 = 0.20; 1 − 0.85 = 0.15. They add up to 1, as they should.

Question 5Short answer

With that mass function (values 0, 1, 2, 4 and probabilities 0.35, 0.30, 0.20, 0.15), what is the mean?

x <- c(0, 1, 2, 4); p <- c(0.35, 0.30, 0.20, 0.15)
sum(x * p)
Solution

Answer: 1.3

E(X) = Σ x·p = 0 · 0.35 + 1 · 0.30 + 2 · 0.20 + 4 · 0.15 = 0 + 0.30 + 0.40 + 0.60 = 1.30 voice notes a day.

Question 6Match

Match each phrase with its probability in R (discrete X with p… = pbinom, ppois…):

At most k
At least k
More than k
Between a and b, both included

Definitions:

  • p…(k, …)
  • p…(k + 1, …)
  • p…(b, …) − p…(a − 1, …)
  • 1 − p…(k, …)
  • 1 − p…(k − 1, …)
Solution
  • At most k → p…(k, …)
  • At least k → 1 − p…(k − 1, …)
  • More than k → 1 − p…(k, …)
  • Between a and b, both included → p…(b, …) − p…(a − 1, …)

They are the at_least, more_than and between functions in «For your script»: with them you never get the boundary wrong.

Question 7Single choice

Which model fits «number of questions in 20 messages, each a question with probability 0.15 independently»?

  1. Binomial(20; 0.15)
  2. Poisson(0.15)
  3. Geometric(0.15)
  4. Negative binomial(3; 0.15)
Solution

Answer: Binomial(20; 0.15)

Have you stated the model with its parameters and what the variable counts? It is the first question of the cheat sheet’s «Before you hand it in».

Question 8True or false

True or false?

  1. The geometric has no memory.
  2. The distribution function of a discrete variable is a step function and its jumps are the mass.
  3. E(aX + b) = a·E(X).
  4. A binomial with large n and small p looks like a Poisson with parameter n·p.
Solution
  • T — The geometric has no memory.
  • T — The distribution function of a discrete variable is a step function and its jumps are the mass.
  • F — E(aX + b) = a·E(X).
  • T — A binomial with large n and small p looks like a Poisson with parameter n·p.

E(aX + b) = a·E(X) + b: the added constant does count in the mean (not in the variance).

Question 9Short answer

X ~ Pois(4). What is E(2^X)? (Bonus hint: it is the mgf at log 2, φ(t) = exp(λ(e^t − 1)).)

round(exp(4 * (exp(log(2)) - 1)), 2)
Solution

Answer: 54.6

E(c^X) = E(e^{X·ln c}) = φ(ln c). With c = 2: exp(4 · (2 − 1)) = e⁴ = 54.60.

Question 10Single choice

What does modes(0:16, dpois(0:16, 6)) do (the function returns the values where the mass is largest)?

modes <- function(x, p) x[abs(p - max(p)) < 1e-12]
modes(0:16, dpois(0:16, 6))
  1. It returns 5 and 6: a Poisson with integer λ has two modes, λ − 1 and λ
  2. It returns only 6
  3. It returns 6.5
  4. It gives an error because of the tie
Solution

Answer: It returns 5 and 6: a Poisson with integer λ has two modes, λ − 1 and λ

That is why the function compares with a tolerance instead of using which.max, which would only return the first.

Try it in the R playground

Open the site’s R playground (or RStudio) and solve exercise 2 again with different data: 25% questions, 30 messages and 10 questions per hour.

p <- 0.25; n <- 30; lambda <- 10
c(P_at_least_8 = 1 - pbinom(7, n, p), mean = n * p)
c(more_than_6 = 1 - pgeom(6, p), mean = (1 - p) / p)
qnbinom(0.9, 3, p)
plot(0:25, dpois(0:25, lambda), type = "h", lwd = 4)

What to look at:

  1. In Environment, p, n and lambda: change one and run again with Ctrl+Shift+Enter.
  2. With λ=10\lambda = 10, the plot has the double mode at 9 and 10. Change to lambda <- 9.5 and the mode becomes a single one.
  3. Check that 1 - pgeom(6, p) equals (1 - p)^7.
  4. Type ?pnbinom and look for the lower.tail argument: pbinom(7, n, p, lower.tail = FALSE) is another way of writing P(X>7)P(X > 7).

Cheat sheet: discrete random variables

The essentials in 30 seconds. Mass: P(X=xi)≥0P(X = x_i) \geq 0 and they add up to 1. Distribution: F(x)=P(X≤x)F(x) = P(X \leq x), a step function, right-continuous; the jumps are the mass. Mean ∑xipi\sum x_i p_i; variance E(X2)−E(X)2E(X^2) – E(X)^2; mgf E(etX)E(e^{tX}), φ(k)(0)=E(Xk)\varphi^{(k)}(0) = E(X^k), φ(0)=1\varphi(0) = 1.

ModelP(X=k)P(X = k)ValuesMeanVariancemgf
Bernoulli pppk(1−p)1−kp^k(1-p)^{1-k}0, 1ppp(1−p)p(1-p)1−p+pet1 – p + pe^t
Binomial n,pn, p(nk)pk(1−p)n−k\binom{n}{k}p^k(1-p)^{n-k}0…nnpnpnp(1−p)np(1-p)(1−p+pet)n(1 – p + pe^t)^n
Geometric pp(1−p)kp(1-p)^k p0, 1, …1−pp\frac{1-p}{p}1−pp2\frac{1-p}{p^2}p1−(1−p)et\frac{p}{1-(1-p)e^t}
Neg. binomial r,pr, p(k+r−1k)pr(1−p)k\binom{k+r-1}{k}p^r(1-p)^k0, 1, …r(1−p)p\frac{r(1-p)}{p}r(1−p)p2\frac{r(1-p)}{p^2}(p1−(1−p)et)r\left(\frac{p}{1-(1-p)e^t}\right)^r
Poisson λ\lambdae−λλkk!e^{-\lambda}\frac{\lambda^k}{k!}0, 1, …λ\lambdaλ\lambdaeλ(et−1)e^{\lambda(e^t-1)}
Uniform 1..n1..n1n\frac{1}{n}1…nn+12\frac{n+1}{2}n2−112\frac{n^2-1}{12}—
I want…CodeResult (example)
P(X=k)P(X = k)dbinom(3, 20, 0.15)0.24
P(X≤k)P(X \leq k)pbinom(4, 20, 0.15)0.83
P(X≥k)P(X \geq k)1 - pbinom(4, 20, 0.15)0.17 (at least 5)
P(a≤X≤b)P(a \leq X \leq b)ppois(8, 6) - ppois(3, 6)0.70
Quantileqnbinom(0.9, 3, 0.15)31
Mass from FFdiff(c(0, F))jumps
Mean and variance of a tablesum(x * p), sum(x^2 * p) - m^2

Other results: E(aX+b)=aE(X)+bE(aX + b) = aE(X) + b; Var(aX+b)=a2Var(X)\text{Var}(aX + b) = a^2\text{Var}(X). Geometric: P(X≥n)=(1−p)nP(X \geq n) = (1-p)^n, F(n)=1−(1−p)n+1F(n) = 1 – (1-p)^{n+1}, memoryless. If X∈{0,1,…}X \in \{0, 1, \ldots\}: E(X)=∑n≥1P(X≥n)E(X) = \sum_{n \geq 1} P(X \geq n). Series: ∑k≥0ak=11−a\sum_{k \geq 0} a^k = \frac{1}{1-a}, ∑k≥1kak=a(1−a)2\sum_{k \geq 1} k a^k = \frac{a}{(1-a)^2}, ∑k≥0zkk!=ez\sum_{k \geq 0} \frac{z^k}{k!} = e^z, ∑k≥1zkk!=ez−1\sum_{k \geq 1} \frac{z^k}{k!} = e^z – 1. E(cX)=φX(ln⁡c)E(c^X) = \varphi_X(\ln c). Poisson over another interval: λ\lambda proportional to its length. Binomial with large nn and small pp ≈Pois(np)\approx \text{Pois}(np).

SymptomLikely cause
P(X≥k)P(X \geq k) comes out half what you expected1 - p…(k, …) instead of k - 1
A geometric probability does not add upYou passed trials instead of failures
Negative variance or bigger than E(X2)E(X^2)You did not subtract E(X)2E(X)^2, or a wrong derivative
The mass does not add up to 1A value is missing, or the series starts somewhere else
The mgf is not 1 at t=0t = 0A mistake summing the series
R’s mode does not match the plotwhich.max() gives the position, or there is a double mode

Before you hand it in, ask yourself:

  1. Have I stated the model with its parameters, and what the variable counts?
  2. Is every “at least”, “more than” and “at most” translated into ≤\leq with the right value?
  3. Have I given the results with 2 decimals?
  4. Is the code for every part I did with R in my .R file?
ME1 exam coming up?The script for every topic, the printable cheat sheet and the pack for each exam, for free.See the pack

For your script

The topic’s code, ready to paste into your exam .R file. They are functions of my own that solve the typical parts and the code for this article’s exercises, without the questions. It has been run as is.

# ===== Discrete random variables (ME1, topic 3) =====

# --- A variable given by its table ---
# x: values; p: probabilities. Returns mean, variance and standard deviation.
rv_summary <- function(x, p) {
  stopifnot(abs(sum(p) - 1) < 1e-8, all(p >= 0))   # it must be a mass function
  m <- sum(x * p)
  v <- sum(x^2 * p) - m^2
  round(c(mean = m, variance = v, sd = sqrt(v)), 2)
}
# Mass from the distribution function (F in the order of x)
mass_from_F <- function(F) diff(c(0, F))
# Skewness and kurtosis from the table
rv_shape <- function(x, p) {
  m <- sum(x * p); s <- sqrt(sum((x - m)^2 * p))
  round(c(skewness = sum((x - m)^3 * p) / s^3,
          kurtosis = sum((x - m)^4 * p) / s^4), 2)
}
# Plot of F with the jumps in the right place
plot_F <- function(x, p, ...) {
  F <- cumsum(p)
  plot(c(min(x) - 1, x, max(x) + 1), c(0, F, 1), type = "s", ylab = "F(x)", ...)
  points(x, F, pch = 19)
}

# --- mgf: check moments with numerical derivatives ---
# mgf: a function of t. Returns approximations of E(X) and E(X^2).
mgf_moments <- function(mgf, h = 1e-4) {
  d1 <- (mgf(h) - mgf(-h)) / (2 * h)
  d2 <- (mgf(h) - 2 * mgf(0) + mgf(-h)) / h^2
  round(c(EX = d1, EX2 = d2, var = d2 - d1^2), 2)
}

# --- Tail probabilities without getting the boundary wrong ---
# fp: the model's p... function (pbinom, pgeom, pnbinom, ppois); ...: its parameters
at_least  <- function(k, fp, ...) 1 - fp(k - 1, ...)     # P(X >= k)
more_than <- function(k, fp, ...) 1 - fp(k, ...)         # P(X > k)
between   <- function(a, b, fp, ...) fp(b, ...) - fp(a - 1, ...)   # P(a <= X <= b)
# Mode (or modes) of a mass function at the values x
modes <- function(x, p) x[abs(p - max(p)) < 1e-12]

# --- A mass series with a parameter: check sum, mean and mgf ---
check_mass <- function(px, x = 1:200, t = 0.1) {
  p <- px(x)
  round(c(sum = sum(p), mean = sum(x * p), mgf_t = sum(exp(t * x) * p)), 4)
}

# ===== Solved exercises =====
# Exercise 1: Marcos's voice notes
x1 <- c(0, 1, 2, 4); F1 <- c(0.35, 0.65, 0.85, 1)
p1 <- mass_from_F(F1); p1
rv_summary(x1, p1)
mgf_moments(function(t) sum(exp(t * x1) * p1))
plot_F(x1, p1, xlab = "Voice notes per day")
# Exercise 2: questions about the assignment
round(c(P3 = dbinom(3, 20, 0.15), at_least_5 = at_least(5, pbinom, 20, 0.15)), 2)
round(more_than(6, pgeom, 0.15), 2)
round(pnbinom(10, 3, 0.15), 2); qnbinom(0.9, 3, 0.15)
round(between(4, 8, ppois, 6), 2)
modes(0:16, dpois(0:16, 6))
# Exercise 3: zero-truncated Poisson, theta = 2
check_mass(function(x) 2^x / (factorial(x) * (exp(2) - 1)), x = 1:60)
# Bonus: E(2^X) with X ~ Pois(4) is the mgf at log(2)
exp(4 * (exp(log(2)) - 1))

Output:

[1] 0.35 0.30 0.20 0.15
    mean variance       sd 
    1.30     1.81     1.35 
  EX  EX2  var 
1.30 3.50 1.81 
        P3 at_least_5 
      0.24       0.17 
[1] 0.32
[1] 0.31
[1] 31
[1] 0.7
[1] 5 6
   sum   mean  mgf_t 
1.0000 2.3130 1.2707 
[1] 54.59815

Each line of the output matches one part: the mass of exercise 1 and its summary, the moments from the mgf (1.3 and 3.5), the probabilities of exercise 2, the double mode (5 and 6), the check of exercise 3 (sum 1 and mean 2.3130) and the bonus (54.6).

What next?

With this topic you finish discrete random variables. In topic 4 we move on to continuous random variables, where sums turn into integrals and the mass function into a density function; the ideas from this topic (distribution, mean, variance, mgf) stay. If you want to review first, go back to the theory, the proofs or the R practice.

Keep goingAll Statistics articles

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

Ready for the exam?ME1 EC1 mock exam — the 4-hour exam with R alongside, four versions, a mark out of 50 and every solution with its code (and why the .R counts too)