Study notes-Matlab algorithm articles-time series

sequentially

01 time series model

Introduction:

Time series are data series that are arranged in chronological order, change over time and are interrelated. The method of analyzing time series constitutes an important field of data analysis, namely time series analysis. Time series can have different classifications according to the research basis.

1 . According to the number of points of the research object, there are one-dimensional time series and multiple time series.

2 . According to the continuity of time, time series can be divided into discrete time series and continuous time series.

3 . According to the statistical characteristics of the series, there are stationary time series and non-stationary time series. If the probability distribution of a time series has nothing to do with time t , the series is called a strictly stationary time series. If the first and second moments of the sequence exist, and for any time t satisfies: ( 1 ) the mean is a constant; ( 2 ) the covariance is a function of the time interval τ . The sequence is called a wide stationary time series, also called a generalized stationary time series. The time series we study are mainly wide stationary time series.

4 . According to the distribution law of time series, there are Gaussian time series and non-Gaussian time series.

Deterministic time series analysis method:

       Time series forecasting technology is to study the changing trend of the forecast target's own time series. A time series is often the superposition or coupling of the following types of changes.

( 1 ) Long-term trend changes. It refers to the tendency of a time series to continuously rise or fall in a certain direction, or stay at a certain level, and it reflects the main trend of changes in objective things.

( 2 ) Seasonal changes.

( 3 ) Cycle changes. Usually refers to fluctuations with a period of more than one year and similar fluctuations caused by non-seasonal factors.

( 4 ) Irregular changes. Usually it is divided into sudden changes and random changes.

 

02 Moving Average

Moving average method: The moving average method is a method of gradually calculating time series averages containing a certain number of items according to the time series data to reflect the long-term trend. When the value of the time series fluctuates greatly due to periodic changes and irregular changes, and it is difficult to show the development trend, the moving average method can be used to eliminate the influence of these factors to analyze and predict the long-term trend of the sequence. Moving average methods include simple moving average method, weighted moving average method, trend moving average method, etc.

 

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 分别取4和5
for i=1:length(n)
%循环序列,求平均
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

result:

y12 =

  993.6000  958.1600


s =

  150.5121  182.3851

 

The simple moving average method is only suitable for short-term forecasting, and it is a situation where the development trend of the forecast target has not changed much. If there are other changes in the development trend of the target, the simple moving average method will produce a larger forecast deviation and lag. In the simple moving average formula, the data of each period has the same effect in averaging. However, the amount of information contained in each period of data is different, and recent data contains more confidence in the future. Therefore, it is unreasonable to treat the data of each period equally. The importance of the data of each period should be considered, and the recent data should be given greater weight. This is the basic idea of ​​the weighted moving average method.

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;
yhat=zeros(1,m-n+1);
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)

 result:

>> e02

yhat =

    6.2350    6.4367    6.8317    7.4383    8.1817    8.6917    9.0733    9.4833


err =

    0.0638    0.0998    0.1341    0.1470    0.0848    0.0634    0.0741


T_err =

    0.0950


y1989 =

   10.4788

       Simple moving average method and weighted moving average method can accurately reflect the actual situation when there is no obvious trend change in the time series. But when the time series has a linear increase or decrease trend, the simple moving average method and the weighted moving average method will be used to predict the lagging deviation. Therefore, it needs to be corrected. The method of correction is to make a second moving average and use the law of the lagging deviation of the moving average to establish a linear trend prediction model. This is the trend moving average method.

Example (e03.m) : China 's total power generation from 1965 to 1985 is shown in the following table. Try to predict the total power generation in 1986 and 1987 .

 

clc,clear
load y.txt %加载发电量
m1=length(y);
n=6; %n 前6个值取平均
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

result: 

yhat1 =

   1.0e+03 *

  1 至 11 列

    0.8483    0.9663    1.0828    1.2318    1.3938    1.5635    1.7088    1.8505    2.0242    2.2162    2.4358

  12 至 16 列

    2.6250    2.8327    3.0460    3.2467    3.4612


yhat2 =

   1.0e+03 *

    1.1811    1.3245    1.4719    1.6288    1.7928    1.9665    2.1434    2.3307    2.5300    2.7337    2.9412


a21 =

   3.9811e+03


b21 =

  207.9778


y1986 =

   4.1891e+03


y1987 =

   4.3971e+03

 

03 Exponential Smoothing

Example (e04.m) : The sales of certain electrical appliances in a city ​​from 1976 to 1987 are shown in the table below. Try to predict the sales of this electrical appliance in 1988 .

clc,clear
load dianqi.txt %加载实际销售额
yt=dianqi; n=length(yt);
alpha=[0.2 0.5 0.8];m=length(alpha);
yhat(1,1:m)=(yt(1)+yt(2))/2;
for i=2:n
yhat(i,:)=alpha*yt(i-1)+(1-alpha).*yhat(i-1,:);
end
yhat
err=sqrt(mean((repmat(yt,1,m)-yhat).^2))
yhat1988=alpha*yt(n)+(1-alpha).*yhat(n,:)

result: 

yhat =

   51.0000   51.0000   51.0000
   50.8000   50.5000   50.2000
   51.0400   51.2500   51.6400
   50.2320   49.1250   47.9280
   50.3856   50.0625   50.3856
   50.1085   49.5313   49.2771
   49.6868   48.7656   48.2554
   49.9494   49.8828   50.4511
   47.9595   44.9414   42.0902
   47.9676   46.4707   46.8180
   48.7741   49.2354   50.9636
   49.2193   50.1177   50.9927


err =

    4.5029    4.5908    4.8426


yhat1988 =

   51.1754   54.5588   57.3985

 

Guess you like

Origin blog.csdn.net/seek97/article/details/108348904