This project presents a comprehensive study on forecasting Microsoft (MSFT) stock prices using multiple advanced deep learning models. The core objective is to implement, train, and benchmark various architectures—including Temporal Fusion Transformer (TFT), Temporal Convolutional Network (TCN), DeepAR (RNN), and N-BEATS—to identify the most effective approach for this time-series challenge.
The primary goal is to accurately predict the future closing price of Microsoft (MSFT) stock. This involves tackling the inherent complexities of financial data, such as non-linearity, trends, and volatility. Rather than focusing on a single model, this project aims to demonstrate a robust methodology for comparative analysis, showcasing the strengths and weaknesses of different state-of-the-art forecasting techniques in a real-world scenario.
The solution follows a structured machine learning pipeline designed to ensure a fair and rigorous comparison between models.
-
Data Acquisition & Preprocessing: Historical stock data for Microsoft (MSFT) was downloaded using the
yfinancelibrary. The data underwent thorough cleaning, including handling missing business days and ensuring data integrity. -
Advanced Feature Engineering: To enhance model performance, I engineered two types of covariates:
- Past Covariates: Created a rich set of technical indicators using the
talibrary, such as RSI, MACD, Bollinger Bands, ATR, ROC, and moving averages. These features provide historical context on market momentum, trend, and volatility. - Future Covariates: Generated time-based features like the day, month, weekday, and year. These are known in advance and help models like TFT capture seasonality and long-term patterns.
- Past Covariates: Created a rich set of technical indicators using the
-
Modeling and Comparative Analysis: Four distinct deep learning models from the Darts library were implemented and trained:
- Temporal Fusion Transformer (TFT): A state-of-the-art attention-based architecture designed to handle both past and future covariates.
- Temporal Convolutional Network (TCN): A powerful convolutional architecture adapted for sequential data.
- DeepAR (RNN-LSTM): A popular probabilistic forecasting model based on recurrent neural networks.
- N-BEATS: A block-based architecture known for its strong performance on diverse time-series datasets.
-
Training and Evaluation: All models were trained using PyTorch Lightning, with callbacks such as
EarlyStoppingandModelCheckpointto ensure efficient and robust training. Performance was measured using Mean Absolute Error (MAE) and Mean Absolute Percentage Error (MAPE) on a held-out test set.
- 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, spanning 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 packages likedarts,yfinance,torch,ta, etc.) -
Run the Jupyter Notebook:
jupyter notebook TCNModel,deepar,nbeats,TFT_project.ipynb
The models were evaluated on the test set, and their performance was compared using MAE and MAPE metrics. The TCN model demonstrated exceptional performance, significantly outperforming the other architectures on this specific task.
| Model | MAE | MAPE (%) |
|---|---|---|
| DeepAR | 0.586363 | 44.65 |
| N-BEATS | 0.168436 | 12.69 |
| TFT | 0.302638 | 22.81 |
| TCN | 0.062426 | 4.74 |
This comparison highlights the TCN's effectiveness in capturing the complex patterns within the MSFT stock price data.
Below is the final comparison plot visualizing the forecasts from all four models against the actual stock prices.
This project provided deep insights into the practical application and comparative evaluation of modern time-series models.
- End-to-End Forecasting Pipeline: I successfully implemented a complete workflow, from data ingestion and sophisticated feature engineering to training and benchmarking multiple complex models.
- Model Architecture Nuances: Gained hands-on experience with four different deep learning architectures, understanding their specific data requirements (e.g., covariates for TFT vs. N-BEATS) and hyperparameter tuning.
- Systematic Benchmarking: The decision to compare multiple models was intentional to demonstrate a rigorous, data-driven approach. It shows that while models like TFT are powerful, simpler architectures like TCN can sometimes be superior depending on the dataset.
- Practical Tooling: This project solidified my skills with the Darts and PyTorch Lightning libraries, which are essential for efficient and scalable deep learning for time-series.
💡 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.