Thursday, August 20, 2009

Coke, Currency, and Contagion

Recently, there was a report, from the American Chemical Society, that about 90 percent of U.S. currency in circulation has detectable traces of cocaine on it. Apparently, the middle currencies—from Lincoln on up through Jackson—are the most susceptible. I guess Washington and Franklin don't rate. Also, not surprisingly, the percentage varies according to the community. Rural areas are less hit by cocaine-laden dollar bills, but in major metropolitan centers, essentially every piece of currency has coke on it. What's more, the percentage appears to be rising. In 1985, a study found that anywhere from a third to a half of bills had cocaine on them; in 1995, the proportion was three in four; and in 1997, it rose to four in five. Now it's nine in ten.

No need to panic, though. First of all, the traces are generally tiny, much smaller than a grain of sand, and not enough to get any kind of buzz from. And secondly, probably much, though apparently not all, of this increase has to do with the improved sensitivity of the cocaine sniffing tools.

The question is, how does cocaine get on all these bills? Certainly not all of the bills get cocaine on them because they were directly around the stuff, either during deals or during use. A small number do, of course, but the vast majority get them through contamination. But is that really plausible? Can so many bills be contaminated so quickly?

Well, let's take a look at that. Suppose that, initially, some small fraction of all the dollar bills have detectable cocaine on them; these are the initial set that get cocaine on them through direct contact with bulk quantities of the drug. Let's call this proportion p. The money isn't discarded, generally; it's put back into circulation (let's not get into how they get put back into circulation). Once that happens, those bills come into contact with other bills, which pick up some proportion of the drug. Apparently, there's an attraction between the drug particles and the green ink used to print U.S. currency.

When I use a bill, and it goes somewhere else, it now comes into contact with, let's say, one new bill. If a contaminated bill comes into contact with another contaminated bill, nothing happens to p, of course; both bills were already contaminated. Same thing holds true if an uncontaminated bill comes into contact with another uncontaminated bill.

But if the bill I had was contaminated and its new companion wasn't, or vice versa, then one new bill gets contaminated. The probability of this happening depends on the current value of p; specifically, it must be proportional to p (1 - p), since we need a contaminated bill and an uncontaminated one. We can put this in terms of a differential equation:

dp / dt = kp (1 - p)

The constant of proportionality k indicates how quickly bills come into contact with one another, and can be eliminated by setting the unit of time equal to the mean time it takes for a bill to be used (and therefore find a new neighbor). I don't have any hard figures, but from my own, non-cocaine-related currency use, it seems to be about a week or so. We can then set k = 1 and solve this equation fairly straightforwardly to yield the formula

p = C e t / (1 + C e t )

where C is closely related to the initial proportion of contaminated bills. (To be exact, C = q / (1 - q), where q is the initial proportion. Where q is very small, as in most cases, the two are almost exactly the same.) As t increases, C e t gets large pretty quickly, and p very quickly approaches 1. If, for instance, q = 0.000001—that is, one bill in a million is contaminated directly by the drug—then it takes a bit more than three months for the fraction of contaminated bills to exceed one-half. But because of the rapid growth of the exponential function, it takes only one more week for the proportion to exceed three-fourths. By the end of the fourth month, the fraction of uncontaminated bills is less than one percent. (Click to enlarge.)

That exceeds even the ACS's report. Why? Well, for one thing, even today's instruments are not perfectly sensitive; there still remain bills with undetectable traces of cocaine, surely. And after a while, there just isn't enough cocaine to go around (for the bills, that is). If, for the sake of argument, we assume that the initial fraction is one in a million, then the ACS's estimate of 90 percent contamination indicates that that first direct contamination can only be split about twenty times before it drops below undetectability.

But a second reason is that bills don't stay in circulation forever. According to the U.S. Treasury, currency stays in circulation, on average, for about 20 months—about 85 to 90 weeks. This makes the dynamical solution to the differential equation a bit more complicated. Let's simplify matters and only look at the equilibrium solution. At equilibrium, the contaminated dollar bills being taken out of circulation each week equal those being contaminated by new contact each week. That is,

p (1 - p) = rp

which yields an equilibrium solution of p = 1 - r, where r is the fraction of bills being taken out of circulation each week (about 1/85 to 1/90). So even with this new influx of bills, if detection tools were perfect, they'd detect traces of cocaine on about 99 percent of bills. Apparently, we still have a few rounds of "alarming" reports about cocaine contamination of currency to look forward to.

OK, here's a less overblown concern. The same model can essentially be used to analyze long-lived infections (such as oral herpes, which infects about 60 to 70 percent of all people worldwide). Such infections are removed from the population only when a person dies. As the above models show, if people were immortal, they'd eventually all be infected with such diseases (and in fairly short order, too). Of course, such diseases couldn't incapacitate their hosts too much, because otherwise they'd fail to be transmitted.

Thursday, August 13, 2009

Queueing Theory and You

Some thoughts on traffic—the automobile kind, not the network kind—while there's maintenance work going on in the office across the hall.

So the other day I'm driving into work, and I encounter not one but two traffic jams. Neither, as it turns out, was due to particularly heavy traffic loads. Rubbernecking (a.k.a. looky-looing) was the culprit in both cases. In both cases, the accident/attraction was off to the side of the road but managed to clog up the roads all the same.

I think it's generally underappreciated how much rubbernecking contributes to traffic jams. No one disputes that the accident itself can precipitate the jam. But a moment's satisfaction of curiosity? On the surface, it seems innocuous, right? As one of the drivers stuck in the jam yourself, you've already spent 10, 15, 25 minutes waiting behind this long line of cars—what could it possibly hurt to glance over for a second or two? But it's precisely that kind of glance that keeps the jam going. The reason for this lies in queueing theory, the study of waiting in lines, and comes about from the interplay between the level of traffic applied to a road, and the carrying capacity of the road.

Roads, like any other conduit, have a certain capacity, which is related to the size of the road but is also determined in large part by driving habits. You're taught, when you're driving, to leave at least three seconds of space between you and the car in front of you—more if it's dark or rainy or whatever. In the Los Angeles area, where I live, it's essentially impossible to do this; if you try, someone will invariably slide into the space, cutting yours down to a second or two, after which your options are to either to stay up close, or to back off until you're three seconds behind the new car, in which case the process repeats.

But actually the exact time is not all that important; what's important is that there is a characteristic following time, which determines the carrying capacity of the road. If the following time is two seconds, then the road can carry half a car per second (per lane). Note that this capacity is roughly accurate no matter how fast the traffic is going—whether traffic is flowing at the speed limit or crawling at 15 mph—as long as the following time is roughly the same. Only when traffic slows so much that cars take a significant time to travel their own body length (the following distance isn't head-to-head, but tail-to-head) does this rule break down.

