CS109 Course Feedback Process
Studley Study Notes - Random Variables: Binomial, Bernoulli, and Variance
Random Variables and Expectation Review
Random Variables
A random variable is a variable whose value is determined by a probabilistic process. It takes on numerical values, but the exact outcome is unknown beforehand. Random variables are typically denoted by capital letters (e.g., X, Y) to distinguish them from non-random variables.
The most important aspect of a random variable is the relationship between the values it can take and their probabilities. This relationship is captured by the probability mass function (PMF).
Definition: A random variable X is a function from the sample space to real numbers. For any outcome, X assigns a number.
The PMF, often denoted pX(x) or P(X=x), gives the probability that X equals a specific value x.
Probability Mass Function: pX(x)=P(X=x) for all possible values x of X.
Key observations:
Asking a probability question about a random variable always involves an event (e.g., X=2, X<3).
The PMF is the complete description of the random variable; all probability questions can be answered from it.
The PMF can be expressed as an equation, a graph, or even code.
Expectation
Expectation (or expected value) is a summary statistic that reduces a random variable to a single number representing its center of mass. It is the probability-weighted average of all possible values.
Definition: The expectation of a random variable X is
E[X]=∑xx⋅P(X=x)where the sum is over all values x that X can take.
Three important properties of expectation:
Linearity of Expectation: For any random variables X and Y, and constants a and b: $$E[aX + bY] = aE[X] + bE[Y]$ThisholdsregardlessofanydependencebetweenXandY$.
Expectation from Data: If given a dataset of observed values of X, the expectation is simply the arithmetic mean (average) of those values. This follows from the frequentist interpretation: P(X=x)≈count(x)/n.
Law of the Unconscious Statistician (LOTUS): For any function g, the expectation of g(X) is $$E[g(X)] = \sum_x g(x) \cdot P(X = x)$$ This allows computing expectations of transformed random variables without deriving their PMF.
Binomial Random Variable
The Coin Flip Metaphor
One of the most common and useful random variables arises from a simple narrative: n independent trials of the same experiment, each with the same probability of "success" p. The random variable counts the number of successes.
Examples of this narrative in the real world:
Coin flips: Number of heads in n flips of a biased coin.
Bit strings: Number of 1's in a random bit string of length n.
Computer crashes: Number of crashes among n independent servers.
Voting: Number of voters who support a candidate out of n voters.
Quality control: Number of defective items in a batch of n.
Definition and Notation
A binomial random variable X counts the number of successes in n independent Bernoulli trials (each success probability p).
Notation: X∼Bin(n,p)
n = number of trials
p = probability of success on each trial
Once a random variable is declared to be binomial, all its properties are inherited from the binomial family.
PMF Formula
The probability mass function of a binomial random variable is:
Binomial PMF:
P(X=k)=(kn)pk(1−p)n−kfor k=0,1,…,n.
Where (kn)=k!(n−k)!n! is the binomial coefficient (number of ways to choose k successes out of n trials).
Derivation intuition: Each specific sequence of k successes and n−k failures has probability pk(1−p)n−k, and there are (kn) such mutually exclusive sequences. The PMF comes from summing over them.
Examples
Flipping three coins
Let X be the number of heads when flipping three fair coins (p=0.5). Then X∼Bin(3,0.5). The PMF gives:
P(X=0)=(03)(0.5)0(0.5)3=0.125
P(X=1)=(13)(0.5)1(0.5)2=0.375
P(X=2)=(23)(0.5)2(0.5)1=0.375
P(X=3)=(33)(0.5)3(0.5)0=0.125
Ad clicks
1,000 ads are served; each ad is clicked with probability 0.01. Let H be the number of clicks. Then H∼Bin(1000,0.01). The probability of exactly 5 clicks is:
Program crashes
Seven independent runs of a program; each run crashes with probability 0.3. Let C be the number of crashes. Then C∼Bin(7,0.3). The probability of exactly 3 crashes:
Computing with Python (scipy)
The scipy.stats library provides a function binom.pmf(k, n, p) that returns P(X=k) for a binomial with parameters n and p. Example:
from scipy import stats
n = 1000
p = 0.01
k = 5
prob = stats.binom.pmf(k, n, p)
print(prob) # outputs approximate probabilityThis is useful for computing probabilities without manual calculation, especially when summing over many values.
Handling "At Least" Questions
If the question asks for P(X≥k) or P(X<k), use the mutually exclusive nature of binomial outcomes.
For P(X≥4) when X∼Bin(7,0.55):
Each term comes from the binomial PMF. Since events {X=k} are mutually exclusive, the probability of the union is the sum.
Similarly, P(X<10)=∑k=09P(X=k).
In Python:
from scipy import stats
n = 7
p = 0.55
total_prob = sum(stats.binom.pmf(k, n, p) for k in range(4, 8))
print(total_prob)Common Pitfall: Stopping Early in Sports Series
In a best-of-seven basketball series, the series ends as soon as one team wins four games. However, to compute the probability of winning the series, it is correct to model all seven games as binomial (assuming each game is independent with constant win probability). The reasoning:
All seven games are considered "played" hypothetically, even if the series ends early.
The probability of winning the series is exactly P(X≥4) where X∼Bin(7,p), because the unplayed "ghost games" do not change the outcome of the event (winning at least 4 of the first 7 games is equivalent to winning the series).
A common mistake is to try to derive using a variable number of experiments, but the binomial with fixed n=7 works seamlessly.
Bernoulli Random Variable
Definition
A Bernoulli random variable is a special case of the binomial where n=1. It takes on only two values:
X=1 with probability p (success)
X=0 with probability 1−p (failure)
Notation: X∼Bernoulli(p) or simply X∼Ber(p).
Since there is only one trial, the Bernoulli random variable is essentially a probabilistic bit.
Expectation
The expectation of a Bernoulli random variable is simply p:
Relationship to Binomial
A binomial random variable is the sum of n independent and identically distributed Bernoulli random variables. If Yi∼Bernoulli(p) for i=1,…,n, then
This relationship is powerful for deriving properties of the binomial, such as its expectation.
Derivation of Binomial Expectation using Bernoulli Sum
Let X∼Bin(n,p). Represent X as the sum of n independent Bernoulli variables: X=Y1+Y2+⋯+Yn, where each Yi∼Bernoulli(p). Then by linearity of expectation:
Thus, the expected number of successes in n trials is np.
Variance
Need for a Measure of Spread
Expectation alone does not fully characterize a random variable. Two random variables can have the same expectation but very different spreads (dispersion). For example, consider three distributions with the same mean of 3:
Distribution A: values tightly clustered around 3.
Distribution B: moderate spread.
Distribution C: wide spread.
To quantify this difference, we need a measure of spread or variability.
Peer Grading Example
Suppose three graders evaluate student work. The true grade is a certain value (e.g., 70). Each grader has a distribution of grades they assign:
Grader A: gives grades that vary widely (high spread).
Grader B: gives grades close to the true grade (low spread).
Grader C: systematically gives lower grades (biased).
Expectation may be the same for A and B (both centered on the true grade), but reliability differs. Variance captures this difference.
Quantifying Spread: Distance from Mean
A natural approach is to measure how far each value is from the mean, and then average those distances. However, simply taking the average of (X−μ) would cancel out positive and negative deviations.
Two common ways to fix this:
Absolute deviation: ∣X−μ∣, then average.
Squared deviation: (X−μ)2, then average.
Probability theory adopted the squared deviation, leading to the definition of variance.
Definition of Variance
Variance (of a random variable X) is the expected squared deviation from its mean μ=E[X]:
Var(X)=E[(X−μ)2]
Variance measures the average squared distance between X and its mean. Units are the square of the original units (e.g., "points squared").
Standard Deviation
Because variance is in squared units, the standard deviation is more interpretable:
Standard Deviation: SD(X)=Var(X)
Standard deviation is in the same units as the original random variable.
Law of Unconscious Statistician (LOTUS) for Variance
Using LOTUS, the variance of X can also be expressed as:
This formula directly applies the definition.
Alternative Formula for Variance
A more convenient computational formula is derived by expanding the square:
So:
Alternative formula:
Var(X)=E[X2]−(E[X])2
Here E[X2] is the second moment of X (the expectation of X2), computed using LOTUS:
This formula is often easier to compute than the definition because it avoids subtracting the mean for each term separately.
Second Moment
The term E[X2] is called the second moment (about the origin). Higher moments (third, fourth, etc.) exist but are beyond the scope of this course. Variance is the second central moment (central because it is about the mean).
Note: E[X2]=(E[X])2 in general. The difference E[X2]−(E[X])2 is always non-negative (by Jensen's inequality) and equals zero only when X is constant.

Ask anything
We've read this whole study set, so questions about it get the best answers.