Skip to content

UtkarshMidha/Stock_Price_Prediction_using_LSTM_and_ARIMA

Repository files navigation

Stock Price Prediction using LSTM and ARIMA

GitHub Python TensorFlow Keras Pandas Matplotlib

This project focuses on predicting stock prices using two popular time series forecasting models: LSTM (Long Short-Term Memory) and ARIMA (AutoRegressive Integrated Moving Average). The stock data used is for SAP SE (SAP.DE), scraped from Yahoo Finance.


Table of Contents

  1. Introduction
  2. Features
  3. Overview
  4. Technologies Used
  5. Dataset
  6. Installation & Setup
  7. Stock Price Prediction Using LSTM
  8. Stock Price Prediction Using ARIMA
  9. Conclusion
  10. License

Introduction

Stock price prediction is a challenging task due to the volatile nature of financial markets. This project leverages LSTM (a type of Recurrent Neural Network) and ARIMA (a statistical model) to predict future stock prices based on historical data. The goal is to provide accurate predictions that can assist investors in making informed decisions.


Features

  • LSTM Model:

    • Predicts stock prices using a deep learning approach.
    • Uses a sliding window of 30 days to forecast the next day's closing price.
    • Includes early stopping to prevent overfitting.
  • ARIMA Model:

    • Predicts stock prices using a statistical approach.
    • Automatically selects the best parameters using auto_arima.
    • Provides confidence intervals for predictions.
  • Data Visualization:

    • Visualizes historical stock prices, moving averages, and model predictions.
    • Compares actual vs. predicted prices.
  • Performance Metrics:

    • Evaluates models using metrics like MAE, RMSE, MAPE, SMAPE, and .

Stock Price Prediction using LSTM and ARIMA

📌 Overview

This project implements Stock Price Prediction using LSTM (Long Short-Term Memory) Networks and ARIMA (AutoRegressive Integrated Moving Average) models. The dataset consists of SAP SE (SAP.DE) stock prices collected from Yahoo Finance. The goal is to predict future stock prices using deep learning and time-series forecasting techniques.

🚀 Technologies Used

  • Python (NumPy, Pandas, Matplotlib, Seaborn)
  • Machine Learning & Deep Learning (TensorFlow/Keras, Scikit-learn)
  • Time Series Analysis (Statsmodels, ARIMA, pmdarima)
  • Yahoo Finance API (yfinance)

📂 Dataset

Data Source: Yahoo Finance

  • Stock: SAP SE (SAP.DE)
  • Timeframe: Last 5 Years
  • Columns: Date, Open, High, Low, Close, Adj Close, Volume

🔧 Installation & Setup

To set up the project, follow these steps:

  1. Clone the repository:
    git clone https://github.com/UtkarshMidha/stock_price_prediction_using_LSTM_and_ARIMA.git
    cd stock_price_prediction_using_LSTM_and_ARIMA
    
  2. Install the required dependencies:
    pip install -r requirements.txt
    
  3. Download the stock data:
  • The data is scraped from Yahoo Finance using the yfinance library.
  • Alternatively, you can use the provided CSV file (5Y_SAP_Stock_Data.csv).

📊 Stock Price Prediction Using LSTM

🔹 LSTM Model Architecture

  • Input Layer: 30 days of past stock prices
  • Hidden Layers:
    • LSTM layer with 50 neurons (return sequences: True)
    • LSTM layer with 50 neurons
    • Dense Layer with 25 neurons
  • Output Layer: 1 neuron (Predicted Stock Price)
  • Optimizer: Adam
  • Loss Function: Mean Squared Error (MSE)

🔹 LSTM Training & Results

  • Data Preprocessing: MinMax Scaling, Sliding Window Approach
  • Training: 80% train / 20% test split
  • Model trained with Early Stopping to prevent overfitting

📈 LSTM Results:

  • MAE: 1.79
  • MSE: 7.63
  • RMSE: 2.76
  • MAPE: 1.31%
  • SMAPE: 1.30%
  • : 0.996

🔮 Stock Price Prediction Using ARIMA

🔹 Steps:

  1. Check for Stationarity using the ADF Test
  2. First-Order Differencing applied to make the series stationary
  3. Auto ARIMA to find the best model parameters
  4. Model Training & Forecasting

🔹 ARIMA Model Configuration:

  • Order (p,d,q): (0,1,0)
  • Evaluation Metrics:
    • MAE: 1.44
    • MSE: 4.68
    • RMSE: 2.16
    • MAPE: 32.79%
    • SMAPE: 29.97%
    • : 0.997

📈 ARIMA Results -

📅 Forecast for March 2025

Date Predicted Price (EUR)
11 March 2025 €242.52
12 March 2025 €242.40

📌 Conclusion

  • LSTM:

    • Excels in long-term forecasting and capturing non-linear patterns.
    • Achieves low percentage errors (MAPE: 1.31%, SMAPE: 1.30%).
    • Ideal for trend analysis and applications requiring proportional accuracy.
  • ARIMA:

    • Better for short-term forecasting with lower absolute errors (MAE: 1.44, RMSE: 2.16).
    • Struggles with percentage accuracy (MAPE: 32.79%, SMAPE: 29.97%).
    • Suitable for scenarios prioritizing absolute error minimization.

Future Improvements 🚀

  • Explore hybrid models to combine the strengths of LSTM and ARIMA.
  • Experiment with Transformer-based models for improved long-term dependency capture.
  • Incorporate sentiment analysis from news or social media to enhance predictive accuracy.
  • Perform hyperparameter tuning and error analysis to further refine model performance.

This project highlights the strengths and limitations of both LSTM and ARIMA models, providing a foundation for future improvements in stock price forecasting.

🤝 Contributing

Contributions are welcome! Feel free to open an issue or submit a pull request.

📜 License

This project is licensed under the MIT License.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published