Search Write Andrey Kartsev Top highlight This member-only story is on us. Upgrade to access all of Medium. Member-only story Time-Series Forecasting: Predicting Stock Prices Using Facebook’s Prophet Model In this post I show you how to predict stock prices using a forecasting model publicly available from Facebook Data Science team: The Prophet Serafeim Loukas, PhD Towards Data Science Serafeim Loukas, PhD · Follow Published in Towards Data Science · 8 min read · Jun 13, 2020 616 7 Figure created by the author in Python. 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. An example of a time-series. Plot created by the author in Python. 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. Another resource. Learn Data Science and ML with the help of an 🤖 AI-powered tutor. Start here https://aigents.co/learn choose a topic and he will show up where you need him. No paywall, no signups, no ads. 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: How to download the stock price history. 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: 5 first lines of our dataset. 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 In this post I show you how to predict stock prices using a forecasting LSTM model towardsdatascience.com Check also my recent article using an ARIMA model: Time-Series Forecasting: Predicting Stock Prices Using An ARIMA Model In this post I show you how to predict the TESLA stock price using a forecasting ARIMA model towardsdatascience.com 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 The most useful FREE books and online courses for someone who wants to learn more about Data Science. medium.com ROC Curve Explained using a COVID-19 hypothetical example: Binary & Multi-Class Classification… In this post I clearly explain what a ROC curve is and how to read it. I use a COVID-19 example to make my point and I… towardsdatascience.com Support Vector Machines (SVM) clearly explained: A python tutorial for classification problems… In this article I explain the core of the SVMs, why and how to use them. Additionally, I show how to plot the support… towardsdatascience.com PCA clearly explained — How, when, why to use it and feature importance: A guide in Python In this post I explain what PCA is, when and why to use it and how to implement it in Python using scikit-learn. Also… towardsdatascience.com Everything you need to know about Min-Max normalization in Python In this post I explain what Min-Max scaling is, when to use it and how to implement it in Python using scikit-learn but… towardsdatascience.com How Scikit-Learn's StandardScaler works In this post I am explaining why and how to apply Standardization using scikit-learn towardsdatascience.com 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 Machine Learning Time Series Forecasting Python Data Science Stock Market 616 7 Serafeim Loukas, PhD Towards Data Science Written by Serafeim Loukas, PhD 2.6K Followers · Writer for Towards Data Science Senior Data Scientist / Research Scientist @ Natural Cycles (Switzerland). PhD, MSc, M.Eng. Bespoke services: https://www.patreon.com/TheDataScienceHub Follow More from Serafeim Loukas, PhD and Towards Data Science PCA clearly explained — How, when, why to use it and feature importance: A guide in Python Serafeim Loukas, PhD Serafeim Loukas, PhD in Towards Data Science PCA clearly explained — How, when, why to use it and feature importance: A guide in Python In this post I explain what PCA is, when and why to use it and how to implement it in Python using scikit-learn. Also, I explain how to… · 7 min read · May 30, 2020 623 7 How I Won Singapore’s GPT-4 Prompt Engineering Competition Sheila Teo Sheila Teo in Towards Data Science How I Won Singapore’s GPT-4 Prompt Engineering Competition A deep dive into the strategies I learned for harnessing the power of Large Language Models (LLMs) · 23 min read · Dec 29, 2023 10.3K 119 How to Learn AI on Your Own (a self-study guide) Thu Vu Thu Vu in Towards Data Science How to Learn AI on Your Own (a self-study guide) If your hands touch a keyboard for work, Artificial Intelligence is going to change your job in the next few years. · 12 min read · Jan 5 2.5K 26 Forecasting Timeseries Using Machine Learning & Deep Learning Serafeim Loukas, PhD Serafeim Loukas, PhD in MLearning.ai Forecasting Timeseries Using Machine Learning & Deep Learning In this post, I show you how to predict stock prices using a forecasting LSTM model & a simple Ridge regression model. · 11 min read · Mar 6, 2023 139 2 See all from Serafeim Loukas, PhD See all from Towards Data Science Recommended from Medium Stock Price Prediction with Quantum Machine Learning in Python Nikhil Adithyan Nikhil Adithyan in DataDrivenInvestor Stock Price Prediction with Quantum Machine Learning in Python An overview of the challenges and opportunities 17 min read · 6 days ago 779 9 Using Sentiment Analysis as a Trading Signal? Beat the market with Transformers! Chedy Smaoui Chedy Smaoui Using Sentiment Analysis as a Trading Signal? Beat the market with Transformers! 10 min read · Jan 21 128 Lists Predictive Modeling w/ Python 20 stories · 842 saves Principal Component Analysis for ML Time Series Analysis deep learning cheatsheet for beginner Practical Guides to Machine Learning 10 stories · 985 saves Coding & Development 11 stories · 408 saves Natural Language Processing 1132 stories · 602 saves Stock Price Prediction using LSTM: A Step-by-Step Guide for SPY ShawnYuShuHearn ShawnYuShuHearn Stock Price Prediction using LSTM: A Step-by-Step Guide for SPY A Step-by-Step Guide for SPY price prediction using LSTM 7 min read · Aug 2, 2023 18 2 Stock Market + Time Series LSTM = Failure Nickolas Discolll Nickolas Discolll Stock Market + Time Series LSTM = Failure I will show you how to predict the stock market with LSTM and time series in this article. The basic machine learning models ruin the… · 17 min read · Dec 18, 2023 213 5 Cover Image The Python Lab The Python Lab in Python in Plain English Multivariate Time Series Forecasting with Keras and TensorFlow · 10 min read · Aug 6, 2023 18 1 Riding the Waves of Stock Prices with Wavelet Transform Signals in Python Cris Velasquez Cris Velasquez Riding the Waves of Stock Prices with Wavelet Transform Signals in Python Towards Unlocking Market Signals for Clearer Trading Insights · 9 min read · Sep 11, 2023 348 3 See more recommendations Help Status About Careers Blog Privacy Terms Text to speech Teams