As any one of us could guess, the market is unstable and, more than often, unpredictable. For several decades researchers have toyed with time-series data to predict future values – of which the most challenging and potentially lucrative application is predicting the values of stocks for a given company. However, as expected, market change depends on many parameters of which only a bunch can be quantified, such as historical stock data, the volume of trade, current prices. Of course, fundamental factors such as a company’s intrinsic value, assets, quarterly performance, recent investments, and strategies all affect the traders’ trust in the company and thus the price of its stock. Only a few of the latter can be incorporated effectively into a mathematical model. This makes stock price prediction using machine learning challenging and unreliable to a certain extent. Moreover, it is nearly impossible to anticipate a piece of news that will shatter or boost the stock market in the coming weeks – a pandemic or a war.
Time Series Analysis Project in R on Stock Market forecasting
Downloadable solution code | Explanatory videos | Tech Support
So, rather than focusing on matching the actual values with high precision, analysts merely focus on making short-term predictions to get a probabilistic estimate of what the market “could” look like soon. With enough historical data and useful features, mathematical and machine learning models might predict short-term fluctuations in the market for an average, uneventful market day.
Table of Contents
- Stock Price Prediction using Machine Learning
- Stock Price as a Time Series Data
- Stock Price Prediction using Moving Average Time Series
- Understanding Long Short Term Memory Network for Stock Price Prediction
- Evaluating Prediction Performance for Stock Price Prediction
- Downloading the Stock Prices Dataset
- Loading the Stock Prices Dataset
- Importing the Libraries
- Data Preprocessing
- Train and Test Sets for Stock Price Prediction
- Building the LSTM model
- Performance Evaluation on Test Set
- LSTM vs. Simple Moving Average vs. Exponential Moving Average for Stock Price Prediction
- FAQs
- Which machine learning algorithm is best for stock price prediction?
- How can machine learning techniques predict the stock market?
Stock Price Prediction using Machine Learning
Stock Price Prediction using machine learning is the process of predicting the future value of a stock traded on a stock exchange for reaping profits. With multiple factors involved in predicting stock prices, it is challenging to predict stock prices with high accuracy, and this is where machine learning plays a vital role.
Stock Price as a Time Series Data
Treating stock data as time-series, one can use past stock prices (and other parameters) to predict the stock prices for the next day or week. Machine learning models such as Recurrent Neural Networks (RNNs) or LSTMs are popular models applied to predicting time series data such as weather forecasting, election results, house prices, and, of course, stock prices. The idea is to weigh out the importance of recent and older data and determine which parameters affect the “current” or “next” day prices the most. The machine learning model assigns weights to each market feature and determines how much history the model should look at to predict future stock prices.
Evolution of Machine Learning Applications in Finance : From Theory to Practice
Stock Price Prediction using Moving Average Time Series
To begin with, we can use moving averages (or MA) to understand how the amount of history (or the number of past data points) considered affects the model's performance. A simple moving average computes the mean of the past N data points and takes this value as the predicted N+1 value.
So,
Where P1 to Pn are n immediate data points that occur before the present, so to predict the present data point, we take the SMA of the size n (meaning that we see up to n data points in the past). The SMA is our predicted value. The precision of the model will vary significantly with the choice of n. Higher n would mean that we are willing to go deeper into the past to compute the present value. For example, n=2 means that we take the average of the stock price of the past two days, while n=50 would consider 50 days' worth of stock prices. Obviously, 50 days’ worth of data will have more information about the trends of the stock and would lead to better predictions. However, based on context, a large n can also destabilize the model as the more granular fluctuations are smoothened off – looking at prices from the past 300 days would be sub-optimal.
Another moving average is the exponential moving average (EMA), giving more weight to the more recent samples. With this, we can look at more data points in the past and still not diminish the more recent trends in fluctuations.
Where Pt is the price at time t and k is the weight given to that data point. EMA(t-1) represents the value computed from the past t-1 points. Clearly, this would perform better than a simple MA. The weight k is computed as k = 2/(N+1).
While implementing these methods, we will see how EMA performs better than SMA, proving that assigning higher weights to more recent data points will yield more fruitful results. But for now, let us assume that that is the case with stock prices as time series data.
So considering more past data and giving more importance to newer samples, EMA performs better than SMA. However, given the static nature of its parameters, EMA might not perform well for all cases. In EMA, we have fixed the value of k (the weight/significance of past data), and it is linked with the window size N (how much past we wish to consider).
It can be difficult to set these parameters manually and impossible to optimize. Thus, we can use more complex models that can compute the significance of each past data point and optimize our predictions. This can be achieved with weight updation while training a machine learning model. And thinking of using past data to compute the future, the most immediate model that comes to mind is the LSTM model!
>Get Closer To Your Dream of Becoming a Data Scientist with 150+ Solved End-to-End ML Projects
Understanding Long Short Term Memory Network for Stock Price Prediction
LSTM is a Recurrent Neural Network that works on data sequences, learning to retain only relevant information from a time window. New information the network learns is added to a “memory” that gets updated with each timestep based on how significant the new sample seems to the model. Over the years, LSTM has revolutionized speech and handwriting recognition, language understanding, forecasting, and several other applications that have become the new normal today.
A standard LSTM cell comprises of three gates: the input, output, and forget gate. These gates learn their weights and determine how much of the current data sample should be remembered and how much of the past learned content should be forgotten. This simple structure is an improvement over the previous and similar RNN model.
As seen in the equations below, i, f, and o represent the three gates: input, forget, and output. C is the cell state that preserves the learned data, which is given as output h. All of this is computed for each timestamp t, considering the learned data from timestamp (t-1).
The forget gate decides what information and how much of it can be erased from the current cell state, while the input gate decides what will be added to the current cell state. The output gate, used in the final equation, controls the magnitude of output computed by the first two gates.
So, as opposed to standard feed-forward neural nets, LSTMs have the potential to remember or erase portions of the past data windows actively. Its feature of reading and training on windows (or timesteps) of data makes its training unique. Let’s build the model in Python.
Upskill yourself for your dream job with industry-level big data projects with source code
Evaluating Prediction Performance for Stock Price Prediction
Before putting the algorithms into practice, let’s clarify the metric to measure the performance of our models. Stock price prediction being a fundamental regression problem, we can use RMSE (Root Mean Squared Error) or MAPE (Mean Absolute Percentage Error) to measure how close or far off our price predictions are from the real world.
Looking closely at the formula of RMSE, we can see how we will be able to consider the difference (or error) between the actual (At) and predicted (Ft) price values for all N timestamps and get an absolute measure of error.
On the other hand, MAPE looks at the error concerning the true value – it will measure relatively how far off the predicted values are from the truth instead of considering the actual difference. This is a good measure to keep the error ranges in check if we deal with too large or small values. For instance, RMSE for values in the range of 10e6 might blow out of proportion, whereas MAPE will keep error in a fixed range.
Stock Price Prediction Project using Machine Learning in Python with Source Code
First, we will implement a simple LSTM network using Keras in Python. Let’s take a look at the dataset. We can work on actual stock data from major public companies such as Facebook, Microsoft, or Apple by simply downloading the data from finance.yahoo.com.
New Projects
Downloading the Stock Prices Dataset
Go to finance.yahoo.com/ and search the company you wish to predict the stock of. For our example, we will look at the Netflix (NFLX) stock over 3 years.
Going to finance.yahoo.com/quote/NFLX/history?p=NFLX in the “Historical Data” section, we see the stock data listed each day. We can filter out the time for which we wish to analyse and download the CSV file using the download button on the right.
The download CSV file will contain the data for Open, High, Low, Close, Adj Close, Volume for each date, as shown in the image above.
Loading the Stock Prices Dataset
Load the CSV file as a DataFrame using Pandas. Since the data is indexed by date (each row represents data from a different date), we can also index our DataFrame by the date column. We have taken the data from March 2019 to March 2022. This will also challenge our model to work with the unpredictable changes caused by the COVID-19 pandemic.
Plotting the High and Low points of Netflix stock over 3 years, we see the below graph.
As noticeable, around March 2020, we see a sudden drop in the price, after which it reports steady growth until recently.
It will be challenging for a machine learning model to correctly estimate the rapid changes that we can see in March 2020 and February 2022. We will focus on evaluating the model performance in predicting the more recent values after training it on the past data.
Similarly, plotting the Open and Close value of the stock for each day gives equivalent observations.
The code for plotting these graphs is as shown below. We use matplotlib to plot the DataFrame columns directly against the Date index column. To make things flexible while plotting against dates, lines 6-8 convert our date strings into datetime format and plot them cleanly and legibly. The interval parameter in line 7 defines the interval in days between each tick on the date axis.
We will use the Open, High, and Low columns to predict the Closing value of the Netflix stock for the next day.
Importing the Libraries
We will be building our LSTM models using Tensorflow Keras and preprocessing our data using scikit-learn. These imports are used in different steps of the entire process, but it is good to club these statements together. Whenever we wish to import something new, just add the statement arbitrarily to the below group.
Data Preprocessing
As with any other machine learning model, it is always good to normalize or rescale the data within a fixed range when dealing with real data. This will avoid features with larger numeric values to unjustly interfere and bias the model and help achieve rapid convergence.
First, we define the features and the target as discussed above.
Next, we use a StandardScaler to rescale our values between -1 and 1.
Scikit-learn also provides a popular MinMaxScaler preprocessing module. However, considering the context, stock prices might max out or minimise on different days, and using those values to influence others might not be great. The change in values from using either of these methods would not be much, so we stick to StandardScaler.
>Get FREE Access to Machine Learning Example Codes for Data Cleaning, Data Munging, and Data Visualization
We have 757 data samples in the dataset.
So, the next step would be to split it into training and testing sets. As explained above, the training of an LSTM model requires a window or a timestep of data in each training step. For instance, the LSTM will take 10 data samples to predict the 10th one by weighing the first nine input samples in one step. So, we need a different approach than the train_test_split provided by scikit-learn.
Let’s define a splitting function called lstm_split() which will make windows of size “n_steps” starting from the first sample of data and ending at n_steps’th sample (if n_steps=10, then the 10th sample) from the end. We understand the latter part because, for each time step, LSTM will take n_steps-1 samples for training and predict the last sample. Loss calculation is done based on the error in this prediction. So if n_steps=10, you cannot use the last 9 samples to predict anything because the “10th” data point for the current step does not exist in the dataset.
The function below takes the entire data and creates windows of size n_steps starting from the beginning. The target y will contain the target value corresponding to the n_steps’th index. So if n_steps is 10, the first element in X will have features from 10 data samples, and y will contain the target of the 10th data sample.
Train and Test Sets for Stock Price Prediction
We split our data into training and testing sets. Shuffling is not permitted in time-series datasets. In the beginning, we take two steps worth of past data to predict the current value. Thus, the model will look at yesterday’s and today’s values to predict today’s closing price.
Note above that the size of X1 is n_steps less than that of the original dataset. As we explained above, you cannot use the last two samples of the original set during training or prediction as we do not have their corresponding ground truth values.
Building the LSTM model
We will use the Sequential and LSTM modules provided by Tensorflow Keras to build a simple, single-unit LSTM model.
Now we can fit this simple model to the training data.
Given the simplicity of the model and the data, we note that the loss reduction stagnates after only 20 epochs. You can observe this by plotting the training loss against the number of epochs, and LSTM does not learn much after 10-20 epochs.
Explore Categories
Data Science Projects in PythonDeep Learning ProjectsNeural Network ProjectsTensorflow ProjectsH2O R ProjectsIoT ProjectsKeras Deep Learning ProjectsNLP ProjectsPytorchData Science Projects in Banking and FinanceData Science Projects in Retail & EcommerceData Science Projects in Entertainment & MediaData Science Projects in Telecommunications
Performance Evaluation on Test Set
Nevertheless, we can check the performance of our model on a test set as below.
To evaluate, first, we plot the curve for true values and overlap it with that for the predicted values.
Thus, we can see that LSTM can emulate the trends of the stock prices to a certain extent. Based on the recent dip in prices, it has also fit the dropping curve well.
As we decided earlier, we can also check the RMSE and MAPE values to evaluate the performance. We will use these values for future comparison.
Let’s try to get better results with the same dataset but a deeper LSTM model.
We added another LSTM layer and increased the number of LSTM units per layer to 50.
While the loss still converges early, the curve is better fitted to the true value.
Moreover, the RMSE and MAPE values are better too.
Thus we observe substantial improvement by adding another LSTM layer to the model. However, further adding even more layers would not be fruitful as the model might overfit or stagnate during training.
Explore MoreData Science and Machine Learning Projects for Practice. Fast-Track Your Career Transition with ProjectPro
Now we will try fitting the same model but with increased time steps. We’ll try for n_steps=10.
We change the value in the block below and rerun the entire process with the same model as before.
The model we used above:
Surprisingly, we get similar performance as before!
We note that LSTM was able to achieve decent RMSE and MAPE values despite the data complexity. Further, we note that creating even deeper networks did not help improve the test performance.
Before we conclude, as promised earlier, let’s look at how better or worse LSTMs perform compared with statistical techniques such as SMA and EMA.
LSTM vs. Simple Moving Average vs. Exponential Moving Average for Stock Price Prediction
Simple Moving Average
Based on this, we find the results as below:
LSTMs perform better under similar conditions.
Similarly, let us also check how the exponential moving average performs. As we noted initially, it is supposed to perform better than SMA.
Get confident to build end-to-end projects.
Access to a curated library of 250+ end-to-end industry projects with solution code, videos and tech support.
Exponential Moving Average
Rather than implementing EMA from scratch, we can use the module provided by statsmodels in Python. For now, we can use the SimpleExpSmoothing module of the TSA API from statsmodels. While fitting this model, we can tune the smoothing_level parameter to get optimal performance – we note that a relatively lower value yields better results.
So in the case of predicting stock price data, SMA and EMA have similar performance and are far behind when compared to LSTMs. We can improve our LSTM model by finetuning the hyperparameters such as the number of cells, batch size, or the loss function. However, using data beyond 2019 (up to 5-10 years' worth of data) would greatly help the model.
Most Watched Projects
In the article, we looked at how we can address the problem of predicting stock market prices by considering stock market data as a time series. Further, we looked into the theory of three existing methods used frequently for time series forecasting: simple moving average (SMA), exponential moving average (EMA), and LSTMs. We also used real-life stock data to predict prices of the Netflix stock using these three methods and conducted a comparative performance analysis using RMSE and MAPE error metrics.
However, a consensus of experts from the financial domain and the AI field says that ML techniques perform poorly in the real world to predict the market. Even if hundreds of variables and real-world market drivers are quantized and incorporated in the data and optimized using the best machine learning methods available, the model would still fall short of making valuable predictions when they matter. Experts note that AI models could not follow the trends disrupted by the COVID-19 pandemic – not during or even towards the end of it. Similarly, in general cases also, AI falls short of substituting human intelligence and intuition about the market trends: Effectiveness of Artificial Intelligence in Stock Market Prediction based on Machine Learning. Nevertheless, these shortcomings are only learning curves for developing robust stock price prediction models and analyzing the capabilities of existing models in further detail.
FAQs
Which machine learning algorithm is best for stock price prediction?
Based on experiments conducted in this article, LSTMs seem to be the best initial approach in solving the stock price prediction problem. Other methods can combine features extracted from LSTM or Bi-LSTM models and fed into a classical ANN regressor. This approach might help extract information previously missed by a simple LSTM regression model. More recent research uses graph neural networks and multi-headed attention mechanisms, while others use reinforcement learning. LSTM-based approaches are also being actively used and researched lately despite such complex mechanisms being proposed regularly—Head over to Stock Market Prediction | Papers With Code to see the latest work in this domain.
How can machine learning techniques predict the stock market?
One can show machine learning models vast amounts of historical data of a company’s stock (several decades' worth of data) and use the model to extract key trends and essential features that define the company’s stock performance. If such trends, parameters, and features are extracted effectively, the model can predict future stock performance.