<jupyter_start><jupyter_text>Importing Libraries & Dataset<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from math import sqrt
%matplotlib inline
import warnings
warnings.filterwarnings(action="ignore", category= FutureWarning)
warnings.filterwarnings(action="ignore", category= DeprecationWarning)

from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error

from statsmodels.tsa.ar_model import AR
from statsmodels.tsa.arima_model import ARMA
from statsmodels.tsa.arima_model import ARIMA
import statsmodels.tsa.api as smt
import statsmodels.api as sm
from statsmodels.tsa.seasonal import seasonal_decompose
dataparse= lambda dates:pd.datetime.strptime(dates, "%Y-%m-%d")
df= pd.read_csv(r"C:\Users\shruti\Desktop\Decodr Session Recording\Project\Decodr Project\Stock forecasting project\gs.us.txt",
                sep=",", parse_dates=["Date"], index_col=["Date"], date_parser=dataparse)
df.head()
df.tail()<jupyter_output><empty_output><jupyter_text>Exploratory Data Analysis<jupyter_code>plt.figure(figsize=(10,5))
plt.xlabel("Dates")
plt.ylabel("Open Prices")
plt.plot(df["Open"])
plt.grid(True)<jupyter_output><empty_output><jupyter_text>Auto-correlation Plot<jupyter_code>values=pd.DataFrame(df["Open"].values)
dataframe=pd.concat([values, values.shift(1), values.shift(5), values.shift(10), values.shift(30)], axis=1)
dataframe.columns= ["t", "t+1", "t+5", "t+10", "t+30"]
dataframe.head()
result=dataframe.corr()
print(result)<jupyter_output>t       t+1       t+5      t+10      t+30
t     1.000000  0.998288  0.992929  0.987001  0.954921
t+1   0.998288  1.000000  0.994216  0.988282  0.956678
t+5   0.992929  0.994216  1.000000  0.992890  0.963463
t+10  0.987001  0.988282  0.992890  1.000000  0.971829
t+30  0.954921  0.956678  0.963463  0.971829  1.000000<jupyter_text>Dickey-Fuller method to check Stationary & Seasonality<jupyter_code>def tsplot(y, lags=None, figsize=(12,8), style="bmh"):
    
    if not isinstance(y, pd.Series):
        y=pd.Series(y)
        
    with plt.style.context(style="bmh"):
        fig=plt.figure(figsize=figsize)
        layout=(2,2)
        
        ts_ax= plt.subplot2grid(layout, (0,0), colspan=2)
        acf_ax= plt.subplot2grid(layout, (1,0))
        pacf_ax= plt.subplot2grid(layout, (1,1))
        
        y.plot(ax=ts_ax)
        p_value=sm.tsa.stattools.adfuller(y)[1]
        ts_ax.set_title("Time Series Analysis Plot\n Dickey-Fuller: p={0:.5f}".format(p_value))
        smt.graphics.plot_acf(y, lags=lags, ax=acf_ax)
        smt.graphics.plot_pacf(y, lags=lags, ax=pacf_ax)
        plt.tight_layout()
tsplot(df.Open, lags=30)
data= df.copy(deep=False)
data1= df.copy(deep=False)
data1.Open= data1.Open - data1.Open.shift(1)
tsplot(data1.Open[1:], lags=30)
train_data, test_data= data[1:-73], data[-73:]
plt.figure(figsize=(12,8))
plt.xlabel=("Dates")
plt.ylabel("Open Prices")
plt.plot(data["Open"].tail(600), "green", label="Train_data")
plt.plot(test_data["Open"], "blue", label="Test_data")
plt.grid(True)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Mean Value Plot<jupyter_code>mean_value= data["Open"].mean()
mean_value
plt.figure(figsize=(10,8))
plt.ylabel("Open Prices")
plt.plot(data["Open"], "green", label="Train_Data")
plt.plot(test_data["Open"], "blue", label="Test_data")
plt.axhline(y=mean_value, xmin=0.85, xmax=1, color= "red")
plt.grid(True)
plt.legend()

