Part V: Foundation Models and Agentic Sensing
Chapter 19: Time-Series Foundation Models

Zero-shot and few-shot forecasting, classification, imputation, anomaly detection

"They asked me to forecast a vibration channel I had never seen. I read the last 512 samples, thought for one forward pass, and did not retrain a single weight. The maintenance team called it magic. I called it Tuesday."

An Unfazed AI Agent

The big picture

Section 19.3 introduced the model families. This section is about what you actually do with them. A time-series foundation model (TSFM) turns four classic sensor problems, forecasting, classification, imputation, and anomaly detection, into variations of a single move: feed a context window in, read a prediction out, with either zero task-specific training (zero-shot) or a handful of labeled examples (few-shot). The context window is the interface. Instead of designing a bespoke network and collecting a training set for every new sensor channel, you pass recent history to a frozen model and let its pretrained prior do the work. This section teaches how each of the four tasks maps onto that prior, when the mapping is faithful, and how to spend a small labeling budget to close the last gap.

This section assumes you understand what a foundation model is (Section 19.1) and how patching and value tokenization encode a series (Section 19.2). It also leans on probabilistic forecasting notation and proper scoring rules from the uncertainty primer in Chapter 4, and on the residual-based detectors of Chapter 12. You do not need to have trained a TSFM yourself; every technique here treats the backbone as a fixed function.

The zero-shot to few-shot spectrum

What distinguishes the regimes is how much task-specific weight update you allow. Zero-shot means no gradient touches the model: you supply a context window \(x_{1:L}\) and the network emits its answer in one forward pass, exactly as a large language model answers a prompt it was never fine-tuned on. Few-shot means you have somewhere between a handful and a few hundred labeled windows, too few to train a model from scratch, and you use them to nudge the pretrained representation. The three standard nudges, from cheapest to most expressive, are the linear probe (freeze the backbone, fit a logistic or ridge head on its embeddings), parameter-efficient fine-tuning such as LoRA (inject a few low-rank adapter matrices, train only those), and full fine-tuning (unfreeze everything, viable only when you have thousands of windows).

Why zero-shot works at all is worth stating plainly: pretraining on hundreds of billions of time points across electricity, traffic, weather, finance, and sensor corpora teaches the model the shared grammar of temporal data, trend, seasonality, autocorrelation, level shifts, and heavy tails. A new vibration or glucose channel is rarely grammatically novel even when it is semantically novel. When the new signal violates that grammar (a control loop with a sampling rate the model never saw, a regime with no analogue in pretraining), zero-shot degrades and the few-shot budget earns its keep.

Key insight: one backbone, four heads

Forecasting, imputation, and anomaly detection are the same generative task viewed through different masks. Forecasting masks the future. Imputation masks interior gaps. Anomaly detection compares the model's prediction to reality and flags the surprise. Only classification needs a genuinely different head, and even then a frozen embedding plus a linear classifier is often enough. If you internalize this masking view, you stop asking "which model for this task" and start asking "which mask, on which frozen backbone."

Zero-shot forecasting: the flagship capability

Forecasting is where TSFMs shine hardest because it is the task most of them were pretrained on directly. How it runs: you choose a context length \(L\) (often 512 to 2048 samples), the model consumes the (instance-normalized) window, and it emits a distribution over the next \(H\) steps. Good TSFMs are probabilistic: they return quantiles \(q_\alpha(x_{L+h})\) rather than a single point, so you get calibrated intervals for free. That matters for sensor decisions, where a predictive-maintenance alert needs a confidence band, not just a mean. Evaluate with a quantile loss such as the weighted quantile loss, \(\mathrm{WQL} = \tfrac{2}{\sum_h |x_{L+h}|}\sum_{h,\alpha} \big[\alpha(x - q_\alpha)_+ + (1-\alpha)(q_\alpha - x)_+\big]\), which rewards sharp, calibrated intervals and reduces to mean absolute error at the median.

Two practical knobs dominate zero-shot quality. First, context length: too short and the model cannot see the dominant seasonality; too long and irrelevant ancient history dilutes the prior. Match \(L\) to at least two or three periods of the slowest cycle you care about. Second, instance normalization: nearly all TSFMs standardize each window internally, so a channel measured in \(\mathrm{m/s^2}\) and one in millivolts are treated identically; you rarely need to rescale by hand, but you must ensure the window has enough non-constant signal for the statistics to be meaningful.

Practical example: a wind-farm gearbox nobody labeled

An operator commissions a new turbine model and gets a gearbox oil-temperature channel at one-minute cadence. There is no historical fault data for this exact unit, so a supervised forecaster has nothing to train on. The reliability team feeds the last 1440 samples (one day) to a frozen Chronos-Bolt model and reads a 90-minute-ahead forecast with an 80% interval. When the measured temperature climbs out of the upper quantile band for three consecutive windows during a normal-load period, the system raises a soft alert. No model was trained, no fault labels existed, and the same pipeline that forecasts becomes the anomaly detector described below. The team later confirms a degrading cooling pump. This is precisely the workflow Chapter 36 formalizes for prognostics.

Classification, imputation, and anomaly detection from the same prior

