ECON 410 · Econometrics II: Causal Methods and Time Series
Machine Learning and Economics: Prediction Versus Cause
Machine learning is superb at predicting, but predicting an outcome and finding its cause are different jobs.
Key takeaways
- Machine learning mainly aims to predict an outcome well on new data, not to explain why it happens.
- A model that fits its training data too closely overfits and predicts badly on new cases.
- Splitting data into training and test parts, and cross-validation, are how we measure real predictive skill.
- Regularization, such as lasso, shrinks or removes weak predictors to avoid overfitting.
- A strong predictor is not a cause; policy questions need causal designs, which new methods increasingly combine with machine learning.
A weather app can tell you with fair accuracy that it will rain tomorrow, by watching dark clouds, falling pressure and what the weather did in nearby towns. It can do that without knowing why rain forms. And here is the catch: if you carried a bag of umbrellas around, you would not make it rain, even though umbrella sales are a fine predictor of rain.
That small joke holds the central tension of this lesson. Machine learning is a set of computer methods that find patterns in data to predict things. It is astonishingly good at the job. But the earlier lessons in this course were about a different goal: finding what would change if we did something. Prediction answers "what will happen?" Causal inference answers "what would happen if we changed this?" Economists use both, and mixing them up leads to expensive mistakes.
Two different questions
Imagine a bank. One question is: which borrowers will default? If the bank can predict that well, it can set higher interest rates for risky borrowers or turn them down. For this, it does not matter why a certain pattern predicts default. A model only needs to rank people accurately.
A second question is: if we raise this borrower's credit limit, will the borrower default more or less? That is a causal question about an intervention. The pattern "people with high limits default less" is partly because banks give high limits to people who were already safe. So the number in the data is not the answer to the "what if" question. This is the selection problem from selection bias and matching in a fresh disguise.
The framework in the potential outcomes lesson marks the difference. A prediction model estimates the outcome we will see. A causal model compares two outcomes for the same person, one with the treatment and one without, and we only ever see one of them.
Overfitting: learning the noise
Prediction would be easy if we only cared about the data we already have. A model with a thousand knobs could memorize every point. The danger is overfitting: fitting the quirks and random noise of the data you used, which do not repeat in new data.
Think of a student who memorizes last year's exam, down to the printed typos. That student scores perfectly on a repeat of last year's test and fails when new questions appear. The memorizing did not build skill.
The remedy is to hold some data back. We split the data into a training set, used to build the model, and a test set, kept hidden until the end, used to see how the model does on cases it has not seen.
A worked example: simple versus complex
Suppose we fit two models. Here are the leftover errors (actual minus predicted) on five training points for each model:
Simple model errors on training data: 2, −2, 1, −1, 2
Complex model errors on training data: 0.5, −0.5, 0, 0.5, −0.5 A common score is the mean squared error (MSE): square each error, then average.
Simple, training: (4 + 4 + 1 + 1 + 4) ÷ 5 = 14 ÷ 5 = 2.8
Complex, training: (0.25 + 0.25 + 0 + 0.25 + 0.25) ÷ 5 = 1 ÷ 5 = 0.2 On training data, the complex model looks far better: 0.2 versus 2.8. Now try four new test points:
Simple errors on test data: 3, −2, 2, −3
Complex errors on test data: 6, −5, 4, −7
Simple, test: (9 + 4 + 4 + 9) ÷ 4 = 26 ÷ 4 = 6.5
Complex, test: (36 + 25 + 16 + 49) ÷ 4 = 126 ÷ 4 = 31.5 The ranking flipped. On new data, the complex model's error is about five times bigger (31.5 versus 6.5). It had memorized noise. This is why the test set matters: the training score flatters complex models.
The training error keeps falling as complexity rises, but the error on new data forms a U. The best model sits near the bottom of the U, where it is complex enough to capture the real pattern but not so complex that it chases noise.
Cross-validation and regularization
A single test set uses data only once. Cross-validation is a way to use the data more fully. In 5-fold cross-validation, you cut the data into five equal parts. You train on four and test on the fifth, then rotate until each part has been the test set once. You average the five test scores.
A second defense is regularization: build a penalty into the model so it cannot use too many knobs. The best known version in economics is the lasso (least absolute shrinkage and selection operator). It picks coefficients to minimize:
(sum of squared errors) + λ × (sum of the absolute sizes of the coefficients) Here λ (lambda) is a dial you choose, often by cross-validation. A bigger λ forces coefficients to be smaller, and weak ones are pushed to exactly zero. Suppose a model has three coefficients, 3, 0.4 and −0.2, and λ = 2. The penalty is 2 × (3 + 0.4 + 0.2) = 2 × 3.6 = 7.2. A coefficient has to earn its place by reducing the squared errors by more than it adds to the penalty. The tiny ones usually do not, so lasso drops them. That makes lasso a useful tool when you have hundreds of possible predictors and few observations.
Tree-based methods, such as random forests, take a different route: they average many simple decision trees to cut down on noise. They also split the data into chunks of similar cases, which is why they can find patterns a straight-line model misses.
Judging a prediction fairly
Accuracy depends on what you compare against. Suppose a model predicts defaults with an MSE of 10 on the test set. Is that good? It depends on the simplest guess you could make, such as always predicting the average. If always guessing the average gives an MSE of 12, the fancy model barely helps. If it gives 40, the model is a big gain. Honest studies always report such a benchmark.
Another point is that the test data must look like the future. A model tested on calm years may fail in a crisis, because the relationships it learned can shift. In time-series work, this means testing on the most recent period rather than on randomly chosen dates, a point that links to forecasting models.
The trap: reading a predictor as a cause
Here is a famous kind of story told about hospital data. A model trained to predict which pneumonia patients will die found that patients who also had asthma seemed to do better. A naive reading says asthma protects you. The more likely explanation is that asthma patients were sent straight to intensive care and given aggressive treatment, which saved them. The pattern was real and the prediction was good, but the cause ran through the treatment, not the asthma. Using that model to send asthma patients home would be disastrous.
The lesson is that a predictor and a cause are not the same, and the shape of the data does not tell you which you have. The tools in earlier lessons, such as randomized experiments and instrumental variables, exist to answer the causal question that prediction cannot.
A related mistake is changing the thing you predict from. If a bank begins rejecting people based on a model, people change their behavior, and the old pattern stops holding. The model predicted well in a world with no model; the world with the model is a different world.
Where the two meet
The line between the two is not a wall. Economists now use machine learning in service of causal work. A few well-known directions:
- Prediction policy problems. Some decisions are mostly prediction. If a judge must decide who is likely to skip a hearing, an accurate forecast can help, and researchers have studied such uses.
- Double machine learning. It uses flexible prediction to strip out the influence of many controls, then estimates a causal effect of one chosen variable, with valid standard errors.
- Causal forests. An extension of random forests that estimates how a treatment effect differs across kinds of people.
- Finding the controls. Lasso can pick which of many possible variables to adjust for in a study such as difference-in-differences.
Even here, the causal part still comes from a design, not from the algorithm. The algorithm does the heavy lifting on prediction. The researcher supplies the argument for why the comparison is fair.
Words to know
Machine learning computer methods that learn patterns from data, mainly to predict outcomes.
Overfitting fitting random noise in the training data so that the model predicts new data poorly.
Training set and test set the part of the data used to build a model and the part held back to judge it.
Mean squared error the average of the squared prediction errors, a common accuracy score.
Cross-validation rotating which slice of the data is held out, to measure predictive skill more fully.
Regularization a penalty that keeps a model from using too many or too large coefficients.
Lasso a regularized regression that shrinks weak coefficients, some to exactly zero.
Check yourself
- Explain the difference between a prediction question and a causal question, using an example of your own.
- A model has training errors of 3, −1, 2, −2. What is its training mean squared error?
- A model's test errors are 4, −4, 2, −2. What is its test mean squared error?
- Why can a model with a lower training error be worse for forecasting?
- A lasso has coefficients 2, 0.5 and −1, and λ = 3. What is the penalty term?
- Explain why patients with asthma might look healthier in data without asthma being protective.
Show the answers
- Prediction asks what will happen (will it rain tomorrow?), while a causal question asks what would happen if we changed something (would running a sprinkler change it?); answers vary.
- (9 + 1 + 4 + 4) ÷ 4 = 18 ÷ 4 = 4.5.
- (16 + 16 + 4 + 4) ÷ 4 = 40 ÷ 4 = 10.
- A flexible model can memorize noise in the training data, which does not repeat in new data, so its error on new cases may be larger.
- 3 × (2 + 0.5 + 1) = 3 × 3.5 = 10.5, using absolute sizes of the coefficients.
- Asthma patients may have been given more aggressive care, so their better results came from the treatment, not from asthma itself.