Go deeper · Ai at work

How does AI predict tomorrow and spot the unusual?

It looks at what happened before, in order. Many measurements repeat in rhythms, like busy weekdays and quiet weekends. A forecast guesses the next value from those patterns, and an anomaly detector raises an alarm when reality lands far from the guess. The hard parts are beating very simple guesses, and keeping false alarms rare.

Grades 7+15 minutesLight math: averages and differencesSimpler version for grades 4–6

What you will be able to do

Explain why time order matters, compute the mean absolute error of a naive and a seasonal baseline, choose an alarm threshold while counting true alerts and false alarms, and explain why time series must be tested on the future, never shuffled.

Before you start

Before this: Good at what, for whom, compared with what? for baselines and false alarms. You need to be able to subtract and average a few numbers.

1 · You have already seen this

You already know when it gets busy

Most people can guess which days their local supermarket will be crowded.

After a few weeks of shopping there you know the pattern: some days are busy and others are quiet. Nobody wrote you a rule. You remember what happened before, in order, and expect it to repeat. So when you walk in at a usually busy time and the shop is nearly empty, you notice straight away and wonder what happened.

Computers do the same with a time series, a list of measurements taken one after another. A forecast guesses the next value from earlier patterns, and anomaly detection raises an alarm when reality lands far from the guess. The rest of this page shows how that works, lets you try it, and shows where it goes wrong.

2 · What happens, step by step

From past days to an alarm

Forecasting and alarms in five steps. The next section goes deeper into steps 1 and 2, and the one after it into step 3. After the lab, another section goes deeper into steps 4 and 5.

How forecasts and alarms work

Five steps

  1. 1 · Record in orderWrite down one measurement after another, such as lunches served each school day. The order is part of the information.
  2. 2 · Make a simple guessForecast the next value from earlier ones. Start with very simple guesses such as “same as yesterday,” because a cleverer model is only worth using if it beats them.
  3. 3 · Measure the missesWhen the real number comes in, the gap between it and the forecast is the error. The average size of those gaps shows how good the forecast is.
  4. 4 · Set an alarm linePick a threshold. Any day that lands further from its forecast than that is flagged as unusual.
  5. 5 · Let a person checkThe alarm cannot tell why a day was odd. Someone who knows what happened that day decides what it means.

Going deeper · steps 1 and 2

Data that comes in order

Temperatures, ticket sales, website visits and machine vibrations are all time series: one measurement after another.

A time series is a list of measurements taken in time order: lunches served each school day, the temperature every hour, the vibration of a pump every second. Unlike a pile of photos, the order is part of the information. Tuesday comes after Monday, and what happened on Monday helps you guess Tuesday.

Three patterns show up again and again. A trend is a slow rise or fall, like a library that gets a little busier each year. Seasonality is a repeating rhythm: weekends are quiet, Fridays are pizza day, summers are hot. Noise is the leftover wobble nobody can predict. And now and then there is an unusual event: a snow day, a field trip, a broken sensor.

Before trying any clever model, forecasters write down a few naive baselines: forecasts so simple that anyone could make them. “Same as yesterday.” “Same as the same day last week.” “The average of the last few days.” They sound silly, but they are hard to beat, and a model that cannot beat them is not worth using. To compare forecasts, a common score is the mean absolute error (MAE): the average size of the misses, ignoring whether each one was too high or too low.

Predict first

A library has a strong weekly rhythm: busy weekdays, quiet weekends. Which naive forecast do you expect to do better: “same as yesterday” or “same day last week”?

Show the answer

Same day last week, because it copies the rhythm. “Same as yesterday” forecasts a busy Saturday right after a busy Friday, and a quiet Monday right after a quiet Sunday. The worked example below puts numbers on it.

Worked example

Two naive forecasts for a week of library visits

  1. DataMade-up daily visits. Week 1, Monday to Sunday: 40, 44, 42, 46, 38, 20, 12. Week 2: 42, 45, 44, 45, 36, 22, 14. We forecast each day of week 2 using only earlier days.
  2. Same as yesterdayForecasts: Monday gets Sunday’s 12, Tuesday gets Monday’s 42, and so on: 12, 42, 45, 44, 45, 36, 22.
  3. Its errorsError = actual − forecast, ignoring the sign: |42 − 12| = 30, |45 − 42| = 3, 1, 1, |36 − 45| = 9, |22 − 36| = 14, |14 − 22| = 8. Total = 66.
  4. MAEMean absolute error = total ÷ number of days = 66 ÷ 7 ≈ 9.4 visits.
  5. Same day last weekForecasts are simply week 1: 40, 44, 42, 46, 38, 20, 12. Errors: 2, 1, 2, 1, 2, 2, 2. Total = 12. MAE = 12 ÷ 7 ≈ 1.7 visits.
  6. Read itThe seasonal baseline is more than five times better here, because the library has a weekly rhythm: weekends are quiet. “Yesterday” keeps being surprised on Mondays and Saturdays. Any smarter model should beat 1.7, not 9.4, before anyone calls it good.

