Prophet is Facebook’s library for time series forecasting. It is mainly geared towards business datasets (e.g. predicting adspend or CPU usage), but a natural question that comes up with my students whenever we talk about time series is: “can it predict stock prices?”
In this article, I will discuss how to use FB Prophet to predict stock prices, and I’ll also show you what not to do (things I’ve seen in other popular blogs). Furthermore, we will benchmark the Prophet model with the naive forecast, to check whether or not one would really want to use this.
Note: This is an excerpt from my full VIP course, “Time Series Analysis, Forecasting, and Machine Learning“. If you want the code for this example, along with many, many other code examples on stock prices, sales data, and smartphone data, get the course!
The Prophet section will be part of the VIP version only, so get it now while the VIP coupon is still active!
How does Prophet work?
The Prophet model is a 3 component, non-autoregressive time series model. Specifically:
$$y(t) = g(t) + s(t) + h(t) + \varepsilon(t)$$
The Prophet model is not autoregressive, like ARIMA, exponential smoothing, and the other methods we study in a typical time series course (including my own).
The 3 components are:
1) The trend \( g(t) \) which can be either linear or logistic.
2) The seasonality \( s(t) \), modeled using a Fourier series.
3) The holiday component \( h(t) \), which is essentially a one-hot vector “dotted” with a vector of weights, each representing the contribution from their respective holiday.
How to use Prophet for predicting stock prices
In my course, we do 3 experiments. Our data is Google’s stock price from approximately 2013-2018, but we only use the first 2 years as training data.
The first experiment is “plug-and-play” into Prophet with the default settings.
Here are the results:
Unfortunately, Prophet mistakenly believes there is a weekly seasonal component, which is the reason for the little “hairs” in the forecast.
When we plot the components of the model, we see that Prophet has somehow managed to find some weekly seasonality.
Of course, this is completely wrong! The model believes that the stock price increases on the weekends, which is highly unlikely because we don’t have any data for the weekend.
The second experiment is an example of what not to do. I saw this in every other popular blog, which is yet another “data point” that should convince you not to trust these popular data science blogs you find online (except for mine, obviously).
In this experiment, we set daily_seasonality to True in the model constructor.
Here are the results.
It seems like those weird little “hairs” coming from the weekly seasonal component have disappeared.
“The Lazy Programmer is wrong!” you may proclaim.
However, this is because you may not understand what daily seasonality really means.
Let’s see what happens when we plot the components.
This plot should make you very suspicious. Pay attention to the final chart.
“Daily seasonality” pertains to a pattern that repeats everyday with sub-daily changes.
This cannot be the case, because our data only has daily granularity!
Lesson: don’t listen to those “popular” blogs.
For experiment 3, we set weekly seasonality to False. Alternatively, you could try playing around with the priors.
Here are the results.
Notice that the “little hairs” are again not present.
Is this model actually good?
Just because you can make a nice chart, does not mean you have done anything useful.
In fact, you see the exact same mistakes in those blog articles and terrible Udemy courses promising to “predict stock prices with LSTMs” (which I will call out every chance I get).
One of the major mistakes I see in nearly every blog post about predicting stock prices is that they don’t bother to compare it to a benchmark. And as you’ll see, the benchmark for stock prices is quite a low bar – there is no reason not to compare.
Your model is only useful if it can beat the benchmark.
For stock price predictions, the benchmark is typically the naive forecast, which is the optimal forecast for a random walk.
Random walks are often used as a model for stock prices since they share some common attributes.
For those unfamiliar, the naive forecast is simply where you predict the last-known value.
Example: If today’s price on July 5 is $200 and I want to make a forecast with a 5-day horizon, then I will predict $200 for July 6, $200 for July 7, …, and $200 for July 10.
I won’t bore you with the code (although it’s included in the course if you’re interested), but the answer is: Prophet does not beat the naive forecast.
In fact, it does not beat the naive forecast on any horizon I tried (5 days, 30 days, 60 days).
Sidenote: it’d be a good exercise to try 1 day as well.
How to learn more
Are stock prices really random walks? Although this particular example provides evidence supporting the random walk hypothesis, in my course, the GARCH section will provide strong evidence against it! Again, it’s all explained in my latest course, “Time Series Analysis, Forecasting, and Machine Learning“. Only the VIP version will contain the sections on Prophet, GARCH, and other important tools.
The VIP version is intended to be limited-time only, and the current coupon expires in less than one month!