Provided that that doesn't happen (and we'll get to that in a moment), we can now apply the most basic rule of queueing theory: If the amount of traffic going onto the road is more than the road's carrying capacity, traffic will come to a standstill. Hardly earthshattering news. If the amount of traffic is less than the capacity, traffic can flow. It might, however, flow incredibly slowly.

At first flush, this might sound kind of strange. If a road can carry a car every two seconds, and one car comes down the road only every three seconds on average, shouldn't there be enough room for cars to drive smoothly down the road, with quite a bit to spare? The perhaps surprising answer is that there might not be, and the fault lies in that phrase "on average."

If cars all scrupulously observed at least a two-second following time, and entered the road exactly three seconds after the previous car, then in fact, the cars would be able to flow at the speed limit. They would continue to do so even as you increased the rate of cars entering the road, up until the exact moment when that rate exceeded the capacity. At that point, the cars would start backing up and you'd get a traffic jam. And if you've ever been in a large traffic jam, it might seem that that's exactly what happened.

But that isn't in fact what happens. Generally speaking, the capacity of the road is not exceeded for long stretches. It's just very close. So why doesn't traffic flow smoothly, if the traffic load is less than the capacity? There are a few reasons, but the predominant one is that cars do not observe consistent following time, and don't enter the road at a constant rate. In queueing theory, variation kills.

