1. Home
  2. Explore
  3. Computer Science
  4. CS109 Course Feedback Process

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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., XXX, YYY) 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 XXX is a function from the sample space to real numbers. For any outcome, XXX assigns a number.

The PMF, often denoted pX(x)p_X(x)pX​(x) or P(X=x)P(X = x)P(X=x), gives the probability that XXX equals a specific value xxx.

Probability Mass Function: pX(x)=P(X=x)p_X(x) = P(X = x)pX​(x)=P(X=x) for all possible values xxx of XXX.

Key observations:

  • Asking a probability question about a random variable always involves an event (e.g., X=2X = 2X=2, X<3X < 3X<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 XXX is

E[X]=∑xx⋅P(X=x)E[X] = \sum_{x} x \cdot P(X = x)E[X]=∑x​x⋅P(X=x)

where the sum is over all values xxx that XXX can take.

Three important properties of expectation:

  1. Linearity of Expectation: For any random variables XXX and YYY, and constants aaa and bbb: $$E[aX + bY] = aE[X] + bE[Y]$ThisholdsregardlessofanydependencebetweenThis holds regardless of any dependence betweenThisholdsregardlessofanydependencebetweenXandandandY$.

  2. Expectation from Data: If given a dataset of observed values of XXX, the expectation is simply the arithmetic mean (average) of those values. This follows from the frequentist interpretation: P(X=x)≈count(x)/nP(X = x) \approx \text{count}(x) / nP(X=x)≈count(x)/n.

  3. Law of the Unconscious Statistician (LOTUS): For any function ggg, the expectation of g(X)g(X)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" ppp. The random variable counts the number of successes.

Examples of this narrative in the real world:

  • Coin flips: Number of heads in nnn flips of a biased coin.

  • Bit strings: Number of 1's in a random bit string of length nnn.

  • Computer crashes: Number of crashes among nnn independent servers.

  • Voting: Number of voters who support a candidate out of nnn voters.

  • Quality control: Number of defective items in a batch of nnn.

Definition and Notation

A binomial random variable XXX counts the number of successes in nnn independent Bernoulli trials (each success probability ppp).

Notation: X∼Bin(n,p)X \sim \text{Bin}(n, p)X∼Bin(n,p)

  • nnn = number of trials

  • ppp = 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)=(nk)pk(1−p)n−kP(X = k) = \binom{n}{k} p^k (1-p)^{n-k}P(X=k)=(kn​)pk(1−p)n−k

for k=0,1,…,nk = 0, 1, \dots, nk=0,1,…,n.

Where (nk)=n!k!(n−k)!\binom{n}{k} = \frac{n!}{k!(n-k)!}(kn​)=k!(n−k)!n!​ is the binomial coefficient (number of ways to choose kkk successes out of nnn trials).

Derivation intuition: Each specific sequence of kkk successes and n−kn-kn−k failures has probability pk(1−p)n−kp^k(1-p)^{n-k}pk(1−p)n−k, and there are (nk)\binom{n}{k}(kn​) such mutually exclusive sequences. The PMF comes from summing over them.

Examples

Flipping three coins

Let XXX be the number of heads when flipping three fair coins (p=0.5p = 0.5p=0.5). Then X∼Bin(3,0.5)X \sim \text{Bin}(3, 0.5)X∼Bin(3,0.5). The PMF gives:

  • P(X=0)=(30)(0.5)0(0.5)3=0.125P(X=0) = \binom{3}{0}(0.5)^0(0.5)^3 = 0.125P(X=0)=(03​)(0.5)0(0.5)3=0.125

  • P(X=1)=(31)(0.5)1(0.5)2=0.375P(X=1) = \binom{3}{1}(0.5)^1(0.5)^2 = 0.375P(X=1)=(13​)(0.5)1(0.5)2=0.375

  • P(X=2)=(32)(0.5)2(0.5)1=0.375P(X=2) = \binom{3}{2}(0.5)^2(0.5)^1 = 0.375P(X=2)=(23​)(0.5)2(0.5)1=0.375

  • P(X=3)=(33)(0.5)3(0.5)0=0.125P(X=3) = \binom{3}{3}(0.5)^3(0.5)^0 = 0.125P(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 HHH be the number of clicks. Then H∼Bin(1000,0.01)H \sim \text{Bin}(1000, 0.01)H∼Bin(1000,0.01). The probability of exactly 5 clicks is:

P(H=5)=(10005)(0.01)5(0.99)995P(H = 5) = \binom{1000}{5} (0.01)^5 (0.99)^{995}P(H=5)=(51000​)(0.01)5(0.99)995

Program crashes

Seven independent runs of a program; each run crashes with probability 0.3. Let CCC be the number of crashes. Then C∼Bin(7,0.3)C \sim \text{Bin}(7, 0.3)C∼Bin(7,0.3). The probability of exactly 3 crashes:

P(C=3)=(73)(0.3)3(0.7)4P(C = 3) = \binom{7}{3} (0.3)^3 (0.7)^4P(C=3)=(37​)(0.3)3(0.7)4

Computing with Python (scipy)

The scipy.stats library provides a function binom.pmf(k, n, p) that returns P(X=k)P(X = k)P(X=k) for a binomial with parameters nnn and ppp. Example:

from scipy import stats
n = 1000
p = 0.01
k = 5
prob = stats.binom.pmf(k, n, p)
print(prob)  # outputs approximate probability

This 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)P(X \ge k)P(X≥k) or P(X<k)P(X < k)P(X<k), use the mutually exclusive nature of binomial outcomes.

