Stock Price Predictor Using FacebookProphet and Neural Network

Ojas Pandey
Analytics Vidhya
Published in
6 min readMay 27, 2021

Accurately predicting stock movement can be a mammoth task. The market is highly stochastic and we (try to) make accurate predictions with chaotic data.

In the real world stock prices depend on a number of factors both in and out of the financial world like ,prices on previous days, political factors, journalism, factory related issues like accidents or strikes , natural/environmental factors etc. For example, in the recent past we’ve seen what effects Elon Musk’s tweets had on stock price of GameStop and Bitcoin. Unfortunately, we don’t have such a detailed and informative dataset available for analysis so these factors are not accounted for by our model. Having said that, we can easily get dataset of previous days stock prices online easily. And that is what we will be using in our algorithm.

Before moving forward, I think it’s useful to give a brief explanation of the techniques we’ll be using ahead, Time Series Forecasting and FbProphet .

Time Series Forecasting

It is a method of using past data or experience to estimate the future. It makes forecasts based solely on the historical patterns.

Time series forecasting simple explanation

In simple terms, in time series analysis say we want to predict ‘y’ value on a certain date t. Then y(t) is predicted using y(t-1) i.e value of ‘y’ on the previous day, y(t-1) (‘y’ value day before that) and so on till a given time frame n.

FaceBook Prophet

Fb Prophet, on the other hand, is an open source library for univariate time series forecasting, developed by Facebook. It makes the job of time series forecasting super easy and fully automatic. For more details, click here.

Back to our code

Our dataset has 6 columns namely : Date, Open, Close, High, Low, Adj Close.

In our case, data is from 2016–03–09 to 2021–03–08 (i.e 5 years). It has 1234 rows or instance and 7 columns i.e 7 features out of which one is our y (output) variable.

Then we do some basic data analysis and exploration. Also we clean our data incase it has unwanted values (NaN).

Here we simply remove all the NaN values which might hinder with the proper functioning of our code.

Other methods have also been preformed which can be seen in the code, whose link is provided at the bottom.

Prophet Code

Out of these, for FbProphet, we need only the Date column and the Close column. The reason lies in the working of prophet itself. Prophet requires only two columns for its functioning namely, ‘ds’ and ‘y’ (names cannot be changed!). ‘ds’ meaning Dates and ‘y’ meaning the variable we want to predict.

At its core, the Prophet procedure is an additive regression model with four main components:

‣ A piecewise linear or logistic growth curve trend. Prophet automatically detects changes in trends by selecting changepoints from the data.

‣ A yearly seasonal component modeled using Fourier series.

‣ A weekly seasonal component using dummy variables.

‣ A user-provided list of important holidays

After creating and fitting the model, we’ll see the result as follows

Even though the user inputs only the predicted data, prophet automatically gives us, along with the predicted values, the actual values as well as the error margins.

Here, the black dotted line is the actual data points, or the actual stock price that we know. The deep blue line is the predicted stock prices that our model has predicted. The shaded light blue region gives us the margin for error.

Another brilliance of prophet is that it also gives the weekly, monthly, yearly trends. It’s as follows :

For example, according to the model, buying reliance stocks on Tuesday might not be the best idea since it’s prices tend to be highest on Tuesdays. Also, from monthly trend, we observe that buying reliance stocks in April might be beneficial as prices tend to be lowest that month.

Performance

Here the dots show the absolute percent error for each prediction in cv while the blue line shows the RMSE/MAPE (for either graph), where the mean is taken over the rolling window of 10% (default) of the dots.

We see that RMSE is around 290 for prediction one month into the future and 450 an year into the future with a sharp increase to 410–420 around 220 days into the future.

Similarly MAPE is around 18% are typical for predictions one month into the future and around 20% for predictions that are a year out.

Artificial Neural Network Code

Basic idea of working of neural network

Neural networks are a series of algorithms that mimic the operations of a human brain to recognize relationships between vast amounts of data. They can adapt to changing input, so the network generates the best possible result without needing to redesign the output criteria. Neural networks are highly effective in detecting patterns in input data and then in producing an output free of noise. Its output layer collects the predictions made in the hidden layer and produces the final result.

For this algorithm we will be using Keras, which is a deep learning library, for its simplicity and ease of use in creating and deploying models.

The data and data exploration process remaining same, prior to the model fitting process we have to standardize our data. Why? This is because it is a common experience in Machine Learning when we encounter data that has features whose values vary greatly in magnitudes. And so if we choose not to standardize or normalize the data then their influence on our model will be distributed in the same proportion as their magnitude. This is not desirable as it leads to faulty models.

One of the most important considerations when training a neural network is choosing the number of neurons to include in the input and hidden layers. Since the output layer gives us the result, it contains only 1 neuron by default. Based on research, I came to know that a good number of neurons for the input layer is equal to the number of features taken into consideration + 1, while for number of neurons in hidden layer should be equal to the following,

Number of neurons for input layer :

Number of hidden layers :

The result we get after making predictions from the fitted model is as follows:

Conclusions

Facebook Prophet and Neural networks area vast subject. In this project, I’ve used only the introductory/ basic concepts of NN and FbProphet. There are much more advanced techniques and models that can be deployed. Neural Networks are very calculation intensive, requiring highly efficient computing machines and large datasets. Currently, there is a lot of exciting research going on around neural networks.

Link for the codes is here .

Thank You.

--

--

Ojas Pandey
Analytics Vidhya

Student, Big Data and Machine Learning enthusiast