First, load necessary libraries
import numpy as npLoad the S&P 500 data from yahoo finance
import pandas as pd
import matplotlib.pyplot as pyplot
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']]The Facebook Prophet library is loaded. Data is prepared to the format required by Prophet.
testSp500 = sp500[sp500.Date>='2018-01-01'][['Date','Close']]
from fbprophet import ProphetAll preparation is ready. Here we fit the model using the training data
data = pd.DataFrame({'ds':trainSp500['Date'].values, 'y':trainSp500['Close'].values})
model = Prophet()We make a one-year prediction.
model.fit(data)
future = model.make_future_dataframe(365) # forecasting for 1 yearsIn 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.
forecast = model.predict(future)
No comments:
Post a Comment