Practice / Probability for ML
Discrete random variables: expectation, variance and the binomial, geometric and Poisson
Before you start
A discrete random variable is a number that comes out of a random experiment: the count of correct predictions in a batch, the number of retries before a request succeeds, the number of events in a minute. Its distribution is a list of values and their probabilities, and almost everything asked about it is a weighted sum over that list. Two facts do most of the work. Expectation is linear, , whether or not and are independent, which turns hard counts into sums of easy indicators. Variance is not linear, and the place it goes wrong is the place the squares are. These ten problems build the binomial, geometric and Poisson distributions from first principles, and end with the expected number of distinct examples in a bootstrap sample, the behind bagging's out-of-bag estimate.
- A discrete random variable has a probability mass function with and .
- Expectation. , and for any function , . In general ; in particular .
- Linearity. , and for any and , independent or not.
- Variance. , and the standard deviation is its square root. . For independent and , ; otherwise add .
- Indicators. For an event , if happens and otherwise. and .
- Binomial: the number of successes in independent trials with success probability . for , where counts the orders in which the successes can occur.
- Geometric: the number of the trial on which the first success happens, for Some books count the failures before it instead, which shifts everything by .
- Poisson: for , a count of events that occur independently at an average rate per interval. The series makes the probabilities sum to .
- Conditioning. and .
Problems
- ·
takes the values with probabilities , , and . Find , , and the standard deviation.
- ·
is the roll of a fair six-sided die. Find , and , and then and .
- ··
A classifier labels independent examples, each correctly with probability . Find the probability that exactly are correct, the probability that at least are, and the mean and variance of the number correct.
- ··
Show that a variable has mean and variance by writing it as a sum of indicators.
- ··
letters are put into addressed envelopes in a uniformly random order. Let be the number that land in the right envelope. Find and .
- ··
A request succeeds on each attempt with probability , independently, and is retried until it succeeds. Let be the number of attempts. Find , , the probability of success within attempts, and .
- ··
Errors arrive in a log at an average of per minute, as a Poisson count. Find the probability of no errors in a minute, of at most , and of at least , and show from the series that .
- ···
A rare label occurs with probability per example, independently, in a batch of . Compare the exact binomial probabilities of and of occurrences with the Poisson approximation, and say why the approximation works.
- ···
A fair die is rolled, and if it shows , then fair coins are flipped. Let be the number of heads. Find and .
- ···
A bootstrap sample draws examples uniformly at random, with replacement, from a training set of . Find the expected number of distinct examples in it, evaluate it for , and find the fraction it approaches as grows.
Answers
- , , , standard deviation
- , , ; ,
- , , ,
- ,
- and , for every
- , , ,
- , , ,
- Exact and ; Poisson and
- and
- ; for ; the fraction tends to
Worked solutions
Problem 1
takes the values with probabilities , , and . Find , , and the standard deviation.
- Check the table is a distribution before using it.
- Each value weighted by its probability.
- Square the values, not the probabilities, and keep the same weights.
- The shortcut .
- , , , standard deviation Check from the definition: .
Problem 2
is the roll of a fair six-sided die. Find , and , and then and .
- Each face has probability .
- The same weights on the squares.
- About .
- and Shifting by moves every value and the mean together, so the spread is unchanged; scaling by doubles every deviation and so multiplies their squares by .
- , , ; , Check: takes the values , and the mean of their squares minus is .
Problem 3
A classifier labels independent examples, each correctly with probability . Find the probability that exactly are correct, the probability that at least are, and the mean and variance of the number correct.
- A fixed number of independent trials with the same success probability.
- : the two errors can be any two of the ten examples.
- The events and are disjoint, so their probabilities add.
- and Problem 4 derives both.
- , , , Even at the mean, correct happens less than a third of the time; the standard deviation is examples.
Problem 4
Show that a variable has mean and variance by writing it as a sum of indicators.
- , where if trial succeedsCounting successes is adding a for each one.
- , so Linearity of expectation; no independence needed for this step.
- , so An indicator is or , so it equals its square.
- The trials are independent, so the covariances are and the variances add. This step does use independence.
- , The variance is largest at and is at or , when the outcome is certain.
Problem 5
letters are put into addressed envelopes in a uniformly random order. Let be the number that land in the right envelope. Find and .
- with if letter is in its own envelope, and Each letter is equally likely to be in any of the envelopes.
- Linearity holds even though the are dependent: if letters are right, the last one must be.
- for Given letter is right, letter has envelopes left.
- Expand the square of the sum: diagonal terms and ordered pairs.
- and , for every Each pair has , and the of them add exactly the that the independent formula misses (Mistake 5).
Problem 6
A request succeeds on each attempt with probability , independently, and is retried until it succeeds. Let be the number of attempts. Find , , the probability of success within attempts, and .
- , so is the derivative of the geometric series . In general .
- means the first three attempts all failed.
- The complement: at least one success in three attempts.
- is contained in , so the intersection is .
- , , , Memorylessness: after two failures, the wait for success starts afresh, so the past attempts are no evidence that success is due.
Problem 7
Errors arrive in a log at an average of per minute, as a Poisson count. Find the probability of no errors in a minute, of at most , and of at least , and show from the series that .
- .
- The three terms .
- The complement, instead of an infinite sum.
- The term is ; cancel against and re-index with .
- , , , The same trick on gives : for a Poisson count, mean and variance are equal.
Problem 8
A rare label occurs with probability per example, independently, in a batch of . Compare the exact binomial probabilities of and of occurrences with the Poisson approximation, and say why the approximation works.
- Binomial with , .
- Poisson with : Same mean.
- .
- Poisson: Agreement to about .
- Exact and ; Poisson and With , and as with fixed: many trials, each rarely a success, give a Poisson count.
Problem 9
A fair die is rolled, and if it shows , then fair coins are flipped. Let be the number of heads. Find and .
- Given , , so and Problem 4 with .
- The law of total expectation, with from Problem 2.
- The average spread within each value of .
- The spread of the conditional means, using .
- and Using only the first term, , would ignore the uncertainty in how many coins are flipped.
Problem 10
A bootstrap sample draws examples uniformly at random, with replacement, from a training set of . Find the expected number of distinct examples in it, evaluate it for , and find the fraction it approaches as grows.
- with if example is drawn at least onceCount distinct examples one example at a time.
- Each of the independent draws misses example with probability .
- Linearity; the are dependent, which does not matter for the mean.
- : About distinct examples out of draws.
- ; for ; the fraction tends to . So each bootstrap sample leaves out about of the training set, which bagging uses as a free validation set: the out-of-bag examples.
Where this goes wrong
1. E[X²] taken as (E[X])²
The expectation of a function is computed by putting the mean into the function.
- for the table of Problem 1Right so far.
- “ is the square of the mean.”The step that causes the mistake: expectation does not pass through a square.
- , so A variance of would mean is always , which it never is. , and . The gap between and is exactly the variance, so it is never negative.
2. Var(2X + 3) taken as 2 Var(X)
is linear, and the variance is expected to behave the same way.
- for a fair dieRight so far: Problem 2.
- “Scaling by doubles the variance, and the drops out.”The does drop out; the factor is what goes wrong.
- Variance is in squared units: doubling every deviation from the mean multiplies every squared deviation by . , and the standard deviation, not the variance, doubles.
3. Binomial probability without the coefficient
The probability of one particular sequence of results is written down, and it looks like the answer.
- independent examples, each correct with probability Right so far: Problem 3.
- “Eight correct and two wrong has probability .”True for one specified pattern, such as the first eight right and the last two wrong.
- There are patterns with exactly eight correct, each with that probability, so . Without the coefficient, the eleven probabilities would not sum to .
4. Adding probabilities that overlap
Three tries at a chance sounds like a chance.
- Each attempt succeeds with probability , independentlyRight so far: Problem 6.
- “Success within three attempts is success on the first, the second or the third.”Also right; the events are not disjoint, though.
- Adding counts the outcomes where two or three attempts would succeed more than once; with six attempts the same rule would give . Go through the complement: .
5. Variances of dependent indicators added as if independent
Problem 4 adds the variances of indicators, and the same move looks fine for the matching problem.
- with Right so far: Problem 5.
- “So is times that.”The step that causes the mistake: it needs the to be uncorrelated, and they are not.
- , which is for Every pair has a positive covariance , and the ordered pairs add , so for every . Linearity of expectation needs no independence; adding variances does.
Print this set: discrete-random-variables-and-expectation.pdf (problems, answers, and worked solutions on separate pages).
Read the guide: Expected value and variance of a discrete random variable: weight by the probabilities, then square before you average