An overview of time series forecasting models

2023-05-16

An overview of time series forecasting models

 2019-10-04 09:47:05

This blog is from: https://towardsdatascience.com/an-overview-of-time-series-forecasting-models-a2fa7a358fcb 

 

What is this article about?

This article provides an overview of the main models available for modelling time series and forecasting their evolution. The models were developed in R and Python. The related code is available here.

Time series forecasting is a hot topic which has many possible applications, such as stock prices forecasting, weather forecasting, business planning, resources allocation and many others. Even though forecasting can be considered as a subset of supervised regression problems, some specific tools are necessary due to the temporal nature of observations.

What is a time series?

A time series is usually modelled through a stochastic process Y(t), i.e. a sequence of random variables. In a forecasting setting we find ourselves at time t and we are interested in estimating Y(t+h), using only information available at time t.

How to validate and test a time series model?

Due to the temporal dependencies in time series data, we cannot rely on usual validation techniques. To avoid biased evaluations we must ensure that training sets contains observations that occurred prior to the ones in validation sets.

A possible way to overcome this problem is to use a sliding window, as described here. This procedure is called time series cross validation and it is summarised in the following picture, in which the blue points represents the training sets in each “fold” and the red points represent the corresponding validation sets.

 
Time series cross-validation. Credits to Rob J Hyndman

If we are interested in forecasting the next n time steps, we can apply the cross validation procedure for 1,2,…,n steps ahead. In this way we can also compare the goodness of the forecasts for different time horizons.

Once we have chosen the best model, we can fit it on the entire training set and evaluate its performance on a separate test set subsequent in time. The performance estimate can be done by using the same sliding window technique used for cross validation, but without re-estimating the model parameters.

Short data exploration

In the next section we will apply different forecasting models to predict the evolution of the industrial production index which quantifies the electrical equipment manufactured in the Euro area.

The data can be easily downloaded through the fpp2 package in R. To make the data available outside R you can simply run the following code in a R environment.


library(fpp2)
write.csv(elecequip,file = “elecequip.csv”,row.names = FALSE)

The dataset corresponds to monthly manufacture of electrical equipment (computer, electronic and optical products) in the Euro area (17 countries) in the period January 1996-March 2012. We keep the last 2 years for testing purposes.

 

The time series has a peak at the end of 2000 and another one during 2007. The huge decrease that we observe at the end of 2008 is probably due to the global financial crisis which occurred during that year.

There seems to be a yearly seasonal pattern. To better visualise this, we show data for each year separately in both original and polar coordinates.

 
 

We observe a strong seasonal pattern. In particular there is a huge decline in production in August due to the summer holidays.

Time series forecasting models

We will consider the following models:

  1. Naïve, SNaïve
  2. Seasonal decomposition (+ any model)
  3. Exponential smoothing
  4. ARIMA, SARIMA
  5. GARCH
  6. Dynamic linear models
  7. TBATS
  8. Prophet
  9. NNETAR
  10. LSTM

We are interested in forecasting 12 months of the industrial production index. Therefore, given data up to time t, we would like to predict the values taken by the index at times t+1,…,t+12.

We will use the Mean Absolute Error (MAE) to assess the performance of the models.

1) Naïve, SNaïve

In the Naïve model, the forecasts for every horizon correspond to the last observed value.

Ŷ(t+h|t) = Y(t)

This kind of forecast assumes that the stochastic model generating the time series is a random walk.

An extension of the Naïve model is given by the SNaïve (Seasonal Naïve) model. Assuming that the time series has a seasonal component and that the period of the seasonality is T, the forecasts given by the SNaïve model are given by:

Ŷ(t+h|t) = Y(t+h-T)

Therefore the forecasts for the following time steps are equal to the previous T time steps. In our application, the SNaïve forecast for the next year is equal to the last year’s observations.

These models are often used as benchmark models. The following plots show the predictions obtained with the two models for the year 2007.

 
 

The models were fitted by using the naive and snaive functions of the forecast R package.

2) Seasonal decomposition (+ any model)

