Module 38 of 39

Counting, probability, and expectation for algorithms

Count possibilities and use conditional probability, random variables, and expectation with explicit assumptions.

Probability needs a list of possible outcomes and a rule for how likely they are. If a fair coin is tossed twice, the equally likely outcomes are HH, HT, TH, and TT. Two of them have one head, so the probability of exactly one head is 2/4. If the coin is biased, the four outcomes are not equally likely and counting alone is not enough.

Background for this page. You need fractions and the idea of possible outcomes. An expectation is a weighted average of a numerical result. Independence means learning one event does not change the probability of another. Related explanations: Randomized algorithms and expected running time · Sets, relations, and functions for algorithms.

The product rule says that if one choice has a possibilities and a following choice has b possibilities for each first choice, the pair has ab possibilities. The number of ways to arrange n distinct items is n! = n · (n − 1) · ... · 1. This is why a comparison sort must distinguish among so many possible orders. The number of ways to choose k items from n without regard to order is the binomial coefficient C(n, k).

With a biased coin, outcomes need probability weights.

For an event A, probability lies between 0 and 1. Conditional probability P(A | B) asks how likely A is after we know B happened, provided P(B) > 0. Two events are independent if learning one does not change the probability of the other. Independence is a claim to check, not a default assumption whenever an algorithm makes several random choices.

A random variable assigns a number to each outcome. Its expected value is a probability-weighted average. If X is 1 when an event happens and 0 otherwise, then E[X] = P(event). Such a 0-or-1 variable is an indicator. Linearity of expectation says E[X + Y] = E[X] + E[Y] whether or not X and Y are independent. That fact let us add the chances of record changes in the randomized-algorithms module.

A binomial count models the number of successes in a fixed number of independent trials with the same success probability. A geometric count models how many independent trials we wait for a first success. These models fit only when their assumptions fit the process. Bounds on the probability of a large deviation are useful when we need more than an average, but an expectation by itself does not say how often a very bad result occurs.

Add expectations even when events depend

Choose one position uniformly from three positions. Let X be one when position one is chosen, and zero otherwise. Let Y similarly indicate position two. Each expectation is 1/3, so the expectation of X+Y is 2/3. The events are dependent: if X is one, Y must be zero. Linearity still works.

Independence is needed for the usual multiplication rule P(A and B)=P(A)P(B), not for adding expectations. Confusing these rules can lead to an invalid probability argument even when the final number looks plausible.

Expected value also says little by itself about variation. Two running-time distributions can have the same mean and very different chances of a long delay. Tail guarantees require additional reasoning.

Practice. If an event happens with probability 1/4, what is the expected value of its indicator? Do two indicators have to be independent before their expectations can be added?

Answer. The expected value is 1/4. No. Linearity of expectation works even when the events depend on each other.

Try another case. A job always takes ten steps; another takes zero or twenty with equal probability. Do they have the same expectation and the same behavior?

Reasoning. Both expectations are ten, but only the first always finishes in ten. Equal means do not imply equal predictability.

View the full course and choose another topic.

Image preview

Fit