In this post I show you how to predict stock prices using a forecasting model publicly available from Facebook Data Science team: The Prophet

1. Introduction
1.1. Time-series & forecasting models
Traditionally most machine learning (ML) models use as input features some observations (samples / examples) but there is no time dimension in the data.
Time-series forecasting models are the models that are capable to predict future values based on previously observed values. Time-series forecasting is widely used for non-stationary data. Non-stationary data are called the data whose statistical properties e.g. the mean and standard deviation are not constant over time but instead, these metrics vary over time.
These non-stationary input data (used as input to these models) are usually called time-series. Some examples of time-series include the temperature values over time, stock price over time, price of a house over time etc. So, the input is a signal (time-series) that is defined by observations taken sequentially in time.
A time series is a sequence of observations taken sequentially in time.

Observation: Time-series data is recorded on a discrete time scale.
Disclaimer (before we move on): There have been attempts to predict stock prices using time series analysis algorithms, though they still cannot be used to place bets in the real market. This is just a tutorial article that does not intent in any way to "direct" people into buying stocks.
Note from Towards Data Science‘s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.
- NEW: After a great deal of hard work and staying behind the scenes for quite a while, we’re excited to now offer our expertise through a collaborative platform, the "Data Science Hub" on Patreon (https://www.patreon.com/TheDataScienceHub). This hub is our way of providing you with bespoke consulting services and comprehensive responses to all your inquiries, ranging from Machine Learning to strategic data analytics planning.
1.2 The forecasting model: Facebook’s Prophet
The most commonly used models for forecasting predictions are the autoregressive models. Briefly, the autoregressive model specifies that the output variable depends linearly on its own previous values and on a stochastic term (an imperfectly predictable term).
Recently, in an attempt to develop a model that could capture seasonality in time-series data, Facebook developed the famous Prophet model that is publicly available for everyone. In this article, we will use this state-of-the-art model: the Prophet model. Prophet is able to capture daily, weekly and yearly seasonality along with holiday effects, by implementing additive regression models.
The mathematical equation behind the Prophet model is defined as:
y(t) = g(t) + s(t) + h(t) + e(t)
- with, g(t) representing the trend. Prophet uses a piecewise linear model for trend forecasting.
- s(t) represents periodic changes (weekly, monthly, yearly).
- h(t) represents the effects of holidays (recall: Holidays impact businesses).
- e(t) is the error term.
The Prophet model fitting procedure is usually very fast (even for thousands of observations) and it does not require any data pre-processing. It deals also with missing data and outliers.
In this article, we are going to use the Prophet mode to predict Google’s stock price in the future.
Let’s get started !
– My mailing list in just 5 seconds: https://seralouk.medium.com/subscribe
– Become a member and support me:https://seralouk.medium.com/membership
2. Getting the stock price history data
Thanks to Yahoo finance we can get the data for free. Use the following link to get the stock price history of Google: https://finance.yahoo.com/quote/GOOG/history?period1=1433548800&period2=1591833600&interval=1d&filter=history&frequency=1d
You should see the following:

Click on the Download and save the .csv file locally on your computer.
The data are from 2015 till now (2020) !
3. Python working example
Now that we have the data, let’s inspect the data, build the model and predict the stock price !
3.1. Load & inspect the data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# Load the dataset using pandas
data = pd.read_csv("/Users/loukas/Downloads/GOOG.csv")
data.head(5)
The above code should print the following:

Now, let’s print some statistics such as the mean, median, min, max and standard deviation values for the above features (columns).
data.describe()

Over the past 5 years (2015–2020) the mean stock price at Close is 983.45 ! Really high!
3.2. Build the predictive model
Let’s move on with the modeling now. We will only use the dates and the Close price as features for our model.
# Select only the important features i.e. the date and price
data = data[["Date","Close"]] # select Date and Price
# Rename the features: These names are NEEDED for the model fitting
data = data.rename(columns = {"Date":"ds","Close":"y"}) #renaming the columns of the dataset
data.head(5)
The last Python command should return the first 5 lines of our dataset. You should see something like this:

Here, ds
is the date and y
is the Google Stock price.
In this tutorial, we will not split the data into training and test sets but instead we will use all the data to fit the model and then ask the model to predict future values i.e. the stock price in 2021.
Usually people split the data into training and testing because they do not want to train the model on the test set as well. If we keep a test set hidden, then the model will forecast values on unseen data. In that case, we would be also able to measure the error of the model.
Next, we import the Prophet class from the fbprophet module and then create an object of the Prophet class.
Side-note: The model’s github page is this: https://github.com/facebook/prophet
To install the module type on your console:
pip install fbprophet
.
from fbprophet import Prophet
m = Prophet(daily_seasonality = True) # the Prophet class (model)
m.fit(data) # fit the model using all data
You should see this after the fitting:
Optimization terminated normally: Convergence detected: relative gradient magnitude is below tolerance
3.3. Plot the predictions
Now, for the last step, we will ask the model to predict future values and then visualize the predictions.
future = m.make_future_dataframe(periods=365) #we need to specify the number of days in future
prediction = m.predict(future)
m.plot(prediction)
plt.title("Prediction of the Google Stock Price using the Prophet")
plt.xlabel("Date")
plt.ylabel("Close Stock Price")
plt.show()

The model used all the data for the training (black dots) and predicted the future stock price from June 2020 till June 2021 ! Blue shadow is the confidence interval.
Conclusion: It seems that the Google Stock price will be around 1600 in June 2021 based on the model’s prediction.
Side note: In this case, we cannot measure the error of the model. If someone wanted to do that, then they should split the data into training and test sets, fit the model using the training set only, predict the prices of the test set and then measure the error using the ground truth price values of the test set.
Observation: Huge drop in March 2020 due to the COVID-19 lockdown.
BONUS
3.4. Plot the trend, weekly, seasonally, yearly and daily components
If you want to see the forecast components i.e. trend, weekly, seasonally, yearly and daily components, then you can do this using the following command.
m.plot_components(prediction)
plt.show()

Results
Based on the estimated trends, we can see that usually the stock price is maximum in early January (see 3rd subplot) and mostly on Wednesdays (see 2nd subplot). Finally, the 1st subplot shows an increase of the stock price in the near future (between June 2020 and June 2021).
That’s all folks ! Hope you liked this article!
- We offer our expertise through a collaborative platform, the "Data Science Hub" on Patreon (https://www.patreon.com/TheDataScienceHub). This hub is our way of providing you with bespoke consulting services and comprehensive responses to all your inquiries, ranging from Machine Learning to strategic data analytics planning.
Check also my recent article using a LSTM model:
LSTM Time-Series Forecasting: Predicting Stock Prices Using An LSTM Model
Check also my recent article using an ARIMA model:
Time-Series Forecasting: Predicting Stock Prices Using An ARIMA Model
References
[1] https://facebook.github.io/prophet/docs/quick_start.html#python-api
[2] https://en.wikipedia.org/wiki/Additive_model
Stay tuned & support this effort
If you liked and found this article useful, follow me to be able to see all my new posts.
Questions? Post them as a comment and I will reply as soon as possible.
Latest posts
The Best FREE Data Science Resources: FREE Books & Online Courses
ROC Curve Explained using a COVID-19 hypothetical example: Binary & Multi-Class Classification…
Support Vector Machines (SVM) clearly explained: A python tutorial for classification problems…
PCA clearly explained – How, when, why to use it and feature importance: A guide in Python
Everything you need to know about Min-Max normalization in Python
Get in touch with me
- LinkedIn: https://www.linkedin.com/in/serafeim-loukas/
- ResearchGate: https://www.researchgate.net/profile/Serafeim_Loukas
- EPFL profile: https://people.epfl.ch/serafeim.loukas
- Stack Overflow: https://stackoverflow.com/users/5025009/seralouk