Daily Closing Prices Prediction using ARIMA and LSTM Models
This repository contains two models for forecasting the closing price of Google's stock (GOOGL): a traditional statistical model (ARIMA) and a deep learning model (LSTM).
The project evaluates and compares the performance of both models to determine which one generalizes better for this specific time-series forecasting task. The accompanying Jupyter Notebook (th_nb.ipynb) and the PDF report provide a detailed analysis of the methodology and results.
Models in this Repository
1. ARIMA Model
The AutoRegressive Integrated Moving Average (ARIMA) model is a classic statistical model used for analyzing and forecasting time-series data. It captures the relationships between an observation and a number of lagged observations (AR), uses differencing to make the series stationary (I), and incorporates the dependency between an observation and a residual error from a moving average model (MA).
How to Use the ARIMA Model
You will need pmdarima, joblib, and huggingface_hub to run this model.
1import joblib
2from huggingface_hub import hf_hub_download
3import numpy as np # pmdarima requires numpy
4
5# Download the model from the Hub
6arima_model_path = hf_hub_download(
7 repo_id="touhid155/DataSynthis_ML_JobTask",
8 filename="arima_model.pkl"
9)
10
11# Load the trained ARIMA model
12arima_model = joblib.load(arima_model_path)
13
14# Forecast the next 15 days
15n_periods = 15
16forecasts = arima_model.predict(n_periods=n_periods)
17
18print(f"ARIMA Forecast for the next {n_periods} days:")
19print(forecasts)
2. LSTM Model
The Long Short-Term Memory (LSTM) model is a type of Recurrent Neural Network (RNN) well-suited for sequence prediction problems. LSTMs are capable of learning long-term dependencies, making them powerful for time-series forecasting. This model was built using TensorFlow/Keras.
How to Use the LSTM Model
You will need tensorflow, scikit-learn, numpy, and huggingface_hub. You also need the MinMaxScaler that was used to train the model, which we'll save and load as well.
Note: To make predictions with the LSTM, you need to provide the last 100 days of data as input, scaled in the same way as the training data.
1import numpy as np
2import yfinance as yf
3from tensorflow.keras.models import load_model
4from sklearn.preprocessing import MinMaxScaler
5from huggingface_hub import hf_hub_download
6import joblib
7
8# --- Step 1: Load the LSTM model and the scaler ---
9
10# Download the h5 model file
11lstm_model_path = hf_hub_download(
12 repo_id="touhid155/DataSynthis_ML_JobTask",
13 filename="lstm_model.h5"
14)
15# Load the LSTM model
16lstm_model = load_model(lstm_model_path)
17
18# --- Step 2: Prepare the input data ---
19
20# Fetch the latest stock data to get the last 100 days
21data = yf.download('GOOGL', period='1y')['Close'].values.reshape(-1, 1)
22
23# We need to scale the input data just like it was for training
24# For this example, we'll create a scaler. For perfect accuracy,
25# you should use the exact scaler object saved during training.
26scaler = MinMaxScaler(feature_range=(0, 1))
27scaled_data = scaler.fit_transform(data)
28
29# Get the last 100 data points to make a prediction
30last_100_days = scaled_data[-100:].reshape(1, 100, 1)
31
32# --- Step 3: Make a prediction ---
33
34# Predict the next day's price
35predicted_price_scaled = lstm_model.predict(last_100_days)
36
37# Inverse the scaling to get the actual price
38predicted_price = scaler.inverse_transform(predicted_price_scaled)
39
40print(f"LSTM Predicted price for the next day: ${predicted_price[0][0]:.2f}")
Performance Metrics
| Model | RMSE | MAPE |
|---|
| ARIMA | 3.12 | 1.35% |
| LSTM | 5.66 | 2.47% |