Overview

4 Zero-Shot probabilistic forecasting with Lag-Llama

This chapter presents Lag-Llama, an open-source, zero-shot probabilistic forecasting model positioned as a research- and proof-of-concept-friendly alternative to paid, API-based solutions like TimeGPT. Unlike point-forecasting methods, Lag-Llama outputs full predictive distributions, providing uncertainty estimates via inherent prediction intervals. At the time of writing, it is accessed by cloning its repository (no packaged API), supports univariate forecasting only (no exogenous features), and is best suited for experimentation rather than production. Its pretraining spans diverse domains but focuses on daily to minute-level frequencies with forecast horizons of 24–60 steps, implying weaker zero-shot performance for weekly, monthly, or longer horizons unless fine-tuned.

Architecturally, Lag-Llama adapts a decoder-only Transformer backbone inspired by LLaMA and builds tokens from lagged values and rich date-time features across granularities, enabling the model to infer series frequency implicitly. This tokenization yields a relatively large context window, which can slow inference but enriches temporal structure learning. Forecasts are generated autoregressively, and a distribution head outputs the parameters (mean, scale, degrees of freedom) of a Student’s t-distribution to capture uncertainty robustly. Practically, inference runs through GluonTS interfaces, requiring data in compatible formats and 32-bit floats; users obtain sample paths and derive summary statistics (e.g., medians and confidence intervals) for decision-making.

The chapter walks through end-to-end usage: cloning the repo, installing dependencies, loading pretrained weights, formatting data, and producing zero-shot forecasts on weekly Walmart sales. It highlights the importance of the context length hyperparameter—longer or shorter input windows can materially affect accuracy, with an example where a length of 64 performed best in cross-validation. Fine-tuning on a GPU (e.g., in Colab) further improved metrics over zero-shot, though prediction intervals remained relatively wide under the assumed t-distribution. Overall, Lag-Llama’s strengths are openness and immediate probabilistic outputs; its trade-offs are setup friction, hardware-dependent speed, univariate scope, and frequency/horizon limitations tied to pretraining—making it ideal for research, prototyping, and scenarios where local fine-tuning is feasible.

General architecture of Lag-Llama. It features only the decoder portion of the Transformer, and the distribution head is responsible for generating the distribution of future values.
Tokenization process in Lag-Llama. Here, we see that the token of the current time step includes lagged values of the series over a certain context length, as well as date-time features, like second-of-minute, min-of-hour, all the way to quarter-of-year.
Student’s t-distribution with varying scale and degrees of freedom. We can see that more degrees of freedom results in a lower peak, while the scale, also called the standard deviation, widens the peak and its value increases.
Zero-shot predictions from Lag-Llama for the next eight weeks of sales of four Walmart stores. The shaded area represents the 95% prediction interval.
MAE and sMAPE for each context length. Lower values are better. Here, a context length of 64 time steps generated the best results.
Plotting the best performing forecasts from Lag-Llama using a context length of 64 time steps. We can see the forecasts mostly overlap the true values.

Summary

  • Lag-Llama is an open-source probabilistic foundation model
  • It uses a decoder-only architecture, and it constructs features using past values and date-time features, like second-of-minute, minute-of-hour, all the way to quarter-of-year.
  • Lag-Llama was pretrained on frequencies of every day, hour, 15 minutes, and minute, and on forecast horizons ranging from 24 to 60 time steps.
  • Since the model must be downloaded locally, using a GPU will result in faster inference time and fine-tuning.

FAQ

What is Lag-Llama and how is it different from TimeGPT?Lag-Llama is an open-source, zero-shot probabilistic time series model based on a decoder-only Transformer. Unlike TimeGPT (API-based, easy to use, and eventually paid), Lag-Llama must be used locally by cloning its repository, has no official Python package or API, supports only univariate forecasting (no exogenous features), and is mainly suited for proofs of concept and research.
What does “probabilistic forecasting” mean in the context of Lag-Llama?Instead of a single point forecast, Lag-Llama outputs a distribution of possible future values. It parameterizes a Student’s t-distribution (mean, scale, degrees of freedom) for each step, enabling native prediction intervals. The median of the sampled distribution is typically used as the point forecast.
How does Lag-Llama’s architecture work?Lag-Llama uses a decoder-only Transformer. Inputs are tokenized into vectors that include lagged values and date-time features, then passed through multi-head attention layers with positional encoding. A distribution head outputs the parameters of a Student’s t-distribution, and forecasts are generated autoregressively.
How does tokenization in Lag-Llama encode time and frequency information?Each token aggregates a context window of lagged values plus rich date-time features (e.g., second/minute/hour/day/week/month/quarter). Because only one feature typically changes between adjacent steps, the model can infer series frequency. The trade-off is a larger context that can slow inference.
What data was Lag-Llama pretrained on, and why does it matter?It was pretrained on 20 datasets from domains like energy, transportation, and economics, with frequencies at minute, 15-minute, hourly, and daily, and horizons of 24–60 steps. Expect stronger zero-shot results at those frequencies/horizons and weaker performance on weekly, monthly, or quarterly data or horizons >60 without fine-tuning.
How do I set up Lag-Llama locally?Clone the repository, install requirements, and download the checkpoint (e.g., via huggingface-cli). You’ll also need GluonTS (for data interfaces). Inputs must be formatted as a GluonTS PandasDataset with target values as float32. There is no pip package or hosted API for Lag-Llama.
How do I run zero-shot forecasts and interpret the outputs?Create a LagLlamaEstimator from the checkpoint, set prediction_length and context_length, and build a predictor (CPU or GPU). predictor.predict returns samples shaped [num_samples, horizon]. Use the median as the point forecast and percentiles for prediction intervals at your chosen confidence level.
How should I choose the context length?Treat context_length as a tunable hyperparameter. Longer windows can help when history is stable and informative; shorter windows may work better after recent anomalies. Use cross-validation to compare options; in the chapter’s example, 64 performed best.
Can Lag-Llama be fine-tuned, and what are the essentials?Yes. Fine-tuning is faster on GPU (e.g., Colab T4). Initialize the estimator with fine-tuning args (e.g., lr≈5e-4, batch_size, epochs≈50), train to obtain a predictor, then reuse it for forecasts. Fine-tuning typically improves MAE and sMAPE versus zero-shot, especially outside pretrained frequencies.
What are Lag-Llama’s limitations and best use cases?Limitations: univariate only, no exogenous features, no installable package or API, hardware-dependent speed, and often wide intervals from the Student’s t-distribution. Although anomaly detection is possible, it’s not covered and the model isn’t meant for production. Best for free, open-source experimentation, quick proofs of concept, and research.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Time Series Forecasting Using Foundation Models ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Time Series Forecasting Using Foundation Models ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Time Series Forecasting Using Foundation Models ebook for free