Imputation is forecasting turned inward. Sensor streams drop samples: a wearable loses skin contact, a CAN bus drops a frame, a lidar returns nothing off a black surface. A TSFM with a masked-reconstruction objective (MOMENT is the canonical example) fills interior gaps by conditioning on both past and future context, which is strictly more information than a causal forecaster has, so zero-shot imputation is often excellent. Anomaly detection reuses either head: run the model, form the residual \(r_t = x_t - \hat{x}_t\) (forecast residual) or the reconstruction error (imputation residual), and threshold it. The surprise signal is model-agnostic; you are simply asking "how far did reality fall outside what the pretrained prior expected," which connects directly to the residual detectors and change-point tests of Chapter 12. Classification is the one task that needs a new head: extract a pooled embedding \(z = f_\theta(x_{1:L})\) from the frozen backbone and fit a small classifier on top. Section 19.5 develops this embeddings-as-features view in depth; here the point is that even classification rides the same frozen model.

import numpy as np
from chronos import ChronosBoltPipeline   # frozen TSFM, no training

pipe = ChronosBoltPipeline.from_pretrained("amazon/chronos-bolt-base")

def zero_shot_anomaly(series, context=512, horizon=1, k=3.0):
    """Slide a frozen forecaster over a channel; flag samples that fall
    outside its predicted quantile band by more than k robust sigmas."""
    flags = np.zeros(len(series), dtype=bool)
    for t in range(context, len(series) - horizon):
        ctx = series[t - context:t]
        q = pipe.predict_quantiles(ctx, prediction_length=horizon,
                                   quantile_levels=[0.1, 0.5, 0.9])
        lo, mid, hi = q[0, 0], q[1, 0], q[2, 0]      # 10/50/90 percentiles
        band = max(hi - lo, 1e-6)                     # predicted spread
        resid = abs(series[t] - mid)
        flags[t] = resid > k * band                   # surprise exceeds prior
    return flags
Zero-shot anomaly detection built entirely on a frozen forecaster: no fault labels, no task training. The predicted 10-90 percentile spread supplies a self-calibrating threshold, so a quiet channel gets a tight band and a noisy one gets a wide band automatically. Referenced by the wind-farm example above and the exercise below.

Library shortcut: four tasks, one import

Hand-rolling a probabilistic forecaster, a masked-imputation network, and an embedding classifier from scratch is easily 800 to 1200 lines across three training loops, plus data pipelines and checkpoint management. With a TSFM library (chronos, momentfm, or tirex) the zero-shot path collapses to a from_pretrained call and one predict, imputation is the same call with an interior mask, and classification is a frozen embed plus a scikit-learn LogisticRegression. Roughly a 30-to-1 line reduction, and the library owns instance normalization, patching, quantile decoding, and batching. You write the task glue; the library owns the model.

Few-shot adaptation: spending a small label budget well

When zero-shot leaves a gap, do not jump to full fine-tuning. The efficient frontier is: linear probe first, then LoRA, then full unfreeze only if data justifies it. A linear probe on frozen embeddings needs only tens of examples per class and cannot overfit the backbone, making it the right first move for a new activity-recognition or arrhythmia-screening task. LoRA adds low-rank matrices \(W + \Delta W\) with \(\Delta W = BA\), \(A \in \mathbb{R}^{r\times d}\), \(B \in \mathbb{R}^{d\times r}\) and small rank \(r\); you train perhaps 0.1 to 1% of the parameters, which shifts the forecasting or reconstruction behavior toward your domain without erasing the pretrained prior. Because you touch so few weights, few-shot fine-tuning is also far less prone to the leakage traps of full retraining, though you still hold out a strictly future validation window, a discipline this book returns to in Section 19.6 and in Chapter 5.

Research frontier: in-context adaptation without gradients

The 2024-2026 frontier pushes toward gradient-free few-shot: models such as TimesFM 2.5 and TiRex handle longer context and richer conditioning, and covariate-aware regressors (TimesFM's covariate path, Chronos with exogenous inputs) let you inject related channels as context so the model adapts through its prompt rather than its weights. Related work on in-context fine-tuning shows a TSFM improving on a target domain purely from example series placed in the context window, echoing few-shot prompting in language models. Calibrating the resulting intervals with the conformal methods of Chapter 18 is the current best practice for turning a raw zero-shot band into a guaranteed-coverage one.

Exercise

Take the zero_shot_anomaly function above and evaluate it on a labeled anomaly benchmark of your choice. (1) Sweep the threshold multiplier \(k\) and plot precision versus recall; where does the self-calibrating band beat a fixed global 3-sigma rule? (2) Replace the forecast residual with a masked-imputation residual (use past and future context) and compare detection latency. (3) Now give yourself a budget of 20 labeled anomaly windows and fit a linear probe on the backbone's embeddings instead; report how much the small labeled set improves \(F_1\) over the fully zero-shot detector.

Self-check

1. Why is zero-shot imputation often easier for a TSFM than zero-shot forecasting of the same channel? 2. You have 40 labeled windows for a new gesture class. Rank linear probe, LoRA, and full fine-tuning by expected reliability and explain your ordering. 3. A frozen forecaster's residuals flag an "anomaly" every night at the same hour on a building-energy meter. What is the most likely benign cause, and which knob from this section fixes it?

What's Next

In Section 19.5, we go one level deeper on the classification move that appeared here only in passing: using a frozen foundation model purely as a feature extractor, so its embeddings become drop-in inputs for downstream classifiers, regressors, retrieval, and clustering, and we measure when those embeddings beat hand-engineered features.