## Components of the time series

When analyzing a time series, three components are distinguished: trend, seasonality and noise. A trend is a general trend, seasonality, as the name implies, the effects of periodicity (day of the week, time of year, etc.) and, finally, noise are random factors.

In order to understand the difference between these three quantities, we will simulate the function of the distance from the earth to the moon. It is known that on average the moon every year it moves away by 4 cm - this is a trend, during the day the moon rotates around the earth and the distance fluctuates from ~362600 km to ~405400 km is seasonality. Noise is "random" factors, for example, the influence of other planets. If we draw the sum of these three graphs, then we get a time series - a function showing the change in distance from the earth to the moon in time.

**Chart 1.**Trend

**Graph 2.**Seasonality

**Graph 3.**Noise## Trend. Smoothing methods

Smoothing methods are necessary to remove noise from the time series. There are various ways of smoothing, the main ones are the moving average method and the exponential smoothing method.

### Moving average method

The idea of the moving average method is to shift the chart point by the average value of a certain interval. An odd number of sections are taken as an interval, for example, three - the previous, current and next periods, the average is found and is taken as a smoothed value:

S_{i}= Σ^{k}_{j=-k}(x_{i+j})/(2k+1)

This method has a problem: a random high or low value strongly affects the moving line. As weights were introduced to the solutions. Window functions are used for weight distribution, the main window functions are the Dirichlet window (rectangular function), B-splines, polynomials, sinusoidal and cosine:

**Graph 4.**Gann window for n=5 (cosine window)

**Graph 5.**Sinusoidal window for n=5

The disadvantages of using a moving average are the complexity of calculations and incorrect data at the ends of the chart.

Source data | Moving Average | Weighted moving average (sinusoidal window, n=5) | Weighted moving average (Gann window, n=5) |

800 | 875 | 283 | 0 |

949 | 902 | 490 | 400 |

958 | 997 | 548 | 475 |

1280 | 1109 | 634 | 479 |

1248 | 1214 | 710 | 640 |

1370 | 1463 | 780 | 624 |

1952 | 1430 | 908 | 685 |

1150 | 1666 | 933 | 976 |

2192 | 1560 | 1020 | 575 |

944 | 1607 | 918 | 1096 |

2140 | 2055 | 1002 | 472 |

2945 | 1917 | 1222 | 1070 |

1640 | 2232 | 1404 | 1473 |

2204 | 1936 | 1320 | 820 |

954 | 1617 | 1010 | 1102 |

1670 | 1959 | 923 | 477 |

3008 | 2284 | 1118 | 835 |

3503 | 2290 | 1666 | 1504 |

980 | 2410 | 1581 | 1752 |

2149 | 2211 | 1659 | 490 |

Table 1. Moving average smoothing |

**Chart 6.**Smoothing of the moving average. Red is the initial data, peach is the moving average, yellow and ochre are the moving average weighted by a sinusoidal window with n=7 and n=3, respectively

As can be seen from the graph, an increase in n gives a smoother function, thus leveling out smaller fluctuations in the time series.
Note that when smoothing, it does not matter whether the average graph matches the data graph or not,
the goal is to build ** the correct form**.

### Exponential smoothing method

The exponential smoothing method got its name because the smoothed function exponentially decreases the influence of the previous period with a certain sensitivity coefficient. The smoothed value is found as the difference between the previous valid value and calculated value:

D'_{t}= α·D_{t-1}+ (1-α)·D'_{t-1}

The sensitivity coefficient, α, is selected between 0 and 1, the value 0.3 is used as the basis. If there is a sufficient sample, then the coefficient is selected by optimization.

Source data | Exponential smoothing, α=0.1 | Exponential smoothing, α=0.6 | |

800 | 800 | 800 | |

949 | -640 | 160 | |

958 | 671 | 505 | |

1280 | -508 | 373 | |

1248 | 585 | 619 | |

1370 | -402 | 501 | |

1952 | 499 | 622 | |

1150 | -254 | 922 | |

2192 | 344 | 321 | |

944 | -90 | 1187 | |

2140 | 175 | 92 | |

2945 | 57 | 1247 | |

1640 | 243 | 1268 | |

2204 | -55 | 477 | |

954 | 270 | 1132 | |

1670 | -148 | 120 | |

3008 | 300 | 954 | |

3503 | 31 | 1423 | |

980 | 322 | 1533 | |

2149 | -192 | -25 | |

Table 2. Exponential smoothing |

**Graph 7.**Exponential smoothing with α=0.1 (peach line) and α=0.6 (yellow line)## Forecasting methods

Forecasting methods are based on identifying trends in a time series and then using the found value to predict future values. In forecasting methods, trend and seasonality are distinguished, in general, all types of seasonality can be found by successive iterations. For example, when analyzing data for a year, you can highlight the seasonality of the time of year, and in the remaining the trend is to find seasonality by days of the week and so on.

### Double exponential smoothing

Double exponential smoothing produces a smoothed level and trend value.

Attention! There may be confusion, the Holt-Winters method differs in terms: trend, seasonality and noise
**the level, trend and seasonality are called respectively**.

Smooth - smoothing, smoothed level for the period τ, s_{τ}, depends on the value of the level for the current period (D_{τ}), the trend for the previous period (t_{τ-1}) and the calculated smoothed value for the previous period (s_{τ-1}):

s_{τ}= αD_{τ}+ (1 - α)(s_{τ-1}+ t_{τ-1})

Trend - trend, trend for the period τ, t_{τ}, depends on the calculated smoothed value for the previous and current periods (s_{τ}and s_{τ-1}) and from the previous trend:

t_{τ}= β(s_{τ}-s_{τ-1}) + (1-β)t_{τ-1}

The level and trend calculated according to these formulas can be used in forecasting:

D'_{τ+h}= s_{τ}+ h·t_{τ}

When calculating, the values of s and t for the first period are assigned s_{1} = D_{1} and t=0

**Graph 8.**Data (peach line), exponential smoothing - level (yellow line), trend (ochre line) and forecast (black line) by exponential smoothing### Holt-Winters method

The Holt-Winters method includes a seasonal component, i.e. periodicity. There are two varieties of the method - multiplicative and additive. Unlike double exponential smoothing, the Holt-Winters method also studies the influence of periodicity.

The general idea of finding the values of the smoothed level, trend and periodicity is as follows: the smoothed level (s - smooth, sometimes l - level is used)
is the base level of values, the trend (t - trend) is an indicator of the growth rate, the difference between the smoothed values of the current and previous period.
To study the periodicity (p - period), we divide the data into periods of size *k* and highlight the influence of each element (1,2,...,k) of the period on
smoothed level.

For more accurate calculations, a feedback indicator is introduced.

In a general sense, feedback is the influence of previous values for new ones: for example, when you start talking, you adjust the volume of your voice depending on what your ears hear - this is feedback.

To start the calculations, the values of s, t and k, in the simplest form, can be selected as s_{τ} = D_{τ}, t = 0, p = 0.

k is the length of the selected period:

s_{τ}= α(D_{τ}- p_{τ-k}) + (1 - α)(s_{τ-1}+ t_{τ-1})

Adjusted for previous values of t_{τ-k}(feedback)

t_{τ}= β(s_{τ}-s_{τ-1}) + (1-β)t_{τ-1}

Adjusted for previous values of p_{τ-k}(feedback)

p_{τ}= γ(D_{τ}- s_{τ}) + (1-γ)p_{τ-k}

The following formula is used for forecasting:

x_{τ+h}= D_{τ}+ ht_{τ}+ p_{τ-k+h}

### Holt-Winters multiplicative method

The multiplicative method differs from the additive one in that the parameters affecting the periodicity and the smoothed level calculated by the ratio:

p_{τ}= γ(D_{τ}/s_{τ}) + (1-γ)p_{τ-k}

s_{τ}= α(D_{τ}/p_{τ-k}) + (1 - α)(s_{τ-1}+ t_{τ-1})

t_{τ}= β(s_{τ}-s_{τ-1}) + (1-β)t_{τ-1}

The following formula is used for forecasting:

x_{τ+h}= (D_{τ}+ ht_{τ})p_{τ-k+h}

### Holt-Winters method in excel

Table for downloading in formats ods and xls.

## Prediction quality

Checking the quality of forecasting is possible if there is a sufficient sample and is an important test for reliability to check and optimize the values of α, β and γ, it is necessary to build a forecast on existing data, for example, if we have data for five years and we want to predict the next year, then it is necessary to build a model on the first four years, check and optimize the coefficients to minimize the error between forecast and data for the 5th year. After optimization the model can be rebuilt taking into account the last period to improve accuracy, followed by the construction of a forecast.

Optimization methods will be described in a separate article, below is an example of forecasting by the Holt Winters method.

**Graph 9.**Site traffic data for four weeks# | Data | s | t | p | s | t | p |
---|---|---|---|---|---|---|---|

1 | 93 | 93 | 0 | 0 | 93 | 0 | 0 |

2 | 91 | 92 | -0.1 | -0.5 | 92 | -0.1 | 0.99 |

3 | 72 | 84 | -0.89 | -6 | 84 | -0.89 | 0.93 |

4 | 75 | 80 | -1.2 | -2.5 | 80 | -1.2 | 0.97 |

5 | 75 | 77 | -1.38 | -1 | 77 | -1.38 | 0.99 |

6 | 57 | 68 | -2.14 | -5.5 | 68 | -2.14 | 0.92 |

7 | 66 | 66 | -2.13 | 0 | 66 | -2.13 | 1 |

8 | 123 | 88 | 0.28 | 17.5 | 38 | -4.72 | 1.62 |

9 | 85 | 87 | 0.15 | -1.25 | 54 | -2.65 | 1.28 |

10 | 85 | 89 | 0.34 | -5 | 67 | -1.09 | 1.1 |

11 | 91 | 91 | 0.51 | -1.25 | 77 | 0.02 | 1.08 |

12 | 102 | 96 | 0.96 | 2.5 | 87 | 1.02 | 1.08 |

13 | 73 | 90 | 0.26 | -11.25 | 85 | 0.72 | 0.89 |

14 | 60 | 78 | -0.97 | -9 | 75 | -0.35 | 0.9 |

15 | 99 | 79 | -0.77 | 18.75 | 69 | -0.92 | 1.53 |

16 | 108 | 91 | 0.51 | 7.88 | 75 | -0.23 | 1.36 |

17 | 98 | 96 | 0.96 | -1.5 | 80 | 0.29 | 1.16 |

18 | 104 | 100 | 1.26 | 1.38 | 87 | 0.96 | 1.14 |

19 | 83 | 93 | 0.43 | -3.75 | 84 | 0.56 | 1.03 |

20 | 68 | 88 | -0.11 | -15.63 | 81 | 0.2 | 0.86 |

21 | 62 | 81 | -0.8 | -14 | 76 | -0.32 | 0.86 |

22 | 59 | 64 | -2.42 | 6.88 | 61 | -1.79 | 1.25 |

23 | 80 | 66 | -1.98 | 10.94 | 59 | -1.81 | 1.36 |

24 | 121 | 87 | 0.32 | 16.25 | 76 | 0.07 | 1.38 |

25 | 112 | 97 | 1.29 | 8.19 | 85 | 0.96 | 1.23 |

26 | 85 | 94 | 0.86 | -6.38 | 85 | 0.86 | 1.02 |

27 | 106 | 106 | 1.97 | -7.82 | 101 | 2.37 | 0.95 |

28 | 82 | 103 | 1.47 | -17.5 | 100 | 2.03 | 0.84 |

**Graph 9.**Example of predicting site traffic based on data for four weeks. The yellow line is the initial data, the red line is the forecast for the fifth a week based on the first four. The smoothed level line is filled in at α=0.4, β=0.1, γ=0.5