The numbers are invented for teaching. With real data, check that the baseline is computed on exactly the same days as the model.

Going deeper · step 3

Never shuffle a time series

The one rule that matters most: train on the past, test on the future.

On other ARQIV pages, a dataset is split at random into training and test parts. Do that with a time series and you fool yourself. If the test day is a Wednesday and the model trained on the Tuesday and the Thursday around it, guessing Wednesday is easy. In real life you never get to see Thursday before forecasting Wednesday. The test score looks great and the real forecasts are worse. That is a form of data leakage: information from the future leaked into training.

The fix is simple. Keep the order. Train on the earlier part and test on the later part. Better still, repeat it: train on weeks 1–3 and test on week 4, then train on weeks 1–4 and test on week 5, and so on. Forecasters call this evaluating on a rolling origin, and scikit-learn has a ready-made splitter for it.

Time also brings change. A new school timetable, a new menu or a new road can shift the pattern for good. A model trained on last year can quietly get worse, which is called drift, so forecasts need to be checked against what really happened, again and again.

Testing a forecast: split by time, not at random Top row, the right way: the first 10 days are training data and the last 4 days are test data, so the model is always tested on days after everything it learned from. Bottom row, the wrong way: test days are scattered at random among training days, so the model learns from days that come after the days it is tested on. RIGHT: learn from the past,test on the future days 1–10: learn11–14: test WRONG: shuffle, then split test days sit between days it learned from:the model peeks at the future
Fourteen days. Pink days are the test. Shuffling lets the model learn from days that come after the days it is tested on.

3 · Try it

Forecast lunches, set the alarm

Four weeks of made-up lunch counts at a school cafeteria. Wednesdays are short days; Friday is pizza day.

Predict first

On week 3 Tuesday, two grades are on a field trip. With “same day last week,” which other day might trigger a false alarm?

Show the answer

Week 4 Tuesday. Its forecast copies week 3 Tuesday, the field-trip day with only 225 lunches. A normal Tuesday of 312 is then 87 lunches “too high” and sets off the alarm. An unusual day echoes into the forecasts that copy it. A common fix is to leave known unusual days out of the history the forecast copies.

Lab

Forecast and flag

Pick a forecast method and look at its errors. Then move the alarm threshold. Count true alerts (the two days the school calendar marks as unusual), false alarms and missed days.

Simulated in your browser with 20 made-up school days. No real school data.

Forecast
Average error (MAE)–
True alerts–
False alarms–
Missed odd days–

Bars: lunches served (week 1 is history, gray). Yellow ticks: the forecast. Red ring: alarm. Labels mark the two days the school calendar says were unusual. On a small screen, swipe the chart sideways.

School dayLunchesForecastErrorAlarm?

The interactive version needs JavaScript. Here is the same lab as tables. Week 1 is history: Mon 300, Tue 310, Wed 245, Thu 305, Fri 380. Each method forecasts weeks 2–4 using only earlier days. An alarm goes off when a day is at least the threshold away from its forecast. The two truly unusual days are week 3 Tuesday (field trip) and week 4 Thursday (food fair).

Forecasts and errors for weeks 2–4 (made-up data)
School dayLunchesPrevious school daySame day last week3-day average
Week 2 Mon296380 (error -84)300 (error -4)310 (error -14)
Week 2 Tue314296 (error 18)310 (error 4)327 (error -13)
Week 2 Wed241314 (error -73)245 (error -4)330 (error -89)
Week 2 Thu309241 (error 68)305 (error 4)283.7 (error 25.3)
Week 2 Fri376309 (error 67)380 (error -4)288 (error 88)
Week 3 Mon304376 (error -72)296 (error 8)308.7 (error -4.7)
Week 3 Tue (Field trip: two grades away)225304 (error -79)314 (error -89)329.7 (error -104.7)
Week 3 Wed249225 (error 24)241 (error 8)301.7 (error -52.7)
Week 3 Thu302249 (error 53)309 (error -7)259.3 (error 42.7)
Week 3 Fri383302 (error 81)376 (error 7)258.7 (error 124.3)
Week 4 Mon298383 (error -85)304 (error -6)311.3 (error -13.3)
Week 4 Tue312298 (error 14)225 (error 87)327.7 (error -15.7)
Week 4 Wed243312 (error -69)249 (error -6)331 (error -88)
Week 4 Thu (Food fair)352243 (error 109)302 (error 50)284.3 (error 67.7)
Week 4 Fri378352 (error 26)383 (error -5)302.3 (error 75.7)
Average error and alarms for each method
MethodAverage error (MAE)Alarm at 30Alarm at 50Alarm at 90
Previous school day61.52 true, 9 false, 0 missed2 true, 9 false, 0 missed1 true, 0 false, 1 missed
Same day last week19.52 true, 1 false, 0 missed2 true, 1 false, 0 missed0 true, 0 false, 2 missed
3-day average54.62 true, 7 false, 0 missed2 true, 6 false, 0 missed1 true, 1 false, 1 missed

