COMP2121 Chap.8 Random Variables, Expectation and Variance
Random Variables, Expectation and Variance
Random variables and expectation
A random variable is a function from outcomes to numbers, and each of its values defines an event. Its expectation is a weighted average that can be summed over outcomes or over values.
Linearity of expectation holds for any random variables, which makes indicator variables a powerful counting tool.
Standard distributions and spread
Bernoulli and binomial variables model successes in trials, and waiting for a first success gives an expectation of one over p.
The product rule for expectations needs independence, and variance measures the expected squared distance from the mean.
Choosing between two ways to compute an expectation
An expectation can be summed over outcomes, weighting each value by the probability of its outcome, or over values, weighting each value by the probability that the variable takes it. The second is shorter once the distribution is known.
When the distribution is hard to find, break the variable into a sum of simpler variables, often indicators, and use linearity instead. The expected number of fixed points of a random permutation is one for every n, found this way without any distribution.
Variance and independence
Variance equals the mean of the square minus the square of the mean.
For independent variables the variance of a sum is the sum of the variances, which gives n times p times one minus p for a binomial count. Without independence an extra covariance term appears. A fair die has variance 35 over 12, which follows from a mean of 3.5 and a mean square of 91 over 6.
What this chapter covers
- 01
Random variables as functions
- 02
Probability distributions
- 03
Expected value
- 04
Linearity and indicators
- 05
Independence and the product rule
- 06
Variance
Worked example · free
Read an expectation from a distribution
- 2Count the pairs whose larger face is k.
- 1Write the probabilities and check they sum to 1.
- 2Compute the weighted average.
Key terms
- Random variable
- A function from the sample space of an experiment to the real numbers.
- Expected value
- The probability-weighted average of the values of a random variable.
- Indicator variable
- A random variable equal to 1 when an event occurs and 0 otherwise.
- Variance
- The expected squared deviation of a random variable from its mean.
Random Variables, Expectation and Variance FAQ
Does linearity of expectation need independence?
No. The expectation of a sum equals the sum of the expectations for any random variables. That is why indicator variables can be added even when they depend on each other.
When does the expectation of a product factor?
It factors when the variables are independent. Without independence it can fail, and equal values of the two sides do not prove independence. Two dependent variables can still happen to satisfy the product rule.
How long do I wait for a first success?
If each independent trial succeeds with probability p, the expected number of trials up to and including the first success is one over p. With a fair die and a six as success, the expected wait is six rolls.
Why is the variance of a binomial np times one minus p?
A binomial count is a sum of n independent Bernoulli variables, each with variance p times one minus p. Variances of independent variables add, so the sum has variance n times p times one minus p.
What is an indicator variable used for?
An indicator equals one when an event happens and zero otherwise, so its expectation is the probability of the event. Writing a count as a sum of indicators turns a hard expectation into a sum of probabilities, as with fixed points of a permutation or faces that never appear in repeated rolls.
Exam move
For each expectation question, first look for indicators whose sum is the quantity asked for. Keep one worked distribution table to practise summing over values. Memorise the conditions attached to each shortcut: linearity needs nothing, the product rule needs independence, and variance is computed as the mean of the square minus the square of the mean.
For each new formula, write one small example where it is checked by direct enumeration over a sample space you can list. Practise recognising when a count is a sum of indicators, because that single move solves many expectation questions quickly.
Working through Random Variables, Expectation and Variance in COMP2121? Sia is AskSia’s AI Computer Science tutor — ask any COMP2121 Random Variables, Expectation and Variance question and get a clear, step-by-step explanation grounded in how COMP2121 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.