Table of Contents

Introduction

The Holt-Winter model, also known as the triple exponential smoothing method, is a forecasting technique widely used in time series analysis. It was developed by Charles Holt and Peter Winters in 1960 as an improvement on Holt’s double exponential smoothing method.

The Holt-Winter model is used to predict future values of a time series that exhibits a trend and seasonality. The model uses three smoothing parameters, one for estimating the trend, another for estimating the level or base level of the time series, and another for estimating seasonality. These parameters are called α, β and γ, respectively.

The Holt-Winter model is an extension of Holt’s double exponential smoothing method, which uses only two smoothing parameters to estimate the trend and base level of the time series. The Holt-Winter model improves the accuracy of the forecasts by adding a third smoothing parameter for seasonality.

One of the main advantages of the Holt-Winter model is that it is easy to implement and does not require a large amount of historical data to generate accurate predictions. Furthermore, the model is highly adaptable and can be customized to fit a wide variety of time series with seasonality.

However, the Holt-Winter model has some limitations. For example, the model assumes that the time series is stationary and that seasonality is constant. If the time series is not stationary or has non-constant seasonality, the Holt-Winter model may not be the most appropriate.

In general, the Holt-Winter model is a useful and widely used technique in time series analysis, especially when the series is expected to exhibit a constant trend and seasonality.

Holt-Winters Method

The Holt-Winters seasonal method comprises the forecast equation and three smoothing equations — one for the level t\ell_{t}, one for the trend btb_t, and one for the seasonal component sts_t , with corresponding smoothing parameters α\alpha , β\beta^* and γ\gamma. We use mm to denote the period of the seasonality, i.e., the number of seasons in a year. For example, for quarterly data m=4m=4, and for monthly data m=12m=12.

There are two variations to this method that differ in the nature of the seasonal component. The additive method is preferred when the seasonal variations are roughly constant through the series, while the multiplicative method is preferred when the seasonal variations are changing proportional to the level of the series. With the additive method, the seasonal component is expressed in absolute terms in the scale of the observed series, and in the level equation the series is seasonally adjusted by subtracting the seasonal component. Within each year, the seasonal component will add up to approximately zero. With the multiplicative method, the seasonal component is expressed in relative terms (percentages), and the series is seasonally adjusted by dividing through by the seasonal component. Within each year, the seasonal component will sum up to approximately mm.

Holt-Winters’ additive method

Holt-Winters’ additive method is a time series forecasting technique that extends the Holt-Winters’ method by incorporating an additive seasonality component. It is suitable for time series data that exhibit a seasonal pattern that changes over time.

The Holt-Winters’ additive method uses three smoothing parameters - alpha (α), beta (β), and gamma (γ) - to estimate the level, trend, and seasonal components of the time series. The alpha parameter controls the smoothing of the level component, the beta parameter controls the smoothing of the trend component, and the gamma parameter controls the smoothing of the additive seasonal component.

The forecasting process involves three steps: first, the level, trend, and seasonal components are estimated using the smoothing parameters and the historical data; second, these components are used to forecast future values of the time series; and third, the forecasted values are adjusted for the seasonal component using an additive factor.

One of the advantages of Holt-Winters’ additive method is that it can handle time series data with an additive seasonality component, which is common in many real-world applications. The method is also easy to implement and can be extended to handle time series data with changing seasonal patterns.

However, the method has some limitations. It assumes that the seasonality pattern is additive, which may not be the case for all time series. Additionally, the method requires a sufficient amount of historical data to accurately estimate the smoothing parameters and the seasonal component.

Overall, Holt-Winters’ additive method is a powerful and widely used forecasting technique that can be used to generate accurate predictions for time series data with an additive seasonality component. The method is easy to implement and can be extended to handle time series data with changing seasonal patterns.

The component form for the additive method is:

where kk is the integer part of (h1)/m(h-1)/m, which ensures that the estimates of the seasonal indices used for forecasting come from the final year of the sample. The level equation shows a weighted average between the seasonally adjusted observation (ytstm)(y_{t} - s_{t-m}) and the non-seasonal forecast (t1+bt1)(\ell_{t-1}+b_{t-1}) for time tt. The trend equation is identical to Holt’s linear method. The seasonal equation shows a weighted average between the current seasonal index, (ytt1bt1)(y_{t}-\ell_{t-1}-b_{t-1}), and the seasonal index of the same season last year (i.e., mm time periods ago).

The equation for the seasonal component is often expressed as

st=γ(ytt)+(1γ)stm.s_{t} = \gamma^* (y_{t}-\ell_{t})+ (1-\gamma^*)s_{t-m}.

If we substitute t\ell_{t} from the smoothing equation for the level of the component form above, we get

st=γ(1α)(ytt1bt1)+[1γ(1α)]stm,s_{t} = \gamma^*(1-\alpha) (y_{t}-\ell_{t-1}-b_{t-1})+ [1-\gamma^*(1-\alpha)]s_{t-m},

which is identical to the smoothing equation for the seasonal component we specify here, with γ=γ(1α)\gamma=\gamma^*(1-\alpha). The usual parameter restriction is 0γ10\le\gamma^*\le1, which translates to 0γ1α0\le\gamma\le 1-\alpha.

Holt-Winters’ multiplicative method

The Holt-Winters’ multiplicative method uses three smoothing parameters - alpha (α), beta (β), and gamma (γ) - to estimate the level, trend, and seasonal components of the time series. The alpha parameter controls the smoothing of the level component, the beta parameter controls the smoothing of the trend component, and the gamma parameter controls the smoothing of the multiplicative seasonal component.

The forecasting process involves three steps: first, the level, trend, and seasonal components are estimated using the smoothing parameters and the historical data; second, these components are used to forecast future values of the time series; and third, the forecasted values are adjusted for the seasonal component using a multiplicative factor.

One of the advantages of Holt-Winters’ multiplicative method is that it can handle time series data with a multiplicative seasonality component, which is common in many real-world applications. The method is also easy to implement and can be extended to handle time series data with changing seasonal patterns.

However, the method has some limitations. It assumes that the seasonality pattern is multiplicative, which may not be the case for all time series. Additionally, the method requires a sufficient amount of historical data to accurately estimate the smoothing parameters and the seasonal component.

Overall, Holt-Winters’ multiplicative method is a powerful and widely used forecasting technique that can be used to generate accurate predictions for time series data with a multiplicative seasonality component. The method is easy to implement and can be extended to handle time series data with changing seasonal patterns.

In the multiplicative version, the seasonality averages to one. Use the multiplicative method if the seasonal variation increases with the level.

Mathematical models in the ETS taxonomy

I hope that it becomes more apparent to the reader how the ETS framework is built upon the idea of time series decomposition. By introducing different components, defining their types, and adding the equations for their update, we can construct models that would work better in capturing the key features of the time series. But we should also consider the potential change in components over time. The “transition” or “state” equations are supposed to reflect this change: they explain how the level, trend or seasonal components evolve.

As discussed in Section 2.2, given different types of components and their interactions, we end up with 30 models in the taxonomy. Tables 1 and 2 summarise mathematically all 30 ETS models shown graphically on Figures 1 and 2, presenting formulae for measurement and transition equations.

Table 1: Additive error ETS models

NonseasonalAdditiveMultiplicative
No trendyt=lt1+ϵtlt=lt1+αϵt\begin{aligned} &y_{t} = l_{t-1} + \epsilon_t \\ &l_t = l_{t-1} + \alpha \epsilon_t \end{aligned}yt=lt1+stm+ϵtlt=lt1+αϵtst=stm+γϵt\begin{aligned} &y_{t} = l_{t-1} + s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + \alpha \epsilon_t \\ &s_t = s_{t-m} + \gamma \epsilon_t \end{aligned}yt=lt1stm+ϵtlt=lt1+αϵtstmst=stm+γϵtlt1\begin{aligned} &y_{t} = l_{t-1} s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + \alpha \frac{\epsilon_t}{s_{t-m}} \\ &s_t = s_{t-m} + \gamma \frac{\epsilon_t}{l_{t-1}} \end{aligned}
Additiveyt=lt1+bt1+ϵtlt=lt1+bt1+αϵtbt=bt1+βϵt\begin{aligned} &y_{t} = l_{t-1} + b_{t-1} + \epsilon_t \\ &l_t = l_{t-1} + b_{t-1} + \alpha \epsilon_t \\ &b_t = b_{t-1} + \beta \epsilon_t \end{aligned}yt=lt1+bt1+stm+ϵtlt=lt1+bt1+αϵtbt=bt1+βϵtst=stm+γϵt\begin{aligned} &y_{t} = l_{t-1} + b_{t-1} + s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + b_{t-1} + \alpha \epsilon_t \\ &b_t = b_{t-1} + \beta \epsilon_t \\ &s_t = s_{t-m} + \gamma \epsilon_t \end{aligned}yt=(lt1+bt1)stm+ϵtlt=lt1+bt1+αϵtstmbt=bt1+βϵtstmst=stm+γϵtlt1+bt1\begin{aligned} &y_{t} = (l_{t-1} + b_{t-1}) s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + b_{t-1} + \alpha \frac{\epsilon_t}{s_{t-m}} \\ &b_t = b_{t-1} + \beta \frac{\epsilon_t}{s_{t-m}} \\ &s_t = s_{t-m} + \gamma \frac{\epsilon_t}{l_{t-1} + b_{t-1}} \end{aligned}
Additive dampedyt=lt1+ϕbt1+ϵtlt=lt1+ϕbt1+αϵtbt=ϕbt1+βϵt\begin{aligned} &y_{t} = l_{t-1} + \phi b_{t-1} + \epsilon_t \\ &l_t = l_{t-1} + \phi b_{t-1} + \alpha \epsilon_t \\ &b_t = \phi b_{t-1} + \beta \epsilon_t \end{aligned}yt=lt1+ϕbt1+stm+ϵtlt=lt1+ϕbt1+αϵtbt=ϕbt1+βϵtst=stm+γϵt\begin{aligned} &y_{t} = l_{t-1} + \phi b_{t-1} + s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + \phi b_{t-1} + \alpha \epsilon_t \\ &b_t = \phi b_{t-1} + \beta \epsilon_t \\ &s_t = s_{t-m} + \gamma \epsilon_t \end{aligned}yt=(lt1+ϕbt1)stm+ϵtlt=lt1+ϕbt1+αϵtstmbt=ϕbt1+βϵtstmst=stm+γϵtlt1+ϕbt1\begin{aligned} &y_{t} = (l_{t-1} + \phi b_{t-1}) s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} + \phi b_{t-1} + \alpha \frac{\epsilon_t}{s_{t-m}} \\ &b_t = \phi b_{t-1} + \beta \frac{\epsilon_t}{s_{t-m}} \\ &s_t = s_{t-m} + \gamma \frac{\epsilon_t}{l_{t-1} + \phi b_{t-1}} \end{aligned}
Multiplicativeyt=lt1bt1+ϵtlt=lt1bt1+αϵtbt=bt1+βϵtlt1\begin{aligned} &y_{t} = l_{t-1} b_{t-1} + \epsilon_t \\ &l_t = l_{t-1} b_{t-1} + \alpha \epsilon_t \\ &b_t = b_{t-1} + \beta \frac{\epsilon_t}{l_{t-1}} \end{aligned}yt=lt1bt1+stm+ϵtlt=lt1bt1+αϵtbt=bt1+βϵtlt1st=stm+γϵt\begin{aligned} &y_{t} = l_{t-1} b_{t-1} + s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} b_{t-1} + \alpha \epsilon_t \\ &b_t = b_{t-1} + \beta \frac{\epsilon_t}{l_{t-1}} \\ &s_t = s_{t-m} + \gamma \epsilon_t \end{aligned}yt=lt1bt1stm+ϵtlt=lt1bt1+αϵtstmbt=bt1+βϵtlt1stmst=stm+γϵtlt1bt1\begin{aligned} &y_{t} = l_{t-1} b_{t-1} s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} b_{t-1} + \alpha \frac{\epsilon_t}{s_{t-m}} \\ &b_t = b_{t-1} + \beta \frac{\epsilon_t}{l_{t-1}s_{t-m}} \\ &s_t = s_{t-m} + \gamma \frac{\epsilon_t}{l_{t-1} b_{t-1}} \end{aligned}
Multiplicative dampedyt=lt1bt1ϕ+ϵtlt=lt1bt1ϕ+αϵtbt=bt1ϕ+βϵtlt1\begin{aligned} &y_{t} = l_{t-1} b_{t-1}^\phi + \epsilon_t \\ &l_t = l_{t-1} b_{t-1}^\phi + \alpha \epsilon_t \\ &b_t = b_{t-1}^\phi + \beta \frac{\epsilon_t}{l_{t-1}} \end{aligned}yt=lt1bt1ϕ+stm+ϵtlt=lt1bt1ϕ+αϵtbt=bt1ϕ+βϵtlt1st=stm+γϵt\begin{aligned} &y_{t} = l_{t-1} b_{t-1}^\phi + s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} b_{t-1}^\phi + \alpha \epsilon_t \\ &b_t = b_{t-1}^\phi + \beta \frac{\epsilon_t}{l_{t-1}} \\ &s_t = s_{t-m} + \gamma \epsilon_t \end{aligned}yt=lt1bt1ϕstm+ϵtlt=lt1bt1ϕ+αϵtstmbt=bt1ϕ+βϵtlt1stmst=stm+γϵtlt1bt1\begin{aligned} &y_{t} = l_{t-1} b_{t-1}^\phi s_{t-m} + \epsilon_t \\ &l_t = l_{t-1} b_{t-1}^\phi + \alpha \frac{\epsilon_t}{s_{t-m}} \\ &b_t = b_{t-1}^\phi + \beta \frac{\epsilon_t}{l_{t-1}s_{t-m}} \\ &s_t = s_{t-m} + \gamma \frac{\epsilon_t}{l_{t-1} b_{t-1}} \end{aligned}

Table 2: Multiplicative error ETS models

NonseasonalAdditiveMultiplicative
No trendyt=lt1(1+ϵt)lt=lt1(1+αϵt)\begin{aligned} &y_{t} = l_{t-1}(1 + \epsilon_t) \\ &l_t = l_{t-1}(1 + \alpha \epsilon_t) \end{aligned}yt=(lt1+stm)(1+ϵt)lt=lt1+αμy,tϵtst=stm+γμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} + s_{t-m})(1 + \epsilon_t) \\ &l_t = l_{t-1} + \alpha \mu_{y,t} \epsilon_t \\ &s_t = s_{t-m} + \gamma \mu_{y,t} \epsilon_t \end{aligned}yt=lt1stm(1+ϵt)lt=lt1(1+αϵt)st=stm(1+γϵt)\begin{aligned} &y_{t} = l_{t-1} s_{t-m}(1 + \epsilon_t) \\ &l_t = l_{t-1}(1 + \alpha \epsilon_t) \\ &s_t = s_{t-m}(1 + \gamma \epsilon_t) \end{aligned}
Additiveyt=(lt1+bt1)(1+ϵt)lt=(lt1+bt1)(1+αϵt)bt=bt1+βμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} + b_{t-1})(1 + \epsilon_t) \\ &l_t = (l_{t-1} + b_{t-1})(1 + \alpha \epsilon_t) \\ &b_t = b_{t-1} + \beta \mu_{y,t} \epsilon_t \end{aligned}yt=(lt1+bt1+stm)(1+ϵt)lt=lt1+bt1+αμy,tϵtbt=bt1+βμy,tϵtst=stm+γμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} + b_{t-1} + s_{t-m})(1 + \epsilon_t) \\ &l_t = l_{t-1} + b_{t-1} + \alpha \mu_{y,t} \epsilon_t \\ &b_t = b_{t-1} + \beta \mu_{y,t} \epsilon_t \\ &s_t = s_{t-m} + \gamma \mu_{y,t} \epsilon_t \end{aligned}yt=(lt1+bt1)stm(1+ϵt)lt=(lt1+bt1)(1+αϵt)bt=bt1+β(lt1+bt1)ϵtst=stm(1+γϵt)\begin{aligned} &y_{t} = (l_{t-1} + b_{t-1}) s_{t-m}(1 + \epsilon_t) \\ &l_t = (l_{t-1} + b_{t-1})(1 + \alpha \epsilon_t) \\ &b_t = b_{t-1} + \beta (l_{t-1} + b_{t-1}) \epsilon_t \\ &s_t = s_{t-m} (1 + \gamma \epsilon_t) \end{aligned}
Additive dampedyt=(lt1+ϕbt1)(1+ϵt)lt=(lt1+ϕbt1)(1+αϵt)bt=ϕbt1+βμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} + \phi b_{t-1})(1 + \epsilon_t) \\ &l_t = (l_{t-1} + \phi b_{t-1})(1 + \alpha \epsilon_t) \\ &b_t = \phi b_{t-1} + \beta \mu_{y,t} \epsilon_t \end{aligned}yt=(lt1+ϕbt1+stm)(1+ϵt)lt=lt1+ϕbt1+αμy,tϵtbt=ϕbt1+βμy,tϵtst=stm+γμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} + \phi b_{t-1} + s_{t-m})(1 + \epsilon_t) \\ &l_t = l_{t-1} + \phi b_{t-1} + \alpha \mu_{y,t} \epsilon_t \\ &b_t = \phi b_{t-1} + \beta \mu_{y,t} \epsilon_t \\ &s_t = s_{t-m} + \gamma \mu_{y,t} \epsilon_t \end{aligned}yt=(lt1+ϕbt1)stm(1+ϵt)lt=lt1+ϕbt1(1+αϵt)bt=ϕbt1+β(lt1+ϕbt1)ϵtst=stm(1+γϵt)\begin{aligned} &y_{t} = (l_{t-1} + \phi b_{t-1}) s_{t-m}(1 + \epsilon_t) \\ &l_t = l_{t-1} + \phi b_{t-1} (1 + \alpha \epsilon_t) \\ &b_t = \phi b_{t-1} + \beta (l_{t-1} + \phi b_{t-1}) \epsilon_t \\ &s_t = s_{t-m}(1 + \gamma \epsilon_t) \end{aligned}
Multiplicativeyt=lt1bt1(1+ϵt)lt=lt1bt1(1+αϵt)bt=bt1(1+βϵt)\begin{aligned} &y_{t} = l_{t-1} b_{t-1} (1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1} (1 + \alpha \epsilon_t) \\ &b_t = b_{t-1} (1 + \beta \epsilon_t) \end{aligned}yt=(lt1bt1+stm)(1+ϵt)lt=lt1bt1+αμy,tϵtbt=bt1+βμy,tlt1ϵtst=stm+γμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} b_{t-1} + s_{t-m})(1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1} + \alpha \mu_{y,t} \epsilon_t \\ &b_t = b_{t-1} + \beta \frac{\mu_{y,t}}{l_{t-1}} \epsilon_t \\ &s_t = s_{t-m} + \gamma \mu_{y,t} \epsilon_t \end{aligned}yt=lt1bt1stm(1+ϵt)lt=lt1bt1(1+αϵt)bt=bt1(1+βϵt)st=stm(1+γϵt)\begin{aligned} &y_{t} = l_{t-1} b_{t-1} s_{t-m} (1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1} (1 + \alpha \epsilon_t) \\ &b_t = b_{t-1} (1 + \beta \epsilon_t) \\ &s_t = s_{t-m} (1 + \gamma \epsilon_t) \end{aligned}
Multiplicative dampedyt=lt1bt1ϕ(1+ϵt)lt=lt1bt1ϕ(1+αϵt)bt=bt1ϕ(1+βϵt)\begin{aligned} &y_{t} = l_{t-1} b_{t-1}^\phi (1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1}^\phi (1 + \alpha \epsilon_t) \\ &b_t = b_{t-1}^\phi (1 + \beta \epsilon_t) \end{aligned}yt=(lt1bt1ϕ+stm)(1+ϵt)lt=lt1bt1ϕ+αμy,tϵtbt=bt1ϕ+βμy,tlt1ϵtst=stm+γμy,tϵt\begin{aligned} &y_{t} = (l_{t-1} b_{t-1}^\phi + s_{t-m})(1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1}^\phi + \alpha \mu_{y,t} \epsilon_t \\ &b_t = b_{t-1}^\phi + \beta \frac{\mu_{y,t}}{l_{t-1}} \epsilon_t \\ &s_t = s_{t-m} + \gamma \mu_{y,t} \epsilon_t \end{aligned}yt=lt1bt1ϕstm(1+ϵt)lt=lt1bt1ϕ(1+αϵt)bt=bt1ϕ(1+βϵt)st=stm(1+γϵt)\begin{aligned} &y_{t} = l_{t-1} b_{t-1}^\phi s_{t-m} (1 + \epsilon_t) \\ &l_t = l_{t-1} b_{t-1}^\phi \left(1 + \alpha \epsilon_t\right) \\ &b_t = b_{t-1}^\phi \left(1 + \beta \epsilon_t\right) \\ &s_t = s_{t-m} \left(1 + \gamma \epsilon_t\right) \end{aligned}

From a statistical point of view, formulae in Tables 1 and 2 correspond to the “true models”, they explain the models underlying potential data, but when it comes to their construction and estimation, the ϵt\epsilon_t is substituted by the estimated ete_t (which is calculated differently depending on the error type), and time series components and smoothing parameters are also replaced by their estimates (e.g. α^\hat \alpha instead of α\alpha). However, if the values of these models’ parameters were known, it would be possible to produce point forecasts and conditional h steps ahead expectations from these models.

Model selection

A great advantage of the Holt Winters statistical framework is that information criteria can be used for model selection. The AIC, AIC_c and BIC, can be used here to determine which of the Holt Winters models is most appropriate for a given time series.

For Holt Winters models, Akaike’s Information Criterion (AIC) is defined as

AIC=2log(L)+2k,\text{AIC} = -2\log(L) + 2k,

where LL is the likelihood of the model and kk is the total number of parameters and initial states that have been estimated (including the residual variance).

The AIC corrected for small sample bias (AIC_c) is defined as

AICc=AIC+2k(k+1)Tk1AIC_c = AIC + \frac{2k(k+1)}{T-k-1}

and the Bayesian Information Criterion (BIC) is

BIC=AIC+k[log(T)2]\text{BIC} = \text{AIC} + k[\log(T)-2]

Three of the combinations of (Error, Trend, Seasonal) can lead to numerical difficulties. Specifically, the models that can cause such instabilities are ETS(A,N,M), ETS(A,A,M), and ETS(A,Ad,M), due to division by values potentially close to zero in the state equations. We normally do not consider these particular combinations when selecting a model.

Models with multiplicative errors are useful when the data are strictly positive, but are not numerically stable when the data contain zeros or negative values. Therefore, multiplicative error models will not be considered if the time series is not strictly positive. In that case, only the six fully additive models will be applied.

Loading libraries and data

Tip

Statsforecast will be needed. To install, see instructions.

Next, we import plotting libraries and configure the plotting style.

import matplotlib.pyplot as plt
import seaborn as sns
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plt.style.use('grayscale') # fivethirtyeight  grayscale  classic
plt.rcParams['lines.linewidth'] = 1.5
dark_style = {
    'figure.facecolor': '#008080',  # #212946
    'axes.facecolor': '#008080',
    'savefig.facecolor': '#008080',
    'axes.grid': True,
    'axes.grid.which': 'both',
    'axes.spines.left': False,
    'axes.spines.right': False,
    'axes.spines.top': False,
    'axes.spines.bottom': False,
    'grid.color': '#000000',  #2A3459
    'grid.linewidth': '1',
    'text.color': '0.9',
    'axes.labelcolor': '0.9',
    'xtick.color': '0.9',
    'ytick.color': '0.9',
    'font.size': 12 }
plt.rcParams.update(dark_style)


from pylab import rcParams
rcParams['figure.figsize'] = (18,7)

Read Data

df=pd.read_csv("https://raw.githubusercontent.com/Naren8520/Serie-de-tiempo-con-Machine-Learning/main/Data/ads.csv")
df.head()
TimeAds
02017-09-13T00:00:0080115
12017-09-13T01:00:0079885
22017-09-13T02:00:0089325
32017-09-13T03:00:00101930
42017-09-13T04:00:00121630

The input to StatsForecast is always a data frame in long format with three columns: unique_id, ds and y:

  • The unique_id (string, int or category) represents an identifier for the series.

  • The ds (datestamp) column should be of a format expected by Pandas, ideally YYYY-MM-DD for a date or YYYY-MM-DD HH:MM:SS for a timestamp.

  • The y (numeric) represents the measurement we wish to forecast.

df["unique_id"]="1"
df.columns=["ds", "y", "unique_id"]
df.head()
dsyunique_id
02017-09-13T00:00:00801151
12017-09-13T01:00:00798851
22017-09-13T02:00:00893251
32017-09-13T03:00:001019301
42017-09-13T04:00:001216301
print(df.dtypes)
ds           object
y             int64
unique_id    object
dtype: object

We can see that our time variable (ds) is in an object format, we need to convert to a date format

df["ds"] = pd.to_datetime(df["ds"])

Explore Data with the plot method

Plot some series using the plot method from the StatsForecast class. This method prints a random series from the dataset and is useful for basic EDA.

from statsforecast import StatsForecast

StatsForecast.plot(df)

The Augmented Dickey-Fuller Test

An Augmented Dickey-Fuller (ADF) test is a type of statistical test that determines whether a unit root is present in time series data. Unit roots can cause unpredictable results in time series analysis. A null hypothesis is formed in the unit root test to determine how strongly time series data is affected by a trend. By accepting the null hypothesis, we accept the evidence that the time series data is not stationary. By rejecting the null hypothesis or accepting the alternative hypothesis, we accept the evidence that the time series data is generated by a stationary process. This process is also known as stationary trend. The values of the ADF test statistic are negative. Lower ADF values indicate a stronger rejection of the null hypothesis.

Augmented Dickey-Fuller Test is a common statistical test used to test whether a given time series is stationary or not. We can achieve this by defining the null and alternate hypothesis.

  • Null Hypothesis: Time Series is non-stationary. It gives a time-dependent trend.

  • Alternate Hypothesis: Time Series is stationary. In another term, the series doesn’t depend on time.

  • ADF or t Statistic < critical values: Reject the null hypothesis, time series is stationary.

  • ADF or t Statistic > critical values: Failed to reject the null hypothesis, time series is non-stationary.

from statsmodels.tsa.stattools import adfuller

def Augmented_Dickey_Fuller_Test_func(series , column_name):
    print (f'Dickey-Fuller test results for columns: {column_name}')
    dftest = adfuller(series, autolag='AIC')
    dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','No Lags Used','Number of observations used'])
    for key,value in dftest[4].items():
        dfoutput['Critical Value (%s)'%key] = value
    print (dfoutput)
    if dftest[1] <= 0.05:
        print("Conclusion:====>")
        print("Reject the null hypothesis")
        print("The data is stationary")
    else:
        print("Conclusion:====>")
        print("The null hypothesis cannot be rejected")
        print("The data is not stationary")
Augmented_Dickey_Fuller_Test_func(df["y"],'Ads')
Dickey-Fuller test results for columns: Ads
Test Statistic         -7.089634e+00
p-value                 4.444804e-10
No Lags Used            9.000000e+00
                            ...     
Critical Value (1%)    -3.462499e+00
Critical Value (5%)    -2.875675e+00
Critical Value (10%)   -2.574304e+00
Length: 7, dtype: float64
Conclusion:====>
Reject the null hypothesis
The data is stationary

Autocorrelation plots

Autocorrelation Function

Definition 1. Let {xt;1tn}\{x_t;1 ≤ t ≤ n\} be a time series sample of size n from {Xt}\{X_t\}. 1. xˉ=t=1nxtn\bar x = \sum_{t=1}^n \frac{x_t}{n} is called the sample mean of {Xt}\{X_t\}. 2. ck=t=1nk(xt+kxˉ)(xtxˉ)/nc_k =\sum_{t=1}^{n−k} (x_{t+k}- \bar x)(x_t−\bar x)/n is known as the sample autocovariance function of {Xt}\{X_t\}. 3. rk=ck/c0r_k = c_k /c_0 is said to be the sample autocorrelation function of {Xt}\{X_t\}.

Note the following remarks about this definition:

  • Like most literature, this guide uses ACF to denote the sample autocorrelation function as well as the autocorrelation function. What is denoted by ACF can easily be identified in context.

  • Clearly c0 is the sample variance of {Xt}\{X_t\}. Besides, r0=c0/c0=1r_0 = c_0/c_0 = 1 and for any integer k,rk1k, |r_k| ≤ 1.

  • When we compute the ACF of any sample series with a fixed length nn, we cannot put too much confidence in the values of rkr_k for large k’s, since fewer pairs of (xt+k,xt)(x_{t +k }, x_t ) are available for calculating rkr_k as kk is large. One rule of thumb is not to estimate rkr_k for k>n/3k > n/3, and another is n50,kn/4n ≥ 50, k ≤ n/4. In any case, it is always a good idea to be careful.

  • We also compute the ACF of a nonstationary time series sample by Definition 1. In this case, however, the ACF or rkr_k very slowly or hardly tapers off as kk increases.

  • Plotting the ACF (rk)(r_k) against lag kk is easy but very helpful in analyzing time series sample. Such an ACF plot is known as a correlogram.

  • If {Xt}\{X_t\} is stationary with E(Xt)=0E(X_t)=0 and ρk=0\rho_k =0 for all k0k \neq 0,thatis,itisa white noise series, then the sampling distribution of rkr_k is asymptotically normal with the mean 0 and the variance of 1/n1/n. Hence, there is about 95% chance that rkr_k falls in the interval [1.96/n,1.96/n][−1.96/\sqrt{n}, 1.96/\sqrt{n}].

Now we can give a summary that (1) if the time series plot of a time series clearly shows a trend or/and seasonality, it is surely nonstationary; (2) if the ACF rkr_k very slowly or hardly tapers off as lag kk increases, the time series should also be nonstationary.

fig, axs = plt.subplots(nrows=1, ncols=2)

plot_acf(df["y"],  lags=30, ax=axs[0],color="fuchsia")
axs[0].set_title("Autocorrelation");

plot_pacf(df["y"],  lags=30, ax=axs[1],color="lime")
axs[1].set_title('Partial Autocorrelation')

plt.show();

Decomposition of the time series

How to decompose a time series and why?

In time series analysis to forecast new values, it is very important to know past data. More formally, we can say that it is very important to know the patterns that values follow over time. There can be many reasons that cause our forecast values to fall in the wrong direction. Basically, a time series consists of four components. The variation of those components causes the change in the pattern of the time series. These components are:

  • Level: This is the primary value that averages over time.
  • Trend: The trend is the value that causes increasing or decreasing patterns in a time series.
  • Seasonality: This is a cyclical event that occurs in a time series for a short time and causes short-term increasing or decreasing patterns in a time series.
  • Residual/Noise: These are the random variations in the time series.

Combining these components over time leads to the formation of a time series. Most time series consist of level and noise/residual and trend or seasonality are optional values.

If seasonality and trend are part of the time series, then there will be effects on the forecast value. As the pattern of the forecasted time series may be different from the previous time series.

The combination of the components in time series can be of two types: * Additive * Multiplicative

Additive time series

If the components of the time series are added to make the time series. Then the time series is called the additive time series. By visualization, we can say that the time series is additive if the increasing or decreasing pattern of the time series is similar throughout the series. The mathematical function of any additive time series can be represented by: y(t)=level+Trend+seasonality+noisey(t) = level + Trend + seasonality + noise

Multiplicative time series

If the components of the time series are multiplicative together, then the time series is called a multiplicative time series. For visualization, if the time series is having exponential growth or decline with time, then the time series can be considered as the multiplicative time series. The mathematical function of the multiplicative time series can be represented as.

y(t)=LevelTrendseasonalityNoisey(t) = Level * Trend * seasonality * Noise

Additive

from statsmodels.tsa.seasonal import seasonal_decompose 
a = seasonal_decompose(df["y"], model = "additive", period=24)
a.plot();

Multiplicative

from statsmodels.tsa.seasonal import seasonal_decompose 
a = seasonal_decompose(df["y"], model = "Multiplicative", period=24)
a.plot();

Split the data into training and testing

Let’s divide our data into sets

  1. Data to train our Holt Winters Model.
  2. Data to test our model

For the test data we will use the last 30 hours to test and evaluate the performance of our model.

train = df[df.ds<='2017-09-20 17:00:00'] 
test = df[df.ds>'2017-09-20 17:00:00']
train.shape, test.shape
((186, 3), (30, 3))

Now let’s plot the training data and the test data.

sns.lineplot(train,x="ds", y="y", label="Train", linestyle="--",linewidth=2)
sns.lineplot(test, x="ds", y="y", label="Test", linewidth=2, color="yellow")
plt.title("Ads watched (hourly data)");
plt.show()

Implementation of Holt-Winters Method with StatsForecast

To also know more about the parameters of the functions of the Holt-Winters Model, they are listed below. For more information, visit the documentation.

season_length : int
    Number of observations per unit of time. Ex: 12 Monthly data.
error_type : str
    The type of error of the ETS model. Can be additive (A) or multiplicative (M).
alias : str
    Custom name of the model.
prediction_intervals : Optional[ConformalIntervals]
    Information to compute conformal prediction intervals.
    By default, the model will compute the native prediction
    intervals.

Load libraries

from statsforecast import StatsForecast
from statsforecast.models import HoltWinters

Instantiating Model

Import and instantiate the models. Setting the argument is sometimes tricky. This article on Seasonal periods by the master, Rob Hyndmann, can be useful for season_length.

In this case we are going to test two alternatives of the model, one additive and one multiplicative.

season_length = 24 # Hourly data 
horizon = len(test) # number of predictions

models = [HoltWinters(season_length=season_length, error_type="A", alias="Add"),
          HoltWinters(season_length=season_length, error_type="M", alias="Multi")]

We fit the models by instantiating a new StatsForecast object with the following parameters:

models: a list of models. Select the models you want from models and import them.

  • freq: a string indicating the frequency of the data. (See panda’s available frequencies.)

  • n_jobs: n_jobs: int, number of jobs used in the parallel processing, use -1 for all cores.

  • fallback_model: a model to be used if a model fails.

Any settings are passed into the constructor. Then you call its fit method and pass in the historical data frame.

sf = StatsForecast(df=df,
                   models=models,
                   freq='H', 
                   n_jobs=-1)

Fit the Model

sf.fit()
StatsForecast(models=[Add,Multi])

Let’s see the results of our Holt Winters Model. We can observe it with the following instruction:

result=sf.fitted_[0,0].model_
print(result.keys())
print(result['fit'])
dict_keys(['loglik', 'aic', 'bic', 'aicc', 'mse', 'amse', 'fit', 'residuals', 'components', 'm', 'nstate', 'fitted', 'states', 'par', 'sigma2', 'n_params', 'method', 'actual_residuals'])
results(x=array([ 2.44373452e-02,  1.38129692e-03,  3.10806480e-02,  8.90103969e-01,
        1.24049460e+05, -8.68954810e+01, -3.89677433e+04, -2.62776804e+04,
       -1.53306670e+04,  1.04556014e+04,  3.40799540e+04,  3.61115645e+04,
        2.95135877e+04,  2.70191012e+04,  2.61439737e+04,  2.73344984e+04,
        2.70239387e+04,  2.94934401e+04,  2.12740663e+04,  4.02181550e+03,
       -7.08633837e+03, -1.29309441e+04, -1.23216723e+04, -9.32780505e+03,
       -3.92528658e+03, -1.18781713e+03, -2.43122964e+04, -3.56913114e+04,
       -4.32670579e+04]), fn=5078.452315148393, nit=1000, simplex=array([[ 2.40487996e-02,  1.40159178e-03,  2.98715521e-02,
         8.89367428e-01,  1.23345215e+05, -8.56751447e+01,
        -3.94103267e+04, -2.58132744e+04, -1.46476758e+04,
         1.04203903e+04,  3.50465498e+04,  3.59654863e+04,
         2.93863399e+04,  2.77527548e+04,  2.63918127e+04,
         2.74059591e+04,  2.59885977e+04,  2.94583381e+04,
         2.19996508e+04,  4.22020655e+03, -7.12092088e+03,
        -1.29452569e+04, -1.21734962e+04, -9.28051739e+03,
        -4.09832793e+03, -1.22159944e+03, -2.40047026e+04,
        -3.59264081e+04, -4.37141281e+04],
       [ 2.30584659e-02,  1.29870085e-03,  3.23295869e-02,
         8.95699018e-01,  1.23226757e+05, -8.51134029e+01,
        -3.94188347e+04, -2.66463805e+04, -1.46040706e+04,
         1.00508287e+04,  3.48121192e+04,  3.66574880e+04,
         2.95156329e+04,  2.75214239e+04,  2.61766232e+04,
         2.70638403e+04,  2.59821671e+04,  2.92220715e+04,
         2.15353216e+04,  4.11207780e+03, -7.26888692e+03,
        -1.31025981e+04, -1.24323871e+04, -9.46450899e+03,
        -3.87523173e+03, -1.29619824e+03, -2.48262400e+04,
        -3.65297643e+04, -4.26697834e+04],
       [ 2.38566990e-02,  1.38393698e-03,  2.86485845e-02,
         8.91732054e-01,  1.22667885e+05, -8.63064081e+01,
        -3.95610795e+04, -2.64459058e+04, -1.50401318e+04,
         1.03748608e+04,  3.46772905e+04,  3.61258281e+04,
         3.03028728e+04,  2.71476029e+04,  2.59766785e+04,
         2.83781299e+04,  2.62171593e+04,  2.87088506e+04,
         2.11399641e+04,  4.26251638e+03, -6.86374732e+03,
        -1.30572982e+04, -1.25961133e+04, -9.28515605e+03,
        -3.92065232e+03, -1.29918039e+03, -2.41322902e+04,
        -3.62733255e+04, -4.35070867e+04],
       [ 2.27798959e-02,  1.34604573e-03,  3.14497882e-02,
         8.97672321e-01,  1.23457441e+05, -8.39505753e+01,
        -3.84752143e+04, -2.70271535e+04, -1.51565030e+04,
         1.01558921e+04,  3.56374371e+04,  3.57659522e+04,
         2.92214649e+04,  2.64450530e+04,  2.59612798e+04,
         2.79127971e+04,  2.65311803e+04,  2.95053263e+04,
         2.20459941e+04,  4.27175853e+03, -7.11671275e+03,
        -1.28483284e+04, -1.20024908e+04, -9.14008142e+03,
        -3.94099609e+03, -1.27913818e+03, -2.43917933e+04,
        -3.59036544e+04, -4.37145499e+04],
       [ 2.29779847e-02,  1.35988450e-03,  3.20546039e-02,
         8.97392076e-01,  1.23999353e+05, -8.57416139e+01,
        -3.86796082e+04, -2.61112159e+04, -1.46410300e+04,
         1.00447411e+04,  3.46746838e+04,  3.56015924e+04,
         2.92653819e+04,  2.72512111e+04,  2.64957723e+04,
         2.82119577e+04,  2.61112530e+04,  2.90487409e+04,
         2.15387214e+04,  4.05550604e+03, -6.88742058e+03,
        -1.28509450e+04, -1.21550165e+04, -9.26733160e+03,
        -4.09280472e+03, -1.31561243e+03, -2.42237989e+04,
        -3.58787629e+04, -4.36947890e+04],
       [ 2.34931397e-02,  1.36066473e-03,  2.92309535e-02,
         8.94631252e-01,  1.23220366e+05, -7.99821563e+01,
        -3.97471338e+04, -2.62212988e+04, -1.54426040e+04,
         1.01804753e+04,  3.45628830e+04,  3.61783359e+04,
         2.83321227e+04,  2.75443313e+04,  2.65554748e+04,
         2.85866656e+04,  2.65994443e+04,  2.99352483e+04,
         2.12904274e+04,  4.19579939e+03, -7.04756866e+03,
        -1.35378259e+04, -1.21517302e+04, -9.38313255e+03,
        -3.93098135e+03, -1.23125948e+03, -2.43161758e+04,
        -3.56808651e+04, -4.30934906e+04],
       [ 2.34226303e-02,  1.36919062e-03,  3.05044515e-02,
         8.93651006e-01,  1.22119669e+05, -8.45830782e+01,
        -3.83859519e+04, -2.66545546e+04, -1.46663235e+04,
         1.03044558e+04,  3.37863355e+04,  3.58787384e+04,
         2.98614652e+04,  2.79521053e+04,  2.65483349e+04,
         2.80130243e+04,  2.67030580e+04,  2.95716350e+04,
         2.13867299e+04,  4.20532353e+03, -7.19231268e+03,
        -1.29869745e+04, -1.22214265e+04, -9.35200831e+03,
        -3.99601887e+03, -1.25191151e+03, -2.45210288e+04,
        -3.55315031e+04, -4.33561528e+04],
       [ 2.33686092e-02,  1.38777500e-03,  3.05842986e-02,
         8.95765424e-01,  1.24435621e+05, -8.44258777e+01,
        -3.95388846e+04, -2.70585507e+04, -1.42279741e+04,
         1.04108267e+04,  3.41935238e+04,  3.61927880e+04,
         2.96134192e+04,  2.72521230e+04,  2.63894944e+04,
         2.73577833e+04,  2.63681631e+04,  2.89873337e+04,
         2.13122122e+04,  4.27459659e+03, -7.10258507e+03,
        -1.26644787e+04, -1.22537168e+04, -9.49233627e+03,
        -4.01010478e+03, -1.23803020e+03, -2.39978907e+04,
        -3.60888141e+04, -4.29553133e+04],
       [ 2.34816233e-02,  1.37991019e-03,  3.06089532e-02,
         8.94874645e-01,  1.23617905e+05, -8.40833189e+01,
        -3.85116704e+04, -2.63905726e+04, -1.43692871e+04,
         1.05551899e+04,  3.51276038e+04,  3.58524187e+04,
         2.94603806e+04,  2.74703366e+04,  2.55213418e+04,
         2.83361549e+04,  2.60687975e+04,  2.86600785e+04,
         2.14422052e+04,  4.10932930e+03, -7.16315898e+03,
        -1.34079834e+04, -1.23877734e+04, -9.20876927e+03,
        -3.95489699e+03, -1.23349778e+03, -2.47612959e+04,
        -3.48066942e+04, -4.33211841e+04],
       [ 2.41575092e-02,  1.39907614e-03,  3.02357059e-02,
         8.91102368e-01,  1.23794366e+05, -8.48796907e+01,
        -3.90608848e+04, -2.64189926e+04, -1.45758290e+04,
         9.86006031e+03,  3.44455393e+04,  3.62279168e+04,
         2.93705791e+04,  2.75920864e+04,  2.50923293e+04,
         2.84262381e+04,  2.67984741e+04,  2.89640135e+04,
         2.16286886e+04,  4.29540433e+03, -7.25171917e+03,
        -1.29078390e+04, -1.19948534e+04, -9.29963339e+03,
        -3.99568567e+03, -1.24285574e+03, -2.43650156e+04,
        -3.65231527e+04, -4.28366363e+04],
       [ 2.36961761e-02,  1.38273987e-03,  3.10961396e-02,
         8.93478329e-01,  1.22419456e+05, -8.54817124e+01,
        -3.83003011e+04, -2.62799796e+04, -1.47684198e+04,
         1.03088809e+04,  3.52182251e+04,  3.52679730e+04,
         2.92136442e+04,  2.74002342e+04,  2.61909267e+04,
         2.78569566e+04,  2.68610704e+04,  2.89820187e+04,
         2.15006485e+04,  4.28929006e+03, -6.97610950e+03,
        -1.28970410e+04, -1.24884156e+04, -9.67725756e+03,
        -3.89959668e+03, -1.20865626e+03, -2.39696630e+04,
        -3.60623458e+04, -4.25044368e+04],
       [ 2.33721259e-02,  1.37576033e-03,  3.07287778e-02,
         8.95381214e-01,  1.23361917e+05, -8.59292902e+01,
        -3.97494538e+04, -2.59881823e+04, -1.42733538e+04,
         1.01850859e+04,  3.48288372e+04,  3.50586273e+04,
         2.97507897e+04,  2.71064540e+04,  2.67061201e+04,
         2.78353166e+04,  2.65264771e+04,  2.98277569e+04,
         2.14489843e+04,  4.23889638e+03, -7.08556474e+03,
        -1.31498142e+04, -1.22079975e+04, -9.28165029e+03,
        -3.89871487e+03, -1.25831398e+03, -2.48016766e+04,
        -3.59037067e+04, -4.20714772e+04],
       [ 2.26415137e-02,  1.34430820e-03,  3.23341885e-02,
         8.99285053e-01,  1.22878310e+05, -8.53335700e+01,
        -3.92562952e+04, -2.59721752e+04, -1.48816794e+04,
         1.00898476e+04,  3.50587088e+04,  3.62440964e+04,
         2.98438930e+04,  2.79605178e+04,  2.61637574e+04,
         2.76187001e+04,  2.60357145e+04,  2.90427564e+04,
         2.07810067e+04,  4.42036847e+03, -7.03406014e+03,
        -1.26056030e+04, -1.19217451e+04, -8.98726114e+03,
        -3.90645105e+03, -1.24678256e+03, -2.42136819e+04,
        -3.58509789e+04, -4.43751915e+04],
       [ 2.31801372e-02,  1.37749747e-03,  2.93541139e-02,
         8.95770558e-01,  1.22858350e+05, -8.80052223e+01,
        -3.86617013e+04, -2.59278016e+04, -1.47751488e+04,
         1.03042927e+04,  3.41708552e+04,  3.56988813e+04,
         2.91682837e+04,  2.67894217e+04,  2.58456196e+04,
         2.70119460e+04,  2.68253248e+04,  2.93127849e+04,
         2.12248716e+04,  4.22651962e+03, -7.12436510e+03,
        -1.29474406e+04, -1.21520227e+04, -9.53356225e+03,
        -3.96352288e+03, -1.27666223e+03, -2.44328890e+04,
        -3.56137046e+04, -4.35671367e+04],
       [ 2.30363008e-02,  1.34895700e-03,  3.31052656e-02,
         8.96994359e-01,  1.23002355e+05, -8.50280976e+01,
        -3.91939881e+04, -2.63829796e+04, -1.46326214e+04,
         1.04634298e+04,  3.45295261e+04,  3.54928661e+04,
         2.95432569e+04,  2.75345263e+04,  2.50916467e+04,
         2.68369024e+04,  2.68635715e+04,  3.03912706e+04,
         2.11714956e+04,  4.28329750e+03, -6.90261152e+03,
        -1.30645780e+04, -1.22326849e+04, -9.08641469e+03,
        -4.04851644e+03, -1.32494294e+03, -2.38092257e+04,
        -3.57905204e+04, -4.35116477e+04],
       [ 2.44373452e-02,  1.38129692e-03,  3.10806480e-02,
         8.90103969e-01,  1.24049460e+05, -8.68954810e+01,
        -3.89677433e+04, -2.62776804e+04, -1.53306670e+04,
         1.04556014e+04,  3.40799540e+04,  3.61115645e+04,
         2.95135877e+04,  2.70191012e+04,  2.61439737e+04,
         2.73344984e+04,  2.70239387e+04,  2.94934401e+04,
         2.12740663e+04,  4.02181550e+03, -7.08633837e+03,
        -1.29309441e+04, -1.23216723e+04, -9.32780505e+03,
        -3.92528658e+03, -1.18781713e+03, -2.43122964e+04,
        -3.56913114e+04, -4.32670579e+04],
       [ 2.34225937e-02,  1.37504026e-03,  3.20762187e-02,
         8.94715930e-01,  1.22649715e+05, -8.51815678e+01,
        -3.92349107e+04, -2.60372086e+04, -1.46713893e+04,
         1.03940388e+04,  3.42200364e+04,  3.54151977e+04,
         2.86168968e+04,  2.72219207e+04,  2.65021110e+04,
         2.77555263e+04,  2.61144978e+04,  2.88679128e+04,
         2.14504642e+04,  4.26955607e+03, -7.33178601e+03,
        -1.28672736e+04, -1.23366734e+04, -9.27407389e+03,
        -3.98684306e+03, -1.26689130e+03, -2.42014964e+04,
        -3.60431164e+04, -4.35735281e+04],
       [ 2.30245102e-02,  1.33633606e-03,  3.17433016e-02,
         8.95865886e-01,  1.23029482e+05, -8.29602756e+01,
        -3.96755895e+04, -2.63099175e+04, -1.48152395e+04,
         1.05185870e+04,  3.52063359e+04,  3.52312467e+04,
         2.99077028e+04,  2.71533181e+04,  2.66237164e+04,
         2.78798999e+04,  2.74977313e+04,  2.80935898e+04,
         2.09427481e+04,  4.09547938e+03, -7.09751151e+03,
        -1.29766286e+04, -1.18250113e+04, -9.54750290e+03,
        -3.97635927e+03, -1.32445291e+03, -2.43437156e+04,
        -3.60393575e+04, -4.30058401e+04],
       [ 2.35957957e-02,  1.31866210e-03,  3.04466963e-02,
         8.94704385e-01,  1.23506905e+05, -8.81033940e+01,
        -3.92910011e+04, -2.67861535e+04, -1.43723619e+04,
         1.02596397e+04,  3.42680222e+04,  3.49198652e+04,
         2.96607616e+04,  2.71335556e+04,  2.62749412e+04,
         2.78456008e+04,  2.67384571e+04,  2.90090597e+04,
         2.21391413e+04,  4.25656010e+03, -6.97489434e+03,
        -1.31391614e+04, -1.21060497e+04, -9.02606877e+03,
        -3.96765488e+03, -1.25601904e+03, -2.41311166e+04,
        -3.64834934e+04, -4.39441979e+04],
       [ 2.36751650e-02,  1.36543428e-03,  2.91691871e-02,
         8.94846111e-01,  1.22793505e+05, -8.69820381e+01,
        -3.90199341e+04, -2.60601993e+04, -1.47352545e+04,
         1.01516410e+04,  3.51794657e+04,  3.57287886e+04,
         2.98594202e+04,  2.80104436e+04,  2.60402555e+04,
         2.73922257e+04,  2.62342050e+04,  2.91820141e+04,
         2.17442081e+04,  4.06104487e+03, -7.27342325e+03,
        -1.29440707e+04, -1.20623302e+04, -9.39257599e+03,
        -4.11688432e+03, -1.22491978e+03, -2.42825476e+04,
        -3.61681582e+04, -4.31967736e+04],
       [ 2.36356407e-02,  1.40249143e-03,  3.11055579e-02,
         8.94345588e-01,  1.22524471e+05, -8.18301894e+01,
        -3.97067939e+04, -2.64168124e+04, -1.48209981e+04,
         1.02443748e+04,  3.46517106e+04,  3.60741202e+04,
         2.94602626e+04,  2.75042300e+04,  2.60176469e+04,
         2.71780405e+04,  2.62591460e+04,  2.90272777e+04,
         2.21503102e+04,  4.14004508e+03, -6.53769777e+03,
        -1.28541437e+04, -1.20646212e+04, -9.61019431e+03,
        -4.01591048e+03, -1.26480647e+03, -2.51893997e+04,
        -3.57918334e+04, -4.39872769e+04],
       [ 2.31857120e-02,  1.35188810e-03,  3.04128998e-02,
         8.96437230e-01,  1.23431237e+05, -8.47006111e+01,
        -3.99591747e+04, -2.66301164e+04, -1.49368574e+04,
         1.04096179e+04,  3.42782255e+04,  3.52664081e+04,
         2.93964291e+04,  2.71483335e+04,  2.55844710e+04,
         2.85473780e+04,  2.62439803e+04,  2.95137214e+04,
         2.16999982e+04,  4.21616087e+03, -7.13754628e+03,
        -1.20070878e+04, -1.24988683e+04, -9.28901448e+03,
        -3.95465776e+03, -1.31514929e+03, -2.43986957e+04,
        -3.54573986e+04, -4.29463528e+04],
       [ 2.37859054e-02,  1.39168374e-03,  3.09656129e-02,
         8.93949631e-01,  1.22688446e+05, -8.27726967e+01,
        -3.85019011e+04, -2.61638638e+04, -1.47580029e+04,
         1.02622252e+04,  3.44676069e+04,  3.60705301e+04,
         2.96185616e+04,  2.73176309e+04,  2.65596714e+04,
         2.75277449e+04,  2.62402989e+04,  2.92453590e+04,
         2.17648536e+04,  4.23176214e+03, -7.23546281e+03,
        -1.29448992e+04, -1.21012800e+04, -9.30023489e+03,
        -3.83220485e+03, -1.35293557e+03, -2.39553815e+04,
        -3.50787839e+04, -4.32983892e+04],
       [ 2.31755551e-02,  1.36972669e-03,  3.10073566e-02,
         8.95714770e-01,  1.23159605e+05, -8.46149210e+01,
        -3.94983330e+04, -2.62406849e+04, -1.47819273e+04,
         1.01373745e+04,  3.46650362e+04,  3.63296076e+04,
         2.87720185e+04,  2.77427498e+04,  2.67639279e+04,
         2.72905992e+04,  2.74381779e+04,  2.87693629e+04,
         2.18008833e+04,  4.23717627e+03, -7.10957534e+03,
        -1.29620482e+04, -1.25000167e+04, -9.03037734e+03,
        -3.96084115e+03, -1.27278527e+03, -2.43366281e+04,
        -3.52451140e+04, -4.32780284e+04],
       [ 2.36133672e-02,  1.35985554e-03,  3.11513958e-02,
         8.94275972e-01,  1.23447227e+05, -7.88721079e+01,
        -3.84923510e+04, -2.59996897e+04, -1.50010097e+04,
         1.02508651e+04,  3.47205430e+04,  3.55858360e+04,
         3.04894948e+04,  2.62117116e+04,  2.68062560e+04,
         2.76499265e+04,  2.64629471e+04,  2.93271473e+04,
         2.09505773e+04,  4.35292061e+03, -7.25190305e+03,
        -1.28526873e+04, -1.24771698e+04, -8.93110886e+03,
        -4.29429884e+03, -1.25765558e+03, -2.46098978e+04,
        -3.55435309e+04, -4.37079615e+04],
       [ 2.36439940e-02,  1.33889715e-03,  3.01748889e-02,
         8.94229932e-01,  1.23290474e+05, -8.80150842e+01,
        -3.88097928e+04, -2.63137034e+04, -1.44499856e+04,
         1.03428185e+04,  3.49849551e+04,  3.63993785e+04,
         2.87588452e+04,  2.74069044e+04,  2.61451007e+04,
         2.81270537e+04,  2.64494936e+04,  2.92261510e+04,
         2.06734305e+04,  4.38913406e+03, -6.90340334e+03,
        -1.27939382e+04, -1.19989815e+04, -9.20233589e+03,
        -4.06279150e+03, -1.35076768e+03, -2.50712735e+04,
        -3.61164361e+04, -4.28106292e+04],
       [ 2.38312121e-02,  1.35348442e-03,  3.11372755e-02,
         8.92879959e-01,  1.23435424e+05, -8.75236266e+01,
        -3.98877150e+04, -2.63840774e+04, -1.41741810e+04,
         1.00040238e+04,  3.50671919e+04,  3.62991187e+04,
         2.94645434e+04,  2.66796707e+04,  2.60886751e+04,
         2.78872141e+04,  2.65673983e+04,  2.94202012e+04,
         2.15059513e+04,  4.29327789e+03, -6.99260131e+03,
        -1.29350082e+04, -1.21356464e+04, -9.63679687e+03,
        -3.99062189e+03, -1.21073235e+03, -2.41469724e+04,
        -3.49995291e+04, -4.46667304e+04],
       [ 2.28820531e-02,  1.34709017e-03,  3.12930885e-02,
         8.98042023e-01,  1.23363231e+05, -8.40520054e+01,
        -3.88532804e+04, -2.57281498e+04, -1.43468660e+04,
         1.04202224e+04,  3.44137444e+04,  3.67956433e+04,
         2.94663774e+04,  2.69333507e+04,  2.59652714e+04,
         2.82039633e+04,  2.67333645e+04,  2.91868638e+04,
         2.16875935e+04,  4.18880730e+03, -7.00016622e+03,
        -1.28357717e+04, -1.21595618e+04, -9.33735990e+03,
        -3.90969568e+03, -1.27052486e+03, -2.42933318e+04,
        -3.69259218e+04, -4.41040622e+04],
       [ 2.38049951e-02,  1.39030825e-03,  3.06405290e-02,
         8.92994409e-01,  1.24289098e+05, -8.40140819e+01,
        -3.85337082e+04, -2.61684441e+04, -1.46993627e+04,
         1.01634568e+04,  3.47937273e+04,  3.51012725e+04,
         2.94226408e+04,  2.77672823e+04,  2.62946187e+04,
         2.75564784e+04,  2.68482111e+04,  2.92367476e+04,
         2.13296806e+04,  4.24587345e+03, -7.14882503e+03,
        -1.30628882e+04, -1.22409211e+04, -9.52582674e+03,
        -3.83384250e+03, -1.23631839e+03, -2.45485642e+04,
        -3.59539920e+04, -4.45739752e+04],
       [ 2.37778011e-02,  1.37778734e-03,  3.02599768e-02,
         8.92655976e-01,  1.22304944e+05, -8.16288959e+01,
        -3.89852188e+04, -2.65123655e+04, -1.43437856e+04,
         1.02586834e+04,  3.55133375e+04,  3.57277510e+04,
         2.93369637e+04,  2.69961210e+04,  2.64681386e+04,
         2.77244225e+04,  2.68049260e+04,  2.95848184e+04,
         2.10648643e+04,  4.03875969e+03, -7.09865270e+03,
        -1.28342462e+04, -1.23781572e+04, -9.07575255e+03,
        -4.05235581e+03, -1.28589801e+03, -2.43611569e+04,
        -3.65053195e+04, -4.43511137e+04]]))

Let us now visualize the fitted values of our models.

As we can see, the result obtained above has an output in a dictionary, to extract each element from the dictionary we are going to use the .get() function to extract the element and then we are going to save it in a pd.DataFrame().

residual=pd.DataFrame(result.get("residuals"), columns=["residual Model"])
residual
residual Model
0-2012.193015
1-699.563899
21246.127401
2131355.434583
2144926.937284
2152467.814647
import scipy.stats as stats

fig, axs = plt.subplots(nrows=2, ncols=2)

residual.plot(ax=axs[0,0])
axs[0,0].set_title("Residuals");

sns.distplot(residual, ax=axs[0,1]);
axs[0,1].set_title("Density plot - Residual");

stats.probplot(residual["residual Model"], dist="norm", plot=axs[1,0])
axs[1,0].set_title('Plot Q-Q')

plot_acf(residual,  lags=35, ax=axs[1,1],color="fuchsia")
axs[1,1].set_title("Autocorrelation");

plt.show();

Forecast Method

If you want to gain speed in productive settings where you have multiple series or models we recommend using the StatsForecast.forecast method instead of .fit and .predict.

The main difference is that the .forecast doest not store the fitted values and is highly scalable in distributed environments.

The forecast method takes two arguments: forecasts next h (horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 30 hours ahead.

  • level (list of floats): this optional parameter is used for probabilistic forecasting. Set the level (or confidence percentile) of your prediction interval. For example, level=[90] means that the model expects the real value to be inside that interval 90% of the times.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals. Depending on your computer, this step should take around 1min.

Y_hat = sf.forecast(horizon, fitted=True)
Y_hat
dsAddMulti
unique_id
12017-09-22 00:00:0075020.84375075016.414062
12017-09-22 01:00:0073714.43750074739.921875
12017-09-22 02:00:0081196.57031280907.437500
12017-09-23 03:00:0092962.23437592432.453125
12017-09-23 04:00:00115738.320312112928.710938
12017-09-23 05:00:00113103.835938111366.179688

With the forecast method we can also extract the fitted values from the model and visualize it graphically, with the following instruction we can do it.

values=sf.forecast_fitted_values()
values.head()
dsyAddMulti
unique_id
12017-09-13 00:00:0080115.082127.19531277955.531250
12017-09-13 01:00:0079885.080584.56250077934.929688
12017-09-13 02:00:0089325.088078.87500084123.835938
12017-09-13 03:00:00101930.099432.60156296152.687500
12017-09-13 04:00:00121630.0122571.562500117567.875000
StatsForecast.plot(values)

Adding 95% confidence interval with the forecast method

sf.forecast(h=horizon, level=[95])
dsAddAdd-lo-95Add-hi-95MultiMulti-lo-95Multi-hi-95
unique_id
12017-09-22 00:00:0075020.84375056622.95312593418.73437575016.41406262760.34375087272.476562
12017-09-22 01:00:0073714.43750055310.50390692118.36718874739.92187562483.85546986995.992188
12017-09-22 02:00:0081196.57031262786.07421999607.06250080907.43750068651.36718893163.500000
12017-09-23 03:00:0092962.23437574304.234375111620.24218892432.45312579563.328125105301.585938
12017-09-23 04:00:00115738.32031297069.117188134407.531250112928.710938100059.578125125797.835938
12017-09-23 05:00:00113103.83593894423.414062131784.265625111366.17968898497.054688124235.304688
Y_hat=Y_hat.reset_index()
Y_hat
unique_iddsAddMulti
012017-09-22 00:00:0075020.84375075016.414062
112017-09-22 01:00:0073714.43750074739.921875
212017-09-22 02:00:0081196.57031280907.437500
2712017-09-23 03:00:0092962.23437592432.453125
2812017-09-23 04:00:00115738.320312112928.710938
2912017-09-23 05:00:00113103.835938111366.179688
Y_hat1 = pd.concat([df,Y_hat],  keys=['unique_id', 'ds'])
Y_hat1
dsyunique_idAddMulti
unique_id02017-09-13 00:00:0080115.01NaNNaN
12017-09-13 01:00:0079885.01NaNNaN
22017-09-13 02:00:0089325.01NaNNaN
ds272017-09-23 03:00:00NaN192962.23437592432.453125
282017-09-23 04:00:00NaN1115738.320312112928.710938
292017-09-23 05:00:00NaN1113103.835938111366.179688
fig, ax = plt.subplots(1, 1)
plot_df = pd.concat([train, Y_hat1]).set_index('ds').tail(300)
plot_df['y'].plot(ax=ax, linewidth=2)
plot_df[ "Add"].plot(ax=ax, linewidth=2, color="yellow")
plot_df[ "Multi"].plot(ax=ax, linewidth=2, color="red")
ax.set_title(' Forecast', fontsize=22)
ax.set_ylabel('Ads watched (hourly data)', fontsize=20)
ax.set_xlabel('Monthly [t]', fontsize=20)
ax.legend(prop={'size': 15})
ax.grid(True)

Predict method with confidence interval

To generate forecasts use the predict method.

The predict method takes two arguments: forecasts the next h (for horizon) and level.

  • h (int): represents the forecast h steps into the future. In this case, 30 hours ahead.

  • level (list of floats): this optional parameter is used for probabilistic forecasting. Set the level (or confidence percentile) of your prediction interval. For example, level=[95] means that the model expects the real value to be inside that interval 95% of the times.

The forecast object here is a new data frame that includes a column with the name of the model and the y hat values, as well as columns for the uncertainty intervals.

This step should take less than 1 second.

sf.predict(h=horizon)
dsAddMulti
unique_id
12017-09-22 00:00:0075020.84375075016.414062
12017-09-22 01:00:0073714.43750074739.921875
12017-09-22 02:00:0081196.57031280907.437500
12017-09-23 03:00:0092962.23437592432.453125
12017-09-23 04:00:00115738.320312112928.710938
12017-09-23 05:00:00113103.835938111366.179688
forecast_df = sf.predict(h=horizon, level=[80,95]) 
forecast_df
dsAddAdd-lo-95Add-lo-80Add-hi-80Add-hi-95MultiMulti-lo-95Multi-lo-80Multi-hi-80Multi-hi-95
unique_id
12017-09-22 00:00:0075020.84375056622.95312562991.10937587050.57812593418.73437575016.41406262760.34375067002.60156283030.21875087272.476562
12017-09-22 01:00:0073714.43750055310.50390661680.75000085748.11718892118.36718874739.92187562483.85546966726.10937582753.73437586995.992188
12017-09-22 02:00:0081196.57031262786.07421969158.59375093234.54687599607.06250080907.43750068651.36718872893.62500088921.24218893163.500000
12017-09-23 03:00:0092962.23437574304.23437580762.421875105162.054688111620.24218892432.45312579563.32812584017.789062100847.125000105301.585938
12017-09-23 04:00:00115738.32031297069.117188103531.187500127945.460938134407.531250112928.710938100059.578125104514.039062121343.375000125797.835938
12017-09-23 05:00:00113103.83593894423.414062100889.359375125318.312500131784.265625111366.17968898497.054688102951.507812119780.851562124235.304688

We can join the forecast result with the historical data using the pandas function pd.concat(), and then be able to use this result for graphing.

pd.concat([df, forecast_df]).set_index('ds')
yunique_idAddAdd-lo-95Add-lo-80Add-hi-80Add-hi-95MultiMulti-lo-95Multi-lo-80Multi-hi-80Multi-hi-95
ds
2017-09-13 00:00:0080115.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-13 01:00:0079885.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-13 02:00:0089325.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-23 03:00:00NaNNaN92962.23437574304.23437580762.421875105162.054688111620.24218892432.45312579563.32812584017.789062100847.125000105301.585938
2017-09-23 04:00:00NaNNaN115738.32031297069.117188103531.187500127945.460938134407.531250112928.710938100059.578125104514.039062121343.375000125797.835938
2017-09-23 05:00:00NaNNaN113103.83593894423.414062100889.359375125318.312500131784.265625111366.17968898497.054688102951.507812119780.851562124235.304688
df_plot= pd.concat([df, forecast_df]).set_index('ds')
df_plot
yunique_idAddAdd-lo-95Add-lo-80Add-hi-80Add-hi-95MultiMulti-lo-95Multi-lo-80Multi-hi-80Multi-hi-95
ds
2017-09-13 00:00:0080115.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-13 01:00:0079885.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-13 02:00:0089325.01NaNNaNNaNNaNNaNNaNNaNNaNNaNNaN
2017-09-23 03:00:00NaNNaN92962.23437574304.23437580762.421875105162.054688111620.24218892432.45312579563.32812584017.789062100847.125000105301.585938
2017-09-23 04:00:00NaNNaN115738.32031297069.117188103531.187500127945.460938134407.531250112928.710938100059.578125104514.039062121343.375000125797.835938
2017-09-23 05:00:00NaNNaN113103.83593894423.414062100889.359375125318.312500131784.265625111366.17968898497.054688102951.507812119780.851562124235.304688

Now let’s visualize the result of our forecast and the historical data of our time series, also let’s draw the confidence interval that we have obtained when making the prediction with 95% confidence.

def plot_forecasts(y_hist, y_pred, models):
    _, ax = plt.subplots(1, 1, figsize = (20, 7))
    df_plot = pd.concat([y_hist, y_pred]).set_index('ds').tail(12*10)
    df_plot[['y'] + models].plot(ax=ax, linewidth=3 , )
    colors = ['black', "lime"]
    for model, color in zip(models, colors):
        ax.fill_between(df_plot.index, 
                        df_plot[f'{model}-lo-95'], 
                        df_plot[f'{model}-hi-95'],
                        alpha=.35,
                        color=color,
                        label=f'HoltWinter{model}-level-95')
    for model, color in zip(models, colors):
        ax.fill_between(df_plot.index, 
                        df_plot[f'{model}-lo-80'], 
                        df_plot[f'{model}-hi-80'],
                        alpha=.35,
                        color=color,
                        label=f'HoltWinter{model}-level-80')
    ax.set_title('', fontsize=22)
    ax.set_ylabel("Ads watched (hourly data)", fontsize=20)
    ax.set_xlabel('Hourly', fontsize=20)
    ax.legend(prop={'size': 20})
    ax.grid(True)
    plt.show()
plot_forecasts(df, forecast_df, models=["Add"])

plot_forecasts(df, forecast_df, models=["Multi"])

Let’s plot the same graph using the plot function that comes in Statsforecast, as shown below.

sf.plot(df, forecast_df)

Cross-validation

In previous steps, we’ve taken our historical data to predict the future. However, to asses its accuracy we would also like to know how the model would have performed in the past. To assess the accuracy and robustness of your models on your data perform Cross-Validation.

With time series data, Cross Validation is done by defining a sliding window across the historical data and predicting the period following it. This form of cross-validation allows us to arrive at a better estimation of our model’s predictive abilities across a wider range of temporal instances while also keeping the data in the training set contiguous as is required by our models.

The following graph depicts such a Cross Validation Strategy:

Perform time series cross-validation

Cross-validation of time series models is considered a best practice but most implementations are very slow. The statsforecast library implements cross-validation as a distributed operation, making the process less time-consuming to perform. If you have big datasets you can also perform Cross Validation in a distributed cluster using Ray, Dask or Spark.

In this case, we want to evaluate the performance of each model for the last 5 months (n_windows=), forecasting every second months (step_size=12). Depending on your computer, this step should take around 1 min.

The cross_validation method from the StatsForecast class takes the following arguments.

  • df: training data frame

  • h (int): represents h steps into the future that are being forecasted. In this case, 12 months ahead.

  • step_size (int): step size between each window. In other words: how often do you want to run the forecasting processes.

  • n_windows(int): number of windows used for cross validation. In other words: what number of forecasting processes in the past do you want to evaluate.

crossvalidation_df = sf.cross_validation(df=df,
                                         h=horizon,
                                         step_size=30,
                                         n_windows=3)

The crossvaldation_df object is a new data frame that includes the following columns:

  • unique_id: index. If you dont like working with index just run crossvalidation_df.resetindex().
  • ds: datestamp or temporal index
  • cutoff: the last datestamp or temporal index for the n_windows.
  • y: true value
  • model: columns with the model’s name and fitted value.
crossvalidation_df
dscutoffyAddMulti
unique_id
12017-09-18 06:00:002017-09-18 05:00:0099440.0134578.328125133820.109375
12017-09-18 07:00:002017-09-18 05:00:0097655.0133548.781250133734.000000
12017-09-18 08:00:002017-09-18 05:00:0097655.0134798.656250135216.046875
12017-09-21 21:00:002017-09-20 17:00:00103080.0103021.726562103086.851562
12017-09-21 22:00:002017-09-20 17:00:0095155.089544.05468890028.406250
12017-09-21 23:00:002017-09-20 17:00:0080285.078090.21093878823.953125

Model Evaluation

We can now compute the accuracy of the forecast using an appropiate accuracy metric. Here we’ll use the Root Mean Squared Error (RMSE). To do this, we first need to install datasetsforecast, a Python library developed by Nixtla that includes a function to compute the RMSE.

!pip install datasetsforecast

The function to compute the RMSE takes two arguments:

  1. The actual values.
  2. The forecasts, in this case, Holt Winters Model.
from datasetsforecast.losses import mse, mae, rmse

def evaluate_cross_validation(df, metric):
    models = df.drop(columns=['ds', 'cutoff', 'y']).columns.tolist()
    evals = []
    for model in models:
        eval_ = df.groupby(['unique_id', 'cutoff']).apply(lambda x: metric(x['y'].values, x[model].values)).to_frame() # Calculate loss for every unique_id, model and cutoff.
        eval_.columns = [model]
        evals.append(eval_)
    evals = pd.concat(evals, axis=1)
    evals = evals.groupby(['unique_id']).mean(numeric_only=True) # Averages the error metrics for all cutoffs for every combination of model and unique_id
    evals['best_model'] = evals.idxmin(axis=1)
    return evals
evaluation_df = evaluate_cross_validation(crossvalidation_df, rmse)

evaluation_df
AddMultibest_model
unique_id
112742.01953113012.641602Add

Acknowledgements

We would like to thank Naren Castellon for writing this tutorial.

References

  1. Changquan Huang • Alla Petukhina. Springer series (2022). Applied Time Series Analysis and Forecasting with Python.
  2. Ivan Svetunkov. Forecasting and Analytics with the Augmented Dynamic Adaptive Model (ADAM)
  3. James D. Hamilton. Time Series Analysis Princeton University Press, Princeton, New Jersey, 1st Edition, 1994.
  4. Nixtla Parameters.
  5. Pandas available frequencies.
  6. Rob J. Hyndman and George Athanasopoulos (2018). “Forecasting principles and practice, Time series cross-validation”..
  7. Seasonal periods- Rob J Hyndman.