The most important part of the model is fitting, such as linear and exponential fitting. For the known formula, the fitting type can be seen directly, and the unknown type needs code fitting.
1, Time series
The time series is a little: ARMA model is generally used to fit the time series and predict the future value of the time series. Daniel tests stationarity. Automatic regression AR(Auto regressive) and moving average MA(Moving Average) prediction models. The disadvantage is that when there are great changes in the outside world, there are often large deviations. The effect of time series prediction method for medium and short-term prediction is better than that of long-term prediction.
Based on time series, there are three types:
Time series processing method
Suppose we guide the values from 1 to T and let us predict the value of T+1, then we take the average value as the predicted value of T+1D.
Of course, there will be errors:
This example is smooth. Take corporate income as an example:
This method is suitable for data with stable numerical value. y10 is a moving average, standard error.
2, Grey prediction
When dealing with less eigenvalue data, it does not need a large enough sample space, which can solve the problems of less historical data, low sequence integrity and reliability, and generate irregular original data to obtain a strong regular generation sequence. The disadvantage is that it is only suitable for medium and short-term prediction, and only suitable for prediction similar to exponential growth.
2.1 GM(1,1) model
Applicable to: grey model with first-order differential equation and only one variable. It is suitable for sequences with strong exponential law. The sequences involved are: cumulative sequence and mean sequence
If we process the data and add an explanation, we should subtract this constant from the final prediction result. Don't forget.
One time accumulation generation sequence:
Mean generation sequence
According to the above results, the differential equation will be obtained:
White differential equation
Find out a and B to get the relationship between x(1) and t.
How to solve a and b? least square method
The white differential equation is obtained:
x(0) is the original data.
2.2 summary steps
(1) Original sequence (reference data column):
(2) Primary accumulation sequence (1-AGO):
(3) One time progressive subtraction sequence (1-IAGO):
(that is, in the original sequence, the latter item successively subtracts the value of the previous item, for example, [x(2)-x(1),x(3-x(2),..., x(n)-x(n-1))].)
(4) Mean generation sequence: (this is the result of the cumulative sequence "(previous + latter) / 2")
Draw a diagram to clarify the idea:
2.3GM(1,1) actual combat
The prediction test of GM (1,1) is used to test "the average sound level data of road noise traffic in a city in the north from 1986 to 1992:
The code is:
x0 = [71.1 72.4 72.4 72.1 71.4 72 71.6]'; %Here is the column vector, which is equivalent to the dependent variable in the original data n = length(x0); lamda = x0(1:n-1)./x0(2:n) %Calculated stage ratio range = minmax(lamda') %Range of calculated stage ratio x1 = cumsum(x0) B = [-0.5*(x1(1:n-1)+x1(2:n)),ones(n-1,1)]; %This is the constructed data matrix B Y = x0(2:n); %data vector Y u = B\Y %Fitting parameters u(1)=a,u(2)=b syms x(t) x = dsolve(diff(x)+u(1)*x==u(2),x(0)==x0(1)); %Modeling and solving xt = vpa(x,6) %Displays the solution of the differential equation in decimal format prediction1 = subs(x,t,[0:n-1]); %Calculate the predicted value of known data prediction1 = double(prediction1); %Convert signed numbers to numeric types for differential operations prediction = [x0(1),diff(prediction1)] %Differential operation, restore data epsilon = x0'-prediction %Calculate residual delta = abs(epsilon./x0') %Calculate relative residual rho = 1-(1-0.5*u(1))/(1+0.5*u(1))*lamda'%Calculate the stage ratio deviation value, u(1)=a
2.4 Python implementation
# coding=gbk """ Author: Chuan Chuan @time : 2021/9/2 3:57 Group: 970353786 """ import numpy as np import math #Carry out level ratio test on the data. If it does not meet the requirements, carry out translation transformation, and return the transformed data and transformation times def deal_data(data, c): times = 0 while True: i = 1 while i < len(data): proportion = data[i - 1] / data[i] if math.exp(-2 / (len(data) + 1)) < proportion < math.exp(2 / (len(data) + 1)): i = i + 1 else: data = data + [c] * len(data) times += 1 break if i == len(data): break return data, times #forecast def predict(data): x1 = data.cumsum() B1 = -(x1[:len(x1) - 1] + x1[1:]) / 2.0 B = np.array([B1, np.ones_like(B1)]).T Y = data[1:] u = np.dot(np.dot(np.linalg.inv(np.dot(B.T, B)), B.T), Y) a, b = u, u return np.array([(data - b / a) * math.exp(-a * i) + b / a for i in range(len(data))]) #The above algorithms are fixed and can be copied if __name__ == '__main__': x = np.array([71.1, 72.4, 72.4, 72.1, 71.4, 72.0, 71.6])#Existing data c = 0.1 # Translation constant, self setting data, times = deal_data(x, c) # Data verification, and return the translated data and the number of horizontal shifts predict_data = predict(data) # Forecast x1 result = np.ediff1d(predict_data) # Diminishing result = result - c * times # Subtract the effect of translation print(result)#Prediction results print((np.array(result[:len(x)]) - np.array(x[1 :len(x)])) / np.array(x[1 :len(x)]))#standard deviation
It seems a little around. I also refer to a lot of materials here. The algorithms are consistent and the codes are basically the same. Let's talk about it first. Tomorrow I'll talk about a practical battle. At that time, I'll see how to modify this article. It's sent out first for the time being. If there's a problem, leave a message to correct it.