If data shows some seasonality (e.g. daily, weekly, quarterly, yearly) it may be useful to decompose the original time series into the sum of three components:

Y(t) = S(t) + T(t) + R(t)

where S(t) is the seasonal component, T(t) is the trend-cycle component, and R(t) is the remainder component.

There exists several techniques to estimate such a decomposition. The most basic one is called classical decomposition and it consists in:

  1. Estimating trend T(t) through a rolling mean
  2. Computing S(t) as the average detrended series Y(t)-T(t) for each season (e.g. for each month)
  3. Computing the remainder series as R(t)=Y(t)-T(t)-S(t)

The classical decomposition has been extended in several ways. Its extensions allow to:

  • have a non-constant seasonality
  • compute initial and last values of the decomposition
  • avoid over-smoothing

To get an overview of time series decomposition methods you can click here. We will take advantage of the STL decomposition, which is known to be versatile and robust.

 
STL decomposition on industrial production index data

One way to use the decomposition for forecasting purposes is the following:

  1. Decompose the training time series with some decomposition algorithm (e.g. STL): Y(t)= S(t)+T(t)+R(t).
  2. Compute the seasonally adjusted time series Y(t)-S(t). Use any model you like to forecast the evolution of the seasonally adjusted time series.
  3. Add to the forecasts the seasonality of the last time period in the time series (in our case, the fitted S(t) for last year).

In the following picture we show the seasonally adjusted industrial production index time series.

 

The following plot shows the predictions obtained for the year 2007 by using the STL decomposition and the naïve model to fit the seasonally adjusted time series.

 

The decomposition was fitted by using the stl function of the stats R package.

3) Exponential smoothing

Exponential smoothing is one of the most successful classical forecasting methods. In its basic form it is called simple exponential smoothing and its forecasts are given by:

Ŷ(t+h|t) = ⍺y(t) + ⍺(1-⍺)y(t-1) + ⍺(1-⍺)²y(t-2) + …

with 0<⍺<1.

We can see that forecasts are equal to a weighted average of past observations and the corresponding weights decrease exponentially as we go back in time.

Several extensions of the simple exponential smoothing have been proposed in order to include trend or damped trend and seasonality. The exponential smoothing family is composed of 9 models which are fully described here.

The following plots show the predictions obtained for the year 2007 by using exponential smoothing models (automatically selected) to fit both the original and the seasonally adjusted time series.

 
 

The models were fitted by using the ets function of the forecast R package.

4) ARIMA, SARIMA

As for exponential smoothing, also ARIMA models are among the most widely used approaches for time series forecasting. The name is an acronym for AutoRegressive Integrated Moving Average.

In an AutoRegressive model the forecasts correspond to a linear combination of past values of the variable. In a Moving Average model the forecasts correspond to a linear combination of past forecast errors.

Basically, the ARIMA models combine these two approaches. Since they require the time series to be stationary, differencing (Integrating) the time series may be a necessary step, i.e. considering the time series of the differences instead of the original one.

The SARIMA model (Seasonal ARIMA) extends the ARIMA by adding a linear combination of seasonal past values and/or forecast errors.

For a complete introduction to ARIMA and SARIMA models, click here.

The following plots show the predictions obtained for the year 2007 by using a SARIMA model and an ARIMA model on the seasonally adjusted time series.

 
 

The models were fitted by using the auto.arima and Arima functions of the forecast R package.

5) GARCH

The previous models assumed that the error terms in the stochastic processes generating the time series were heteroskedastic, i.e. with constant variance.

Instead, the GARCH model assumes that the variance of the error terms follows an AutoRegressive Moving Average (ARMA) process, therefore allowing it to change in time. It is particularly useful for modelling financial time series whose volatility changes across time. The name is an acronym for Generalised Autoregressive Conditional Heteroskedasticity.

Usually an ARMA process is assumed for the mean as well. For a complete introduction to GARCH models you can click here and here.

The following plots show the predictions obtained for the year 2007 by using a GARCH model to fit the seasonally adjusted time series.

 

