Stock Price Prediction of Tesla & Apple using LSTM

— The stock market has always piqued academics' interest in predicting. Since they were originally created, machine learning algorithms have been increasingly popular for predicting changes in the stock market. The intricacy and unstable nature of the stock market make it challenging to predict stock values. In recent years, stock value forecasting has used both sentiment analysis and machine learning techniques. In this work, we present a study to anticipate the stock prices of Tesla and Apple using sentiment analysis and long short-term memory (LSTM). Using historical stock prices and sentiment data from Twitter, our LSTM model will be trained and put to the test. To evaluate the proposed model, metrics like normal error, mean square error (MSE), and mean absolute error (MAE) are taken into account. The obtained results showed very promising results and can be incorporated in stock price prediction applications.

INTRODUCTION Stock market forecasting is a challenging venture because of how complicated stock prices are and how many different factors may influence them. In the past, stock value predictions have been made using both fundamental analysis and technical analysis. However, there are several limitations to these methods, and there are doubts about their accuracy. Academics have explored the potential of using machine learning algorithms to estimate stock prices as their use has increased. Sentiment analysis is a subject that has gained popularity recently, particularly when it comes to analyzing social media data to predict stock prices. Social media platforms like Twitter, Facebook, and LinkedIn are being used to study the emotions of traders and investors. Sentiment analysis can provide insight into how the stock market may be impacted by the emotions of investors [1][2].
Because of its ability to spot patterns and make predictions based on data, machine learning algorithms have been used more and more in recent years for stock market forecasting. These algorithms are able to sift through voluminous historical data, spot trends, and utilize those forecasts to estimate future stock values. The ANN is among the methods of machine learning that are frequently used to forecast stock prices. The operation of the human brain is mimicked by an ANN, a machine learning algorithm. Each node processes data and communicates with other nodes via links, which connect the nodes together. Because ANN can recognise patterns in the data that humans would overlook, it has been successfully utilized to predict market values. Hence, forecasting stock prices with the use of machine learning algorithms has been effective. There is no guarantee that the current trends will continue in the complex and dynamic environment of the stock market. Therefore, it is important to proceed with caution when making financial decisions that are based on stock market estimates produced by machine learning algorithms [3][4].

II.
LITERATURE STUDY For many years, there has been interest in the application of machine learning algorithms to forecast stock market direction. The prospect of employing machine learning algorithms to anticipate stock values has been investigated in several research. Several significant literary works are examined in this section. A deep learning system called LSTM was employed by the author in [5] to forecast stock values on the Korean stock market. According to the study, LSTM performs better than conventional statistical techniques in forecasting stock prices on the Korean stock market. The authors' findings imply that deep learning algorithms may be a practical tool for predicting stock value. Authors in [6] used Twitter data in a 2011 study to examine traders' and investors' feelings. The study showed that it is feasible to predict the movement of the Dow Jones Industrial Average index by using the emotional content of Twitter users. The researchers arrived at the conclusion that sentiment analysis can help stock price projections. Sentiment research is another topic of interest in stock market forecasting. Natural language processing methods are used in sentiment analysis to examine traders' and investors' attitudes. The potential use of sentiment analysis for stock price prediction has been examined in several research.
Moreover, authors in [7] used sentiment analysis in a different study to predict stock prices on the Shanghai Stock Exchange. The study found that sentiment analysis outperforms traditional statistical methods in predicting stock prices on the Shanghai Stock Exchange. The researchers arrived at the conclusion that sentiment analysis can help with stock price predictions in emerging nations. Mehtab et al.'s hybrid technique [8] for predicting changes in stock values combines deep learning, machine learning, and natural language processing. Limit order book data was utilized by Nousi et al.'s machine learning (ML) algorithms [9] to forecast future price movements. Backtesting this strategy over a variety of markets and lengthy time horizons is the major goal of Matsunaga et al.'s [10] rolling window study.
Furthermore, a hybrid stock price prediction model developed by Mehtab et al. [11] based on deep learning and machine learning techniques is offered. Mehtab et al. in [12] provide a hybrid approach to stock price prediction that uses both machine learning and deep learning methods. To determine if sentiment analysis may increase the precision of stock forecasts, the authors of [13] employ historical data and sentiment analysis scores. The authors of [14] provide a system for forecasting stock prices that is extremely reliable and accurate. They use statistics, machine learning, and deep learning models. However, it is challenging to achieve the utmost level of accuracy. Gite et al.'s [15] recommendation states that to accurately anticipate stock values, a deep learning method known as LSTM should be used with effective machine learning methods. According to Li et al. [16], ensemble deep learning algorithms may really predict future stock price patterns more accurately than other conventional approaches and can help investors make the best investment decisions.
In this research paper, we conduct a detailed evaluation of sentiment analysis and stock market forecasts using machine learning approaches. The study's objective is to investigate the ways in which sentiment analysis and machine learning techniques may be used to anticipate stock prices. To evaluate the effectiveness of several machine learning algorithms and sentiment analysis approaches, we employ a dataset of stock prices and social media data. The findings of this study can aid traders and investors in making wise judgements and deepening their knowledge of the stock market.

III. ARCHITECTURE & METHODOLOGY
The various aspects of the proposed system include data collection, data preprocessing, LSTM model training, prediction, and assessment. We gathered data on prior stock price movements as well as sentiment ratings derived from Twitter data in the data collection module. We cleaned, normalized, and produced a time-series structure that is appropriate for prediction with the aid of the Data Preprocessing module. We trained an LSTM model to forecast the future stock values of Tesla and Apple using the preprocessed data in the LSTM Model Training module. The Prediction module projected the future stock values of Tesla and Apple using the learned LSTM model and sentiment scores. Using MAE and MSE, the model's performance in the Evaluation module was assessed.

A. LSTM Model
To make future stock price predictions for Apple and Tesla, we employed an LSTM model. The LSTM model predicts the stock price for the following day using an input sequence of previous stock values. For Apple and Tesla, respectively, both LSTM models were trained. With 64 units per layer, a three-layer LSTM architecture is employed. When building the models, we trained them using both the Adam optimizer and the MSE loss function. Through the use of 64 batches, we trained the models across 100 epochs.

B. Data Collection & Preprocessing
Through Yahoo Finance, we are able to get historical stock price information for Apple and Tesla. We removed duplicates, extreme values, and blank values from the data before cleaning and normalizing it. Additionally, we used min-max scaling to normalize the stock price data and emotion ratings between 0 and 1, and then scaled the data using the stock price information. We integrated stock price data with mood data to produce a time-series style that is appropriate for forecasting.

C. Evaluation
Various parameters are used to evaluate the proposed model. The MAE calculates the average absolute difference (DAI) between the stock prices today and yesterday. The absolute differences between the actual and anticipated values are added for every single data point in the test dataset. The MAE statistic is a helpful tool for more clearly evaluating the accuracy of forecasts since it shows the average difference between anticipated and actual values. If a model's MAE is smaller, it will be better at predicting stock values.
Moreover, the average squared difference between the stock prices that actually happen and the stock prices that are projected is another calculation method used by MSE. It is calculated by averaging the squared differences between the actual and predicted values for each data point in the test dataset. When larger errors have a higher impact than smaller errors, MSE is a useful metric for evaluating the model's overall performance. A lower MSE also indicates a model with superior stock price forecasting capabilities. Both MAE and MSE are frequently used in the field of stock price forecasting and can provide details on the accuracy of the model's forecasts.

IV. RESULT & DISCUSSION
In this section the proposed model is analyzed in terms of MAE, MSE and comparative analysis of GT with predicted prices during training and testing phases. The outcomes will be useful in understanding whether the suggested approach is capable of precisely forecasting the stock values of both Apple and Tesla. Fig. 1(a-b) presents a comparative analysis of the stock price predicted by the model to the original price at the moment. It is observed that for most of the times the forecasted price is most of the cases is equal to the real price, which shows the robustness of the proposed M-CNN model. However, at the later stage a little deviation is observed, especially in the tesla stock. A similar analysis is presented in Fig. 2(a-b) for the training dataset and some inconsistency is observed in tesla stock, in contrast apple stock price had close approximation to the actual price.
(b) Fig. 1 Difference between GT and testing data predicted stock price (a) Apple (b) Tesla stock Further, Fig. 3(a-b) presents MAE Vs epoch plot for apple and tesla stocks. For both of the stocks, in the beginning very high loss is observed, but after a few iterations it starts to decline. The curve is comparatively smoother for tesla stock as compared to apple stock. Further, in Fig. 4(a-b) MSE Vs epoch plot of the proposed model is presented. Similar to MAE, it is observed that with increase in iterations the MSE decreases. After certain epochs the MSE reduces, which shows that after a few initial iterations the model becomes highly accurate. However, very high error is observed in the case of tesla at the beginning, but it reduces with few initial iterations. In this article, we put out a research employing LSTM to forecast the stock values of Tesla and Apple. For this purpose historical information on Tesla and Apple stock prices is gathered and used for training the LSTM model. The findings demonstrated that, when measured against conventional statistical approaches, our suggested strategy beat them in forecasting the stock values of Apple and Tesla. A close approximation is observed for both the training and testing dataset predicted prices with the GT. Moreover, after a few initial iterations the MAE and MSE curves seem to be constant, which shows high efficacy of the proposed model. Trades and investments may be made more profitably with the aid of our methodology. Future research may focus on enhancing stock price prediction using DL algorithms and sentiment analysis approaches. Traders and investors can use the proposed method as a tool to assist them in making educated investment selections.