Time series analysis and modelling are complex procedures in the field of data science. We always find that time series modelling is a combination of different processes and time taking processes. So to reduce the time complexity in time series modelling there is always a requirement of automacy. The prophet is a toolkit that aims to provide automacy in time series modelling. In this article, we are going to discuss the prophet toolkit provided by Meta (Facebook) and how we can use it. The major points to be discussed in the article are listed below.
Table of contents
- What is the Prophet?
- Implementation
- Loading data
- Importing modules
- Initiating model
- Making predictions
- Plotting prediction
Let’s start with understanding what the Prophet is.
What is the Prophet?
The prophet is a toolkit or library for time series analysis that is available to us as an open-source. Utilizing this toolkit we can perform time series analysis and forecasting very easily and fast. This toolkit has various features that can make our time series analysis procedure accurate and efficient. Some of the best features are automatic hyperparameter tuning, skilful and accurate forecasting. Model plotting and compatibility with R and python language.
This toolkit is developed by the researcher of Facebook that is known as Meta now. The modules designed under this toolkit can be referred to as additive time series models for forecasting. One thing that the researcher of the toolkit claims is it is best with time series that have seasonal effects. This toolkit provides fully automated features to deal with messy time series data with less manual effort. Using it we can work on detecting outliers, missing data, and irrelevant changes in the time series.
We can install this toolkit in our environment using the following line of codes:
!pip install prophet
After installation, we are ready to use this toolkit for time series analysis.
Implementation
Loading data
In this article, we are going to use the share price data of the State Bank of India. To get the present data we are required to use the yfinance toolkit that can be downloaded in the environment using the following lines of codes:
!pip install yfinance
After installation, we are required to import some modules that are related to the DateTime values.
import datetime as dt
from datetime import datetime as dt
from dateutil.relativedelta import relativedelta
After this import, we can define the end and start date of the data like following
end = dt.today()
start = dt.today() - relativedelta(years=1)
After defining the date range we can use the yfinance to download the data for the given date range.
import yfinance as yf
data = yf.download('SBIN.NS', start, end)
Output:
Let’s check our data.
data.head()
Output:
Let’s plot the data so that we can understand how the time series is going.
from matplotlib import pyplot
data.plot(y = 'Close')
pyplot.show()
Output:
The reason behind plotting the data is I wanted to let the reader know we can use this toolkit with the time series having huge seasonality. In the above plot, we can see that there is a trend in the data that is going up and a monthly seasonality that is reducing the price of the shares of SBI.
Treating such data manually will need a lot of processing and the procedure will become so much time taking. Let’s check the prophet how much it is capable of dealing with such data.
Importing modules
from prophet import Prophet as ph
from prophet.plot import plot_plotly, plot_components_plotly
Here in the above, we can see that we have imported only two modules one is for modelling and the second one is for plotting the results from the model.
Initiating model
We can initiate a basic model using the object prophet. The toolkit provides the forecasting procedure into this object and then we can fit this using the .fit() command on any time series. One thing that is required by the prophet toolkit is it need a date-time column named as ds and the target variable named as ‘y’. Using the following line we can change the column name and fit the data into the model.
data = data.rename(columns={'Close': 'y'})
model = ph()
model.fit(data)
Output:
‘Here we can see some of the information about the modelling using prophet. In the above codes, we have called the object prophet using the model instance and on the model instance, we fit our data.
Making predictions
After initiating the model instance we are ready to make predictions but before generating the prediction we are required to make some dates of the future. Let’s see the tails of our imported data.
data.tail(10)
Output:
Here we can see that we have data till data 2022-03-14, we can make dates using the make_future_dataframe module of the toolkit.
future_dates = model.make_future_dataframe(periods=30)
future_dates.tail(10)
Output:
Here we have created dates for a whole month. Now, these blank dates require some values. Using the older data we can estimate them. This is what a time series modelling procedure does and here we are using the prophet toolkit for time series modelling.
We can utilize this toolkit for forecasting in the following way:
future = model.predict(future_dates)
Lets see how the predicted data is :
future[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(10)
Output:
Here we can see that the toolkit provided object predicts values in range here yhat is an exact prediction and yhat_lower and yhat_upper are the upper and lower range of the predictions.
Plotting prediction
We can also utilize modules from the prophet for plotting the predictions.
fig2 = model.plot(future)
Output:
In the above plot, we can see what the predictions are and also we can see that to make the prediction object has utilized an average from the older values, outliers are separated and predictions have a maintained seasonality.
Let’s segregate the predicted time series in its components that can provide us with more detailed information about the predictions.
fig3 = model.plot_components(future)
Output:
Here we can see the trend of the prediction and the yearly components shows the seasonality of the predictions and how the series is changing weakly.
We can also use prophet with plotly to generate interactive plots. For example, using the below lines of codes we can make an interactive plot of prediction with filters that are telling us reports in weekly, monthly, half-yearly, yearly, and whole-time series filters.
plot_plotly(model, future)
Output:
Here we can not post the interaction with the plot we can check this plot in this notebook. Here we have completed our procedure and see how we can generate predictions using the prophet toolkit with real-life data.
Final words
In this article, we have gone through the prophet toolkit, using this toolkit we can perform effective, efficient, and accurate time series modelling. We can see in the above all the things we performed required only one or two lines of code.