The model was fitted by using the ugarchfitfunction of the rugarch R package.

6) Dynamic linear models

Dynamic linear models represent another class of models for time series forecasting. The idea is that at each time t these models correspond to a linear model, but the regression coefficients change in time. An example of dynamic linear model is given below.

y(t) = ⍺(t) + tβ(t) + w(t)

⍺(t) = ⍺(t-1) + m(t)

β(t) = β(t-1) + r(t)

w(t)~N(0,W) , m(t)~N(0,M) , r(t)~N(0,R)

In the previous model the coefficients ⍺(t) and β(t) follow a random walk process.

Dynamic linear models can be naturally modelled in a Bayesian framework; however maximum likelihood estimation techniques are still available. For a complete overview of dynamic linear models, click here.

The following plot shows the predictions obtained for the year 2007 by using a dynamic linear model to fit the seasonally adjusted time series. Due to heavy computational costs I had to keep the model extremely simple which resulted in poor forecasts.

 

The model was fitted by using the dlmMLEfunction of the dlm R package.

7) TBATS

The TBATS model is a forecasting model based on exponential smoothing. The name is an acronym for Trigonometric, Box-Cox transform, ARMA errors, Trend and Seasonal components.

The main feature of TBATS model is its capability to deal with multiple seasonalities by modelling each seasonality with a trigonometric representation based on Fourier series. A classic example of complex seasonality is given by daily observations of sales volumes which often have both weekly and yearly seasonality.

For a complete introduction of TBATS model, click here.

The following plot shows the predictions obtained for the year 2007 by using a TBATS model to fit the time series.

 

The model was fitted by using the tbats function of the forecast R package.

8) Prophet

Prophet is another forecasting model which allows to deal with multiple seasonalities. It is an open source software released by Facebook’s Core Data Science team.

The prophet model assumes that the the time series can be decomposed as follows:

y(t) = g(t) + s(t) + h(t) + ε(t)

The three terms g(t)s(t) and h(t) correspond respectively to trend, seasonality and holiday. The last term is the error term.

The model fitting is framed as a curve-fitting exercise, therefore it does not explicitly take into account the temporal dependence structure in the data. This also allows to have irregularly spaced observations.

There are two options for trend time series: a saturating growth model, and a piecewise linear model. The multi-period seasonality model relies on Fourier series. The effect of known and custom holydays can be easily incorporated into the model.

The prophet model is inserted in a Bayesian framework and it allows to make full posterior inference to include model parameter uncertainty in the forecast uncertainty.

For a complete introduction to Prophet model, click here.

The following plot shows the predictions obtained for the year 2007 by using a Prophet model to fit the time series.

 

The model was fitted by using the prophet function of the prophet R package.

9) NNETAR

The NNETAR model is a fully connected neural network. The acronym stands for Neural NETwork AutoRegression.

The NNETAR model takes in input the last elements of the sequence up to time t and outputs the forecasted value at time t+1. To perform multi-steps forecasts the network is applied iteratively.

In presence of seasonality, the input may include also the seasonally lagged time series. For a complete introduction to NNETAR models, click here.

The following plots show the predictions obtained for the year 2007 obtained by using a NNETAR model with seasonally lagged input and a NNETAR model on the seasonally adjusted time series.

 
 

The models were fitted by using the nnetar function of the forecast R package.

10) LSTM

LSTM models can be used to forecast time series (as well as other Recurrent Neural Networks). LSTM is an acronym that stands for Long-Short Term Memories.

The state of a LSTM network is represented through a state space vector. This technique allows to keep tracks of dependencies of new observations with past ones (even very far ones).

LSTMs may benefit from transfer learning techniques even when applied to standard time series, as shown here. However, they are mostly used with unstructured data (e.g. audio, text, video).

For a complete introduction on using LSTM to forecast time series, click here.

The following plot shows the predictions for the first year in the test set obtained by fitting a LSTM model on the seasonally adjusted time series.

 

The model was fitted by using the Keras framework in Python.

Evaluation

We performed model selection through the cross-validation procedure described previously. We didn’t compute it for dynamic linear models and LSTM models due to their high computational cost and poor performance.

In the following picture we show the cross-validated MAE for each model and for each time horizon.

 

We can see that, for time horizons greater than 4, the NNETAR model on the seasonally adjusted data performed better than the others. Let’s check the overall MAE computed by averaging over different time horizons.

 
Cross-validated MAE

The NNETAR model on the seasonally adjusted data was the best model for this application since it corresponded to the lowest cross-validated MAE.

To get an unbiased estimation of the best model performance, we computed the MAE on the test set, obtaining an estimate equal to 5,24. In the following picture we can see the MAE estimated on the test set for each time horizon.

 

How to further improve performance

Other techniques to increase models performance could be:

  • Using different models for different time horizons
  • Combining multiple forecasts (e.g. considering the average prediction)
  • Bootstrap Aggregating

The last technique can be summarised as follows:

  1. Decompose the original time series (e.g. by using STL)
  2. Generate a set of similar time series by randomly shuffling chunks of the Remainder component
  3. Fit a model on each time series
  4. Average forecasts of every model

For a complete introduction to Bootstrap Aggregating, click here.

Other models

Other models not included in this list are for instance:

  • Any standard regression model, taking time as input (and/or other features)
  • Encoder Decoder models, which are tipically used in NLP tasks (e.g. translation)
  • Wavenet and other Attention Networks, which are tipically applied to unstructured data (e.g. Text-to-Speech)

Final remarks

The goal of this project wasn’t to fit the best possible forecasting model for industrial production index, but to give an overview of forecasting models. In a real world application a lot of time should be spent on preprocessing, feature engineering and feature selection.

Most of the previously described models allow to easily incorporate time varying predictors. These could be extracted from the same time series or could correspond to external predictors (e.g. the time series of another index). In the latter case we should pay attention to not use information from the future, which could be satisfied by forecasting the predictors or by using their lagged versions.

 

转载于:https://www.cnblogs.com/wangxiaocvpr/p/11621303.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

An overview of time series forecasting models 的相关文章

  • 将日期(系列)列从一个 DataFrame 添加到其他 Pandas,Python

    我正在尝试将日期列从 df1 广播 到 df2 在 df1 中 我有所有用户的姓名及其基本信息 在 df2 中 我有一个用户购买的列表 df1 和 df2 代码 https i stack imgur com sN0uJ png 假设我有一
  • 使用Python查明时区当前是否处于夏令时[重复]

    这个问题在这里已经有答案了 我们有一个在 GMT 时间运行的服务器 我需要编写一个 Python 脚本来确定当前 此时此刻 加利福尼亚州洛杉矶是否为夏令时 DST 我怎样才能做到这一点 我查看了 pytz 和 time 但我无法弄清楚 我意
  • CLOCKS_PER_SEC 与 std::clock() 的结果不匹配

    我正在使用以下短程序来测试std clock include
  • 在 C++20 中获取当前日期/时间是线程安全的吗?

    简短的问题 在 C 17 之前 包括 C 17 C 不提供线程安全的方式来获取当前时间或日期 这个问题会在 C 20 中修复吗 长问题 获取当前时间和日期的唯一可移植方法是使用 std gmtime 或 std localtime 函数 这
  • 除了在断点处停止之外,如何测量一大块代码的时间?

    我正在 Windows 上开发 C 游戏 模拟 图形应用程序 编辑开始 如果重要的话 我正在使用 Visual Studio 2013 编辑完 Setup 我正在使用 QueryPerformanceCounter 测量从一帧到下一帧的时间
  • 在C中将毫秒转换为秒

    简单的C问题 如何正确 简洁地将毫秒转换为秒 有两个限制 我在这个微小的 C 编译器子集中没有浮点支持 我需要将秒四舍五入到最接近的秒 1 499ms 向下舍入 500 999ms 向上舍入 不需要关心负值 int mseconds 160
  • Time.valueOf 方法返回错误值

    我使用 Time valueOf 方法将字符串 09 00 00 转换为 Time 对象 如下所示 Time valueOf LocalTime parse 09 00 00 当我调用 getTime 来显示我得到的值时 28800000
  • 如何在 Go 中获取给定月份的第一个星期一?

    我正在尝试获取给定月份的第一个星期一 我能想到的最好方法是循环前 7 天 然后返回 Weekday Monday 有一个更好的方法吗 通过查看时间的 Weekday 您可以计算出第一个星期一 package main import fmt
  • 在Excel中显示毫秒

    我正在尝试在 Excel 宏中显示毫秒 我有一列整数 它们是以毫秒为单位的时间戳 例如 28095200 是上午 7 48 15 200 我想在它旁边创建一个新列 以保持运行平均值并以hh mm ss 000格式 Dim Cel As Ra
  • 如何在数据库中保存未来(!)日期

    这个问题专门涉及未来的日期和时间 对于过去的值 UTC 无疑是首选 我想知道是否有人对拯救生命的 最佳 方法有建议futureMySQL 数据库中的日期和时间 或者就此而言一般来说 特别是在该列可以保存不同时区时间的情况下 考虑到时区规则可
  • 当x轴不连续时如何删除冗余日期时间 pandas DatetimeIndex

    我想绘制一个 pandas 系列 其索引是无数的 DatatimeIndex 我的代码如下 import matplotlib dates as mdates index pd DatetimeIndex 2000 01 01 00 00
  • jQuery 时间戳之前的时间?

    下面是一个非常好的 jQuery 插件 与他们在 SO 上使用的插件非常相似 对我来说问题是它用它来转换时间
  • 我使用 tm/mktime 是否错误,如果没有,有解决方法吗?

    我认为下面的程序应该输出从公元 1 年到 1970 年每年第一天到 1970 年的秒数 前面是time t在编译它的系统上 CHAR BIT是一个宏 所以我认为你不能只是复制编译后的可执行文件并假设它是正确的 尽管实际上一切都使用 8 位c
  • 使用python删除未访问的文件

    My django app解析用户上传的一些文件 用户上传的文件可能会在服务器中保留很长时间 而没有被应用程序解析 如果很多用户上传大量文件 这可能会增加大小文件 我需要删除应用程序最近未解析的那些文件 比如not accessed for
  • FBProphet:了解回归量对多元预测的影响

    请参阅此示例 因为我正在从事的项目非常相似 但有大约 8 个回归器而不是 2 个 我需要了解每个回归器如何影响预测模型 https towardsdatascience com forecast model tuning with addi
  • 如何在Python中测量时间?

    我想启动我的程序 测量程序启动的时间 然后等待几秒钟 按下按钮 K RIGHT 并测量按下按钮的时间 我正在使用 Pygame 来注册 Keydown 但在我下面的代码中它没有注册我的 Keydown 我在这里做错了什么 start tim
  • setTimeout - 将变量作为时间传递

    我有一个 setTimeout 我希望能够使用变量作为计时器 var that this var time this spawnTime setTimeout function time that SpawnCounter time 这似乎
  • R 根据事件更新值

    我最近发布了这个问题 该问题已经与我在笔记本电脑上本地使用的 Mysql 数据库相关 由于我在 Mysql 中没有找到问题的解决方案 其他人似乎也没有找到解决方案 所以我想再次发布它 但现在与 R 相关 我使用带有 RMysql 包的数据库
  • 如何将完整的日期格式拆分为日期和时间?

    我有很多格式为我的示例所示的字符串 我必须解析它们 我正在尝试确定今天是哪根弦 我的问题是 时间快到了 我只需要比较那个日期 接下来我想检查时间是否在 after 和 before 的两个时间戳 HH mm ss 之间 但存在问题 日期几乎
  • 如何从非英语字符串解析go中的月份

    我想将以下字符串解析为 go 中的日期 This item will be released on March 9 2014 我跟着this https stackoverflow com questions 14106541 go par

随机推荐