
Lecture 10: Discrete Random Variables
August 22, 2026
๐ Last lecture we looked at the fundamentals of probability, including:
sample() Functionreplicate() Function๐ This week we will be continuing our exploration of probability by studying:
The sample space \(\Omega\) is the collection of all outcomes \(\omega\) of a random experiment.
Example:
A dice rolling experiment has 6 possible outcomes, rolling a number 1 through 6.
We say that an event is certain (guaranteed, ensured, definitely going to happen) if it occurs with probability 1.
Additionally, we say that an event occurs with probability zero if it โneverโ happens.
๐ This is not the same as being impossible!
Example
There is a subtle difference between an event that is impossible and an event that occurs with probability zero.
Infinite Bullseye

โผ๏ธ This doesnโt mean that it will never happen!
In fact, anytime we randomly select a number from a continuous distribution we are observing a probability zero event.

Events with probability zero can occur โ and do all the time.
Intuitive breakdowns are common in many probability settings:
Letโs now turn to the topic of mutually exclusive and mutually inclusive events.
We say that two events are mutually exclusive when
\[ \mathbb{P}(A\cap B) = \mathbb{P}(A~\text{AND}~B)= 0. \]

When two events are not mutually exclusive we have that
\[ \mathbb{P}(A\cap B) > 0. \]

The addition rule in probability states that
\[ \mathbb{P}(A\cup B) = \mathbb{P}(A) + \mathbb{P}(B) - \mathbb{P}(A\cap B). \]
Recall that when \(A\) and \(B\) are mutually exclusive we have \(\mathbb{P}(A\cap B) = 0\), and so for such events
\[ \mathbb{P}(A\cup B)=\mathbb{P}(A)+\mathbb{P}(B). \]
The complement of an event \(A\), written \(A^c\), means all other outcomes besides those contained in \(A\), i.e. the event such that
\[ \mathbb{P}(A^c) = 1-\mathbb{P}(A) \]

Sometimes it is useful to think of the probability of an event \(A\) assuming some condition about a different event \(B\).
Formally, the conditional probability of event \(A\) given \(B\) is denoted
\[ \mathbb{P}(A|B)=\frac{\mathbb{P}(A\cap B)}{\mathbb{P}(B)}. \]
Examples
Mutually exclusive events means that two events cannot happen at the same time.
Independence, on the other hand, simply means that the outcome of one event does not impact the outcome of another.
Formally, two events \(A\) and \(B\) are independent when:
\[ \mathbb{P}(A|B) = \mathbb{P}(A), \]
It is often very hard to solve \(\mathbb{P}(A|B)\) but very easy to solve \(\mathbb{P}(B|A)\).
In this case, Bayesโ Theorem provides a useful identity for linking the two.
Bayesโ Theorem states that:
\[ \mathbb{P}(A|B) = \frac{\mathbb{P}(B|A)\cdot \mathbb{P}(A)}{\mathbb{P}(B)}. \]
๐ Bayesโ Theorem is particularly useful when it is easy to simulate \(\mathbb{P}(A)\), \(\mathbb{P}(B)\) and \(\mathbb{P}(B|A)\), but very difficult to simulate \(\mathbb{P}(A|B)\).
04:00
Suppose you roll 3 fair 6-sided dice.
m = 10000 simulations to approximate the probability that their sum is at least 12 given that their product is at least 9.Make sure to set a seed using set.seed() โ you might find the functions apply() and which() helpful.
[1] 0.4459681
n = 10000 rolls of 3 dice, storing each roll as a column of dice_rolls.which() to find the columns (rolls) whose product is at least 9, then subset dice_rolls down to just those columns.apply() and mean() to compute the proportion whose sum is at least 12 โ this directly approximates \(\mathbb{P}(\text{sum} \geq 12 \mid \text{product} \geq 9)\).[1] 0.4459681
n trials โ no subsetting yet.sum_rolls >= 12 & prod_rolls >= 9) and divide by the count satisfying just the conditioning event (prod_rolls >= 9).In the next section we will introduce the following concepts:
We will first quickly cover some heuristic definitions before considering several examples.
๐ Donโt worry too much about the mathematics โ focus on intuition!
A random variable is a mathematical object that can take numerical values corresponding to outcomes of a random experiment.

