Prophet library has been developed by Facebook as a tool for time series forecasting.
Neural network machine learning models for time series prediction - like LSTM neural nets need typically thousands of instances of time series to learn from. On the other hand, Prophet learns only from the one time series that is given to it and makes predictions only based on that. That is great benefit. For the usecase that has been tested here the learning and predicting part takes only few seconds per one time series instance.
Super cool and super simple.
Prophet works incredibly well on historical data that has reocurring pattern - some kind of seasonality. Yearly, monthly, weekly or daily seasonality.
Hence Prophet library is ideal for prediction of electricity consumption during the year or sales of already established product with long selling history.
Stock price is not seasonal in general. While some sectors are cyclical, in general we cannot say that every Tuesday Google stock goes up.
Making predictions with prophet is super easy, prediction takes only one line of code, all the heavy lifting is done under the hood.
Prophet().fit(df)
Of course, that Prophet library does not know the future. But it learns from past dataset and past priceaction and tries to extrapolate the behaviour into the future.
Example prophet predictions - VNO and XOM stocks
Above are examples of two stocks with rather complex behaviour (they just dont go up or just down). Looks like Prophet learned from the steep drop and in further predictions also predicts another drop after significant price runup. That intuitively seems like reasonable thing to do.
# Optional installation with Anaconda
# conda install -c conda-forge fbprophet
import pandas as pd
from fbprophet import Prophet
from datetime import datetime
import yfinance as yf
#suppress warnings
import warnings
warnings.filterwarnings("ignore")
We will download daily data from yahoo finance and make prediction n
days into the future.
Prophet requires us to use specific names for for time(ds
) and price columns (y
).
# prophet needs specific column naming
df['ds'] = df['Date']
df['y'] = df['Adj Close']
So we just feed the data into prophet and make predictions.
See more info on official Prophet quick start guide: https://facebook.github.io/prophet/docs/quick_start.html
start=datetime(2018,1,1)
end=datetime.now().date().isoformat() # today
# pick stock ticker
# symbol = 'BTC-USD' # usage for crypto
symbol = 'AAPL' # usage for stocks
df = yf.download(symbol, start=start, end=end)
df.head(2)
df = df.reset_index()
df.head(2)
# prophet needs specific column naming
df['ds'] = df['Date']
df['y'] = df['Adj Close']
df.head(2)
m = Prophet()
#m.fit(df.iloc[:-365])
m.fit(df)
future = m.make_future_dataframe(periods=180)
future.tail(2)
forecast= m.predict(future)
forecast.tail(2)
fig1=m.plot(forecast)
Since we have the dataframe generated by Prophet, it is very easy to plot it with our favourite python plotting library. In this case we are utilizing matplotlib.
import matplotlib.pyplot as plt
plt.figure(figsize=(15,5))
plt.title(symbol + ' stock price prediction')
plt.plot(df['ds'], df['Adj Close'])
plt.plot(forecast['ds'], forecast['yhat'])
plt.plot(forecast['ds'], forecast['yhat_lower'], alpha=0.1, color='blue')
plt.plot(forecast['ds'], forecast['yhat_upper'], alpha=0.1, color='blue')
plt.fill_between(forecast['ds'], forecast['yhat_upper'], forecast['yhat_lower'], alpha=0.1)
#plt.plot(forecast['ds'], forecast['trend'])
plt.show()