## What the hell is a harmonic mean? OR: A few thoughts on traffic intersections

July 14th, 2011 | Published in amth, math, maths, meth, minth, morth

*"In which Karl works through math he should have understood a decade ago"*

### Traffic

Something I was thinking about as I biked about my current home of North Austin, where they have been adding traffic lights to accommodate the increasing population...

Suppose you are trying to time a stoplight. That is, you are tasked with setting the durations, order, and whatnot of the various stages of the light cycle. What quantity exactly do you try to maximize?

So actual traffic intersection design is a tricky balancing act--you want to get lots of cars through, but you're not going to want to make it unsafe in doing so. You'll also want some notion of "fairness"--it's a Bad Thing to keep a car stopped at a red light for 20 minutes, even if by so doing you maximize the number of cars getting through the intersection per minute.

Let's simplify, however, and just think about maximizing one variable. What do we choose? Well, the first thing I thought of was throughput--the number of cars getting through the intersection per minute. A well-designed intersection will try to maximize this.

Suppose, however, we don't have a way of measuring this directly, but we do have the cars' average speed from the time they enter the vicinity of the intersection to the time they leave it. What then? (You may ask yourself what "average" speed here means--that's actually exactly what we're going to answer.)

The first thing I thought in my Texas-summer-induced haze was that we would just maximize the arithmetic mean of the cars' speeds. The arithmetic mean will give us the "average", right? Well...

### What is the Harmonic Mean?

I didn't hear the phrase "harmonic mean" until about my junior year in college, and I didn't really figure out what it was until somewhat recently. Basically, the harmonic mean is the best way to average rates of things--if you have a bunch of quantities which all take the form "unit1's per unit2" (miles per hour, price per earnings, etc.), then the harmonic mean is likely the average you want.

If you have numbers , then the harmonic mean of them is given by

Say we fix a distance and have two cars drive distance . The first goes speed and the second goes speed . What do we want out of an "average speed"? Well, we want a speed such that if a single car were to go distance at speed , it would take the same amount of time as if the car were to travel the first distance at speed , and then travel the second distance at speed . This is exactly what the harmonic mean gives us! (A short proof of this fact appears at the end of this note; if it's not obvious to you--it wasn't to me--give it a look, or, better, try to prove it yourself--it's not complicated.)

### So...

So I realized I want the harmonic mean instead of the arithmetic mean! Neat! Done! We'll time the stop light in such a way that we maximize the harmonic mean of the speeds of the cars within some vicinity of the intersection. That'll be $200,000 please, City of Austin.

### That was an unsatisfying explanation.

I agree! So I sat down and tried to think of a more convincing argument that maybe a well-designed intersection should maximize the harmonic mean of the speeds of the cars traveling through it.

A wholly and totally reasonable quantity to want to minimize is the sum time all the cars spend in an intersection. That is, suppose we have cars, and each car spends time in the vicinity of the intersection. Then we will want to minimize the function

A bit more accurately, suppose we have a stoplight setup . The time it takes car to get through the intersection is a function of . So really what we want to minimize is

Great. Let's further suppose the distance traveled by these cars is (that is, the length of the "vicinity" to which we're referring is ). Further, each car goes an average speed , which is a function of the stoplight setup: different intersection setups will yield different average speeds for cars. (Again, the "average speed" of a car here is going to mean what you probably think it means if you've gotten this far.)

So let's do a little bit of algebra! If you're not familiar with the notation, gives you the value of at which the function is minimized. Similarly, gives you the value of at which is maximized. The variable with respect to which we are finding the argmin/argmax here is something like "all conceivable intersection setups".

(The first step holds because ; the second step holds because the numerator will stay the same regardless of what we choose for ; the third step holds because for any ; the fourth holds for the same reason as the second.)

Neat! We have shown that constructing our intersection to minimize the total amount of time cars spend in it is the same thing as designing our intersection to maximize the harmonic mean of the speeds of the cars within its vicinity (and NOT the arithmetic mean).

This isn't a particularly interesting result, but it wasn't immediately obvious to me.

Anyways, moral of the story: **if you are averaging rates, think very seriously about using the harmonic mean.**

### So is maximizing the harmonic mean of velocities really different from maximizing the arithmetic mean?

Yes! Suppose we are considering two stoplight setups and , and suppose we are running a simulation with two cars. In , the two cars have velocities 1 and 9. In , the cars have velocities 4 and 4. Now,

So if we are picking stoplight setups based on harmonic mean of velocities, we'll pick . However, if we're picking based on arithmetic means, we'll pick (which gives an arithmetic mean of 5, vs 's arithmetic mean of 4).

So the type of average we use matters!

(You can verify for yourself that minimizes the total time spent in the intersection.)

### Post-scriptum: Promised proof of above statement

OK, so we are showing that if car 1 goes distance at speed and car 2 goes distance at speed , then the ideal average speed is the harmonic mean of and . By "ideal average speed" I mean simply the following: suppose we have car 3, which goes distance at speed . Then it takes the same amount of time for car 3 to go at speed is it does to go at speed and then at speed . Denote by the time it takes car 1 to go distance ; denote by the time it takes car 2 to go distance ; denote by the time it takes car 3 to go distance . Then what we want to show holds is that

In fact, the average speed that makes this true is the harmonic mean of and .

Assume is the harmonic mean of and . Then:

This completes the proof.