We use cookies to improve your browsing experience and to analyze our website traffic. By clicking “Accept All” you agree to our use of cookies. Privacy policy.
11 readsMIT License

Enhancing Time Series Forecasting with Darts: A Temporal Fusion Transformer Approach

Table of contents

Abstract

This paper presents an implementation of a car sales demand forecasting system using the Temporal Fusion Transformer (TFT) model integrated with the Darts time series library. By leveraging historical car sales data along with external covariates, our system predicts future vehicle demand with high accuracy. We detail the data preprocessing, model training, prediction, and evaluation processes, demonstrating the utility of TFT in capturing temporal dependencies and complex nonlinear relationships in car sales data. Experimental results, illustrated through comprehensive visualizations, highlight the model’s forecasting performance as measured by metrics such as Mean Absolute Percentage Error (MAPE).

The complete code for this project is available on GitHub:

Keywords:

Time Series Forecasting, Car Sales, Temporal Fusion Transformer, Demand Forecasting, Darts, Deep Learning, MAPE, Predictive Modeling

1. Introduction

Accurate demand forecasting is crucial in the automotive industry for inventory management, production planning, and strategic decision-making. Traditional forecasting methods often struggle to capture the intricate temporal dynamics and external influences affecting car sales. Recent advancements in deep learning, particularly the Temporal Fusion Transformer (TFT), offer a promising solution by modeling both long-term dependencies and short-term variations.

In this project, we apply the TFT model to forecast car sales using historical sales data. Our dataset includes features such as YEAR_QUARTER, MODEL, CUSTOMER, and QUANTITY (representing the number of vehicles sold), along with external covariates like promotional events or economic indicators that may influence demand patterns.

Alt text

2. Methodology

2.1 Data Preparation

The dataset is structured with the following columns:

- YEAR_QUARTER: Represents the time period in YYYY Qn format.
- MODEL: The specific car model for which demand is being forecasted.
- CUSTOMER: Customer identifier (optional).
- QUANTITY: Historical sales data (target variable, i.e., the number of cars sold).

Additional covariates, such as promotional events, market trends, or economic factors, are included to enhance forecasting accuracy. The data preprocessing step involves:

  • Loading and merging datasets.
  • Handling missing values.
  • Feature engineering (e.g., extracting YEAR, QUARTER, and combining them as QUARTER-YEAR).

2.2 Model Training

Our implementation utilizes the Darts library for time series forecasting. The TFT model is trained on the preprocessed data through the following steps:

1. Data Normalization: A MinMaxScaler is applied to ensure stable model training.
2. Training/Validation Split: The dataset is partitioned based on a specified split_date to evaluate model performance on unseen data.
3. Model Training: The TFT model is trained to learn the temporal dependencies and forecast future car sales.

The training process is encapsulated within the train_model() function in our codebase.
Alt text

2.3 Demand Prediction and Evaluation

After training, the model generates predictions for future car sales over a specified period (e.g., the next four quarters). Predictions are evaluated using metrics such as Mean Absolute Percentage Error (MAPE) to quantify accuracy. The functions predict_model() and perform_backtest() manage the prediction and evaluation phases.

2.4 Visualization

Visualization is key to interpreting model performance. Our approach includes:

  • Plotting actual versus predicted car sales for each model.
  • Displaying MAPE scores directly on the plots for clear performance insights.
  • Saving the final predictions in CSV format for further analysis.

Alt text

3. Implementation

The main script (main.py) is structured as follows:

1. Data Preparation:
Load and preprocess car sales data.
Create additional time-related features (e.g., YEAR, QUARTER, QUARTER-YEAR).

2. Training:
Split the data into training and validation sets.
Normalize the data using a MinMaxScaler.
Train the TFT model using the train_model() function.

3. Prediction and Evaluation:
Generate car sales demand predictions using the predict_model() function.
Evaluate predictions with perform_backtest() and eval_backtest(), calculating metrics such as MAPE.

4. Visualization and Output:
Visualize actual versus predicted car sales.
Save the predictions and evaluation metrics for further analysis.
The project also allows for customization through parameters such as toggling training, handling outliers, and including external covariates.

4. Experimental Results

In our experiments, the TFT model achieved a MAPE of below 20% on the validation set for a specific car model. These results demonstrate that the TFT can effectively capture the temporal patterns and external influences present in car sales data. Visual comparisons between actual and predicted sales confirm the robustness of our approach, making it suitable for real-world demand forecasting in the automotive sector.

5. Conclusion

This work demonstrates the successful application of the Temporal Fusion Transformer for car sales demand forecasting. By integrating historical sales data with relevant external covariates, our Darts-based implementation provides accurate and interpretable predictions. Future work may explore hyperparameter optimization, alternative evaluation metrics, and the inclusion of additional external variables to further enhance forecasting performance.

The complete code for this project is available on GitHub:

6. Acknowledgments

We acknowledge the contributions of the Darts library developers and the broader research community for their insights into deep learning-based time series forecasting.

7. References

Darts: A Python Library for Easy Manipulation and Forecasting of Time Series

Models

There are no models linked

Datasets

There are no datasets linked