Practical Guide to Detecting and Explaining Anomalies in Indicator Data
A cookbook for data analysts and curators in national statistical organizations and development agencies. Version 0.1 (working draft), December 2026.
This guide is a working draft from the AI for Data – Data for AI program. It collects practical approaches and open standards. It does not represent official World Bank Group guidance or policy and is not legal advice; an organization applies it under its own statistical law, data protection rules, and institutional policies. Comments and corrections are welcome as GitHub issues, and the contributing page explains how to propose a recipe.
A statistical office or a development agency that publishes hundreds of indicator series for dozens of countries cannot read every value before release. Some values are errors in data entry or in processing, some follow from real events such as a conflict or a disaster, and some come from a revision of the method. The first task is to find the values that are unusual within their own series. The second task is to decide, for each of them, which of these causes applies. The first task suits statistical methods, and the second task needs a person, who can be helped by a first hypothesis with the evidence for it.
This guide follows the two tasks in four chapters, each of which answers one question of an analyst. The first chapter prepares the series so that they can be compared on one scale. The second runs several detection methods and counts how many of them flag each observation. The third selects the flagged series that deserve an explanation, asks a language model for a structured hypothesis about each, and writes the answers into a file for review. The fourth covers the review, the record of each run, and the repetition of the run on a new release of the data.
The recipes use two components of the open-source ai4data package of the program. The detection suite, ai4data.anomaly.detection, rebuilds in Python the workflow of the macroanomaly R package of the World Bank, with methods taken from several R packages that are named in Chapter 2. The explanation pipeline, ai4data.anomaly.explanation, builds the requests to the language model and reads its answers. The guide states what to do and why, and it links to the detection reference and the explanation handbook for how each method and tool works.
Prerequisites
The recipes assume the following are in place or can be arranged:
- Indicator series in a table, with a code for the indicator, a code for the country or region, the year, and the value, and a person who knows the indicators well enough to judge a flagged value.
- Python 3.10 or later with the
anomalyextra of the package installed (pip install ai4data[anomaly]), and basic skill in running a script from a terminal. The recipes run offline on the files of this guide. - At the AI-ready level, a provider account or a locally hosted model for the batch of explanation requests, a policy that decides which series may be sent to it, and a place to store the run records.
- At the AI-native level, a team that keeps the reviewers' verdicts and uses them to test changes to the thresholds and the prompts.
First steps
For a team with limited time, this sequence gives the most useful result first:
- Run the detection script of Chapter 2 on one table of series and read the list of observations that most methods flag. Allow about one hour.
- Check the share of flagged observations and set the thresholds to the number that reviewers can read, using Recipe 2.2. Allow about one hour.
- Build the batch file for the shortlist with Recipe 3.2 and read one request, to see what a model would be asked. Allow about one hour.
The review, the run records, and the comparison of runs in Chapter 4 follow when the first cycle has produced explanations to review.
Chapters and questions
| Chapter | Question | Practical topics |
|---|---|---|
| 1. Preparing series for detection | Can the series be compared on one scale? | Long table, imputation of gaps, trend removal, standardised scores |
| 2. Detecting anomalies with several methods | Which observations are unusual, and how sure is the pipeline? | Five methods, count of agreement, thresholds, check on planted anomalies |
| 3. Explaining the flagged series | Which flags deserve an explanation, and what does the language model say? | Shortlist, batch file, structured answers, review file |
| 4. Reviewing and running the pipeline | How are results reviewed, recorded and repeated on new data? | Reviewer application, run record, comparison of two runs |
Maturity levels
Each chapter describes three levels. An organization can sit at different levels in different chapters, and the first level is a complete, useful state on its own.
| Level | Meaning |
|---|---|
| Foundational | The practice exists in a form a person can use and a crawler can read. |
| AI-ready | The practice is structured, identified, and accessible through documented interfaces, so that software, including AI systems, can use it without manual steps. |
| AI-native | The practice offers AI-oriented interfaces or methods, with provenance and verification built in. |
Running example
The recipes use one fictional panel, so that each chapter picks up where the previous one ends. The panel has four indicators, eight fictional countries, and the years 2000 to 2024, which gives 32 series. Every value and every country name is invented. The panel was generated by the function demo_panel of the demonstration notebook, with a fixed seed, and it is not a part of the shared running example of the other guides. Jumps, runs of shifted values, and constant tails were planted in half of the series, and some years are missing, so that every step has something to do.
| Indicator code | Indicator, as labelled in the file |
|---|---|
SH.MORT.U5 | Under-five mortality rate (per 1,000 live births) |
SE.PRM.NENR | School enrolment, primary (% net) |
EG.ELC.ACCS | Access to electricity (% of population) |
SI.POV.HEAD | Poverty headcount ratio (% of population) |
The countries have the codes AAA to HHH: Aldoria, Brenvale, Caldmere, Dovenia, Estrella, Fenmark, Garmoor, and Halvern. The labels follow the pattern of the indicator codes of the World Bank, but the values describe no real country. A second file adds the invented year 2025, so that Chapter 4 can repeat the run on a new release.
The explanation step needs a provider key or a batch run, and the language model is not called in the examples. The stand-in answers of Recipe 3.3 are fixed and are not output of a model. The demonstration notebook replays recorded stand-in answers in the same way.
Scope of the examples
The examples are small on purpose, so that a reader can run them in an afternoon and see the shape of each check. They are starting points. Each recipe ends with a What this does not do note that says where the example stops and what the full version adds. The settings of the methods fit the example panel and are not recommendations for other data, the planted anomalies are simpler than real ones, and the review decisions are invented.
Files
The files are in website/static/cookbook-files/anomaly-detection-and-explanation/. The scripts require ai4data[anomaly] and are tested in tests/test_cookbook_anomaly_detection_and_explanation.py.
| File | Used in |
|---|---|
| indicator_panel.csv | The fictional panel of 780 rows, which every chapter uses. |
| planted_anomalies.csv | The list of the 16 planted anomalies, for Recipe 2.3. |
| prepare_series.py | Preparation report: gaps, imputation, dropped series, and scale (Chapter 1). |
| detect_and_shortlist.py | Detection, flagged file, shortlist, batch file, and run record (Chapters 2 to 4). |
| score_against_planted.py | Comparison of the flags with the planted anomalies (Recipe 2.3). |
| stand_in_answers.py | Fixed answers in the format of a batch output, for runs without a provider key (Recipe 3.3). |
| review_export.py | Parsing of the answers, review file, and summary (Recipe 3.3). |
| feedback_example.csv | Fictional reviewer decisions in the layout of the application's export (Recipe 4.1). |
| review_summary.py | Status of the review and the list of items to follow up (Recipe 4.1). |
| extend_panel.py and indicator_panel_2025.csv | The panel with an invented year 2025, as a new release (Recipe 4.3). |
| compare_runs.py | Comparison of the flags of two runs (Recipe 4.3). |
Related resources
| Resource | Purpose |
|---|---|
| This guide | What an organization can do, and why, in a sequence of practical steps. |
| Detection reference | How each preparation step, method, and option works, and where the Python methods differ from the R packages. |
| Anomaly explanation handbook | The method, the schema, and the limits of explanations from a language model. |
| Demonstration notebook | The sequence of this guide on the fictional panel, with pictures. Path in the repository: notebooks/data-anomaly/Anomaly_Detection_Suite.ipynb. |
| Code | Open-source software to deploy or adapt. |
The methods of the detection suite are rebuilt in Python from the macroanomaly package of the World Bank, by L. Perfetti Villa, D. Newhouse, and O. Dupriez, and from the R packages anomaly, feasts, pracma, isotree, and outliertree. The sources of the methods are cited in the chapters where they are used.
Citation
World Bank. 2026. Practical Guide to Detecting and Explaining Anomalies in Indicator Data. Version 0.1. AI for Data – Data for AI. https://worldbank.github.io/ai4data/cookbook/anomaly-detection-and-explanation/
Version history
- 0.1 (December 2026). First draft.