For P(X≥4)P(X \ge 4)P(X≥4) when X∼Bin(7,0.55)X \sim \text{Bin}(7, 0.55)X∼Bin(7,0.55):

P(X≥4)=P(X=4)+P(X=5)+P(X=6)+P(X=7)P(X \ge 4) = P(X=4) + P(X=5) + P(X=6) + P(X=7)P(X≥4)=P(X=4)+P(X=5)+P(X=6)+P(X=7)

Each term comes from the binomial PMF. Since events {X=k}\{X=k\}{X=k} are mutually exclusive, the probability of the union is the sum.

Similarly, P(X<10)=∑k=09P(X=k)P(X < 10) = \sum_{k=0}^{9} P(X = k)P(X<10)=∑k=09​P(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)P(X \ge 4)P(X≥4) where X∼Bin(7,p)X \sim \text{Bin}(7, p)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=7n=7n=7 works seamlessly.


Bernoulli Random Variable

Definition

A Bernoulli random variable is a special case of the binomial where n=1n = 1n=1. It takes on only two values:

  • X=1X = 1X=1 with probability ppp (success)

  • X=0X = 0X=0 with probability 1−p1-p1−p (failure)

Notation: X∼Bernoulli(p)X \sim \text{Bernoulli}(p)X∼Bernoulli(p) or simply X∼Ber(p)X \sim \text{Ber}(p)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 ppp:

E[X]=1⋅p+0⋅(1−p)=pE[X] = 1\cdot p + 0\cdot(1-p) = pE[X]=1⋅p+0⋅(1−p)=p

Relationship to Binomial

A binomial random variable is the sum of nnn independent and identically distributed Bernoulli random variables. If Yi∼Bernoulli(p)Y_i \sim \text{Bernoulli}(p)Yi​∼Bernoulli(p) for i=1,…,ni = 1,\dots,ni=1,…,n, then

X=∑i=1nYi∼Bin(n,p)X = \sum_{i=1}^n Y_i \sim \text{Bin}(n, p)X=∑i=1n​Yi​∼Bin(n,p)

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)X \sim \text{Bin}(n, p)X∼Bin(n,p). Represent XXX as the sum of nnn independent Bernoulli variables: X=Y1+Y2+⋯+YnX = Y_1 + Y_2 + \dots + Y_nX=Y1​+Y2​+⋯+Yn​, where each Yi∼Bernoulli(p)Y_i \sim \text{Bernoulli}(p)Yi​∼Bernoulli(p). Then by linearity of expectation:

E[X]=E[∑i=1nYi]=∑i=1nE[Yi]=∑i=1np=npE[X] = E\left[\sum_{i=1}^n Y_i\right] = \sum_{i=1}^n E[Y_i] = \sum_{i=1}^n p = npE[X]=E[∑i=1n​Yi​]=∑i=1n​E[Yi​]=∑i=1n​p=np

Thus, the expected number of successes in nnn trials is npnpnp.


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−μ)(X - \mu)(X−μ) would cancel out positive and negative deviations.

Two common ways to fix this:

  1. Absolute deviation: ∣X−μ∣|X - \mu|∣X−μ∣, then average.

  2. Squared deviation: (X−μ)2(X - \mu)^2(X−μ)2, then average.

Probability theory adopted the squared deviation, leading to the definition of variance.

Definition of Variance

Variance (of a random variable XXX) is the expected squared deviation from its mean μ=E[X]\mu = E[X]μ=E[X]:

Var(X)=E[(X−μ)2]\text{Var}(X) = E[(X - \mu)^2]Var(X)=E[(X−μ)2]

Variance measures the average squared distance between XXX 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)\text{SD}(X) = \sqrt{\text{Var}(X)}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 XXX can also be expressed as:

Var(X)=E[(X−μ)2]=∑x(x−μ)2⋅P(X=x)\text{Var}(X) = E[(X - \mu)^2] = \sum_x (x - \mu)^2 \cdot P(X = x)Var(X)=E[(X−μ)2]=∑x​(x−μ)2⋅P(X=x)

This formula directly applies the definition.

Alternative Formula for Variance

A more convenient computational formula is derived by expanding the square:

Var(X)=E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μE[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2\begin{aligned} \text{Var}(X) &= E[(X - \mu)^2] \\ &= E[X^2 - 2\mu X + \mu^2] \\ &= E[X^2] - 2\mu E[X] + \mu^2 \\ &= E[X^2] - 2\mu^2 + \mu^2 \\ &= E[X^2] - \mu^2 \end{aligned}Var(X)​=E[(X−μ)2]=E[X2−2μX+μ2]=E[X2]−2μE[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2​

So:

Alternative formula:

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

Here E[X2]E[X^2]E[X2] is the second moment of XXX (the expectation of X2X^2X2), computed using LOTUS:

E[X2]=∑xx2⋅P(X=x)E[X^2] = \sum_x x^2 \cdot P(X = x)E[X2]=∑x​x2⋅P(X=x)

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]E[X^2]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])2E[X^2] \neq (E[X])^2E[X2]=(E[X])2 in general. The difference E[X2]−(E[X])2E[X^2] - (E[X])^2E[X2]−(E[X])2 is always non-negative (by Jensen's inequality) and equals zero only when XXX is constant.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent