Facebook Prophet Model with Python

In this article, I will take you through how to forecast stock prices using Facebook Prophet Model with Python programming language, which is used for the tasks of time series forecasting.

Introduction to Facebook Prophet Model

The Facebook research team has come up with an easier implementation of time series forecasting with its new library called Prophet. An analyst capable of producing high-quality forecast data is rarely seen. This is one of the reasons why the Facebook research team has come up with an easily accessible way to use advanced concepts for time series forecasting. 

Also, Read – 100+ Machine Learning Projects Solved and Explained.

We as Python users can easily relate to this library because it uses an API which is similar to Scikit-Learn. The main goal of the Prophet team is to enable experts and non-experts to make high-quality forecasts that meet demand.

In my opinion, Prophet works best with datasets that are heavily influenced by seasonality e.g. utility bills, restaurant visitors, etc.

Facebook Prophet Model with Python

Now let’s analyze stock prices and use the Facebook Prophet model with Python programming language. I will start with importing the necessary python libraries and the dataset:

import numpy as np 
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
from fbprophet import Prophet
df = pd.read_csv('all_stocks_5yr.csv')
df.head()
dateopenhighlowclosevolumeName
02013-02-0815.0715.1214.6314.758407500AAL
12013-02-1114.8915.0114.2614.468882000AAL
22013-02-1214.4514.5114.1014.278126000AAL
32013-02-1314.3014.9414.2514.6610259500AAL
42013-02-1414.9414.9613.1613.9931879900AAL

Here I will replace the column name from name to ticks:

df = df.rename(columns={'Name': 'Ticks'})

For this simple Facebook Prophet Model tutorial with Python, I’ll analyze the Amazon stock and see what the trend for the near future of that stock will look like based on past stock prices:

amzn = df.loc[df['Ticks'] == 'AMZN']

We need to make sure the date column is either a categorical type or a data type. In our case, the date column is a categorical data type, so we need to change it to DateTime. Before doing that, I will first create a copy of the data to avoid the SettingWarning:

amzn_df = amzn.copy()
# Change to datetime datatype.
amzn_df.loc[:, 'date'] = pd.to_datetime(amzn.loc[:,'date'], format="%Y/%m/%d")

Data Visualization

Now, let’s plot our data to see what we need to create a model for:

amazon stock prices

Using Facebook Prophet Model with Python

Steps to use the Facebook Prophet template:

  1. Be sure to substitute the close price for y and the date for ds.
  2. Fit this data frame to the Prophet to detect future patterns.
  3. Predict the prices above and below the closing price.

Now let’s see how to use the Facebook Prophet Model with Python programming language:

m = Prophet()

# Drop the columns
ph_df = amzn_df.drop(['open', 'high', 'low','volume', 'Ticks'], axis=1)
ph_df.rename(columns={'close': 'y', 'date': 'ds'}, inplace=True)

ph_df.head()
dsy
463872013-02-08261.95
463882013-02-11257.21
463892013-02-12258.70
463902013-02-13269.47
463912013-02-14269.24
facebook prophet model
fig2 = m.plot_components(forecast)
plt.show()
seasonality affects
m = Prophet(changepoint_prior_scale=0.01).fit(ph_df)
future = m.make_future_dataframe(periods=12, freq='M')
fcst = m.predict(future)
fig = m.plot(fcst)
plt.title("Monthly Prediction \n 1 year time frame")

plt.show()
monthly FB prophet model

Observations:

From the visualizations above, we can observe that:

  1. Amazon’s stock price is showing signs of an uptrend every year.
  2. Amazon’s share price shows signs of an uptrend in January (December sales tend to boost Amazon’s share price)
  3. There is no weekly trend for stock prices.

I hope you liked this article on a tutorial on Facebook Prophet model with Python programming language. Feel free to ask your valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

Data Strategist at Statso. My aim is to decode data science for the real world in the most simple words.

Articles: 1609

Leave a Reply

Discover more from thecleverprogrammer

Subscribe now to keep reading and get access to the full archive.

Continue reading