MLearning.ai
Published in

MLearning.ai

Efficiency Analysis of Stock Price Prediction using Machine Learning

Machine Learning techniques have been used to analyze and predict the stock prices of a given company. These systems are quite common nowadays. But how reliable are these predictions? It is a known fact that stock prices fluctuate on many factors like Economic factors, Company trends, etc. In this article, we will evaluate the reliability of these predictions by training a model on a stock price dataset and then trying to predict when the price fluctuated due to some Economic conditions. This will help us analyze the reliability of such systems when the situation deviates from the normal situation.

Previous work

The Stock Price prediction system consists of a linear or non-linear model (Artificial Neural Network). The majority of the models use Opening price as the feature and Closing price as the target variable while some models include the date as a feature wall. The historical data of the stock prices are used to train and test the model.

Since Stock prices are Time series data, LSTMs are used majorly for a better prediction. Apart from LSTMs, ANN and Linear Models (for linear data if present) are also used.

The opening price of the stocks is the commonly used feature for the model and the closing price is the target variable. In few systems, dates are also used as a feature, although date as a feature is also rarely used.

Methodology

An ML model is used to train on the historical data of the stock prices of a chosen stock price dataset (Nifty50). This is then used to predict the stock prices during a recent unstable economic condition (COVID-19 lockdown) to evaluate its reliability.

The dataset used will be the Nifty50 Stock Price dataset and the data is taken from January 2018 — February 2020. Hence, we learn the data till the lockdown period and predict the prices during the lockdown period to evaluate how well it copes up with the unstable economic conditions compared to the trend in the previous years.

In this experiment, we will be evaluating the model prediction in both quantitative (absolute error) and qualitative way (From the perspective of an investor) to conclude its reliability.

Implementation

The dataset is collected for the dates from Jan 2018-Feb 2020. The reason being that the lockdown started in March 2020 from where the economic situations started diverting from normalcy.

Observing the trend between the opening and closing prices of the stock, the below graph is obtained.

The trend between opening and closing price shows its a linear fit

For such scenarios, linear models will work fine. The below code builds a linear model and trains it. Once the training and testing are done, it performs a prediction on an outlier value and shows the result in a graph.

Results and Conclusions

The trained model performs well with a train loss of 0.00005 and validation loss of 0.0001. From the loss graph, it is clear that the train and validation curves have merged completely which shows the model has attained a good fit. But when a prediction is done for an outlier situation it is clear that the model performs poorly. This shows that stock prices depend on many other factors and not only on its opening price. The plummet in opening price was caused by the COVID-19 lockdown situation while the unusual hike in closing price was because of the big traders buying the stock at a lower price because of the low price and because they saw the potential for growth after the COVID-19 impact.

The green dot is the actual closing price and the red dot is the predicted closing price

Performing both qualitative and quantitative analysis, we can confirm that the model performs poorly. The 1000 unit difference in prediction is a huge gap for a predicted stock price.

Thus, it can be concluded that stock prediction requires you to consider both industry and economic factors as well as the opening stock price. Machine Learning projects out certain restrictions when it comes to predicting volatile values like stock prices. Hence, Machine Learning models can be considered partially efficient for stock price prediction.

Thank you for reading!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vishnu U

Vishnu U

I am a Computer Science Student undergraduate from Dayananda Sagar University, Bangalore.