What the lab shows. “Same day last week” has the smallest average error (19.5 lunches, against 61.5 for the previous school day and 54.6 for the 3-day average), because it follows the weekly rhythm. Its alarm catches both odd days with a threshold anywhere from 10 to 50, with exactly one false alarm: the echo of the field trip. With the “previous school day” forecast, no threshold catches both odd days without at least three false alarms, because normal Mondays, Wednesdays and Fridays already look surprising. A good alarm needs a good forecast first.

Unusual events

Alarms, false alarms and the person who checks

Anomaly detection means flagging data that does not fit the usual pattern. One common recipe is exactly what the lab does: forecast what “normal” should look like, measure how far reality is from it, and raise an alarm above a threshold. Other methods flag points far from every group of normal examples, like the odd ones out on Can a computer find groups nobody labeled?

The threshold is a trade-off. Set it low and you catch every real problem but drown in false alarms, and people soon start ignoring them. Set it high and the alarms are trustworthy but you miss real problems. It is the same trade-off as the screening slider on Where should the AI draw the line? When unusual events are rare, even a small false-alarm rate can mean that most alarms are false, so count both kinds.

This is how predictive maintenance works in factories, trains and wind turbines. Sensors record vibration, temperature or power use many times a second. A model learns the machine’s normal rhythm, and an alarm asks a technician to look when readings drift away from it, ideally before something breaks. The alarm does not know why a reading is odd. A fire drill, a new menu or a sensor knocked loose all look the same to it, so a person who knows the context decides what to do.

Forecasting is also the heart of weather prediction, where physics-based simulations and, more recently, machine-learning models trained on many years of past weather are used to predict the days ahead. See AI in weather.

4 · Common mix-ups

Where forecasts and alarms trip people up

Four ideas that are easy to get wrong, and what to think instead.

  • Mix-up: A forecasting AI must be better than simple rules.

    Better: Not automatically. Naive baselines are often hard to beat. Always report the baseline’s error on the same days.

  • Mix-up: You can test a time-series model like any other, with a random split.

    Better: Random splits let the model peek at the future. Train on earlier days and test on later days.

  • Mix-up: Every alarm means something is wrong.

    Better: Many alarms are false. Some are echoes of an earlier odd day. A person who knows the context should check.

  • Mix-up: An anomaly is an error in the data.

    Better: It may be a real event, like a field trip or a broken machine, a measuring mistake, or just a rare normal day. The detector cannot tell which.

5 · Check yourself

Three quick questions

Answer in your head or on paper before you open each one.

  1. A café sold 50, 52 and 60 drinks on three days. A forecast said 48, 55 and 57. What is the MAE?

    Check your answer

    Errors ignoring the sign: |50 − 48| = 2, |52 − 55| = 3, |60 − 57| = 3. Total 8. MAE = 8 ÷ 3 ≈ 2.7 drinks.

  2. A student shuffles a year of daily bus ridership, trains a model on a random 80% and tests on the other 20%. The error is tiny. Why should you be suspicious?

    Check your answer

    The test days are mixed in among training days, so the model saw the days just before and after each test day. Real forecasting never has tomorrow’s data. Test on a later stretch of time instead, for example train on January to September and test on October to December.

  3. An alarm for a school’s heating system goes off 20 times in a month, and only 2 were real problems. What could the team try, and what is the risk?

    Check your answer

    Raise the threshold, or improve the forecast of “normal” (for example, account for weekends and holidays) so normal days stop looking unusual. The risk of a higher threshold is missing a real problem, so they should check how many real problems the new setting would still have caught.

For younger learners

A grades 4–6 version

No formulas: guess tomorrow, then score the guesses.

Two weeks · five minutes a day

The class forecast

  1. Pick something counted every school day: how many students bring lunch, or library books returned.
  2. Each morning, two teams write a guess: Team Yesterday copies yesterday; Team Last Week copies the same day last week.
  3. Each afternoon, count the real number and write down how far off each team was.
  4. After two weeks, add up the misses. Which team won, and on which days did it lose?

Exit question

Was it strange?

On one day the number was very different. Was that a mistake, or did something unusual happen? Good answers say that you have to find out what happened that day before deciding.

Honest limits

What this page leaves out

What this page leaves out

Statistical forecasting models (such as exponential smoothing and ARIMA), machine-learning and deep-learning forecasters, forecasting with extra information such as the weather or the school calendar, and forecast uncertainty (giving a range, not just one number) are left out. So are other error scores. The free textbook by Hyndman and Athanasopoulos in the sources covers all of them.