Time Series

Data points indexed in time order, where the sequence matters and rows aren't independent — analyzed to forecast future values or flag anomalies.

Published Updated

On this page

Definition

A time series is a sequence of data points indexed in time order — a stock's closing price each trading day, a server's CPU load each second, a patient's heart rate each beat. Time-series analysis and forecasting use that ordered history to predict future values or to flag observations that break the pattern.

What sets a time series apart from ordinary tabular data is that the rows are not independent, and their order carries information. In a normal dataset of house prices you could shuffle the rows and lose nothing; each house stands alone. In a time series, yesterday's temperature tells you a great deal about today's, and reordering the rows destroys exactly the structure you are trying to model. This is the load-bearing insight of the whole field: because the past informs the future, you cannot shuffle the data, and — as the rest of this page shows — you cannot split it at random either without cheating.

How It Works

Most of time-series analysis rests on one idea: an observed value can be broken into components that behave differently. The classic decomposition treats a value as a trend (the slow drift — a company's revenue climbing over years), a seasonal component (a pattern that repeats on a fixed period — ice-cream sales peaking every summer, web traffic dipping every weekend), and noise (the irreducible random wobble left over). Written additively, that is simply value = trend + seasonal + noise. Separating the three lets you extrapolate the trend, repeat the seasonal shape, and stop trying to predict the noise.

The property that makes any of this possible is autocorrelation: a series correlated with a lagged copy of itself. If the value at lag 1 (yesterday) is highly correlated with today, then yesterday is a useful predictor. A correlation spike at lag 12 in monthly data is the fingerprint of a yearly season; a spike at lag 7 in daily data is a weekly one. Autocorrelation is what a random dataset lacks and a time series has — it is the usable structure hiding in the ordering.

A related concept is stationarity. A series is stationary when its statistical properties — mean, variance, autocorrelation — stay constant over time. A great many classical methods assume stationarity, because a fixed set of parameters can only describe a process whose behavior is fixed. Real series usually are not stationary: they trend, they grow more volatile, they shift regime. The standard fix is to transform the series toward stationarity first, most simply by differencing — modeling the change from one step to the next rather than the raw level, which strips out a linear trend before the model ever sees the data.

Once you understand these three ideas — decomposition, autocorrelation, stationarity — the methods sort themselves along a spectrum. Classical statistics models the autocorrelation directly: ARIMA (AutoRegressive Integrated Moving Average) combines an autoregression on recent values, a differencing step for the "Integrated" part, and a moving average of recent errors; exponential smoothing forecasts as a weighted average of past values with weights that decay geometrically into the past. These are not obsolete relics — in the M4 forecasting competition (2018), which pitted methods against 100,000 real series, pure machine-learning entries were beaten by these statistical methods and simple combinations of them, a result that still surprises people who assume newer means better. Machine-learning and deep approaches earn their keep on large collections of long, related series: recurrent neural networks, and LSTMs in particular, carry a hidden state forward through the sequence to capture dependencies across many steps, while transformers use attention to relate distant points in a long history directly. Until recently the honest summary was that method choice followed data size — little data favored the statistics, a lot of related in-domain data favored the networks — but in both cases you fit a fresh model to your own series before you could forecast it.

That last assumption — that a forecaster has to be trained on the very series it will predict — is what time-series foundation models have overturned since 2023. Borrowing the recipe that produced large language models, systems such as Nixtla's TimeGPT (2023), Google's TimesFM, Amazon's Chronos, and Salesforce's Moirai (the latter three from 2023–2024) are pretrained once on enormous, diverse collections of unrelated series — web traffic, electricity load, retail demand, sensor readings — and then forecast a series they have never seen, with no per-series fitting at all. This is zero-shot forecasting: you hand the model your history at inference time and it returns a forecast, the way an LLM answers a prompt it was never trained on. On many public benchmarks these models match or come close to forecasters trained directly on each individual dataset. The practical consequence inverts the old rule of thumb — you no longer need a long in-domain history and a bespoke model per series to get a competitive forecast, and the sensible first move on a new series is increasingly to run a pretrained model zero-shot before building anything custom. What does not change is the mechanism this section described: a foundation model still earns its forecasts by exploiting trend, seasonality, and autocorrelation, and the future can still leak into your evaluation. Only the assumption that every forecaster is trained on its own target series has fallen away.

Types

Two independent distinctions matter, and it helps to keep them apart.

The first is the shape of the data. A univariate series tracks one quantity over time — a single thermostat's temperature reading. A multivariate series tracks several interacting quantities together — temperature, humidity, and pressure recorded at the same station — where the variables inform one another and cannot be forecast in isolation.

The second is the task you are performing on the series. Forecasting predicts future values from the past (next week's electricity demand). Classification assigns a whole series or window to a label (is this ECG trace normal or arrhythmic?). Anomaly detection flags individual points or stretches that depart from the learned pattern (a sudden latency spike in a monitored service). The same underlying series can feed all three tasks, and they need different models and different evaluation.

Real-World Applications

Demand and supply-chain forecasting is the largest commercial use. Retailers and cloud platforms forecast how much of each product will sell, or how much compute a region will need, thousands of series at a time — Amazon's DeepAR, available in SageMaker, is a probabilistic recurrent forecaster built for exactly this many-related-series setting, producing not a single number but a distribution over likely demand.

Electricity grids run on short-horizon forecasts. System operators must match generation to load in real time, so day-ahead and hour-ahead demand forecasting — heavily seasonal, with daily, weekly, and yearly cycles layered together — decides how much generation to schedule and what it costs.

Finance uses time-series models for volatility and risk as much as for price. GARCH models, whose author Robert Engle shared the 2003 Nobel Memorial Prize in Economics, forecast how a return series' variance clusters over time — the fact that turbulent days tend to follow turbulent days — and underpin risk limits and option pricing.

Weather forecasting has become a showcase for machine learning on physical time series: DeepMind's GraphCast, published in Science in 2023, produces global medium-range forecasts from a learned model rather than solving the physics equations directly, and matches or beats the operational numerical systems on many variables while running far faster.

Operations and health monitoring lean on the anomaly-detection task. Site-reliability teams watch CPU, latency, and error-rate series for the departures that signal an outage; intensive-care units watch a patient's vitals for the drift that precedes deterioration. In both, the value is not the forecast but the alarm when the series stops behaving.

Challenges

The split leaks the future — this is the mistake that ruins results silently. A random train/test split, the default everywhere else in machine learning, is catastrophic here. Scatter dates at random into train and test and the model is scored on days whose immediate neighbors — the days before and after — are already in the training set. Given yesterday and tomorrow, guessing today is trivial, so the score looks excellent and then collapses the moment the model faces genuinely unseen future dates. You must split by time: train on the earliest stretch, test on the latest, so the test set is always the model's future. The ## Code Example below does exactly this, and Cross-Validation's standard k-fold shuffling has to be replaced with a forward-chaining variant for the same reason.

Feature engineering leaks the future too. The subtler cousin of the split problem is computing a feature using information that would not have been available at prediction time — normalizing by a statistic taken over the whole series (which includes the future), or attaching a value that is only known after the fact. This look-ahead bias inflates offline accuracy exactly the way the split does, and it is easy to introduce by accident.

Non-stationarity means the past can stop being a guide. A model fit on last year's pattern can decay when the underlying process shifts — a new competitor, a pandemic, a policy change. Because forecasting assumes the future resembles the past, a regime change is the one thing the approach is structurally blind to, and the only defense is to monitor forecast error and refit when it drifts.

Missing timestamps and irregular sampling break the assumptions. Most methods assume evenly spaced observations; real sensors drop readings and real events arrive irregularly. Gaps must be filled or the model reformulated, and a naive fill can itself invent structure that was never there.

Code Example

This block builds a short daily series, smooths it with a 3-day moving average to expose the trend, then forecasts the future with a time-respecting split — training on the first eight days and testing on the last four, never shuffling. Each forecast is the mean of the three days before it; we score with Mean Absolute Error (MAE), the average size of the miss.

# A short daily series: an upward trend with noise on top.
series = [10, 12, 13, 12, 15, 16, 14, 18, 17, 20, 19, 22]

# A 3-day moving average smooths the noise and exposes the trend.
def moving_average(x, k):
    return [sum(x[i:i+k]) / k for i in range(len(x) - k + 1)]

ma3 = moving_average(series, 3)
print("3-day moving average:", [round(v, 2) for v in ma3])

# Split by TIME, never at random: train on the past, test on the future.
train, test = series[:8], series[8:]
print("train (past):", train)
print("test  (future):", test)

# One-step-ahead forecast: predict each day as the mean of the 3 days before it,
# then reveal the true value and walk forward one step.
def ma_forecast(history, k=3):
    return sum(history[-k:]) / k

history = list(train)
preds = []
for actual in test:
    preds.append(ma_forecast(history, 3))
    history.append(actual)

mae = sum(abs(p - a) for p, a in zip(preds, test)) / len(test)
print("forecasts:", [round(p, 2) for p in preds])
print("MAE on the future:", round(mae, 2))

Output:

3-day moving average: [11.67, 12.33, 13.33, 14.33, 15.0, 16.0, 16.33, 18.33, 18.67, 20.33]
train (past): [10, 12, 13, 12, 15, 16, 14, 18]
test  (future): [17, 20, 19, 22]
forecasts: [16.0, 16.33, 18.33, 18.67]
MAE on the future: 2.17

You can verify the first moving-average value by hand: (10 + 12 + 13) / 3 = 11.67. The first forecast is the mean of the last three training days, (16 + 14 + 18) / 3 = 16.0, against an actual of 17 — a miss of 1.0. Averaging the four misses gives an MAE of 2.17. The point is not the number but the discipline that produced it: every forecast used only days strictly earlier than the one it predicted. Shuffle series before the split and the same code would report a far rosier MAE that no real deployment could ever reproduce, because the model would have been quietly trained on its own test days. That gap between the shuffled score and the honest one is the future leaking in — the single error most worth avoiding in time-series work. For the general practice of learning a function from labeled past examples, see supervised learning; for fitting continuous values specifically, regression; and for the broader field, machine learning.

Frequently Asked Questions

The rows are ordered and not independent — yesterday's value carries information about today's. That single fact means you cannot shuffle the data, and most standard machine-learning assumptions about independent samples no longer hold.
A random split scatters future dates into the training set, so the model is scored on gaps whose neighbors on both sides it already saw. That leaks the future into training and reports an accuracy you will never see in production. Split by time instead: train on the past, test on the future.
Classical statistical methods like ARIMA and exponential smoothing remain strong baselines and frequently beat neural networks on short or noisy series — a result confirmed by the M4 forecasting competition. Custom deep models (LSTMs, transformers) traditionally paid off only with many long, related series to train on. Since 2023, though, pretrained time-series foundation models such as TimeGPT, TimesFM, Chronos, and Moirai can forecast a new series zero-shot, with no per-series training — often the strongest baseline to try first.
A stationary series has statistical properties (mean, variance, autocorrelation) that don't drift over time. Many classical models assume it, so a trending or seasonal series is often differenced or decomposed first to make it stationary before fitting.
It is the correlation of a series with a lagged copy of itself. Strong autocorrelation at lag 1 means each value closely tracks the one before it; a spike at lag 12 on monthly data signals yearly seasonality. It is the property that makes the ordering usable for prediction.

Continue Learning

Explore our use-case guides and prompts to deepen your AI knowledge.