Some time series are generated from very low frequency data. These data generally exhibit multiple seasonalities. For example, hourly data may exhibit repeated patterns every hour (every 24 observations) or every day (every 24 * 7, hours per day, observations). This is the case for electricity load. Electricity load may vary hourly, e.g., during the evenings electricity consumption may be expected to increase. But also, the electricity load varies by week. Perhaps on weekends there is an increase in electrical activity.
In this example we will show how to model the two seasonalities of the time series to generate accurate forecasts in a short time. We will use hourly PJM electricity load data. The original data can be found here.
Libraries
In this example we will use the following libraries:
StatsForecast. Lightning ⚡️ fast forecasting with statistical and econometric models. Includes the MSTL model for multiple seasonalities.
DatasetsForecast. Used to evaluate the performance of the forecasts.
PJM Interconnection LLC (PJM) is a regional transmission organization (RTO) in the United States. It is part of the Eastern Interconnection grid operating an electric transmission system serving all or parts of Delaware, Illinois, Indiana, Kentucky, Maryland, Michigan, New Jersey, North Carolina, Ohio, Pennsylvania, Tennessee, Virginia, West Virginia, and the District of Columbia. The hourly power consumption data comes from PJM’s website and are in megawatts (MW).
We clearly observe that the time series exhibits seasonal patterns. Moreover, the time series contains 32,896 observations, so it is necessary to use very computationally efficient methods to display them in production.
MSTL model
The MSTL (Multiple Seasonal-Trend decomposition using LOESS) model, originally developed by Kasun Bandara, Rob J Hyndman and Christoph Bergmeir, decomposes the time series in multiple seasonalities using a Local Polynomial Regression (LOESS). Then it forecasts the trend using a custom non-seasonal model and each seasonality using a SeasonalNaive model.
StatsForecast contains a fast implementation of the MSTL model. Also, the decomposition of the time series can be calculated.
First we must define the model parameters. As mentioned before, the electricity load presents seasonalities every 24 hours (Hourly) and every 24 * 7 (Daily) hours. Therefore, we will use [24, 24 * 7] as the seasonalities that the MSTL model receives. We must also specify the manner in which the trend will be forecasted. In this case we will use the AutoARIMA model.
intervals = ConformalIntervals()mstl = MSTL( season_length=[24, 24*7], # seasonalities of the time series trend_forecaster=AutoARIMA(), # model used to forecast trend prediction_intervals = intervals)
Once the model is instantiated, we have to instantiate the StatsForecast class to create forecasts.
sf = StatsForecast( models=[mstl], # model used to fit each time series freq='H', # frequency of the data fallback_model = SeasonalNaive(season_length=7))
Fit the model
Afer that, we just have to use the fit method to fit each model to each time series.
sf = sf.fit(df=df)
Decompose the time series in multiple seasonalities
Once the model is fitted, we can access the decomposition using the fitted_ attribute of StatsForecast. This attribute stores all relevant information of the fitted models for each of the time series.
In this case we are fitting a single model for a single time series, so by accessing the fitted_ location [0, 0] we will find the relevant information of our model. The MSTL class generates a model_ attribute that contains the way the series was decomposed.
sf.fitted_[0, 0].model_
data
trend
seasonal24
seasonal168
remainder
0
22259.0
26183.898892
-5215.124554
609.000432
681.225229
1
21244.0
26181.599305
-6255.673234
603.823918
714.250011
2
20651.0
26179.294886
-6905.329895
636.820423
740.214587
3
20421.0
26176.985472
-7073.420118
615.825999
701.608647
4
20713.0
26174.670877
-7062.395760
991.521912
609.202971
...
...
...
...
...
...
32891
36392.0
33123.552727
4387.149171
-488.177882
-630.524015
32892
35082.0
33148.242575
3479.852929
-682.928737
-863.166767
32893
33890.0
33172.926165
2307.808829
-650.566775
-940.168219
32894
32590.0
33197.603322
748.587723
-555.177849
-801.013195
32895
31569.0
33222.273902
-967.124123
-265.895357
-420.254422
32896 rows × 5 columns
Let’s look graphically at the different components of the time series.
We observe that there is a clear trend towards the high (orange line). This component would be predicted with the AutoARIMA model. We can also observe that every 24 hours and every 24 * 7 hours there is a very well defined pattern. These two components will be forecast separately using a SeasonalNaive model.
Produce forecasts
To generate forecasts we only have to use the predict method specifying the forecast horizon (h). In addition, to calculate prediction intervals associated to the forecasts, we can include the parameter level that receives a list of levels of the prediction intervals we want to build. In this case we will only calculate the 90% forecast interval (level=[90]).
In the next section we will plot different models so it is convenient to reuse the previous code with the following function.
Performance of the MSTL model
Split Train/Test sets
To validate the accuracy of the MSTL model, we will show its performance on unseen data. We will use a classical time series technique that consists of dividing the data into a training set and a test set. We will leave the last 24 observations (the last day) as the test set. So the model will train on 32,872 observations.
In addition to the MSTL model, we will include the SeasonalNaive model as a benchmark to validate the added value of the MSTL model. Including StatsForecast models is as simple as adding them to the list of models to be fitted.
sf = StatsForecast( models=[mstl, SeasonalNaive(season_length=24)], # add SeasonalNaive model to the list freq='H')
To measure the fitting time we will use the time module.
from time import time
To retrieve the forecasts of the test set we only have to do fit and predict as before.
We note that MSTL produces very accurate forecasts that follow the behavior of the time series. Now let us calculate numerically the accuracy of the model. We will use the following metrics: MAE, MAPE, MASE, RMSE, SMAPE.
from datasetsforecast.losses import ( mae, mape, mase, rmse, smape)
We observe that MSTL has an improvement of about 60% over the SeasonalNaive method in the test set measured in MASE.
Comparison with Prophet
One of the most widely used models for time series forecasting is Prophet. This model is known for its ability to model different seasonalities (weekly, daily yearly). We will use this model as a benchmark to see if the MSTL adds value for this time series.
times = pd.DataFrame({'model': ['MSTL', 'Prophet'], 'time (mins)': [time_mstl, time_prophet]})times
model
time (mins)
0
MSTL
0.982553
1
Prophet
0.576759
We observe that the time required for Prophet to perform the fit and predict pipeline is greater than MSTL. Let’s look at the forecasts produced by Prophet.
We note that Prophet is able to capture the overall behavior of the time series. However, in some cases it produces forecasts well below the actual value. It also does not correctly adjust the valleys.
In terms of accuracy, Prophet is not able to produce better forecasts than the SeasonalNaive model, however, the MSTL model improves Prophet’s forecasts by 69% (MASE).
With respect to numerical evaluation, NeuralProphet improves the results of Prophet, as expected, however, MSTL improves over NeuralProphet’s foreacasts by 68% (MASE).
Important
The performance of NeuralProphet can be improved using hyperparameter optimization, which can increase the fitting time significantly. In this example we show its performance with the default version.
Conclusion
In this post we introduced MSTL, a model originally developed by Kasun Bandara, Rob Hyndman and Christoph Bergmeir capable of handling time series with multiple seasonalities. We also showed that for the PJM electricity load time series offers better performance in time and accuracy than the Prophet and NeuralProphet models.