Expectation

After thiswhat you will be able to doCompute the expected comparison count of a front-to-back search from a stated distribution, and show how changing the weights changes the average.

Questionwhat this lesson answersThe average case averages a cost over inputs, but an average has to know how much each input counts. What is that average actually taken over, and why is the weighting a choice you make rather than a fact of the problem?

Not coveredwhat this lesson leaves outWe define the expected value of a cost that takes finitely many values with known weights, and we compute it for one search. We do not prove the laws of probability, and we take the weighting as given rather than arguing for which one is right.

The phrase “average case” can sound as if an algorithm comes with one ready-made ordinary cost. It does not. Consider a search that begins at the front of a list and checks one item after another until it finds the target, the value it was asked to find. This front-to-back examination is a scan. Each check against the target is a comparison. If the target is in position k{k}, the scan makes k{k} comparisons. A target at the front costs one comparison; a target at the end of a list of n{n} items costs n{n}. The algorithm has a different cost for each possible position.

For one fixed list size, each possible comparison count is one possible result. A possible result of this kind is called an outcome. The count changes according to which outcome occurs. A number whose value is selected by an outcome is called a random variable. Here the random variable is the number of comparisons: it can take the values from one through n{n}. Naming these outcomes does not say how often they happen, so it does not yet give an average.

Decide how much each outcome counts

To form an average, attach a share to every possible position. Each share must be at least zero, at most one, and all the shares together must make one whole. It says how much that outcome counts when we combine the costs. Such a share is called a weight. When the weight is read as a claim about how often an outcome would occur across many searches, it is a probability. The complete list of outcomes together with their weights is called a distribution.

The numbers do not have to be guessed. They may come from a stated model of how requests arrive. Until that model is stated, the arithmetic has no single average to calculate.

This is a choice, not a fact revealed by the scan. Suppose every position receives the same share. An even spread of weights is called uniform, and a distribution with that even spread is a uniform distribution. With n{n} positions, each one has weight 1n{\dfrac{1}{n}}. That gives the front position no special privilege and the final position no special penalty. It is a clean assumption, but it is still an assumption.

There is now a precise way to combine the costs. Multiply every possible comparison count by its weight, then add the results. An average made by that multiplication-and-addition rule is called a weighted average. For outcomes treated as probabilities, that weighted average is called the expected value, or expectation. Write it with the plain letter E{E}, for expected value. For the uniform scan, it is

E=k=1nk1n=1n(1+2++n).\begin{aligned} E &= \sum_{k = 1}^{n} k \cdot \dfrac{1}{n} \\ &= \dfrac{1}{n}\left(1 + 2 + \cdots + n\right). \end{aligned}

The summing-a-loop lesson obtained the triangular sum by pairing its rows. It gives

1+2++n=n(n+1)2.1 + 2 + \cdots + n = \dfrac{n(n + 1)}{2}.

Putting that value into the weighted sum gives

E=1nn(n+1)2=n+12.\begin{aligned} E &= \dfrac{1}{n} \cdot \dfrac{n(n + 1)}{2} \\ &= \dfrac{n + 1}{2}. \end{aligned}

Under the uniform choice, the scan therefore makes about half as many comparisons as the worst case:

n+12n2.\dfrac{n + 1}{2} \approx \dfrac{n}{2}.
Step 1 of 4

Give every position equal weight

Begin by treating every target position as equally likely. The balance point lands halfway to the farthest possible comparison count.

Choose the input assumption

Weights set where the average balances

Every bar is a possible target position. Its height says how much that position counts in the average. The triangle under the beam marks the resulting balance point.

Position weighting
weight0.12510.12520.12530.12540.12550.12560.12570.1258average 4.5target position

The bars use the same vertical scale in both views. Changing their heights changes the balance point, even though the checks performed by the search stay the same.

Positions
8
Expected comparisons
4.5
Worst case
8

equal weight at every position gives 4.5 expected comparisons out of a possible 8.

The average belongs to the assumption too

The result n+12{\dfrac{n + 1}{2}} is not a fact about the scan alone. It is a fact about this scan plus the uniform distribution. The scan has not promised that targets arrive evenly across the list. If a target is almost always near the end, later outcomes receive more weight. Multiplying those larger weights by larger comparison counts moves the expected value toward the worst case. If targets are usually near the front, the same calculation moves the expected value in the other direction.

Nothing in the code changed. The possible outcomes remained the same numbers, and the cost of an outcome remained its position. Only the distribution changed. That is why “average case” is meaningless until the weighting is named. Saying merely that a cost is average hides the part that decides what is being averaged over.

The expected cost is still a function of n{n}. Under the uniform assumption it is n+12{\dfrac{n + 1}{2}}, which grows in step with n{n}, a linear term. It can therefore be classified with O{O}, Ω{\Omega}, and Θ{\Theta} in exactly the same way as any other cost function, using the machinery the earlier O(f) lesson built. Classifying it does not turn the chosen distribution into a universal truth.

An average can also conceal a bad outcome. A rare case can contribute little to the weighted sum while still being unacceptable when it occurs, and then the comfortable typical result is the wrong summary. Placing this weighted average beside the smallest and largest costs for the same algorithm, so the three finally come apart, is what the doors below lead to.

Doorswhat to read next, and why

Symbolswhat each one means, and whether we defined it, measured it, or just started there

nStatus: defined
the number of items in the list being searched
kStatus: defined
the position of the target in the list, which is also the number of comparisons the scan makes to reach it
an outcomeStatus: defined
one of the finitely many values the cost can take on a given input size
a weightStatus: defined
how much an outcome counts in the average, a number between zero and one, with all the weights adding to one
the distributionStatus: defined
the whole list of outcomes with their weights, which is the assumption you are choosing when you say "average case"
EStatus: defined
the expected value, the weighted sum of the outcomes, which is the average this lesson is defining
What these classifications mean
defined
circular by construction, true because we chose it
empirical
a measured claim about the world that could have come out otherwise
bottoms out
a primitive of the model, with nothing under it here
door
used here, explained elsewhere