Suppose that the following time is always at least two seconds, but that cars enter the road every three seconds only on average. Sometimes it's less, sometimes it's more. If it's less—let's say it's a second and a half—the new car now has to wait a half a second before it can proceed, because it's trying to maintain a minimum two-second following distance. On the other hand, if it's more, it doesn't have to wait at all. But it also doesn't try to speed up to catch up to the previous car; it's not trying to maintain exactly a two-second following distance, just a minimum of two seconds.

In short, if the time between successive cars is low enough, it slows traffic down, but no amount of time between cars will speed the traffic up. What's more, the closer the traffic rate gets to capacity, the more often a cluster of cars will arrive to slow down traffic, while the gaps between the clusters still fail to speed it up. We can express this effect graphically, by plotting traffic waiting time (a measure of the intensity of the traffic jam) as a function of the traffic rate R.


The exact shape of this graph depends on how following time and the time between cars entering the road vary randomly, but the basic effect is consistent: Instead of the waiting time (the blue curve) being constant at zero until R reaches the road's capacity C, it actually begins ramping up immediately, slowly at first but with increasing intensity until it spikes upward just as it approaches C (the dotted red line). And when you get close enough to C, the waiting time T gets large enough that you notice it as a honest-to-goodness traffic jam.

So what happens when people rubberneck? Yes, it's true, you might have been waiting for a long time, and you're only looking for a second or two. And you're still kind of driving at the time. But you slow down, just for a split second, and increase your following time. Instead of maintaining a minimum two-second following time, you increase it, maybe to two-and-a-half seconds. And if most everybody does this, the capacity of the road is effectively decreased, by 20 percent. It would have the same effect as closing one lane of a five-lane highway.

