This project showcases a sophisticated approach to time-series forecasting by implementing the Temporal Fusion Transformer (TFT) model to predict Microsoft (MSFT) stock prices. The entire workflow, from data acquisition and feature engineering to model training and comparative analysis, is demonstrated to highlight a deep understanding of modern forecasting techniques.
The primary objective is to accurately forecast the future closing price of Microsoft (MSFT) stock. This involves addressing the complexities of financial time-series data, such as trends, seasonality, and volatility, by building a robust predictive model. The goal is not only to create a single high-performing model but also to benchmark its performance against other established deep learning architectures to validate its effectiveness.
The solution follows a structured machine learning pipeline designed for time-series forecasting:
-
Data Acquisition & Preprocessing: Historical stock data for Microsoft (MSFT) was downloaded using the
yfinancelibrary. The data was cleaned by ensuring proper data types, checking for duplicates, and handling any missing business day values. -
Feature Engineering: To enrich the model's predictive power, two types of covariates were engineered:
- Past Covariates: A comprehensive set of technical indicators were created using the
talibrary, including RSI, MACD, Bollinger Bands, ATR, ROC, and moving averages. These features provide the model with historical context on market momentum and volatility. - Future Covariates: Time-based features such as the day of the month, month of the year, day of the week, and the year were generated. These are known in advance and help the model capture seasonality and long-term trends.
- Past Covariates: A comprehensive set of technical indicators were created using the
-
Modeling with Temporal Fusion Transformer (TFT): The core of this project is the
TFTModelfrom the Darts library. This state-of-the-art architecture is designed to capture complex temporal dynamics at various scales. The model was configured and trained using PyTorch Lightning, with callbacks likeEarlyStoppingandModelCheckpointto ensure efficient training and prevent overfitting. -
Benchmarking: To provide a comprehensive evaluation, the TFT model's performance was compared against two other powerful deep learning models:
- DeepAR-style RNN Model: An LSTM-based recurrent neural network.
- N-BEATS Model: A block-based architecture that has shown strong performance on a wide range of time-series tasks.
- Core Libraries: Python 3.9
- Time-Series & Modeling: Darts, PyTorch, PyTorch Lightning
- Data Handling: Pandas, NumPy
- Data Acquisition: yfinance
- Feature Engineering: ta (Technical Analysis Library)
- Visualization: Matplotlib
The dataset consists of daily stock price information for Microsoft (MSFT), retrieved from Yahoo Finance. It spans from January 1, 2017, to July 21, 2025. The key features used for prediction and feature engineering include:
DateOpenHighLowClose(The target variable)Adj CloseVolume
-
Clone the repository:
git clone https://github.com/your-username/your-repository-name.git cd your-repository-name -
Create and activate a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate`
-
Install the required libraries:
pip install -r requirements.txt
(Note: A
requirements.txtfile should be created containing the necessary packages likedarts,yfinance,torch,ta, etc.) -
Run the Jupyter Notebook:
jupyter notebook TFT_project.ipynb
The primary TFT model was trained to predict future stock prices, and its performance was evaluated against DeepAR and N-BEATS using Mean Absolute Error (MAE) and Mean Absolute Percentage Error (MAPE). This comparative analysis demonstrates the TFT's ability to effectively leverage both historical and future-known information for robust forecasting.
The final code block in the notebook generates a performance comparison table and a plot visualizing the predictions of all three models against the actual test data.
Here is a sample plot from the project showing the raw closing price data over time, illustrating the trend and volatility the model learns from.
This project was a valuable exercise in applying advanced deep learning techniques to a real-world financial forecasting problem. Key takeaways include:
- End-to-End Implementation: Successfully managed the entire project lifecycle, from data ingestion and cleaning to advanced feature engineering, model training, and evaluation.
- Advanced Model Architecture: Gained hands-on experience with the Temporal Fusion Transformer, understanding its components like variable selection networks and attention mechanisms that make it so powerful.
- Importance of Covariates: Demonstrated that carefully engineered past and future covariates significantly enhance a model's ability to understand complex time-series patterns.
- Systematic Benchmarking: By comparing TFT against DeepAR and N-BEATS, I learned to not only build a model but also to rigorously validate its performance in context. The inclusion of these models was a deliberate choice to showcase a thorough and analytical approach to model selection.
💡 Some interactive outputs (e.g., plots, widgets) may not display correctly on GitHub. If so, please view this notebook via nbviewer.org for full rendering.
Email: imehranasgari@gmail.com
GitHub: https://github.com/imehranasgari
This project is licensed under the Apache 2.0 License – see the LICENSE file for details.