Each of the above random variables have a discrete (more specifically a countable) support, and are therefore called discrete random variables.
If a random variable can take (uncountably) infinite values (i.e. a continuous interval) we say that the random variable is continuous.
The probability mass function (PMF) of a discrete random variable \(X\) is a function \(p_X(k)=\mathbb{P}(X=k)\) describing the probability of the random variable taking different values in its support.
Example:
Consider the dice roll experiment. The probability that \(X\) takes the values \(1, ..., 6\) are all \(\frac{1}{6}\) and \(0\) for all other integers. We can write this
\[ p_X(x)=\mathbb{P}(X=x)=\begin{cases} \frac{1}{6} & \text{if } x\in\{1,2,...,6\} \\ 0 & \text{otherwise}. \end{cases} \]
This is known as a discrete uniform distribution, one of several common named distributions.
The following are some of the most common probability distributions for discrete random variables:
A uniform (discrete) random variable \(X\sim\mathcal{U}(n)\) describes the outcome of an experiment with \(n\) equally likely outcomes.
Since we have \(n\) equal probabilities which must all add to one, the probability mass function of \(X\) is given by
\[ p_X(k)=\frac{1}{n};\quad k\in\{1, ..., n\} \]
๐ Often we do not specify that the mass function is zero for values other than the support and leave it implied.
We have already demonstrated how we use the sample() function for the discrete uniform.
Often we are not interested in the probability of a specific event but instead wish to know about the average behaviour of all events.
Letโs consider the set of 12 counts of 2 summed dice.
[1] 4 9 10 6 5 7 7 6 7 5 5 11
[1] 6.833333
๐ก Intuition: If we kept rolling forever, the running average would settle down and stop changing โ that settling-down number is what we call the expectation.
Suppose that we run a large number of random trials.
The expected value or expectation is given by the average of all trials as the number of trials goes to infinity.
Formally, the expectation of a (discrete) random variable \(X\) is given by
\[ \mathbb{E}[X] = \sum_{x\in\mathcal{X}} x\cdot \mathbb{P}(X=x). \]
where \(\mathcal{X}\) is the support of \(X\).
In this course, we can think of the expectation of a random variable \(X\) is given by the limit of the sample mean
\[ \mathbb{E}[X] = \lim_{n\to\infty}\left(\frac{\sum_{i=1}^n x_i}{n}\right). \]
Recall \(X\sim\mathcal{U}(6)\) (one fair die roll) has PMF \(p_X(k)=\frac{1}{6}\) for \(k\in\{1,...,6\}\).
Applying the definition of expectation:
\[ \mathbb{E}[X] = \sum_{k=1}^{6} k\cdot\frac{1}{6} = \frac{1+2+3+4+5+6}{6} = 3.5 \]
We can confirm this by simulating many die rolls and averaging:
๐ In general, for \(X\sim\mathcal{U}(n)\), \(\mathbb{E}[X] = \frac{n+1}{2}\).
04:00
Suppose we repeatedly flip two fair coins. We are interested in determining the expected number of flips it takes to get both coins to land on tails.
Write a function using replicate() to calculate the number of trials until a double-tails flip, then use replicate() again for the 1000 experiments.
Again, you may find the function which() to be helpful.
[1] 3.967
tt() flips two coins 100 times โ sum(sample(0:1, 2)) equals 2 exactly when both land tails โ and returns the index of the first double-tails flip using which(flips == 2)[1].replicate(1000, tt()) repeats that experiment 1000 times, giving 1000 simulated waiting times until a double-tails result.mean(expected_flips) averages those waiting times to approximate the expected number of flips.A Bernoulli random variable \(X\sim\text{Bernoulli}(p)\) describes the outcome of an experiment with a binary outcome (a Bernoulli trial) where the probability of a โsuccessful outcomeโ is \(p\) and the probability of failure is \(1-p\).
The probability mass function of \(X\) is given by
\[ p_X(k)=p^k(1-p)^{1-k};\quad k\in\{0,1\}. \]
Plugging in \(k=0\) and \(k=1\) shows why this formula makes sense:
\[ p_X(0) = p^0(1-p)^{1-0} = 1-p, \qquad p_X(1) = p^1(1-p)^{1-1} = p. \]
A Binomial random variable \(X\sim\text{Bin}(n,p)\) describes the experiment of counting the number of successes in \(n\) independent Bernoulli trials, each with success probability \(p\).
The probability mass function of \(X\) is given by
\[ \mathbb{P}(X=k) = {n \choose k}p^k(1-p)^{n-k}; \quad k \in\{0,1,...,n\}, \]
where the support is all integers from \(0\) (no successes) to \(n\) (all successes).
In the PMF we used the choose function \({n \choose k}\) โ \(n\) choose \(k\) โ which is defined as
\[ {n\choose k} = \frac{n!}{k!(n-k)!}. \]
This definition uses factorials where
\[ n! = \prod_{i=1}^n i = 1 \times 2 ~\times~ ...~ \times~ n. \]
๐ Combinatorially, \({n\choose k}\) counts the number of ways to choose \(k\) items from a set of \(n\), without regard to order.
We can use the built-in function choose(n,k) to compute this in R.
Consider an experiment where we flip the biased coin from our Bernoulli example 12 times. We wish to determine the probability that we obtain 8 heads.
We define the random variable \(X\), which describes the number of heads from the 12 throws. Therefore \(X\sim\text{Bin}(12,0.75)\) with support \(\{0,1,...,12\}\), which gives
\[ \begin{align} \mathbb{P}(X=8) & = {12 \choose 8}\cdot(0.75)^8\cdot (0.25)^4. \end{align} \]


