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 897 283 0
993 946 501 400
1046 1003 575 497
1172 1127 644 523
1296 1325 707 586
1785 1328 847 648
1058 1385 862 893
1402 1361 828 529
1200 1561 750 701
2582 1616 1004 600
1280 1862 1084 1291
2384 2308 1198 640
2984 1865 1350 1192
813 1822 1311 1492
1108 1854 927 407
2510 1756 864 554
2592 2501 1282 1255
3792 2910 1762 1296
2744 3252 1891 1896
3878 3471 2723 1372
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
993 -640 160
1046 675 532
1172 -503 415
1296 570 537
1785 -383 563
1058 523 846
1402 -365 296
1200 469 723
2582 -302 431
1280 530 1377
2384 -349 217
2984 553 1344
813 -199 1253
1108 260 -13
2510 -123 670
2592 362 1238
3792 -67 1060
2744 440 1851
3878 -122 906
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 920