You might expect this to increase the waiting time T by 20 percent, but actually, what effect this has depends on how high R is compared to C. If it's relatively low—if we're on the left side of the curve—then moving C down by 20 percent, while keeping R the same, doesn't really affect T very much. But if it's already kind of high (and in Los Angeles, at least, it's that high about 24 hours every day), then moving C down by 20 percent can move you catastrophically high up that blue curve, increasing T many-fold and changing a mild nuisance into a dinner-delaying, or even dinner-cancelling, jam.

But that's OK. You just go ahead and look at that upside-down pickup. What could it hurt?

Monday, August 3, 2009

Slashed Back

I'd like to call your attention to our latest scourge: Well-meaning radio announcers who, while reading out URLs in commercials, refer to the ordinary slash (/) as a "backslash." Why they feel compelled to even use the word "backslash," goodness only knows, since most people only ever come into contact with the ordinary slash; the backslash is almost exclusively used by DOS and LaTeX heads.

We now return you to your regularly scheduled rant.

Tuesday, July 28, 2009

The Chinese Script Is, or Is Not, Phonetic

A new post to satisfy the likes of the most evil being...in the universe.

My favorite Chinese app (whose continued absence from the iTunes Store is delaying my inevitable purchase of an iPhone) is headlined by a dictionary edited by the recently departed John DeFrancis, who taught Chinese for years at the University of Hawai'i. Aside from his 12-volume Chinese language textbook series, he's probably best known for The Chinese Language: Fact and Fantasy, an accessible deconstruction of several myths regarding the Chinese language: that the Chinese script is ideographic, for instance, or that it is specially tailored to facilitate communication between speakers of mutually unintelligible dialects.

At one point, DeFrancis goes even further and suggests that the Chinese script is not even logographic, with each character signifying a morpheme, but simply phonetic, with each character signifying a phoneme—albeit a tremendously inefficient phonetic script, since in many cases it has dozens of characters representing a single phoneme. He's talking here about what most people would consider homophones: characters like 出 to exit, and 初 the first or opening of a series, both of which are pronounced chū in Mandarin (the most widely spoken dialect). This idea is prelude to a discussion of various proposals to do away with Chinese characters entirely, using in their place a properly designed phonetic script.

DeFrancis's interpretation isn't as crazy as it might sound at first. In at least one limited case—the transliteration of foreign terms—the Chinese script is exactly an inefficient phonetic script. Lacking an official alphabet or syllabary, Chinese represents foreign terms using a sequence of characters, such as 巴巴多斯 bābāduōsī for Barbados. The inefficiency lies in that unless you've previously looked this term up, you'd have no good idea which four characters ought to be used to write out Barbados. There are lots of equally effective ways to write out Barbados using Chinese characters...but only one way that is considered "correct."

More generally, even though the distinction between the written forms 出 and 初 might seem vital to Chinese readers, since they mean different things, the morphemes represented by those characters are used all the time in speech, where there is nothing but context to distinguish them. Apparently, "nothing but context" works pretty darned well. There are written passages that consist of nothing but a long string of homophones—a sort of extended pun—but the fact that these are elaborately conceived literary jokes is actually an indication that the semantic disambiguation the different characters provide isn't strictly speaking necessary.

But I think that last point is an indication of why the Chinese script is not a phonetic script, or at least not only a phonetic script. Because even though the semantic disambiguation isn't necessary, I think it's pretty hard to argue that it doesn't help. One can read a passage in Chinese that is rendered only phonetically, but to someone who's literate, it's a lot faster with ordinary characters. Whether we're talking about an alphabetic language like English or a logographic one like Chinese, people typically read a lot faster than they can speak. To me, that indicates that there's something going on in the reading process other than just reproducing the sounds of speech. Robert Ramsey wrote in his book The Languages of China that there's still a lot we don't understand about the way Chinese people read. Without understanding more about that, it's premature to conclude that Chinese characters are a poor stand-in for a syllabary.

Saturday, June 27, 2009

Do Not Pass Text Design, Do Not Collect $200

A long time ago (I won't say just how long, but it'll soon become fairly obvious), I worked my first summer job at a computer-controlled font engraving place in Mountain View called Xybergraphics. Lots of stories from that summer, which I'll eventually get to when I want to talk about what happens when a 95-pounder drinks 42 ounces of caffeine-laden soda pop, or the beginnings of my fascination with the Police, or what a cubic spline is.

At any rate, my job, which paid me the princely sum of $2.85 each hour (workers under 16 could be paid somewhat under minimum wage), required me to encode fonts for the aforementioned computer-controlled engraver. Typically, the engraved letters would be in the neighborhood of an inch or two in height, but for better precision, the letterforms I worked from, designed by my immediate supervisor, were about 14 inches tall and drafted in pencil on vellum. My job was to take a mouse (this was before the Mac, mind you), and trace along the letterforms, clicking at appropriately spaced points, until the letter was entirely traced. A simple letter, like a capital I, might require 50 points; a more involved letter, such as lower-case m, might require as many as 150. This was a tedious and time-consuming job, you could well imagine.

But I'm nothing if not efficient at boring tasks, especially if I've got my tunes in the background, and in the meantime, I learned quite a lot about fonts—what they should look like, how features are shared in common by various letters, what design rules not to break, and so forth. All this that I learned is both fascinating (to me) and almost entirely useless, which means that it has lodged tight in my memory banks and won't budge.

As a result, I'm exceptionally sensitive to bad typography. For example, I was walking one day in the Denver airport, waiting for my next flight, when a store sign caught my eye. Bad Typography Alert! The capital A in the sign was reversed; its broad stroke went down to the left, rather than to the right, as it should in traditional serifed fonts. And this on the sign for a stationery store! I think you will properly apprehend the depth of my mania when I tell you that I actually reported it to the clerk. She seemed quite receptive to my complaint, although she probably promptly forgot about/rejected it as soon as I turned my head.

There's a similar problem with one of the signs where I work—the name of the building has a capital V in it, with its broad stroke, again, going down to the left. Alas, this is welded on and probably unmulliganable.

But my encounter with the very bottom, the absolute worst, came when I began frequenting a supermarket that opened near our house. There was little wrong with the typography at the supermarket, but across the street was this abomination:


This signage is simply staggering in its wrongness. It's hard for me to convey just how staggering, but the fact that you're reading this post is some indication. Click on the image to see an enlarged version and look at this tangle of thorns.

Practically any letter that could have been misplaced, was, and those that weren't, seem to have been correctly placed to highlight how wrong their identical partners were. To wit: The E in GERMAN is upside-down. The M and A in the same word are backwards. Unbelievably, the N is correctly placed. A mistake must have been made.

In CAR, the C is upside-down. The A is backwards. After the bad E in GERMAN, the E's in SERVICE are right, but the S and the C (again!) are upside-down, and the V is backwards.

The M in BMW is backwards. The W is very strange, it seems to have been made with a pair of leftover V's, both with one stroke broken in half. If so, they should have broken the other stroke on the right half, but I'll give them credit for showing some resourcefulness.

The horror continues in Volkswagen. This V is correct (what happened to the V in SERVICE?), but the l is backwards, the s is upside-down, and the w and even the g (!) are backwards again. How do you screw up the g? The lower-case s is upside-down again in Porsche, and the lower-case c joins its capital brethren in being inverted also.

The Audi is correct, but is set in a narrower font. Must have been added on later.

Considering that many of the letters couldn't have been placed incorrectly (either because they're totally symmetrical or totally unsymmetrical), the percentage of letters placed incorrectly runs at about 15/22 = 68 percent, by my reckoning. A dolphin without opposable thumbs flinging letters up randomly with its tail could have done better.

OK, I realize that I'm probably clinically unhinged on this point, but can we agree that someone screwed up royally here? I mean, please.

Tuesday, June 23, 2009

Game Theory and the Wing-Block Dynamic

In 2004, when the Lakers played the Pistons in the NBA Finals, a lot was made of Kobe Bryant continually jacking up outside jumper after outside jumper—none too efficiently, most of the time—while monster center Shaquille O'Neal was taking fewer shots, but making them much more efficiently. On the surface, it sure seemed as though Shaq should have been getting more shots, and of course Shaq, never a wallflower at the quietest of times, was not loathe to point this out.

In 2009, when the Lakers played the Magic in the NBA Finals, a lot was made of Kobe Bryant continually taking jumper after jumper—somewhat more efficiently than before—while his "newly tough" post player Pau Gasol was taking far fewer shots, but making them more efficiently. On the surface, it sure seemed as though Pau should have been getting more shots, and surprisingly Pau, generally a quiet fellow, pointed this out with a certain degree of mordacity.

Obviously, in retrospect, the two series turned out rather differently for the Lakers, which is why the former case was judged by many as the reason the Lakers lost the series, and the latter is just a footnote. Bryant's reputation as a ballhog, already in force before the 2004 Finals, was substantially bolstered by that series, and has only just faded within the last year or two. But is that fair? Is that the only possible interpretation for Kobe's shot-taking? Or could ballhoggery conceivably help a team?

Let me be clear here. There's no question in my mind that Kobe could stand to take fewer shots than he does (unless he's just red hot). The question isn't whether he should take as many shots as he does, but whether he should take shots even when he's shooting them at a lower percentage than the post players. And this really goes for any wing player who dominates the ball (e.g., LeBron, Wade, etc.). I just mention Kobe because I watch all the Lakers games.

I'm going to look at this from a game theory standpoint. Put into elementary game theory terms, Kobe and the Lakers have a set of tactical options, and the defenders have a set of tactical options. If each side optimizes its strategy with respect to the other side, then in the end, the game will reach what's called a Nash equilibrium: Neither side can improve its result by changing its strategy unless its opponent changes it too. (The equilibrium is not named after award-winning point guard Steve Nash of the Phoenix Suns, but John Nash, award-winning mathematician and subject of the award-winning book/movie, A Beautiful Mind.)

Suppose we simplify matters by assuming that the Lakers have just two options: Kobe shoots, or Kobe passes to the post, which then shoots. And the opponents likewise have just two options: double Kobe, or play man-to-man. And naturally, we assume that Kobe shoots a better percentage over man defense than over a double team, and the post shoots better when Kobe draws a double team than when the defense plays man-to-man.

The conditions of the game do not require either side to do the same thing each time. Strategies can be mixed. So Kobe can shoot 60 percent of the time, and pass 40 percent of the time. The defense can double 70 percent of the time, and play man 30 percent of the time. The defense can even have partial strategies like a weak double versus a strong double. Under these simple assumptions, it's fairly straightforward to find the Nash equilibrium, where neither side can unilaterally improve their result. What's interesting about this Nash equilibrium is that both Kobe and the post should shoot exactly the same percentage.