Since \(X\sim\text{Bin}(n,p)\) is the sum of \(n\) independent Bernoulli\((p)\) trials, and each trial succeeds with probability \(p\) on average, the expected number of successes is simply
\[ \mathbb{E}[X] = np. \]
Recall \(X\sim\text{Bin}(12, 0.75)\) from our biased-coin example. Applying the formula:
\[ \mathbb{E}[X] = 12 \times 0.75 = 9. \]
We can confirm this by simulating many sets of 12 biased-coin flips and averaging the number of heads:
๐ In general, for \(X\sim\text{Bin}(n,p)\), \(\mathbb{E}[X] = np\).
The cumulative distribution function (CDF) of a random variable \(X\), denoted \(F_X(x)\), describes the probability that \(X\) takes a value less than or equal to some value \(x\), i.e.
\[ F_X(x) = \mathbb{P}(X\leq x). \]
For a discrete random variable this probability is just the sum of the probabilities of the random variable taking the values below \(x\), i.e.
\[ F_X(x) = \sum_{k\leq x} p_X(k). \]
Returning to our binomial random variable example, letโs look at computing the CDF, specifically \(F_X(2)\), which is equivalent to
\[ \begin{aligned} F_X(2) & =\mathbb{P}(X\leq 2) \\ & = \mathbb{P}(X=0) + \mathbb{P}(X=1) + \mathbb{P}(X=2) \\ & = {12\choose 0}\cdot0.75^0\cdot0.25^{12} + {12 \choose 1}\cdot 0.75^1\cdot 0.25^{11} + \\ & \quad \quad \quad {12\choose 2}\cdot 0.75^2\cdot 0.25^{10} \\ & = \frac{12!}{0!\,12!}\cdot0.75^0\cdot0.25^{12} + \frac{12!}{1!\,11!}\cdot 0.75^1\cdot 0.25^{11} + \\ & \quad \quad \quad \frac{12!}{2!\,10!}\cdot 0.75^2\cdot 0.25^{10} \\ & \approx 0.0000376. \end{aligned} \]
We can quickly compute this in R.
๐ Writing our own binomial_cdf() function works, but R actually has a built-in function for this โ weโll meet it next.
R has several in-built functions for computing and generating realizations from probability mass functions:
dbinom()
pbinom()
qbinom()
rbinom()
๐ This d/p/q/r naming pattern is consistent across every distribution family in R โ e.g. dnorm(), dpois() and dunif() all follow the same syntax.
dbinom() FunctionThe dbinom() function computes the density of a binomial distribution.
The general syntax is:
Example:
For our \(X\sim\text{Bin}(n = 12, p = 0.75)\) we compute \(\mathbb{P}(X=8)\):
[1] 0.1935777
[1] 0.1935777
pbinom() FunctionThe pbinom() function computes the cumulative distribution function of a binomial distribution.
The general syntax is the same:
Example:
For our \(X\sim\text{Bin}(n = 12, p = 0.75)\) we compute \(\mathbb{P}(X\leq 2)\):
[1] 3.761053e-05
[1] 3.761053e-05
rbinom() FunctionThe rbinom() function generates realizations of a binomial random variable.
The general syntax is the same:
Example:
To simulate 10000 realizations of \(X\sim\text{Bin}(n = 12, p = 0.75)\) and produce a histogram we have:
rbinom() Function
rbinom() Function
๐ค Today we covered a lot of material, including:
dbinom()pbinom(); andrbinom()๐คฉ Next class we will continue exploring probability by looking into: