Mental Models204 cards · 12 fields

Mental Models/Computing & Information

Overfitting

过拟合

Treating flukes in old data as patterns looks accurate until you use it.

When you've been sick, you have probably searched online for "fever" or "sore throat." In 2008 Google launched Flu Trends, which counted searches like these to estimate where in the United States the flu was going around. From a huge pool of search terms the researchers picked out a few dozen. The test was whose search volume rose and fell most closely in step with the flu doctor-visit records kept over the years by the US Centers for Disease Control and Prevention. A curve built from those few dozen terms, laid over the visit records of the past few years, matched them tightly.

But when terms are picked only on how well they match, terms that just happen to rise and fall in winter along with the flu get in too: "high school basketball," whose season happens to fall in winter, at one point also looked related to the flu. The team removed terms like that, which were obvious at a glance. Ones that weren't obvious could still be in there.

In the spring of 2009, H1N1 flu broke out in a season when flu doesn't usually spread, and the system underestimated the first wave. Part of why it had matched the past few years so well was a coincidence: flu always came in winter.

You're studying for a final exam. You learn ten past papers by heart and get 98% right on practice runs; the real exam is a new paper, and you get 80% right. What you memorized includes reasoning that carries over to new questions, and also accidents that belong only to those ten papers, such as one question's answer happening to be C. However much of the second kind you memorize, it won't earn you a single point on a new paper. A model looking for patterns in old data can do the same thing. It records accidents that belong only to this batch of data as if they were patterns, so it scores very high on the old data and goes wrong on new data. This is called overfitting.

9th-degree curve hits every point, swings between
Actual stretch9.5 cm
Straight line9.6 cm
9th-degree curve12.4 cm
051015246810Weights hungStretch in cm9th-degree curveStraight lineActual stretch
0510152.557.510Weights hungStretch in cm9th-degree curveStraight lineActual stretch

Ten spring readings, 1 to 10 weights, each a bit off. Drag "New weights" to compare; at 1.5 the curve's stretch is negative. "Reading error" at 0 merges the lines.

The more a curve can bend, the closer it fits the old points and the wilder it gets between them

Suppose you hang 1 to 10 weights on a spring, one more each time, and write down how many centimeters it stretches. The spring really stretches one centimeter for each weight, but each reading of the scale is a little off. So the ten readings, drawn as points, lie roughly along a sloping line, each a little above or below it. Fit a straight line to the ten points and it misses each one by a bit, and the misses are exactly the reading errors. A quadratic curve misses by slightly less. A ninth-degree curve has ten coefficients, and ten readings are just enough to pin it down completely: it bends to pass through every point, with zero error.

But the places it passes through carry the reading errors. To satisfy ten points that sit above and below at the same time, it has to swing back and forth between them, and the swings get bigger toward both ends. Hang 9.5 weights and the spring should stretch 9.5 centimeters. The straight line estimates 9.6; the ninth-degree curve estimates 12.4. The higher a curve's degree, the more it can bend, and the more slack it has to chase these chance errors. On the old points the error falls all the way to zero. On new points the error is smallest around the straight line and the quadratic, and rises as the degree goes higher.

The more rules you try, the more chances for one to win by luck

You can overfit without fitting any curve, just by screening rules. Test hundreds of indicators and time windows on the same stretch of history, and a few will match the outcome purely by coincidence. The short rule you end up keeping looks simple, but there are hundreds of attempts behind it.

Independent data can check a model only once

Build the rules with one part of the data, choose between options with another part, and finally keep back a set nobody has touched to check the result. When predicting the future, the checking data should come from a later time than the data the model was built on; only then is it like predicting something that hasn't happened yet. Every time you look at the check score and go back to change the rules, the checking data turns into practice questions, and you need a new set. Limiting the parameters (such as the curve's degree) and deleting exceptions patched in along the way means chasing fewer chance details. After the changes, check again on a new set of data.

Overfitting is a problem only when you use limited data to infer cases you haven't seen. When all you need is a faithful record of what happened, every detail should be kept. Complexity itself is not the illness: with enough data and the right constraints, a complex model can predict more accurately. When a new system or a new population changes a relationship that used to be stable, predictions fail too, but that is the world changing; the model didn't learn the old samples wrongly. Cut parameters from every complex model on sight, and what you cut may be the real pattern.

Explain
A recognition program scores full marks in lab tests and keeps making mistakes in the field: what it memorized was the lighting and backgrounds of the test scenes, not the thing it was meant to recognize.
Predict
Estimate how much of a stock-picking rule screened out of dozens of indicators will still hold next year: the more indicators were tried, the bigger the discount.
Design
Set the rules for an internal algorithm contest: the leaderboard uses only public data, and final rankings are scored on a set of data released only after the contest, so contestants can't tune their parameters to the leaderboard.
  1. Does this pattern explain results already known, or predict results that haven't happened yet?
  2. How many indicators, time windows and exceptions were tried to find it? Go back through the original experiment records.
  3. Is there a set of data that played no part in choosing the rule? In a later period or with another group of people, can it still beat a simple baseline?

Investing

Butter explains the stock market

In an article published in 2007, David Leinweber fit the historical level of the S&P 500 using butter production in Bangladesh, and the fit was quite good. When he added other farm variables, the fit got even better.

Business

Promotion rules get finer and finer

You analyze last month's orders. First you choose to send coupons on Fridays, then you add age, city and number of page views, until every purchase can be explained by the rules. Once the rules go live, new customers order at almost the same rate as people who got no coupon. Next you have a new batch of customers get a coupon or not at random, and compare only the extra orders in this batch.

Personal life

Writing coincidences into habits

A runner looks back on the three races he won and finds that each time he wore a blue vest, drank coffee beforehand and arrived ten minutes early, so he makes the three into a routine for winning. For the next several races, he writes down what he wore, when he arrived and how he did. He keeps only the preparations that have a reason behind them.

OverfittingNormal vs Fat-Tailed DistributionsExtremes happen far more often than the bell curve says, so everyday averages make risk look small.You're setting the flood standard for a reservoir. You have fifty years of water-level records, and one huge flood last year stands far above all the rest. When predicting water levels in ordinary months, you shouldn't bend the curve for this one point; it is most likely chance. But setting the height of the dam is about exactly that most extreme point. In the view of normal versus fat-tailed distributions, a single extreme value in a fat-tailed distribution can decide the whole loss. For everyday prediction, treat it as noise; for the flood limit, set it by that point.OverfittingMultiple ComparisonsThe more things you try, the more likely you are to hit a "discovery" that's pure chance.A drug trial misses its endpoint overall. The analysis team splits the patients into twenty groups by age, sex and region and tests each one, and one group comes out significant. Multiple comparisons tells you that the more ways you split, the greater the chance that one significant result turns up by luck. Overfitting asks the question that comes after: does this rule still hold for new patients who weren't part of the splitting?OverfittingOccam's RazorWhen several explanations all fit, start with the one that needs the fewest assumptions.Two prediction schemes score the same on old data. One has three conditions, the other thirty. With no other information, Occam's razor has you keep the simple one first. But once there is new data, if the complex one does clearly better on it, it should stay. What overfitting cares about is the score on new data; the number of conditions is not the test.

You price second-hand homes at a real estate agency. You build a model on 800 sales records, and another 200 sales play no part in building it. Option A uses 3 variables: its average error is 120,000 yuan on the 800 and 130,000 yuan on the 200. Option B uses 40 variables: its average error is 20,000 yuan on the 800 and 190,000 yuan on the 200.

Which option should you use to price new listings?

You have data on 1,000 users and want to predict who will leave. The analyst first picks out the 20 indicators most related to leaving, using all 1,000 people, then splits them at random into 800 for training and 200 for testing. On the test set the hit rate is 88%. After launch, the hit rate is only 61%.

Why did the 88% on the test overstate how the model would do after launch?

A community clinic has only 200 patient records and wants to build a program that predicts whether a patient will come back for a follow-up visit. It can't get any more data. The first version used 60 fields and got 99% right on these 200 records; on new patients it dropped to 70%.

Which of these steps can narrow that gap?