Predicting Stock Prices Using Facebook’s Prophet Model

Rohan Kumar
Analytics Vidhya
Published in
3 min readFeb 22, 2021

Facebook’s Prophet

Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.

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).

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.

Prophet is open source software released by Facebook’s Core Data Science team .

Full documentation and examples available at the homepage: https://facebook.github.io/prophet/

Let’s start coding!

We’ll be using Python to do this example.

Load the data

We’ll be using Intel’s data from (1980–2020) and predict the values for 2021.

Data head

Selecting the specific data

Since we’ll be predicting the “Close” value, we shall only take “Date” and “Close” column.

New dataframe as per Model

The input to Prophet is always a dataframe with two columns: ds and y. The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp. The y column must be numeric, and represents the measurement we wish to forecast.

Building the model

Prophet follows the sklearn model API. We create an instance of the Prophet class and then call its fit and predict methods.

Prediction Time

Now, for the last step, we will ask the model to predict future values and then visualize the predictions.

Conclusion: It seems that the Intel Stock price will be around 57.04 in Feb 2021 based on the model’s prediction.

Verification Time

Well, we have hit the jackpot with this!! Let’s hope it’s prediction for the future is also correct!

I want you guys to try this Model and let me know how was the prediction.

Disclaimer 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.

Also try my Stock prediction models:

LSTM MODEL

ARIMA MODEL

Give me a FOLLOW if you liked this for more tech blogs!

Sayonara!

--

--

Rohan Kumar
Analytics Vidhya

Poet | Story writer | Blogger "I took a walk in the woods and came out taller than the trees."~ Henry David Thorea