Predecitve model for Stock Return forecast (future prediction) for FTS100 Tech-Mark Series (top technical firms) in UK listed on London Stock Exchange
import pandas as pd import sklearn as sl import numpy as np import matplotlib.pyplot as plt import statsmodels.api as sm from statsmodels.tsa.arima.model import ARIMA from statsmodels.graphics.tsaplots import plot_acf, plot_pacf from statsmodels.tsa.stattools import adfuller from statsmodels.tsa.seasonal import seasonal_decompose from statsmodels.tsa.holtwinters import ExponentialSmoothing from statsmodels.tsa.statespace.sarimax import SARIMAX from statsmodels.tools.eval_measures import rmse from arch import arch_model
import yfinance as yf
data = yf.download("SN.L", start="2015-01-01", end="2022-12-31")
CLOSEPRICE = data["Close"].dropna()
print(CLOSEPRICE.describe())
plt.figure(figsize=(10, 6)) plt.plot(CLOSEPRICE) plt.title("Stock Close Price, 2015-2022") plt.xlabel("Days") plt.ylabel("Price") plt.show()
train = CLOSEPRICE[:1518] test = CLOSEPRICE[1518:]
plt.figure(figsize=(10, 6)) plt.plot(CLOSEPRICE, label="Close Price") plt.plot(train, label="Training Data", color="blue") plt.plot(test, label="Testing Data", color="green") plt.legend(loc="lower right") plt.title("Stock Close Price, 2015-2022") plt.xlabel("Days") plt.ylabel("Price") plt.show()
adf_result = adfuller(train) print("ADF Statistic:", adf_result[0]) print("p-value:", adf_result[1])
fig, ax = plt.subplots(2, 1, figsize=(10, 8)) plot_acf(train, ax=ax[0]) plot_pacf(train, ax=ax[1]) plt.show()
diff_train = train.diff().dropna()
adf_result_diff = adfuller(diff_train) print("ADF Statistic (After Differencing):", adf_result_diff[0]) print("p-value (After Differencing):", adf_result_diff[1])
fig, ax = plt.subplots(2, 1, figsize=(10, 8)) plot_acf(diff_train, ax=ax[0]) plot_pacf(diff_train, ax=ax[1]) plt.show()
model = ARIMA(train, order=(2, 1, 2)) model_fit = model.fit() print(model_fit.summary())
residuals = model_fit.resid fig, ax = plt.subplots(2, 1, figsize=(10, 8)) plot_acf(residuals, ax=ax[0]) plot_pacf(residuals, ax=ax[1]) plt.show()
forecast = model_fit.forecast(steps=503) forecast_values = forecast[0]
plt.figure(figsize=(10, 6)) plt.plot(forecast_values, label="Forecasted Values") plt.title("Forecasted Stock Close Price") plt.xlabel("Days") plt.ylabel("Price") plt.legend() plt.show()
rmse_value = rmse(test, forecast_values) print("RMSE:", rmse_value)
model_garch = arch_model(returnss, vol='Garch', p=1, q=1) model_garch_fit = model_garch.fit() print(model_garch_fit.summary())
forecast_garch = model_garch_fit.forecast(start=len(returnss), horizon=503) forecast_values_garch = forecast_garch.variance.values[-1, :]
plt.figure(figsize=(10, 6)) plt.plot(forecast_values_garch, label="Forecasted Values (GARCH)") plt.title("Forecasted Stock Close Price (GARCH)") plt.xlabel("Days") plt.ylabel("Price") plt.legend() plt.show()
rmse_value_garch = rmse(test, forecast_values_garch) print("RMSE (GARCH):", rmse_value_garch)