3.1. Dataset Description and Preprocessing
The traffic accident data used in this study were obtained from the Traffic Incident Management System (TIMS) released by the Federal Highway Administration (FHWA) [
18]. The study focuses on a representative urban freeway segment, Interstate 405 (I-405), located in the Los Angeles metropolitan area of California, United States. The I-405 corridor was selected as a representative urban freeway due to its high traffic demand, complex interchange structure, diverse accident patterns, and long-term availability of high-quality accident records, which together provide a challenging and realistic testbed for accident time-series prediction.
Although the experiments in this study are conducted on a single freeway corridor, the proposed modeling framework is not tied to any specific roadway and is designed to capture general temporal characteristics of sparse and bursty accident time series, making it applicable to other urban freeway scenarios with similar data availability.
From a methodological perspective, the proposed approach focuses on learning general temporal patterns of sparse and bursty accident sequences, rather than corridor-specific spatial characteristics. To further examine the generalization capability of the model beyond a single freeway, extensive experiments were additionally conducted on two public benchmark time-series datasets (ETTh1 and Electricity), which exhibit diverse temporal dynamics and statistical properties. The consistent performance gains observed across these datasets indicate that the proposed framework is not limited to a specific corridor and can generalize to other traffic-related time series scenarios.
In this study, the prediction target is defined as the hourly accident count aggregated over the selected freeway corridor, resulting in a univariate time-series forecasting task.
The geographical location of the selected freeway segment is illustrated in
Figure 4.
The original dataset contains detailed information for each traffic accident event, including occurrence time, geographic coordinates, roadway name, accident severity level, and casualty statistics. After data screening and preprocessing, accident records from January 2018 to December 2024 were retained, resulting in approximately 10,253 valid accident events. Each record corresponds to a single traffic accident occurrence.
After aggregating individual accident events into an hourly time series, the final dataset contains approximately 61,000 time steps spanning from January 2018 to December 2024. The resulting accident count series exhibits a highly imbalanced distribution.
Specifically, the majority of hourly intervals contain zero recorded accidents, while a smaller proportion of time steps correspond to one or two accidents. Only a limited number of periods exhibit multiple accidents within the same hour, representing high-risk traffic conditions. This pronounced sparsity and long-tailed distribution further highlight the challenges of traffic accident forecasting and motivate the use of risk-oriented evaluation metrics beyond conventional error-based measures. The distribution of hourly accident counts after aggregation is summarized in
Table 1.
This distribution further confirms the strong sparsity and long-tailed nature of the accident time series.
During preprocessing, records with missing or invalid key attributes (e.g., accident time or location) were removed. The remaining accident events were aggregated into an hourly time series with a continuous temporal index. Hours without recorded accidents were explicitly retained as zero-valued entries, ensuring temporal continuity without artificial interpolation.
Table 2 provides representative accident records sampled from different years (2018, 2021, and 2024) to illustrate the structure and temporal coverage of the raw data.
A sample of the original accident records is presented in
Table 2, which includes the collision date and time, primary and secondary road names, accident severity, number of injuries and fatalities, as well as latitude and longitude information.
To meet the input requirements of time series forecasting models, individual accident events on the I-405 freeway were aggregated into a univariate time series representing the number of accidents per hour. The resulting sequence exhibits pronounced temporal characteristics, including clear periodic patterns (e.g., weekday–weekend differences and peak–off-peak variations) as well as strong burstiness caused by holidays, extreme weather conditions, or abnormal traffic disturbances. In addition to the accident count series, multiple exogenous variables were constructed to enrich the model input and enhance interpretability.
Traffic accident distributions vary significantly across different time periods. Accident occurrences are frequent during peak hours, while late-night and early-morning periods on weekends tend to be sparse, leading to substantial differences in both mean and variance across time segments. Direct normalization of the entire sequence may suppress informative signals during sparse periods while allowing peak-hour patterns to dominate the learning process. To alleviate this issue, a segmented normalization strategy (denoted as seg4) was adopted.
Specifically, the time series was divided into four temporal categories: weekday peak hours (WD-peak), weekday off-peak hours (WD-off), weekend peak hours (WE-peak), and weekend off-peak hours (WE-off). Z-score normalization was applied independently within each segment, followed by value clipping to suppress extreme outliers [
19]. This preprocessing strategy preserves overall temporal trends while improving the representation of sparse accident occurrences, thereby enhancing model robustness under complex temporal conditions.
Based on the segmented normalization strategy (seg4), several accident-related features were constructed. Specifically, the variable accident_seg4 represents the raw hourly accident count assigned to its corresponding temporal segment (WD-peak, WD-off, WE-peak, or WE-off). The feature accident_z_by_seg4 is obtained by applying Z-score normalization to the accident counts within each segment independently, using the segment-specific mean and standard deviation. This operation ensures that accident intensities are normalized relative to comparable temporal contexts rather than the entire time series.
In addition, accident_z_by_peak is derived by normalizing accident counts with respect to peak-hour statistics only, which emphasizes relative accident severity during high-demand traffic periods. All normalization parameters are computed exclusively from the training data to avoid information leakage.
Without this segmented normalization, the strong dominance of peak-hour accident patterns would bias the learning process, making it difficult for the model to effectively capture low-frequency accident signals during off-peak periods.
Figure 5 illustrates the overall preprocessing pipeline used to transform raw traffic accident records into model-ready time series features.
The final feature set used for model training and evaluation is summarized in
Table 3, which includes temporal cyclic encodings, environmental conditions, road surface information, calendar indicators, and multiple accident-related derived features.
3.2. Experimental Setup and Evaluation Metrics
To evaluate the effectiveness of the proposed model for traffic accident prediction, a series of systematic experiments were conducted, including model training configuration, evaluation metrics, hyperparameter sensitivity analysis, and performance comparisons with multiple baseline methods. All experiments were implemented in a unified experimental environment to ensure fairness and reproducibility.
3.2.1. Experimental Configuration
All experiments were carried out on a local workstation running the Windows 11 operating system. The proposed model and baseline methods were implemented using Python 3.9 and the PyTorch 1.12 deep learning framework. The hardware platform consisted of an AMD Ryzen 7 9700X CPU and an NVIDIA GeForce RTX 5070 Ti GPU. Automatic Mixed Precision (AMP) was enabled during training to accelerate computation and reduce memory consumption.
The detailed experimental environment and configuration are summarized in
Table 4.
For all models, the same input window length, forecasting horizon, and dataset partitioning strategy were adopted to ensure a fair comparison. Model training was performed using the training set, hyperparameters were selected based on validation performance, and final results were reported on the test set.
3.2.2. Evaluation Metrics and Baseline Models
To comprehensively assess the prediction accuracy of the proposed model, two widely used regression metrics were adopted: Mean Squared Error (MSE) and Mean Absolute Error (MAE) [
20]. MSE emphasizes large prediction errors by squaring deviations, while MAE measures the average absolute difference between predicted and ground-truth values, providing stable and interpretable error estimation.
The evaluation metrics are defined as follows:
where
n denotes the total number of samples,
represents the ground-truth accident count at time step
i, and
is the corresponding predicted value.
Given the highly imbalanced nature of traffic accident data, where most time intervals contain zero or low accident counts and only a small number of periods exhibit accident surges, error-based metrics alone may not fully reflect a model’s practical effectiveness in safety-critical scenarios.
Therefore, in addition to MSE and MAE, we introduce Top-K Precision as a complementary evaluation metric to assess the model’s ability to correctly identify high-risk accident periods.
Specifically, let
denote the set of
K time intervals with the highest predicted accident counts, and
denote the set of
K intervals with the highest observed accident counts. Top-K Precision is defined as:
where
denotes the set of
K time intervals ranked with the highest predicted accident counts, and
denotes the set of
K time intervals with the highest observed accident counts.
To validate the effectiveness of the proposed method, several representative time series forecasting models were selected as baseline methods. These baselines cover classical statistical approaches, recurrent neural networks, and state-of-the-art Transformer-based architectures. The selected baseline models are summarized as follows:
ARIMA [
21]: A classical autoregressive integrated moving average model serving as a linear statistical baseline.
LSTM: A long short-term memory network designed to capture long-range temporal dependencies in sequential data.
Transformer: A self-attention-based sequence modeling architecture that captures global dependencies between arbitrary time steps, alleviating the gradient degradation problem of recurrent models.
Informer: A Transformer variant that introduces sparse attention mechanisms to improve efficiency for long-sequence forecasting tasks.
Autoformer: A decomposition-based Transformer model that incorporates trend–seasonality decomposition and autocorrelation mechanisms.
PatchTST: A state-of-the-art time series Transformer model based on patch segmentation and local representation learning.
BasisFormer (original): The baseline version of the BasisFormer model without patch encoding or structural enhancement.
Ours: The proposed model, which integrates patch-aware local perception and multi-basis representation into the original BasisFormer framework.
All baseline models were trained and evaluated under the same experimental protocol, including identical input sequence lengths, forecasting horizons, and data splits. This ensures that the reported performance differences are attributable to model design rather than experimental bias.
3.3. Hyperparameter Sensitivity Analysis
To evaluate the robustness of the proposed model under different hyperparameter settings and to analyze its performance stability, a comprehensive hyperparameter sensitivity analysis was conducted. Four key hyperparameters were investigated: the learning rate, patch segmentation strategy (i.e., patch length and stride), the number of attention heads, and the number of basis functions. For each hyperparameter, five representative values were tested while keeping all other settings unchanged. Model performance was evaluated on the validation set using Mean Squared Error (MSE) and Mean Absolute Error (MAE). The experimental results are illustrated in
Figure 6.
As shown in
Figure 6a, the model achieves relatively low error values when the learning rate is set to 1 × 10
−4 or 2 × 10
−4, indicating stable convergence behavior. When the learning rate increases to 1 × 10
−3, both MSE and MAE rise significantly, suggesting that an excessively large learning rate may cause training instability or divergence. Therefore, smaller learning rates are more conducive to stable optimization and generalization performance.
Figure 6b presents the impact of different patch lengths and stride combinations on model performance. The results indicate that a patch configuration of 16/8 yields the lowest MSE, demonstrating that moderately increasing patch size can help capture local temporal structures more effectively. However, when the patch size becomes excessively large, performance degrades, which may be attributed to the over-smoothing of local patterns and the loss of fine-grained temporal information.
The influence of the number of attention heads is shown in
Figure 6c. The model performs best when the number of heads is set to 4 or 6. In contrast, increasing the number of heads to 12 leads to a notable increase in prediction error. This phenomenon suggests that an excessive number of attention heads may cause information dispersion, thereby weakening the model’s ability to focus on salient temporal dependencies.
Figure 6d illustrates the effect of the number of basis functions on prediction accuracy. The optimal performance is achieved when the number of basis functions is set to
N = 10. When
N is either too small (e.g.,
N = 6) or too large (e.g.,
N = 14), the prediction error increases. This result indicates that an insufficient number of basis functions limits the model’s capacity to represent global temporal structures, whereas an overly large basis set may introduce redundancy and impair generalization.
Based on the sensitivity analysis, the primary hyperparameters of the proposed model were selected as summarized in
Table 5.
3.4. Ablation Study
To further investigate the contribution of each key component in the proposed model, a systematic ablation study was conducted. Several core modules were progressively removed or simplified, and the resulting model variants were evaluated on multiple benchmark datasets. The ablation results are reported in
Table 6.
As shown in
Table 6, removing the patch-based encoding mechanism (denoted as w/o Patch) leads to the most significant performance degradation among all ablation settings. On the ETTh1 dataset, the Mean Squared Error (MSE) increases sharply from 0.3989 to 0.5875, while the Mean Absolute Error (MAE) rises from 0.4184 to 0.5203. This result indicates that for low-frequency time series with strong periodicity, such as electricity load data, the patch mechanism plays a crucial role in capturing local fluctuation patterns and enhancing short-term sensitivity.
In contrast, the Basis module exhibits a more pronounced impact on the Traffic dataset. When the number of basis functions is reduced to one (w/o Basis), the MSE and MAE increase to 0.6227 and 0.4062, respectively, which represents a substantially larger degradation compared with the ETTh1 dataset (0.3989→0.4208 in MSE). This observation suggests that for high-dimensional and multi-sensor traffic data with strong periodic structures, the Basis module serves as a core component for abstracting global trends and modeling cross-channel dependencies.
Further analysis of the auxiliary loss functions shows that removing the Smooth Loss or the InfoNCE Loss results in only minor performance degradation. These findings imply that the two auxiliary objectives mainly contribute to training stability and feature distribution regularization, while their direct impact on final prediction accuracy is relatively limited.
Overall, the ablation results demonstrate a complementary relationship between the Patch and Basis mechanisms. The Patch module is particularly effective in modeling low-frequency time series with pronounced local variations, whereas the Basis module provides stronger global modeling capability for high-frequency, high-dimensional datasets. By integrating both components, the proposed model achieves robust performance across diverse time series forecasting scenarios and maintains strong generalization ability.
3.5. Baseline Comparison Under Multiple Evaluation Metrics
3.5.1. Evaluation Under Standard Error Metrics
To further evaluate the effectiveness and generalization capability of the proposed model across different time series forecasting scenarios, visualized prediction results and quantitative comparisons were conducted on the I-405 traffic accident dataset as well as two public benchmark datasets, namely ETTh1 and Electricity. The prediction results are illustrated in
Figure 7a–c.
As shown in
Figure 7a, the traffic accident time series on the I-405 freeway exhibits high sparsity and significant amplitude variations. Despite these challenges, the proposed model accurately captures sparse peak patterns corresponding to accident surges. By employing patch-wise independent basis representations, the model effectively expresses multi-scale temporal patterns within segmented time windows while preserving global temporal dependencies. As a result, the model achieves a balanced representation of local abrupt changes and long-term trends, which is particularly suitable for accident prediction tasks characterized by burstiness and sparsity.
In the ETTh1 electricity load forecasting task (
Figure 7b), the data exhibit strong periodicity and relatively stable temporal dynamics. The proposed model successfully captures the dominant periodic trends; however, slight phase shifts can be observed in some high-frequency fluctuation regions. This phenomenon suggests that under strongly periodic signals, the global basis representation plays a dominant role, while local patch information still has potential for further refinement in modeling fine-scale variations.
As illustrated in
Figure 7c, the Electricity demand dataset demonstrates concurrent fluctuations across multiple users and regions. The proposed model is able to stably track short-term demand variations, with predicted curves closely aligning with ground-truth values in most time intervals. This result indicates that the combined patch-aware and basis-based architecture exhibits strong generalization capability in scenarios involving multi-variable and multi-region temporal interactions.
The quantitative performance comparison of all baseline models is summarized in
Table 7.
As reported in
Table 7, the proposed model (Ours) achieves the best performance on the I-405 traffic accident dataset, with an MSE/MAE of 0.4630/0.2733. Compared with PatchTST, the proposed method achieves improvements of approximately 9.7% in MSE and 12.6% in MAE, and significantly outperforms BasisFormer as well as other state-of-the-art Transformer-based models such as Informer and Autoformer. These results demonstrate that the proposed model effectively captures the coexistence of abrupt events and periodic patterns in traffic accident data. The patch mechanism enhances responsiveness during accident peaks, while the basis representation improves stability during low-activity periods, leading to superior overall performance.
In contrast, classical statistical methods (e.g., ARIMA) and recurrent neural networks (e.g., LSTM) consistently underperform across all datasets. In particular, on the I-405 dataset, the MSE of ARIMA exceeds that of the proposed model by more than 60%, highlighting the limitations of shallow statistical models and simple sequential memory mechanisms when applied to highly non-stationary and sparse accident data.
On strongly periodic public datasets, differentiated performance characteristics are observed. For the ETTh1 dataset, which is dominated by energy consumption time series with weak burstiness, PatchTST achieves the best results (MSE/MAE = 0.3134/0.3585). Although the proposed model does not surpass PatchTST in this scenario, it outperforms Autoformer and Informer, indicating that the introduction of basis representations remains beneficial for capturing periodic trends. On the Electricity dataset, the proposed model achieves an MSE/MAE of 0.1655/0.2654, outperforming BasisFormer and approaching the performance of PatchTST. This suggests that the integration of patch-aware modeling and basis representations provides a balanced solution for datasets with concurrent local disturbances and global temporal patterns.
3.5.2. Evaluation Under Risk-Oriented Metrics
While standard error metrics such as MSE and MAE provide an overall assessment of average prediction accuracy, traffic accident forecasting is inherently risk-oriented, where the accurate identification of extreme accident periods is of greater practical importance. To further evaluate the proposed model under high-risk scenarios, two additional metrics are introduced: the 95th Percentile Mean Absolute Error (P95-MAE) and the Top-K Recall.
The P95-MAE measures the tail behavior of the error distribution and specifically reflects prediction stability during extreme periods characterized by high accident intensity and large fluctuations. In contrast, Top-K Recall evaluates the model’s ability to correctly identify high-risk time windows by comparing the overlap between the top-K time steps ranked by predicted accident intensity and those ranked by ground-truth values. These two metrics jointly assess the model’s performance from the perspectives of extreme error control and risk period identification, which are more aligned with the requirements of traffic safety analysis and early warning applications.
The P95-MAE is defined as:
where
and
denote the ground-truth and predicted accident values at time step
t, respectively.
The Top-K Recall is defined as:
where
ΩK represents the set of time steps corresponding to the top-K highest accident intensities in the ground truth, and
denotes the set of time steps ranked within the top-K according to model predictions.
The comparative results of all baseline models on the I-405 traffic accident dataset under these risk-oriented metrics are summarized in
Table 8.
As shown in
Table 8, traditional statistical models such as ARIMA and recurrent neural networks such as LSTM perform poorly in extreme accident scenarios, exhibiting both higher P95-MAE values and lower Top-K Recall scores. This indicates that shallow statistical assumptions and simple sequential memory mechanisms are insufficient for modeling highly sparse and bursty accident patterns.
Transformer-based models, including Informer and Autoformer, achieve moderate improvements in both metrics, with P95-MAE reduced to 1.07 and 1.02 and Top-K Recall increased to 0.53 and 0.57, respectively. However, their performance remains unstable in peak accident periods, suggesting limited sensitivity to abrupt risk escalation.
PatchTST demonstrates a stronger ability to capture short-term accident surges, achieving a Top-K Recall of 0.63 due to its patch-wise local modeling strategy. BasisFormer, on the other hand, benefits from interpretable global basis representations and achieves a Top-K Recall of 0.55, highlighting its strength in modeling structured temporal patterns.
The proposed model consistently outperforms all baselines across both metrics, achieving the lowest P95-MAE of 0.82 and the highest Top-K Recall of 0.71. Compared with PatchTST, the P95-MAE is reduced by approximately 12.8%, while the Top-K Recall is improved by approximately 29.1% compared with BasisFormer. These results demonstrate that the integration of patch-aware local perception and global basis representations significantly enhances the model’s sensitivity to high-risk accident periods without sacrificing long-term trend modeling capability.
Overall, the risk-oriented evaluation confirms that the proposed model not only achieves superior average prediction accuracy but also exhibits strong robustness and reliability in extreme accident scenarios. This dual advantage is particularly critical for traffic accident forecasting applications, where timely identification of high-risk periods is essential for proactive traffic safety management and early warning systems.
In addition to Top-K Recall, we further report Top-K Precision to evaluate the accuracy of high-risk period identification from a complementary perspective.
As shown in
Table 9, the proposed model consistently achieves the highest Top-K Precision across different K values, indicating its superior capability in accurately identifying high-risk accident periods compared with baseline methods.
3.6. Interpretability Analysis
To further understand the decision-making mechanism of the proposed model in traffic accident forecasting, an interpretability analysis was conducted by visualizing the learned attention weights within the basis-aware attention module. The visualization aims to reveal how different input feature channels contribute to the learned basis representations under a multi-head attention mechanism.
Figure 8 presents the attention heatmaps learned by the proposed model on the I-405 traffic accident dataset. The horizontal axis represents the basis index (
N = 10), while the vertical axis corresponds to different input feature channels, including temporal encodings, environmental factors, and historical accident-related variables. Color intensity indicates the magnitude of attention weights, reflecting the relative importance of each feature channel to a specific basis representation. Each subfigure corresponds to one attention head (Head 0–Head 5), illustrating the diversity of feature–basis associations learned across multiple subspaces.
As shown in
Figure 8, temporal features such as hour_sin, dow_cos, and is_weekend consistently receive high attention weights across multiple heads, indicating that the model strongly relies on daily and weekly periodic patterns as well as weekend effects when modeling accident occurrence. This observation aligns well with the inherent temporal regularities of traffic accidents, which are closely associated with peak-hour traffic demand and weekday–weekend travel behavior.
Historical accident-related features, including accident_raw and accident_z_by_peak, also exhibit prominent attention weights in several heads. This suggests that recent accident intensity and peak-period normalized signals play a critical role in shaping the basis representations used for future prediction. The result further validates the effectiveness of the segmented normalization strategy adopted in data preprocessing, as these features become salient components in the learned basis combinations.
In contrast, external environmental variables such as weather conditions (WEATHER_1_B, WEATHER_1_F), lighting conditions (LIGHTING_B, LIGHTING_E), and road surface states (ROAD_SURFACE) demonstrate more heterogeneous attention patterns across different heads. This indicates that the multi-head attention mechanism captures complementary contextual information in different subspaces, allowing the model to selectively emphasize environmental factors under varying temporal and traffic conditions rather than assigning them uniform importance.
Notably, distinct attention heads exhibit markedly different basis allocation patterns even for the same feature channel. For example, Head 0 and Head 2 attend to different basis indices for hour_sin, implying that the model learns multiple latent temporal abstractions for the same input signal. This diversity confirms that the multi-head attention mechanism is not redundant but instead enables the model to disentangle complex feature–basis relationships and capture heterogeneous accident patterns at multiple temporal scales.
Overall, the interpretability analysis demonstrates that the proposed model effectively integrates temporal regularities, historical accident dynamics, and contextual environmental factors through basis-aware attention. By learning structured and interpretable feature–basis associations, the model provides not only accurate predictions but also meaningful insights into the underlying mechanisms driving traffic accident risks, thereby enhancing its applicability in practical traffic safety analysis and decision support systems.