← Back to blog

Bus Headways Analysis for Anomaly Detection

· Finished Bachelor in Telecomm. Engineering at UPM (Madrid) · Published in IEEE T-ITS

Anyone who regularly takes the city bus knows the feeling: you wait at the stop for twenty minutes with no sign of a bus, and then three of them arrive simultaneously, bumper-to-bumper. In public transit engineering, this phenomenon is known as bus bunching. But bunching is just one symptom of a broader challenge: how can transit authorities detect breakdowns, abnormal delays, schedule drift, and service anomalies in real time without having to install proprietary, expensive telemetry systems on thousands of vehicles?

That was the central question of my Bachelor's Thesis at Universidad Politécnica de Madrid (UPM) — developed during my first year as a Data Science Researcher intern at Cabify in Madrid — under the supervision of Prof. Pedro J. Zufiria and in collaboration with Carlos García-Mauriño. The work first produced two CSCI 2020 conference papers — “Headway Estimation in Urban Buses based on Available Arrival Time Estimators” and “Bus Pass Time Estimation based on Efficient Data Gathering from a Slow Mobility Server” — and later expanded into the full journal paper published in the IEEE Transactions on Intelligent Transportation Systems (T-ITS) (available in Early Access at the time):"Bus Headways Analysis for Anomaly Detection".

🎤 Presenting at CSCI 2020

Both CSCI 2020 papers were presented at the conference in December 2020, and the recorded talks live on the Cátedra Cabify-UPM YouTube channel — the university-industry chair created in October 2020 by Cabify and ETSIT-UPM to fund research scholarships in intelligent technologies and data science for sustainable mobility, co-directed by my thesis supervisor Prof. Pedro J. Zufiria.

📺 Recorded conference talks from the Cátedra Cabify-UPM YouTube channel (CSCI 2020, virtual edition).

The Headway Paradigm: Why Arrival Gaps Tell the Whole Story

Most traditional approaches try to track individual bus trajectories using GPS coordinates or arrival timestamps. However, GPS pings in dense urban corridors suffer from multipath reflections, signal dropouts, and noisy stop matching. Instead, our core insight was to shift the modeling focus entirely to time headways.

A headway $h_k$ is defined as the elapsed time between the arrival of bus $k-1$ and bus $k$ on the same line at a specific stop (Figure 1). When a bus gets delayed, it encounters more passengers at subsequent stops, which increases dwell times and slows it down further. Meanwhile, the trailing bus encounters fewer passengers and speeds up until it catches the leader. This creates a strong statistical coupling between consecutive headways $(h_k, h_{k+1})$.

Bus headway definition and consecutive arrival time modeling in urban transit corridors
Figure 1 — Sequential bus headway formulation: tracking the time intervals between successive arrivals at a target bus stop to capture queue dynamics and operational deviations.

Stochastic Bivariate Modeling & Operational Time Slices

Urban traffic dynamics change drastically throughout the day. A 10-minute headway might be completely normal during the 3:00 PM off-peak window, but an indicator of major congestion during the 8:00 AM morning rush hour. To capture these non-stationary patterns, we partitioned the operating day into discrete time slices (morning peak, midday valley, afternoon peak, evening, night).

Within each time slice, we model consecutive headway pairs $(h_k, h_{k+1})$ as samples from a joint bivariate distribution. From this stochastic framework, we compute bivariate sample means, covariance matrices, and confidence ellipses ($p = 95\%, 99\%$) as shown in Figure 2 for both the Madrid (EMT) and London (TfL) bus networks.

Bivariate confidence ellipses across operational time slices for Madrid EMT bus routes
Figure 2 — Bivariate statistical confidence ellipses for consecutive headway pairs $(h_k, h_{k+1})$ across different operational time slices on Madrid EMT Line 27.

Real-Time Anomaly Scoring via Mahalanobis Distance

With the baseline confidence ellipses established, real-time anomaly detection becomes elegant and computationally lightweight:

  1. When a new bus arrives at a stop, the system computes the current headway pair $\mathbf{h} = [h_k, h_{k+1}]^T$.
  2. It looks up the corresponding baseline mean $oldsymbol{\mu}_s$ and covariance matrix $oldsymbol{\Sigma}_s$ for that time slice.
  3. It calculates the Mahalanobis distance: $$D_M(\mathbf{h}) = \sqrt{(\mathbf{h} - oldsymbol{\mu}_s)^T oldsymbol{\Sigma}_s^{-1} (\mathbf{h} - oldsymbol{\mu}_s)}$$
  4. If $D_M(\mathbf{h})$ exceeds the calibrated threshold $\chi^2_{2, 1-lpha}$, the event is flagged as an operational anomaly with a tunable false-alarm rate $lpha$.
Anomaly detection time series showing flagged headway deviations and service irregularities
Figure 3 — Real-time anomaly detection scoring on Madrid Line 27: the red markers highlight severe headway deviations, prolonged passenger waits, and bunching events.

Validation on Madrid (EMT), London (TfL), and Black Swan Events

We deployed and validated our data processing pipeline on live open-data APIs from two of Europe's largest bus transit systems: EMT Madrid (analyzing the high-frequency Line 27 along Paseo de la Castellana) and Transport for London (TfL) (analyzing Route 25 through central London).

Beyond daily transit bunching and driver-relief gaps, the framework demonstrated remarkable sensitivity during macro-disruptions. As shown in Figure 4, our system clearly captured the radical structural transition in transit frequency during the initial onset of the March 2020 COVID-19 pandemic lockdown in Madrid, where headway distributions shifted drastically away from historical clusters.

Drastic shift in headway distribution during the COVID-19 lockdown transition in Madrid
Figure 4 — Bivariate density shift of bus headways during the COVID-19 lockdown transition in Madrid, showing the departure from regular pre-pandemic operating regimes.

The repository ships a full interactive web dashboard so you can inspect the whole pipeline in action. Figure 5 shows the London (TfL) live cockpit with its 2D trajectory rotation view, letting operators observe how consecutive headway pairs drift through the bivariate confidence state space in real time.

London TfL headway dashboard with 2D rotation view of bivariate state space
Figure 5 — The London (TfL) monitoring cockpit: real-time 2D-rotated view of consecutive headway pairs traversing the bivariate confidence state space.

All data ingestion scripts, stochastic modeling pipelines, and evaluation notebooks are open source on GitHub.

July 2020 · BSc Thesis & IEEE T-ITSGitHubCSCI 2020 · Headway EstimationCSCI 2020 · Bus Pass TimeCátedra Cabify-UPMTalk · Headway EstimationTalk · Bus Pass TimeUPM Thesis PDFUPM

Alejandro Jarabo-Peñas, Pedro J. Zufiria, Carlos García-Mauriño