This program predicts stock prices using the ARIMA,SARIMA,PROPHET,LSTM,CHRONOS,LINEAR REGRESSION models. It also detects and removes outliers from the stock price data to improve the accuracy of predictions.
#Goal This program shows that LSTM model and Chronos Model fits best for stock price prediction as time series dataset.
To implement stock price prediction using multiple models such as LSTM, ARIMA, Prophet, SARIMA, Linear Regression, Ridge Regression, and a Deterministic Model, you need to approach each method with its specific configuration and implementation. Below is an overview of how you can combine these models and their implementation steps. Steps to Implement
- Load Stock Price Data: Use a single dataset for training all models.
- Preprocessing: Handle missing values, feature scaling, and train-test split.
- Modeling: o Implement ARIMA and SARIMA for time series modeling. o Use LSTM for deep learning-based time series forecasting. o Use Linear Regression and Ridge Regression for standard regression techniques. o Implement Prophet for time-series forecasting using trend decomposition. o Implement Deterministic models using moving averages or other simple statistical techniques.
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error, mean_absolute_error from sklearn.linear_model import LinearRegression, Ridge from statsmodels.tsa.arima.model import ARIMA from statsmodels.tsa.statespace.sarimax import SARIMAX from fbprophet import Prophet import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, LSTM
arima_model = ARIMA(train_data, order=(5,1,0)).fit() arima_pred = arima_model.forecast(steps=len(test_data))
mse_arima = mean_squared_error(test_data, arima_pred)
sarima_model = SARIMAX(train_data, order=(1,1,1), seasonal_order=(1,1,1,12)).fit() sarima_pred = sarima_model.forecast(steps=len(test_data))
mse_sarima = mean_squared_error(test_data, sarima_pred)
prophet_data = close_prices.reset_index().rename(columns={'Date': 'ds', 'Close': 'y'}) prophet_model = Prophet() prophet_model.fit(prophet_data[:train_size])
future = prophet_model.make_future_dataframe(periods=len(test_data)) prophet_pred = prophet_model.predict(future)
mse_prophet = mean_squared_error(test_data['Close'], prophet_pred['yhat'][-len(test_data):])
train_scaled = train_data.values.reshape((train_data.shape[0], 1, 1))
lstm_model = Sequential() lstm_model.add(LSTM(50, input_shape=(train_scaled.shape[1], train_scaled.shape[2]))) lstm_model.add(Dense(1)) lstm_model.compile(optimizer='adam', loss='mse')
lstm_model.fit(train_scaled, train_data.values, epochs=100, batch_size=32, verbose=1)
lstm_pred = lstm_model.predict(test_data.values.reshape((test_data.shape[0], 1, 1)))
mse_lstm = mean_squared_error(test_data, lstm_pred)
X_train = np.arange(len(train_data)).reshape(-1, 1) y_train = train_data.values
X_test = np.arange(len(train_data), len(train_data) + len(test_data)).reshape(-1, 1)
lr_model = LinearRegression() lr_model.fit(X_train, y_train)
lr_pred = lr_model.predict(X_test)
mse_lr = mean_squared_error(test_data, lr_pred)
ridge_model = Ridge(alpha=1.0) ridge_model.fit(X_train, y_train)
ridge_pred = ridge_model.predict(X_test)
mse_ridge = mean_squared_error(test_data, ridge_pred)
window = 5 deterministic_pred = test_data.rolling(window=window).mean().dropna()
mse_deterministic = mean_squared_error(test_data[window-1:], deterministic_pred)