Skip to content

imehranasgari/DL_TimeSeries_Transformer_TCN_yfinance_01

Repository files navigation

Comparative Analysis of Deep Learning Models for Stock Price Forecasting

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.

Problem Statement and Goal of Project

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.

Solution Approach

The solution follows a structured machine learning pipeline designed to ensure a fair and rigorous comparison between models.

  1. Data Acquisition & Preprocessing: Historical stock data for Microsoft (MSFT) was downloaded using the yfinance library. The data underwent thorough cleaning, including handling missing business days and ensuring data integrity.

  2. Advanced Feature Engineering: To enhance model performance, I engineered two types of covariates:

    • Past Covariates: Created a rich set of technical indicators using the ta library, 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.
  3. 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.
  4. Training and Evaluation: All models were trained using PyTorch Lightning, with callbacks such as EarlyStopping and ModelCheckpoint to 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.

Technologies & Libraries

  • 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

Description about Dataset

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:

  • Date
  • Open
  • High
  • Low
  • Close (The target variable)
  • Adj Close
  • Volume

Installation & Execution Guide

  1. Clone the repository:

    git clone https://github.com/your-username/your-repository-name.git
    cd your-repository-name
  2. Create and activate a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
  3. Install the required libraries:

    pip install -r requirements.txt

    (Note: A requirements.txt file should be created containing packages like darts, yfinance, torch, ta, etc.)

  4. Run the Jupyter Notebook:

    jupyter notebook TCNModel,deepar,nbeats,TFT_project.ipynb

Key Results / Performance

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.

Screenshots / Sample Output

Below is the final comparison plot visualizing the forecasts from all four models against the actual stock prices.

Additional Learnings / Reflections

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.

👤 Author

Mehran Asgari


📄 License

This project is licensed under the Apache 2.0 License – see the LICENSE file for details.

About

This project forecasts MSFT stock prices by comparing four advanced deep learning models: TFT, TCN, DeepAR, and N-BEATS. It uses a robust pipeline with technical indicators as features. The TCN model achieved the highest accuracy, demonstrating a comprehensive approach to time-series model selection.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors