Thursday, April 16, 2020

Simple Facebook Prophet Tutorial: Predicting S&P Index

Stock price is very difficult to predict, since it is very complex and influenced by many factors that are not easily quantified. In this post, I will just use Facebook Prophet package to do a simple S&P 500 Index prediction for tutorial purpose. Facebook Prophet is based on additive model and accounts for non-linear components by including seasonality and holiday effect. The data I use is from Yahoo Finance between 1998-01-01 to 2018-12-31. The majority of the data (1998-01-01 to 2017-12-31) are used as training data and the rest are used as validation.

First, load necessary libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as pyplot
Load the S&P 500 data from yahoo finance
sp500 = pd.read_csv("sp500_yahoo_finance.csv", parse_dates=['Date'])
Let us first look at the data
 sp500.head()

As we can see, different index values like "Open", "High", "Low" etc. as well as "Volume". Here we use the "Close" index as our prediction value

Next step. The raw data is split into training data and test data

trainSp500 = sp500[sp500.Date<'2018-01-01'][['Date','Close']]
testSp500 = sp500[sp500.Date>='2018-01-01'][['Date','Close']]
The Facebook Prophet library is loaded. Data is prepared to the format required by Prophet.
from fbprophet import Prophet
data = pd.DataFrame({'ds':trainSp500['Date'].values, 'y':trainSp500['Close'].values})
All preparation is ready. Here we fit the model using the training data
 model = Prophet()
model.fit(data)
We make a one-year prediction.
future = model.make_future_dataframe(365) # forecasting for 1 years
forecast = model.predict(future)
 In the end, the follow graph is made to show how good the prediction. Note the vertical line represents the separation point of  training and testing data. We can see Facebook did a pretty good job at fitting the training set. However, the testing set is quite off and the big dip is not reflected in the prediction at all.





No comments:

Post a Comment