Stock Market Prediction using Long Short-Term Memory
Shubhodh Amaravadi1 , Pulluri Anudeep2, Yeramalla Uttam3, K Nagendra Chary41,2,3B. Tech Scholars, Department of Computer Science and Engineering, SNIST, Hyderabad-501301, India
4Assistant Professor, Department of Computer Science and Engineering, SNIST, Hyderabad-501301, India
***
Abstract - It has never been straightforward to invest in a portfolio of assets since the abnormalities of the financial market prevent simple models from accurately forecasting future asset values. The current main trend in scientific study is machine learning, which consists of teaching computers to execute activities that ordinarily need human intelligence. In order to forecast future stock market values, this article uses recurrent neural networks (RNN), particularly the Long-Short Term Memory model (LSTM). This paper's major goal is to determine how accurately a machine learning algorithm can predict and how much epochs can enhance our model. Time series data are used to represent stock prices, and neural networks are trained to identify patterns from trends in the historical data. To increase the accuracy of stock price prediction, this system used an LSTM algorithm.
Key Words: Machine Learning, RNN, LSTM, Price Prediction,SVM,Regression.
1.INTRODUCTION
Several research have investigated the use of machine learning in the quantitative financial sector. Machine learning algorithms can be used to predict prices for managing and restricting a full portfolio of assets, as well as for the investing process and many other processes. Machine learning, in its broadest sense, refers to all algorithmic techniques that enable computers to identify patterns based solely on data and without the use of programminginstructions.Manymodelsprovideavariety oftechniquesthatcanbecombinedwithmachinelearning inquantitativefinance,particularlywhenchoosingassets, topredictfutureassetvalue.Thisclassofmodelsprovides atechniquethatcombinesweaksourcesofinformationto createanoddtoolthatcanbeappliedeffectively.Acritical neural network, gradient boosted regression trees, support vector machines, and random forecast are just a few of the machine learning methods that have recently benefited from the integration of statistics and learning models. These algorithms can make complicated patterns with nonlinear properties and certain relations that are challenging to find with linear methods. Additionally, compared to linear regression methods, these ones demonstrate greater efficacy and multicollinearity. The application of machine learning techniques in finance is the topic of numerous research some of which utilised tree-basedmodelstoforecastportfolioreturnsandothers whichemployeddeeplearningtocreatefuture.
Themodelcreatedbytheauthorsusesthesupportvector machine (SVM) method and the mean variance (MV) method for portfolio selection. Others then go on to forecast stock returns using this unique decision-making model for day trading investments on the stock market. Deep learning techniques for smart indexing were discussed in another work. In addition, numerous studies haveexaminedawiderangeoftrendsandApplicationsof Machine Learning in Quantitative Finance. The literature review covered in this paper includes return forecasting, portfolio construction, ethics, fraud detection, decision making, language processing, and sentiment analysis. Since these models don't rely on long-term memory (passed-downdatasequences),aclassofmachinelearning techniquesbasedonrecurrentneuralnetworkshasshown to be particularly beneficial in financial market price prediction. The vanishing gradient problem, which arises asaresultoftheRNNblocks'repetitiveusageofthesame parameters at each step, is one of the primary problems withRNN.
While generalising variable-length sequences and maintaining a fixed number of learnable parameters overall, we introduce unique parameters at each step. InternalvariablesknownasGatesarestoredingatedcells. Every time step's information, including early states, determines the value of each gate. The many variables of interestarethenmultipliedbythegate'svalueinorderto affect them. Data collected in a time-series format over a period allows us to track changes over time. Time-series data can monitor development over a period of seconds, days,orevenyears. Thisoverall increasestheaccuracyin predictingthestockprice.
2. LITERATURE SURVEY
The stock exchange has grown to be one of the most important events in today's financial world. The current state of the stock market has a significant impact on the global economy. People from many walks of life, whether they come from business or academic backgrounds, have been drawn to the stock market with great success. The stockmarket'snonlinearcharacterhasmaderesearchonit one of the most important and popular topics worldwide. Peoplechoosetoinvestinthestockmarketbasedontheir predictions or knowledge from earlier studies. In terms of forecasting, people frequently seek out instruments or strategiesthatwouldreducetheirrisksandmaximizetheir earnings; as a result, stock price forecasting assumes a
significantpositioninthealwayscompetitivestockmarket industry.
Adopting conventional methods like fundamental and technical analysis does not seem to guarantee the predictability's consistency and accuracy. As a result, machine learning technologies have emerged as the most recent trend in stock market forecasting, with predictions based on current market values as a result of training on earliervalues.Inordertoforecastthepresenttrendofthe stock market, this article focuses on RNN (Recurrent Neural Networks) and LSTM (Long Short-Term Memory) technology.
3. DESIGN AND IMPLEMENTATION
We will use Python machine learning to estimate stock valueusingtheLSTM.
3.1 Importing the Libraries
Python was used in this study to predict Stock Market Prediction. Several packages have used it, notably Keras andMatplotlib.Thecommandslistedbelowcanbeusedto installthenecessarylibraries:
PipinstallKeras
Pipinstallmatplotlib
Pipinstallnumpy
Pipinstallpandas
Keras:
Stock Price Prediction Using a Keras Long Short-Term Memory (LSTM) Model because they have the capacity to store historical data, LSTMs are particularly effective at solving sequence prediction issues. This is significant in our situation since a stock's historical price plays a key roleindeterminingitsfutureprice.
Matplotlib:
A low-level Python package used for data visualisation is called Matplotlib. It emulates MATLAB-like graphs and visualisations and is simple to use. This library's plots, which include line charts, bar charts, histograms, and more,areconstructedontopofNumPyarrays.
Numpy:
Python numpy library is used in stock market prediction forperformingsomescientificcalculationsallaround.
We are predicting the test data using transform function and providing the epoch values around to 50 and then providing the range of -1 to 1 to the reshape function. In
the transform function the parameter provided is the test_input.
3.1 Dataset
For the purpose of data visualization, the data is taken fromthesampledatasetsandstoredinthecsvfile.
The user can view the data to get more information. This saves the stock prices that are obtained from the test csv file.
4. LONG SHORT-TERM MEMORY (LSTM)
4.1 Python
Python is a preferred programming language because of itsextensivecapabilities,applicability,andsimplicity.Due to its independent platform and widespread use in the programming community, the Python programming languageisthemostsuitableformachinelearning.
Oneofthenumerousvarietiesofrecurrentneuralnetwork (RNN),longshort-termmemory(LSTM),isalsocapableof capturing input from earlier stages and using it to make predictions for the future. An artificial neural network, in general (ANN) comprises the following layers: the input layer,thehiddenlayer,andtheoutputlayer.
The number of nodes in the input layer of a NN with a single hidden layer always depends on the dimension of thedata,andthenodesoftheinputlayerarelinkedtothe hidden layer by connections known as "synapses." Every two-node relationship from (input to the hidden layer) contains a coefficient called weight that determines how signalsareprocessed.
Theresultsofthistransformationformtheoutputlayerof our NN, however, these values might not be the best output, in which case a back propagation process will be used to target the ideal value of error. The back propagation process connects the output layer to the hidden layer, sending a signal that conforms to the best weightwiththeoutputvalues.
The least expensive error across the chosen number of epochs. Repeating this process will help us improve our forecastsandreducepredictionerror.
Thisprocesswillbefinishedwiththemodelbeingtrained. Recurrent Neural Networks (RNN) are a family of neural networks that predict future values based on previous sequencesofobservations.
TheLSTMis a particularclassofRNNsdue to itscapacity for memorizing data sequences. Each LSTM node must consist of a collection of cells that are responsible for storing previous data streams. The upper line in each cell serves as a transport line connecting the models and transfersdata fromthepast tothepresent. Thesigmoidal neural network layer that makes up the gates ultimately drives the cell to an ideal value by discarding or allowing datatopass.
4.1 Recurrent Neural Network (RNN)
Python The deep learning algorithm family includes recurrent neural networks. Because of the feedback links initsarchitecture,itisarecurrentnetwork.Becauseitcan analyze the complete sequence of data, it offers an advantage over conventional neural networks. The cell, input gate, output gate, and forget gate are all parts of its architecture.
We will scale down the stock values to values between 0 and 1 in order to lower the processing cost of the data in thetable.Bydecreasingallthedatainbignumbersinthis manner,memoryuseisdecreased.Additionally,byscaling down,wecangetgreateraccuracybecausethedataisnot widely dispersed. The MinMaxScaler class of the sci-kitlearnlibrarydoesthis.
Text Blob returns a sentence's polarity and subjectivity. Thepolarityscaleis[-1,1],where-1representsanegative emotionand1representsagoodemotion.Negativewords turn the polarity around. Semantic labels in TextBlob facilitatedetailedanalysis.Emoticons, exclamationpoints, emoticons, etc. are a few examples. The range of subjectivity is [0, 1]. Subjectivity measures how much informationandsubjectiveopinionarepresentinthetext. Thecontentcontainsopinionratherthaninformationdue to the text's heightened subjectivity. One other setting for TextBlob is intensity. TextBlob uses the "intensity" to determinesubjectivity. Whethera wordmodifiesthenext word depends on its intensity. Adverbs are used as modifiersinEnglish,suchas"extremelygood."
4.1 Regression
TheRegression-basedmodelreliedonalinearfunctionto forecast continuous values based on previously provided values [2]. The general procedure begins with the extraction of input data from stock movement, then the machine learning algorithm is performed to develop the regression-based model that delivers the final forecast of the stock prices, according to Siew and Nordin. The formula is often V = a + bK + error, where V is the projected continuous value, K is the previously supplied value, and a and b are coefficients. Regression takes into account all five variables, and the R-square test was used to determine the confidence level. The goal and benefit of thistechniqueistoreducetheerrorintheformula.
The resultant graph of batch size 512 and 90 epochs was formed after performing the linear regression algorithm, and the confidence score was 0.86625. The chance of a correct forecast is 86.625% according to the confidence
score. This outcome demonstrates the technique's potentialandhowitenhancedpredictionaccuracy
5. RESULT
The portfolios created using the model results are described in this section. Each trading day, 400 equities were available from which to choose and assemble a portfolio. The portfolios in this study were created by ranking each day's predictions for all stocks and then choosing the equities with the highest predictions. As a result, since the targets of LSTM networks and CART models are return rates along with their respective probabilities, the stocks are simply ranked by the professionalvalues.
Fig -3:OutputoftheStockMarketPrediction
6. CONCLUSIONS
TheCARTmethodologyismodelledafterthestructureofa tree and explains the prediction of stock prices, whereas the ARIMA model is one of many conventional methods that rely on an underlying stochastic model to extract information from the data. CART model underperformed theARIMAmodel.
The LSTM model assumes that the underlying process is unknownandisanalgorithmicmethodforanalyzingtime series data. Compared to the CART model in this investigation, our LSTM model was able to forecast stock pricefluctuationswithahigherdegreeofaccuracy.Thisis due to the LSTM model's inherent ability to analyze sequential data, extract useful information, and discard irrelevantinformationusingadeeplearningtechnique.
Fig -5:PredictedStocksAfterApplyingLSTM
A portfolio of equities would not be diversified if too few stocks were chosen. The benefit of selecting the stocks with the highest prediction would disappear if too many stocks were chosen. The overall investment and transactionfeeswouldbeexcessiveifonechosetoomany equitiestotradeeachday.Asaresult,anoptimalk,orthe number of stocks to choose for each stock portfolio, must beestablished.
The k with the highest portfolio returns on investment waschosenastheoptimalk.
Asaresult,theportfoliochosenbyourLSTMnetworkhad a somewhat more consistent return than the ARIMA model. This is because, in contrast to conventional linear statistical models, a deep learning model is better able to extractthenonlinearrelationshipinthedata.
Practically, we recommend using the LSTM model for stockpriceforecastsduetothedeeplearningapproachof the LSTM model for investors interested in selecting between statistical approaches, machine learning techniques, or deep learning techniques to determine whichstockstobuy.
REFERENCES
[1] Huang, W., Nakamori, Y., Wang, S. Y (2005). Forecasting stock market movement direction with support vector machine. Computers & Operations Research.Vol.32(10),Pages,2513-2522.
[2] Kim, K-J, Han, I (2000). Genetic algorithms approach to feature discretization in artificial neural networks forthepredictionofstockpriceindex.ExpertSystems withApplications,Vol.19.
[3] Lahmiri, S (2012). Linear and nonlinear dynamic systems in financial time series prediction. ManagementScienceLetters2,pages,2551–2556
[4] Levin, N, Zahavi, J (2001). Predictive modelling using segmentation. Journal of Interactive Marketing, Vol. 15.
[5] Wang, S., and Y. Luo. 2012. “Signal Processing: The Rise of the Machines.” Deutsche Bank Quantitative Strategy(5June).
[6] Takeuchi, L., & Lee, Y. Y. A. (2013). Applying deep learning to enhance momentum trading strategies in stocks.InTechnicalReportStanfordUniversity.
[7] Siami-Namini, S., & Namin, A. S. (2018). Forecasting economics and financial time series: Arima vs. lstm. arXivpreprintarXiv:1803.06386.
[8] Patterson J., 2017. Deep Learning: A Practitioner’s Approach,O’ReillyMedia
[9] Olah,C.(2015).Understandinglstmnetworks–Colah’s blog.Colah.GitHub.io.