Stock forecasting and forecasting using LSTM (long-short-term-memory)

1. Description

        Accurately predicting stock market movements has long been an elusive goal for investors and traders. While countless strategies and models have emerged over the years, one approach has recently gained significant attention for its ability to capture complex patterns and dependencies in historical data: Long Short-Term Memory (LSTM). Harnessing the power of deep learning, LSTMs offer a promising avenue to gain insight into the unpredictability of stock markets. In this article, we delve into the field of LSTM-based stock market forecasting and explore how this innovative approach has the potential to transform investment strategies.

Plot prediction after successful analysis and prediction.

Plot prediction after successful analysis and prediction.

        At its core, LSTMs are variants of recurrent neural networks (RNNs) specifically designed to solve the vanishing gradient problem that plagues traditional RNNs. The vanishing gradient problem refers to the phenomenon in which the gradients of early layers in a network become smaller and smaller, hindering their ability to capture long-term dependencies. LSTMs overcome this limitation by incorporating memory cells, gates, and well-designed connections that allow them to selectively retain and propagate information over longer time intervals. This unique architecture enables LSTM models to capture the intricate temporal relationships in sequential data, making them particularly suitable for forecasting time-series data, such as stock prices.

2. LSTM memory network

        To learn more about Round LSTM, visit:

2.1 Import required libraries

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
import math
from sklearn.metrics import mean_squared_error

        Here, we import pandas, matplotlib for plotting, numpy for preprocessing, sklearn for preprocessing, scaling and error calculation, and tensorflow for model building.

2.2 I transplanted the dataset

        You can find the dataset I used in the following GitHub repository.

df = pd.read_csv('D:/stockprice-master/NSE-TATAGLOBAL.csv')
df.head()

3. Data Analysis

df2 = df.reset_index()['Close']
plt.plot(df2)

Diagram showing inventory flow in dataset

We will make stock predictions on the closing price column.

3.1 Data preprocessing

scaler = MinMaxScaler()
df2 = scaler.fit_transform(np.array(df2).reshape(-1,1))
df2.shape

(2035, 1)

Here we scale down   values ​​between (0, 1) .

3.2 Train-Test Split

train_size = int(len(df2)*0.65)
test_size = len(df2) - train_size
train_data,test_data = df2[0:train_size,:],df2[train_size:len(df2),:1]

        Here, we took  65 % of the data for training and the remaining  35%  for testing.

def create_dataset(dataset, time_step = 1):
    dataX,dataY = [],[]
    for i in range(len(dataset)-time_step-1):
                   a = dataset[i:(i+time_step),0]
                   dataX.append(a)
                   dataY.append(dataset[i + time_step,0])
    return np.array(dataX),np.array(dataY)

        Create a function as  create_dataset() that splits the dataset into 2 based on the time step we take. The first dataset i.e; dataX takes values ​​as its input and the second dataset dataY takes values ​​as output. Basically, it creates a dataset matrix from the above dataset.

# calling the create dataset function to split the data into 
# input output datasets with time step 100
time_step = 100
X_train,Y_train =  create_dataset(train_data,time_step)
X_test,Y_test =  create_dataset(test_data,time_step)
# checking values
print(X_train.shape)
print(X_train)
print(X_test.shape)
print(Y_test.shape)

(1221, 100)
[[0.62418301 0.62214052 0.62622549 ...0.83455882 0.86213235 0.85273693]
[0.62214052 0.62622549 0.63378268 ...0.86213235 0.85273693 0.87111928]
[0.62622549 0.63378268 0.62234477 ...0.85273693 0.87111928 0.84497549]
...
[0.34517974 0.31781046 0.33047386 ...0.2816585 0.27001634 0.26531863]
[0.31781046 0.33047386 0.32128268 ...0.27001634 0.26531863 0.27389706]
[0.33047386 0.32128268 0.34007353 ...0.26531863 0.27389706 0.25347222]](612, 100)(612,)

4. Create and fit the LSTM model

model = Sequential()
model.add(LSTM(50,return_sequences = True,input_shape = (X_train.shape[1],1)))
model.add(LSTM(50,return_sequences = True))
model.add(LSTM(50))
model.add(Dense(1))
model.compile(loss = 'mean_squared_error',optimizer = 'adam')

        Here, we added  4  layers of LSTM with 1 layer as input layer, 2  layers as hidden layer and 1  layer as output layer as Dense. In the first  3  layers, we took  50 neurons and  for output.

        We compile the model with the Adam optimizer , which will calculate the loss using the mean squared error .

model.summary()

model.fit(X_train,Y_train,validation_data = (X_test,Y_test),epochs = 100,batch_size = 64,verbose = 1)

Here, the model has been trained for  100  epochs with a batch size of  64 per epoch.

5. Predict and check performance matrix

train_predict = model.predict(X_train)
test_predict = model. Predict(X_test)
# transform to original form
train_predict = scaler.inverse_transform(train_predict)
test_predict = scaler.inverse_transform(test_predict)

        When we scale down the values ​​of the dataset in 0 and 1, we need to invert the transformation again in order to get accurate predictions on the graph, so here we invert the transformations of both predictions.

        Now it's time to calculate the rmse performance matrix.

print(math.sqrt(mean_squared_error(Y_train,train_predict)))
print(math.sqrt(mean_squared_error(Y_test,test_predict)))

166.74853517776896
116.51567464682968

Here, both calculated values ​​are very close, i.e.; a difference of less than 50 indicates good model accuracy .

Six graphic drawing

look_back = 100
trainPredictPlot = np.empty_like(df2)
trainPredictPlot[:,:] = np.nan
trainPredictPlot[look_back : len(train_predict)+look_back,:] = train_predict

        The lookback variable takes the number of values ​​after the current value, ie; remembers the same previous 100 values ​​as LSTM. Here, every time the graph is plotted, trainPredictionPlot  takes the 100 values ​​behind them and plots it. The plot starts with the first 100 values ​​and goes up to the length of the train prediction + backtracking, which is 100.

testPredictPlot = np.empty_like(df2)
testPredictPlot[:,:] = np.nan
testPredictPlot[len(train_predict)+(look_back)*2 + 1 : len(df2) - 1,:] = test_predict

        The same is true for TestPredictionPlot  , but this time it takes the value next to Train_predict . The review here will pick up where the train forecast left off.

plt.plot(scaler.inverse_transform(df2))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

        Here, orange is TrainPredictionPlot, green is TestPredictionPplot, and blue is the actual dataset. Therefore, we can see that our model predicts the stock price very well.

        This model is for learning purposes only and is not recommended for any future investment. Prajwal Chauhan

7. Conclusion

        In conclusion, stock market forecasting using long short-term memory (LSTM) represents a major leap forward in the field of financial forecasting. This innovative approach, based on the power of deep learning, demonstrates its potential to capture complex patterns and dependencies in historical stock market data. By incorporating LSTM models into investment strategies, traders and investors can gain a valuable edge in navigating the unpredictability of the stock market.

Guess you like

Origin blog.csdn.net/gongdiwudu/article/details/132451488