k-tree
E-learning book

Time series analysis

In the examples in this article, data is generated every time the page loads. If you want to see an example with different values - reload the page.

online tool

.

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:

Si = Σkj=-k(xi+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 dataMoving AverageWeighted moving average (sinusoidal window, n=5)Weighted moving average (Gann window, n=5)
800 844 283 0
888 961 476 400
1196 1047 575 444
1304 1144 686 598
1188 1130 747 652
830 1133 674 594
1208 1250 631 415
1773 1477 762 604
2096 1521 1027 887
1007 1674 1015 1048
1820 1623 944 504
1570 1359 911 910
1040 1909 898 785
3205 2336 1104 520
3530 2800 1609 1603
3425 3260 2055 1765
2880 3292 1989 1713
3333 3370 1915 1440
3842 2989 2022 1667
1902 3026 2515 1921
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 = α·Dt-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 dataExponential smoothing, α=0.1Exponential smoothing, α=0.6
800 800 800
888 -640 160
1196 665 469
1304 -479 530
1188 562 570
830 -387 485
1208 431 304
1773 -267 603
2096 418 823
1007 -167 928
1820 251 233
1570 -44 999
1040 197 542
3205 -73 407
3530 386 1760
3425 6 1414
2880 337 1489
3333 -15 1132
3842 347 1547
1902 72 1686
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 s1 = D1 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 stp stp
193 9300 9300
291 92-0.1-0.5 92-0.10.99
372 84-0.89-6 84-0.890.93
475 80-1.2-2.5 80-1.20.97
575 77-1.38-1 77-1.380.99
657 68-2.14-5.5 68-2.140.92
766 66-2.130 66-2.131
8123 880.2817.5 38-4.721.62
985 870.15-1.25 54-2.651.28
1085 890.34-5 67-1.091.1
1191 910.51-1.25 770.021.08
12102 960.962.5 871.021.08
1373 900.26-11.25 850.720.89
1460 78-0.97-9 75-0.350.9
1599 79-0.7718.75 69-0.921.53
16108 910.517.88 75-0.231.36
1798 960.96-1.5 800.291.16
18104 1001.261.38 870.961.14
1983 930.43-3.75 840.561.03
2068 88-0.11-15.63 810.20.86
2162 81-0.8-14 76-0.320.86
2259 64-2.426.88 61-1.791.25
2380 66-1.9810.94 59-1.811.36
24121 870.3216.25 760.071.38
25112 971.298.19 850.961.23
2685 940.86-6.38 850.861.02
27106 1061.97-7.82 1012.370.95
2882 1031.47-17.5 1002.030.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
Download article in PDF format.

Do you find this article curious? /

Seen: 45 933