Forecasting, fraud and anomaly detection
Many valuable AI systems predict a number or flag the odd one out. They use classical machine learning on tables, not chatbots.
Fraud threshold: 20 payments, 4 are fraud
fraud
fraud
ok
fraud
ok
ok
fraud
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
ok
Flagged 6 · caught 3 of 4 fraud · precision 50% · recall 75%
Filled cells are flagged. Lower the threshold to catch every fraud and watch the false alarms pile up.
Words on this page (8)
- forecasting
- : Predicting a future number, such as sales, from past values.
- anomaly detection
- : Finding cases that look unlike normal ones, often without labeled examples of the bad kind.
- baseline
- : A simple method, such as "same as last week", that any model must beat to be worth its cost.
- class imbalance
- : When one outcome is far rarer than the other, such as 100 frauds in 100,000 payments. Plain accuracy then misleads.
- precision
- : Of the cases a system flagged, the share that were truly positive.
- recall
- : Of the truly positive cases, the share the system found.
- leakage
- : When a feature or test example contains information that would not be known at prediction time, so scores look better than real use.
- training
- : Adjusting a model's parameters on data so its predictions improve.
What it is
predicts a future value from past values: sales, energy use, traffic. A good forecast starts with a naive guess such as "same as last week" and then adds patterns like trend and season. Classification sorts cases into labels, such as fraud or not. finds cases that look unlike normal ones when you have few or no labeled examples of the bad kind.
For tables of numbers, simple models (linear models, decision trees, boosted trees) often match or beat large neural networks, train in seconds and are easier to explain.
A worked example: forecast error
Actual sales over three days: 100, 120, 110. Forecast: 105, 115, 120. Errors: 5, 5, 10. The mean absolute error is (5 + 5 + 10) / 3 = 6.7 units a day. The naive "same as yesterday" forecast can only be scored on days 2 and 3: it predicts 100 and 120, so it is off by 20 and 10, a mean of 15. On those same two days the model is off by 5 and 10, a mean of 7.5, so it beats the .
A worked example: rare events
Out of 100,000 card payments, 100 are fraud. A model that always says "not fraud" is 99.9 percent accurate and catches nothing. This is . Use (of the cases flagged, how many were right) and (of the real cases, how many were found). Say a model flags 200 payments and 80 are truly fraud. Precision is 80 / 200 = 40 percent. Recall is 80 / 100 = 80 percent. Which matters more depends on what a miss costs against what a false alarm costs. Try it in the demo above: slide the threshold and watch the false alarms rise as you catch every fraud.
A second example: a better naive forecast
Shop sales last week, Monday to Sunday, were 80, 90, 85, 95, 140, 200, 120. This week they were 84, 92, 88, 100, 150, 210, 126. Forecast Tuesday to Sunday two ways. "Same as yesterday" is off by 8, 4, 12, 50, 60 and 84, an average of 36.3. "Same day last week" is off by 2, 3, 5, 10, 10 and 6, an average of 6.0. The weekly pattern is the signal, so repeat the season, not the last value. Any model must beat 6.0.
How an anomaly score works
A customer's daily spending over five days was 30, 45, 38, 52 and 35 dollars. The mean is 40 and the standard deviation, a measure of typical spread, is about 7.7. A payment of 130 sits (130 - 40) / 7.7 = 11.7 spreads above the mean, a clear outlier. A payment of 55 sits 1.9 spreads away and is normal. The threshold is a choice. Flag everything beyond 3 spreads and, if spending were bell-shaped, about 0.3 percent of ordinary payments still trip it. On 100,000 payments that is roughly 270 false alarms.
What goes wrong: information from the future
A model reports 99 percent recall on test data, then collapses live. One cause is : a feature that is only known after the answer. For fraud, a field like "chargeback filed" is filled in after fraud is found, so the model just reads it. Symptom: one feature dominates and the score is too good. To confirm, drop that feature and watch the score fall. Ask of each column, "would I know this at the moment I must decide?"
Common mistake
Using accuracy on rare events, or testing on data from before the model's period. Test on later data than you trained on, since the model will always be used on the future.
In 2021 the real estate company Zillow shut down its house-buying business after its pricing models paid more for homes than it could resell them for, losing hundreds of millions of dollars. Home prices moved faster than its forecasts, which is the risk of every prediction about the future.
Check yourself
A model says "not fraud" every time and is 99.9 percent accurate. What does this show?
Not quite. With rare positives, always guessing the common label looks accurate while finding nothing.
Why compare a forecast against "same as last week"?
Not quite. A forecast is only worth its cost if it beats the simple guess.
When is anomaly detection a good fit?
Not quite. It finds cases that differ from normal without needing labels for the rare kind.
You got 0 of 3 right.
Go deeper
- Book
Hyndman and Athanasopoulos · Free, practical book on forecasting methods.
- Docs
scikit-learn · Methods for flagging unusual cases, with code.
- Course
Kaggle · Build a first model on a table in an hour.
Spotted a mistake or something unclear? Report it on GitHub.