The University of Hong Kong · FACULTY OF COMPUTER SCIENCE

COMP2121 Chap.8 Random Variables, Expectation and Variance

- one subject, every graph, every model, every mark
6 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 8 of 11 · COMP2121

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.

In this chapter

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

Q [5 marks]. This practice weighting was set for revision and is not an official University marking scheme. Two fair dice are rolled and X is the larger face. Find the distribution of X and its expected value.
The distribution of the larger face when two fair dice are rolled.The distribution of the larger face when two fair dice are rolled. Bar k has height proportional to (2k − 1)/36, rising from 1/36 to 11/36; the dashed line marks the expectation 161/36, about 4.47, pulled towards the heavy right end.1234561/3611/36mean 4.47larger face of two dice
The distribution of the larger face when two fair dice are rolled. Bar k has height proportional to (2k − 1)/36, rising from 1/36 to 11/36; the dashed line marks the expectation 161/36, about 4.47, pulled towards the heavy right end.
  • 2Count the pairs whose larger face is k.
  • 1Write the probabilities and check they sum to 1.
  • 2Compute the weighted average.
Pairs with both faces at most k number k squared, so exactly 2k minus 1 pairs have larger face k, giving probability 2k minus 1 over 36. These sum to 36 over 36. The expectation is 1 times 1 plus 2 times 3 and so on up to 6 times 11, all over 36, which is 161 over 36, about 4.47.
Sia tip — Check that a distribution sums to one before using it.
Glossary

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.
FAQ

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.

Study strategy

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.

A+Everything unlocked
Unlocks this Bible + all 52 of your subjects at The University of Hong Kong, plus 1,000+ more Bibles across the AskSia library.
Sia - your COMP2121 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works