Plainly, that doesn't happen very often. Very often, Kobe shoots a lower percentage than the post (even when factors such as free throws and the three-point line are taken into account); by comparison, it's relatively rare that it happens the other way around. Ostensibly, with Kobe shooting the ball so much, he's not adequately punishing the defense for doubling him. He should instead pass the ball into the post more, gradually causing the defense to double less and play more man defense, up to the point where his shooting percentage rises to match that of the post.

[EDIT: The rest of this post is largely different from what it used to be, because what follows totally swamps in significance what used to be here.]

Having said all that, I'm going to go back and suggest that that strategy actually isn't optimal. How can it be sub-optimal, if it's at the Nash equilibrium? Because the game doesn't stop when the ball hits the rim, so the game theory shouldn't, either.

When players shoot the ball against straight-up defense, the defense has the advantage on rebounding any misses, because they're usually between their man and the basket. However, when a perimeter player shoots against a double team, the rest of the players have a man advantage. In our scenario, this advantage plays out in the post, which means that (a) the chances are much improved for an offensive rebound, and (b) if an offensive rebound is gained, it usually leads to a high-percentage shot.

What effect does that have? Suppose that the man advantage on rebounding leads to an increase of 15 percent in the offensive rebound rate; for example, if the offensive used to get 20 percent of the rebounds, they now get 35 percent. And suppose also that this leads to a successful shot 60 percent of the time. If the wing player misses, let's say, 60 percent of his shots against a double team, and he faces a double team 50 percent of the time, the offensive rebounds effectively amount to an increase in shooting percentage of 0.5 × 0.6 × 0.6 × 0.15, or 2.7 percent. That doesn't sound like much, perhaps, but it's about a standard deviation's worth, the difference between a top-10 guard and a middle-of-the-road guard. And it's how much worse the wing should shoot than the post at the true optimal strategy.

Again, I'm not suggesting that this is how Kobe thinks (although I'm pretty sure he does think that his misses can lead to easy baskets for his team), or that Kobe shoots exactly as much as he ought to. But it might explain why, even if he's shooting a lower (true) percentage than his post players are, he shouldn't necessarily shoot it less.

Thursday, June 18, 2009

Inconsistent Bracketology and Non-Transitivity

Does anyone who routinely does NCAA playoff brackets know the answer to this one? Can you fill in a bracket inconsistently, so that you have (let's say) team A beating team B and team C beating team D in the first round, and yet you have either team B or team D coming out of the second round?

Because it's not hard for the probabilities to come out that way. One simple way is for A and C to be mild favorites over B and D, respectively, but for B and D to be prohibitive favorites over C and A respectively. (Matchups between A and C, or between B and D, can be pick-ems.) So you fill out your bracket to have A and C come out of the first round, but either B or D to come out of the second round. This requires a certain amount of non-transitivity in the teams: For instance, A edges B, which trounces C, which edges D, which trounces A again. But that's hardly unknown in the basketball world, and is usually trotted out as the inevitable "matchup issue" between two teams.

Somewhat more surprising is that it's possible for the same inconsistency to happen without any non-transitivity. Suppose A is a huge favorite over B in the first round, while C is a mild favorite over D in the second round. So you have A and C come out of the first round. But suppose C is also a mild favorite over A, but D is a huge favorite over A. There's no non-transitivity—you can place the teams in the total ordering C > D > A > B—but D is nevertheless the favorite to come out of the second round, despite not being the favorite to come out of the first.

Even though there's no non-transitivity in the second example, it's vaguely unsatisfying because it doesn't match our intuition. We'd like to think that since C beats D, C should be a bigger favorite over A than D is. But the inconsistent bracket result only comes about here because that intuition is violated. So, the semi-open question ("semi-open" because I suspect it won't be that difficult to resolve): Is it possible for a set of tournament contestants to fall under a total ordering in the intuitive way suggested above, and still yield an inconsistent playoff bracket in a binary, single-elimination tournament? It need not be limited to a four-team bracket, but it does have to be 2n for some n.