Time Series Analysis--Moving Average Forecasting Model

      A time series is a chronological, time-varying, and interrelated sequence of data. Methods for analyzing time series constitute an important field of data analysis known as time series analysis.

      If there is no sudden change and the variance of random changes is small within the forecast time range, and it is reasonable to believe that the past and present evolution trends will continue to develop into the future, some empirical methods can be used for forecasting .

1. Moving average method

Moving average methods include simple moving average method, weighted moving average method, trend moving average method and so on.

1.1 Simple Moving Average

      Assume the observation sequence is y_{1},y_{2},...,y_{T}, and take the number of moving items N<T. When the basic trend of the forecast target is to fluctuate at a certain level, a simple moving average method can be used to establish a forecast model:

The essence is to use the average value of the sequence values ​​of the latest N periods as the forecast results of each period in the future.

       Its standard error is

Generally, the value range of N is: 5 ≤ N ≤ 200. When the basic trend of the historical sequence does not change much and there are many random components in the sequence, the value of N should be larger. Otherwise, the value of N should be smaller. In the data with a definite seasonal fluctuation period, the number of items of the moving average should be the length of the period. An efficient way to choose the best value of N is to compare the prediction errors of several models. The one with the smallest forecast standard error is better

Example 1   The time series of sales revenue of an enterprise from January to November is shown in Table 1. Try a simple moving average to forecast sales revenue for the 12th month.

     analyze

       When N=4, the prediction formula is 

 The predicted value \hat{y}_{12}^{(1)}=993.6, the standard error of the forecast is

      When N=5, the prediction formula is 

 

The predicted value \hat{y}_{12}^{(2)}=182.4, the predicted standard error is

 

 When N = 4, the standard error of prediction is small, so N = 4 is selected. The forecasted sales revenue for the 12th month is 993.6.

Matlab implementation is as follows

clc,clear 
y=[533.8 574.6 606.9 649.8 705.1 772.0 816.4 892.7 963.9 1015.1 
1102.7]; 
m=length(y); 
n=[4,5]; %n 为移动平均的项数
for i=1:length(n) 
 %由于 n 的取值不同,yhat 的长度不一致,下面使用了细胞数组
 for j=1:m-n(i)+1 
 yhat{i}(j)=sum(y(j:j+n(i)-1))/n(i); 
 end 
 y12(i)=yhat{i}(end); 
 s(i)=sqrt(mean((y(n(i)+1:m)-yhat{i}(1:end-1)).^2)); 
end 
y12,s

Note : The simple moving average method is only suitable for short-term forecast, and it is the case that the development trend of the forecast target does not change much. If there are other changes in the development trend of the target, the simple moving average method will produce large prediction deviation and lag.

1.2 Weighted moving average

     In the simple moving average, the selected recent data have the same proportion in the forecast calculation, but the recent data generally contain information about the future, so a higher weight is given. Similar to weighted average.

     Assuming that the time series is y_{1},y_{2},...,y_{t},..., the weighted moving average formula is

  M_{tw}is the weighted moving average of period t; is  W_{i} the weight of , which reflects the corresponding importance in the weighted average.y_{t-i+1}y_{t}

     Using the weighted moving average to make predictions, the prediction formula is

That is, the weighted moving average of period t is used as the predicted value of period t+1.

Example 2 The production of raw coal in China from 1979 to 1988 is shown in Table 2. Try the weighted moving average method to predict the production in 1989

 

     SolutionW_{1}=3,W_{2}=2,W_{3}=1 , according to the prediction formula

      Calculate the three-year weighted moving average forecast value, and the results are listed in Table 2. The predicted value of my country's raw coal output in 1989 (100 million tons)

       The predicted value is obviously low, so we use the average relative error to correct it. The method is: first calculate the relative error between the predicted value and the actual value in each year, for example, in 1982,

 Matlab implementation is as follows

y=[6.35 6.20 6.22 6.66 7.15 7.89 8.72 8.94 9.28 
9.8];
w=[1/6;2/6;3/6];
m=length(y);n=3;
for i=1:m-n+1
 yhat(i)=y(i:i+n-1)*w;
end
yhat
err=abs(y(n+1:m)-yhat(1:end-1))./y(n+1:m)
T_err=1-sum(yhat(1:end-1))/sum(y(n+1:m))
y1989=yhat(end)/(1-T_err)

 Note : In the weighted moving average method, W_{t}the choice of , also has certain experience. The general principle is: the weight of recent data is large, and the weight of long-term data is small . As for how large and small it is, it needs to be determined according to the forecaster's understanding and analysis of the sequence

 1.3 Trend moving average

      The simple moving average method and the weighted moving average method can accurately reflect the actual situation when there is no obvious trend change in the time series. However, when the time series shows a trend of linear increase or decrease, there will be lag deviation in forecasting with simple moving average method and weighted moving average method . Therefore, it needs to be corrected, and the corrected method is to make a second moving average , and use the law of moving average lag deviation to establish a linear trend prediction model . This is the trend moving average method.

      A moving average of

      Carrying out a moving average on the basis of a moving average is the second moving average, and its calculation formula is

     Assuming that the time series {y_{t}} has a linear trend from a certain period, and it is believed that the future period will also change according to this linear trend, then the linear trend prediction model can be set as

Where t is the number of the current period; m is the number of periods from t to the forecast period; a_{t}is the intercept; b_{t}and is the slope. Both are also called smoothing coefficients.

 Determine smoothing factor from moving average

 Example 3 The total power generation in China from 1965 to 1985 is shown in Table 3. Try to predict the total power generation in 1986 and 1987.

      Solution First make the scatter plot of the original data Figure 1

 

     It can be seen from the scatter plot 1 that the total power generation basically shows a linear upward trend, which can be predicted by the trend moving average method.

     Take N = 6, calculate the primary and secondary moving averages and list them in Table 3.

 Then by the formula

 Therefore, when t = 21, the linear trend prediction model is

 The total power generation in 1986 and 1987 is predicted to be

 Matlab implementation

clc,clear 
load y.txt %把原始数据保存在纯文本文件 y.txt 中
m1=length(y); 
n=6; %n 为移动平均的项数
for i=1:m1-n+1 
 yhat1(i)=sum(y(i:i+n-1))/n; 
end 
yhat1 
m2=length(yhat1); 
for i=1:m2-n+1 
 yhat2(i)=sum(yhat1(i:i+n-1))/n; 
end 
yhat2 
plot(1:21,y,'*') 
a21=2*yhat1(end)-yhat2(end) 
b21=2*(yhat1(end)-yhat2(end))/(n-1) 
y1986=a21+b21 
y1987=a21+2*b21 

 

Guess you like

Origin blog.csdn.net/weixin_44734502/article/details/126253629