XGBoost For Time Series Forecasting: Don’t Use It Blindly
XGBoost用于时间序列预测:切勿盲目使用
XGBoost For Time Series Forecasting: Don’t Use It Blindly
XGBoost用于时间序列预测:切勿盲目使用
Forecasting techniques don’t work well with all time series
预测技术并非对所有时间序列都有效
When modelling a time series with a model such as ARIMA, we often pay careful attention to factors such as seasonality, trend, the appropriate time periods to use, among other factors.
当使用ARIMA等模型对时间序列建模时,我们通常会仔细关注季节性、趋势、合适的时间周期等因素。
However, when it comes to using a machine learning model such as XGBoost to forecast a time series — all common sense seems to go out the window. Rather, we simply load the data into the model in a black-box like fashion and expect it to magically give us accurate output.
然而,当使用XGBoost这类机器学习模型来预测时间序列时——所有常识似乎都被抛诸脑后了。相反,我们只是以黑箱方式将数据加载到模型中,期望它神奇地给出准确的输出。
A little known secret of time series analysis — not all time series can be forecast, no matter how good the model. Attempting to do so can often lead to spurious or misleading forecasts.
时间序列分析中一个鲜为人知的秘密是——并非所有时间序列都可以被预测,无论模型有多好。试图这样做往往会导致虚假或误导性的预测。
To illustrate this point, let us see how XGBoost (specifically XGBRegressor) varies when it comes to forecasting 1) electricity consumption patterns for the Dublin City Council Civic Offices, Ireland and 2) quarterly condo sales for the Manhattan Valley.
为说明这一点,让我们看看XGBoost(具体来说是XGBRegressor)在预测以下两类数据时的表现差异:1)爱尔兰都柏林市议会市政办公室的电力消耗模式;2)曼哈顿谷的季度公寓销售额。
How XGBRegressor Forecasts Time Series
XGBRegressor如何预测时间序列
XGBRegressor uses a number of gradient boosted trees (referred to as n_estimators in the model) to predict the value of a dependent variable. This is done through combining decision trees (which individually are weak learners) to form a combined strong learner.
XGBRegressor使用多个梯度提升树(模型中称为n_estimators)来预测因变量的值。这是通过组合决策树(单独来看是弱学习器)来形成一个强学习器实现的。
When forecasting a time series, the model uses what is known as a lookback period to forecast for a number of steps forward. For instance, if a lookback period of 1 is used, then the X_train (or independent variable) uses lagged values of the time series regressed against the time series at time t (Y_train) in order to forecast future values.
在预测时间序列时,该模型使用所谓的回看期来预测未来若干步的值。例如,如果回看期为1,则X_train(自变量)使用时间序列的滞后值,与时间t处的目标序列(Y_train)进行回归,以预测未来值。
Forecasting Electricity Consumption
预测电力消耗
Let’s see how this works using the example of electricity consumption forecasting.
让我们通过电力消耗预测的例子来看看这是如何运作的。
The dataset in question is available from data.gov.ie. From this graph, we can see that a possible short-term seasonal factor could be present in the data, given that we are seeing significant fluctuations in consumption trends on a regular basis.
相关数据集可从data.gov.ie获取。从这张图中可以看出,数据中可能存在短期的季节性因素,因为我们定期看到消耗趋势出现显著波动。
Let’s use an autocorrelation function to investigate further.
让我们使用自相关函数做进一步调查。
From this autocorrelation function, it is apparent that there is a strong correlation every 7 lags. Intuitively, this makes sense because we would expect that for a commercial building, consumption would peak on a weekday (most likely Monday), with consumption dropping at the weekends.
从该自相关函数可以明显看出,每7个滞后存在强相关性。直观上这很有道理,因为对于商业建筑,我们预期工作日的消耗量会达到峰值(很可能是周一),而周末则有所下降。
When forecasting such a time series with XGBRegressor, this means that a value of 7 can be used as the lookback period.
当使用XGBRegressor预测此类时间序列时,这意味着可以将7作为回看期。
The model is run on the training data and the predictions are made:
模型在训练数据上运行并做出预测:
Let’s calculate the RMSE and compare it to the test mean (the lower the value of the former compared to the latter, the better).
让我们计算RMSE并将其与测试集均值进行比较(前者相对于后者越低越好)。
We see that the RMSE is quite low compared to the mean (11% of the size of the mean overall), which means that XGBoost did quite a good job at predicting the values of the test set. If you wish to view this example in more detail, further analysis is available here.
我们看到RMSE相对于均值相当低(约占均值的11%),这意味着XGBoost在预测测试集值方面表现相当不错。如果你想更详细地查看此示例,可在此处获取进一步分析。
Forecasting Manhattan Valley Condo Sales
预测曼哈顿谷公寓销售额
In the above example, we evidently had a weekly seasonal factor, and this meant that an appropriate lookback period could be used to make a forecast.
在上面的例子中,我们显然有一个每周季节性因素,这意味着可以使用适当的回看期进行预测。
However, there are many time series that do not have a seasonal factor. This makes it more difficult for any type of model to forecast such a time series — the lack of periodic fluctuations in the series causes significant issues in this regard.
然而,有许多时间序列没有季节性因素。这使得任何类型的模型都更难以预测此类时间序列——序列中缺乏周期性波动在这方面造成了重大问题。
Here is a visual overview of quarterly condo sales in the Manhattan Valley from 2003 to 2015. The data was sourced from NYC Open Data, and the sale prices for Condos — Elevator Apartments across the Manhattan Valley were aggregated by quarter from 2003 to 2015.
以下是2003年至2015年曼哈顿谷季度公寓销售额的可视化概览。数据来源于NYC Open Data,曼哈顿谷公寓——电梯公寓的销售价格按季度汇总,时间跨度为2003年至2015年。
From the above, we can see that there are certain quarters where sales tend to reach a peak — but there does not seem to be a regular frequency by which this occurs.
从上图可以看出,某些季度的销售额往往达到峰值——但似乎没有规律性的频率。
Again, let’s look at an autocorrelation function.
同样,让我们看一下自相关函数。
From the autocorrelation, it looks as though there are small peaks in correlations every 9 lags — but these lie within the shaded region of the autocorrelation function and thus are not statistically significant.
从自相关来看,似乎每9个滞后存在小峰值——但这些峰值位于自相关函数的阴影区域内,因此不具有统计显著性。
What if we tried to forecast quarterly sales using a lookback period of 9 for the XGBRegressor model?
如果我们尝试使用回看期9来训练XGBRegressor模型预测季度销售额会怎样?
The same model as in the previous example is specified:
使用与前一示例相同的模型规格:
Now, let’s calculate the RMSE and compare it to the mean value calculated across the test set:
现在,让我们计算RMSE并将其与测试集上的均值进行比较:
We can see that in this instance, the RMSE is quite sizable — accounting for 50% of the mean value as calculated across the test set.
可以看出,在这种情况下,RMSE相当大——占测试集均值的50%。
This indicates that the model does not have much predictive power in forecasting quarterly total sales of Manhattan Valley condos.
这表明该模型在预测曼哈顿谷公寓季度总销售额方面没有太多预测能力。
Given that no seasonality seems to be present, how about if we shorten the lookback period? Let’s try a lookback period of 1, whereby only the immediate previous value is used.
考虑到似乎不存在季节性,如果我们缩短回看期会怎样?让我们尝试回看期为1,即仅使用前一个值。
The size of the mean across the test set has decreased, since there are now more values included in the test set as a result of a lower lookback period. This has smoothed out the effects of the peaks in sales somewhat. However, we see that the size of the RMSE has not decreased that much, and the size of the error now accounts for over 60% of the total size of the mean.
测试集的均值规模变小了,因为回看期缩短导致测试集中包含更多值。这在一定程度上平滑了销售额峰值的影响。然而,我们看到RMSE的规模并没有下降那么多,误差规模现在占总均值的60%以上。
Therefore, using XGBRegressor (even with varying lookback periods) has not done a good job at forecasting non-seasonal data.
因此,使用XGBRegressor(即使采用不同的回看期)在预测非季节性数据方面表现不佳。
Conclusion
结论
There are many types of time series that are simply too volatile or otherwise not suited to being forecasted outright. However, all too often, machine learning models like XGBoost are treated in a plug-and-play like manner, whereby the data is fed into the model without any consideration as to whether the data itself is suitable for analysis.
有许多类型的时间序列要么过于波动,要么根本不适合直接预测。然而,XGBoost等机器学习模型常常被以即插即用的方式对待,数据被输入模型,而完全不考虑数据本身是否适合分析。
Therefore, the main takeaway of this article is that whether you are using an XGBoost model — or any model for that matter — ensure that the time series itself is firstly analysed on its own merits. This means determining an overall trend and whether a seasonal pattern is present.
因此,本文的主要结论是:无论你使用的是XGBoost模型还是任何其他模型,首先要根据时间序列本身的特性对其进行分析。这意味着要确定整体趋势以及是否存在季节性模式。
The allure of XGBoost is that one can potentially use the model to forecast a time series without having to understand the technical components of that time series — and this is not the case.
XGBoost的吸引力在于,人们可以在不了解时间序列技术成分的情况下使用该模型进行预测——但事实并非如此。
Many thanks for your time, and any questions or feedback are greatly appreciated.
非常感谢您的阅读,任何问题或反馈都深表感激。
Disclaimer: This article is written on an “as is” basis and without warranty. It was written with the intention of providing an overview of data science concepts, and should not be interpreted as professional advice. The findings and interpretations in this article are those of the author and are not endorsed by or affiliated with any third-party mentioned in this article. The author has no relationship with any third parties mentioned in this article.
免责声明:本文按“现状”撰写,不提供任何保证。撰写本文旨在概述数据科学概念,不应被解释为专业建议。本文中的发现和解释仅代表作者个人观点,不代表本文中提及的任何第三方认可或与其关联。作者与本文中提及的任何第三方无任何关系。