plt.figure(figsize=(10,8))
plt.ylabel("Open Prices")
plt.plot(data["Open"].tail(600), "green", label="Train_data")
plt.plot(test_data["Open"], "blue", label="Test_data")
plt.axhline(y=mean_value, xmin=0.85, xmax=1, color= "red")
plt.grid(True)
plt.legend()
print("MSE:" + str(mean_squared_error(test_data["Open"], np.full(len(test_data), mean_value))))
print("MAE:" + str(mean_absolute_error(test_data["Open"], np.full(len(test_data), mean_value))))
print("RMSE:" + str(sqrt(mean_squared_error(test_data["Open"], np.full(len(test_data), mean_value)))))<jupyter_output>MSE:6.614792639144097
MAE:1.9907383591040042
RMSE:2.571923917837403<jupyter_text>Model Building & Validation  Auto-Regressive Mode<jupyter_code>train_ar= train_data["Open"]
test_ar=test_data["Open"]

model=AR(train_ar)
model_fit= model.fit()
window= model_fit.k_ar
coef= model_fit.params
coef
window
history= train_ar[len(train_ar) - window:]
history= [history[i] for i in range(len(history))]
predictions= list()
for t in range(len(test_ar)):
    length= len(history)
    lag= [history[i] for i in range(length-window,length)]
    yhat= coef[0]
    for d in range(window):
        yhat += coef[d+1] * lag[window-d-1]
    
    obs=test_ar[t]
    predictions.append(yhat)
    history.append(obs)
plt.figure(figsize=(10,8))
plt.plot(data.index[-600:], data["Open"].tail(600), color="green", label= "Close price")
plt.plot(test_data.index, test_data["Open"], color= "red", label= "Test Close Price")
plt.plot(test_data.index, predictions, color="blue", label= "Predict Close Price")
plt.xticks(rotation=45)
plt.grid(True)
plt.legend()
plt.show()

plt.figure(figsize=(10,8))
print("Lag: %s" % model_fit.k_ar)
plt.plot(data.index[-100:], data["Open"].tail(100), color="green", label= "Close price")
plt.plot(test_data.index, test_data["Open"], color= "red", label= "Test Close Price")
plt.plot(test_data.index, predictions, color="blue", label= "Predict Close Price")
plt.grid(True)
plt.legend()
plt.show()
print("MSE:" + str(mean_squared_error(test_data["Open"], predictions)))
print("MAE:" + str(mean_absolute_error(test_data["Open"], predictions)))
print("RMSE:" + str(sqrt(mean_squared_error(test_data["Open"], predictions))))<jupyter_output>MSE:6.564935200370728
MAE:2.0082033669768165
RMSE:2.5622129498483783<jupyter_text>Moving Average Model<jupyter_code>train_ma= train_data["Open"]
test_ma= test_data["Open"]

history= [x for x in train_ma]
y= test_ma

predictions= list()
model= ARMA(history, order=(0,8))
model_fit= model.fit(disp=0)
yhat= model_fit.forecast()[0]
predictions.append(yhat)
history.append(y[0])
for i in range(1, len(y)):
    model= ARMA(history, order=(0,8))
    model_fit= model.fit(disp=0)
    yhat= model_fit.forecast()[0]
    predictions.append(yhat)
    obs= y[i]
    history.append(obs)
plt.figure(figsize=(10,8))
plt.plot(data.index[-600:], data["Open"].tail(600), color="green", label= "Train Stock Price")
plt.plot(test_data.index, y, color= "red", label= "Real Close Price")
plt.plot(test_data.index, predictions, color="blue", label= "Predict Stock Price")
plt.grid(True)
plt.legend()
plt.show()

plt.figure(figsize=(10,8))
plt.plot(data.index[-100:], data["Open"].tail(100), color="green", label= "Train Stcok price")
plt.plot(test_data.index, y, color= "red", label= "Real Close Price")
plt.plot(test_data.index, predictions, color="blue", label= "Predict Close Price")
plt.grid(True)
plt.legend()
plt.show()
print("MSE:" + str(mean_squared_error(y, predictions)))
print("MAE:" + str(mean_absolute_error(y, predictions)))
print("RMSE:" + str(sqrt(mean_squared_error(y, predictions))))<jupyter_output>MSE:6.584860061318949
MAE:2.001128892938742
RMSE:2.5660982173952247
{'fraction_non_alphanumeric': 0.17048630519843488, 'fraction_numerical': 0.06414197875908328, 'mean_word_length': 6.236602628918099, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '141058', 'license': 'Unlicense', 'n_tokens_mistral': 3080, 'n_tokens_neox': 2763, 'n_words': 478, 'path': 'Project5- Stock Forecasting .ipynb', 'repo_name': 'vaibhav162/Stock-Forecasting-Project', 'size': '7156'}