MMCAformer (Macro–Micro Cross-Attention Transformer) is a freeway traffic-speed forecasting framework that integrates macroscopic traffic flow states with fine-grained microscopic driving behavior extracted from connected-vehicle (CV) trajectories.
Rather than treating all inputs as homogeneous traffic variables, MMCAformer separately represents macro features (segment speed and CV volume) and micro features (speed volatility and hard/medium/light acceleration and braking), and then learns their spatial and temporal interactions through macro–micro cross-attention. A Student-t negative log-likelihood objective enables both point-wise speed prediction and uncertainty estimation.
This repository provides the processed modeling datasets, scripts, and pre-trained weights used to reproduce the main experiments in our paper: https://arxiv.org/abs/2602.16730
In freeway systems, sudden hard-braking events can introduce localized deceleration disturbances that propagate through car-following interactions and contribute to subsequent traffic-speed deterioration. Hard acceleration and speed volatility may also reflect unstable traffic dynamics and provide informative signals of traffic-flow evolution. However, traditional aggregated traffic-flow data cannot capture such fine-grained driving behavior information, which may limit further improvements in traffic speed prediction.
Fig. 1 — A hard-braking event in the current segment slows traffic upstream and downstream; future speeds depend on both corridor context and local driving behavior.
CV trajectories are first aggregated into two feature groups:
- Macro traffic states: segment speed and CV volume.
- Micro driving behaviors: CV speed volatility and frequencies of hard, medium, and light acceleration and braking.
MMCAformer independently embeds the two feature groups and applies:
- Spatial macro self-attention to capture dependencies among road segments;
- Spatial macro–micro cross-attention to integrate behavior information across segments;
- Temporal macro self-attention to model historical traffic evolution; and
- Temporal macro–micro cross-attention to capture time-dependent interactions between traffic states and driving behaviors.
The final prediction head directly generates multi-step future speeds together with the parameters of a Student-t distribution, allowing the model to quantify predictive uncertainty while accommodating heavy-tailed forecasting errors.
Fig. 2 — MMCAformer: embedding → spatial CA → temporal CA → speed & uncertainty heads.
Under midday congestion, MMCAformer tracks ground-truth speed and provides 90% prediction intervals. Panel (a) uses macro features only (without micro); panel (b) uses the full macro–micro model. Adding micro features yields tighter intervals (lower MPIW) while maintaining coverage. Panel (c) shows co-occurring spikes in volatility and hard braking/acceleration during the same congested periods.
Fig. 3 — Case study on I-4 Hillsborough: (a) w/o micro features, (b) full MMCAformer with micro features, (c) micro dynamics during congestion.
When segment speed drops below 40 mph, cross-attention scores between macro and micro features increase significantly—indicating the model relies on micro dynamics most during congestion.
Fig. 4 — Macro/micro inputs, cross-attention (CA) score matrix, and mean CA scores across speed bins.
CV-based speed predictions from MMCAformer are compared with MVDS (inductive loop detector) measurements—an independent ground-truth source. Predictions closely follow MVDS on both weekday and weekend traffic patterns.
Fig. 5 — Segment-level CV speed vs. MVDS speed (May 2024); weekday and weekend zoom-ins.
- Python 3.8+
- PyTorch
- NumPy
- scikit-learn
- SciPy
iTransformerpackage (for the iTransformer baseline)einops(for the TLAST baseline)
Clone the repository (Git LFS is required for the large .npz files):
git lfs install
git clone https://github.com/UCFLeiHan/MMCAformer.git
cd MMCAformerSupported datasets: I4_Hills, I75_Hills, I275_Hills, I4_Orange (or all).
# Evaluate the main MMCAformer model
python run_MMCAformer.py --dataset I4_Hills --mode test
# Train MMCAformer
python run_MMCAformer.py --dataset I4_Hills --mode train
# Ablation studies (Micro / Cross / S / T)
python run_MMCAformer_ablations.py --ablation Micro --dataset I4_Hills --mode test
# Uncertainty evaluation (Gaussian or Student-t)
python run_MMCAformer_uncertainty.py --dataset I4_Hills --distribution student
# Baseline models (e.g., LSTM, STGCN, TLAST, ...)
python run_baselines.py --model LSTM --dataset I4_Hills --mode testPre-trained weights are loaded from checkpoints/ by default. Use --mode both to train and then evaluate.
Each processed .npz file contains:
sequences: input tensor with shape(S, T, N, C)targets: future speed tensor with shape(S, T, N)
where:
S: number of samplesT: number of time stepsN: number of freeway segmentsC: number of input features
The model uses the past 12 time steps (1 hour at 5-min intervals) to predict the next 12 time steps (1 hour).
Each processed .npz file stores 14 raw channels in this order:
| Index | Field | Role |
|---|---|---|
| 0 | Road_direction |
Segment direction ID (not used by MMCAformer) |
| 1 | mean_of_speed_mean |
Macro: segment-level average CV speed |
| 2 | n_journeys |
Macro: CV volume (unique journeys per 5-min interval) |
| 3 | mean_of_speed_std |
Micro: CV speed volatility within the segment |
| 4 | total_stop |
Stop-event count (not used by MMCAformer) |
| 5 | total_hard_acc |
Micro: hard acceleration event count |
| 6 | total_median_acc |
Micro: medium acceleration event count |
| 7 | total_light_acc |
Micro: light acceleration event count |
| 8 | total_hard_dec |
Micro: hard braking event count |
| 9 | total_median_dec |
Micro: medium braking event count |
| 10 | total_light_dec |
Micro: light braking event count |
| 11 | total_LC |
Lane-change event count (not used by MMCAformer) |
| 12 | hour |
Time-of-day index (used as temporal embedding) |
| 13 | weekday |
Day-of-week index (used as temporal embedding) |
In the main MMCAformer scripts, channels 0, 4, and 11 are dropped. The remaining continuous inputs are grouped as:
Macro features (2)
- Segment speed (
mean_of_speed_mean) - CV volume (
n_journeys)
Micro features (7)
- Speed volatility (
mean_of_speed_std) - Hard / medium / light acceleration frequency (
total_hard_acc,total_median_acc,total_light_acc): number of hard/medium/light acceleration events within each 5-min interval - Hard / medium / light braking frequency (
total_hard_dec,total_median_dec,total_light_dec): number of hard/medium/light braking events within each 5-min interval
Temporal embeddings (2)
hourandweekdayare kept as discrete embeddings (not as continuous macro/micro channels)
Overall, each road segment is represented by 2 macro features + 7 micro features at every 5-min time step, plus hour/weekday embeddings.
The prediction target is the future segment-level average traffic speed in mph.
Graph-based baselines use an adjacency matrix stored as a PyTorch tensor:
- shape:
(N, N) - required by DCRNN, STGCN, GraphWaveNet, and ASTGCN
- The training/test split is performed by date to avoid temporal information leakage.
- Only observations from 06:00–22:00 are used because nighttime CV penetration is substantially lower.
- All scripts use early stopping and retain the model with the lowest validation MAE.
- Speed is normalized during training and inverse-transformed before evaluation.
- MMCAformer and its ablation variants use mixed-precision training (AMP + GradScaler).
- Evaluation is reported at three levels:
- Overall performance
- Per-step performance across 5–60 min prediction horizons
- Per-bin performance under different observed speed ranges (0–20, 20–40, 40–60, >60 mph)




