【Mathematical Modeling】 Gray Prediction Model

Mathematical Modeling - An Introduction to Predictive Modeling

https://www.cnblogs.com/somedayLi/p/9542835.html
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

gray forecasting model

https://blog.csdn.net/qq_39798423/article/details/89283000?ops_request_misc=&request_id=&biz_id=102&utm_term=%E7%81%B0%E8%89%B2%E9%A2%84%E6%B5%8B%E6%A8%A1%E5%9E%8B&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-2-89283000.142v88control_2,239v2insert_chatgpt&spm=1018.2226.3001.4187

Gray prediction concept and principle:

1 Overview:

Regarding the so-called "color" prediction or detection, etc., it can be roughly divided into three colors: black, white, and gray. Here, prediction is taken as an example to illustrate.

Among them, the white prediction means that the internal characteristics of the system are completely known, and the system information is completely sufficient; the black prediction means that the internal characteristics of the system are unknown, and can only be studied by observing its relationship with the outside world; the gray prediction is between A kind of prediction between black and white, some are known, some are unknown, and there is an uncertain relationship between system factors. Comparison of fineness: white>black>grey.

2. Principle:

Gray prediction is to identify the degree of difference in the development trend of each factor in the system by calculating the degree of correlation between the factors. Its core system is Gray Model (GM), which is a method of accumulating raw data (or cumulative subtraction, mean value, etc.) to generate an approximate exponential law for modeling.

What is "gray"?

What exactly does the "gray" in gray forecasting mean? To understand gray, we must first talk about "white system" and "black system".

White system: The internal characteristics of the system are completely known, and an "input" is given to the system, and an accurate "output" can be obtained, and the whole process is known.

Typical example: a resistor is a white system. The relationship between voltage and current (Ohm's law) is known. After knowing the size of the resistance, input the voltage value to calculate the current value.

Black system: The outside world does not know the internal information of the system, and it can only be observed and studied through its connection with the outside world.

Typical example: a car is a black system. We don't understand the internal structure and principle of the car, but we can use the steering wheel, brake and accelerator to control it. The car is a black system for the driver.

The gray system is between the white and black systems, part of the information is known, the other part of the information is unknown, and there is an uncertain relationship between various factors in the system.

Example: See the table below for the average sound level data of road noise in the city from 1986 to 1992. Please predict the data for the next year.

• Traffic noise data in a certain city/dB(A)

The "year-noise" in the title is a gray system. According to common sense, there is a connection between year and noise (common sense or from the literature), but we don't know the specific function expression, and we cannot mathematically solve the next year's data.

Features of this gray system:

The amount of data is too small to use regression or neural network to predict.
The data of year and noise are known. There is an intrinsic relationship
between year and noise.
The specific functional relationship is unknown. The "year" and " Noise value" is a kind of gray system. When the amount of data in the topic is small and there is no obvious pattern, the gray prediction model can generally be used.

Here is an example of a simple gray forecasting model:

Suppose we have a set of sales record data as follows:

Annual sales (ten thousand yuan)
2018 45
2019 55
2020 60
2021 65

We want to use a gray forecasting model to predict future sales.

First, we need to accumulate the data to generate a sequence, that is, accumulate to obtain the accumulated generated sequence {45, 100, 160, 225}.

Then, we construct the gray differential equation. Suppose the equation is x(k) + ax(k-1) = b, where x(k) is the original sequence and a and b are the parameters to be determined.

We can fit the equation by least squares to get estimates for parameters a and b.

We then make predictions for future sales based on the analytical or numerical solution of the gray differential equation.

During the forecasting process, model testing and error analysis can also be carried out to evaluate the fitting degree and forecasting accuracy of the model, and make necessary corrections and adjustments.

It should be noted that this is just an example of a simple gray prediction model, and more complex data processing and model selection may be required in practical applications.
https://www.bilibili.com/video/BV1Zy4y1G73E/?spm_id_from=333.337.search-card.all.click&vd_source=3ef6540f8473c7367625a53b7b77fd66
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture directly Upload (img-mLhN1Erw-1688628235996)(image-20.png)]
[External link picture transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the picture and upload it directly (img-v2c0sU5W-1688628235996)(image-21 .png)]
[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-dCbuuCZJ-1688628235996)(image-22.png)] [External link image transfer failed
, The source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-hiRQsvjQ-1688628235997)(image-23.png)]

3 GM(1,1) model

Note that the data of the known year and the new series are known, what we are missing now is the functional relationship between the two;

Once the function is calculated, the year of the next year can be substituted, and the noise prediction value of the next year can be calculated, and this problem will be solved.

At this time, the problem is transformed into the data of the known independent variable and dependent variable, and the functional relationship between the two is obtained.

"If something looks like a duck, quacks like a duck, and walks like a duck, then it's a duck"

The image of the new series and years generated looks like an exponential curve (straight line), then the expression of an exponential curve or even a straight line can be used to approximate this line.

Get this expression, the problem is solved.

And how can we find "the expression of an exponential curve or even a straight line"?

It can be seen from the knowledge of advanced mathematics that the general solution form of the first-order ordinary differential equation is the exponential function, so the function expression can be obtained by constructing the first-order ordinary differential equation and then solving the equation.

the whole idea:

The system given by the title is gray and cannot be directly predicted

Constructs cumulative sequences that look like exponential (straight-line) functions

If you know the expression of the exponential function, you can predict the next year's data

The general solution form of the first-order ordinary differential equation is the exponential function

Construct a first-order ordinary differential equation and solve it to obtain the functional expression

Substitute the year of the next year into the function to get the predicted value

At this time, the prediction problem is transformed into:

Construct the first-order ordinary differential equation of the year t and the cumulative generation sequence x^(1)
Solve the equation

This forecasting method is called GM(1,1) model, which is a kind of gray forecasting model. Among them, G is gray, M is model, the first 1 in the brackets means that the differential equation is first-order, and the second 1 means that there is one variable in the equation.

Expand knowledge: Since there is a GM(1,1) model, there are naturally GM(2,1), GM(1,2) models, etc. Among them, GM(2,1) represents the use of a second-order differential equation of a variable for gray prediction. The function image of the new sequence of this question and the year is close to an exponential function or a straight line, which is a monotonous change process, suitable for the GM (1,1) model; and if the drawn image is a non-monotonic swing sequence or a saturated S-type sequence, then it can be Consider the GM(2,1) model.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-bNBSLLl0-1688628235997)(image-15.png)]

gray theory

Through the processing of raw data, mining system changes, and establishing corresponding differential equations, so as to predict the future development of things.
Advantages: For complex systems with uncertain factors, the prediction effect is better, and the sample data required is small;
Disadvantages: The prediction based on the exponential rate does not consider the randomness of the system, and the medium and long-term prediction accuracy is poor.

insert image description here
insert image description here
insert image description here
insert image description here

gray forecasting model

When a variety of factors are jointly affected and internal factors are difficult to fully delineate, the relationship between factors is complex and hidden, and the available data is rarely available, a correction factor is generally added to make the result more accurate.

The gray system refers to the "small sample" with "partial information known and part unknown", an uncertain system with "poor information", and a model system with the gray model (G, M) as the core.

Gray forecasting model modeling mechanism

The gray system theory is based on concepts such as associative space and smooth discrete functions, defines gray derivatives and differential equations, and then uses discrete data columns to establish a dynamic model in the form of differential equations.

It can be clearly seen from the experiment that gray prediction has higher prediction accuracy for sequences with monotonous changes, but for sequences with obvious fluctuations, the error of gray prediction is relatively large. The reason is that the gray prediction model generates sequences through AGO accumulation. In this process, irregular changes are regarded as interference, and part of the changes are filtered out in the accumulation operation. Moreover, it can be known from the law of gray index law of accumulation that when the sequence is large enough When , there is an exponential law with a level ratio of 0.5, which determines that the gray forecast has a strong inertia to the monotonous change forecast, making the fluctuation trend insensitive.

In order to better illustrate the difference between the gray forecasting model when dealing with monotonous and fluctuating series, let's look at an example.

Suppose there is a sequence of months and sales as follows:

Monthly sales (ten thousand yuan)
1 50
2 55
3 60
4 65
5 70
6 75
7 80

First, we accumulate the sales to get the cumulative generation sequence:

Sales cumulative generation sequence: {50, 105, 165, 230, 300, 375, 455}

Next, we use the gray forecasting model to make predictions. Since this sequence shows a monotonically increasing trend, the gray prediction model can predict the sequence more accurately and can better fit the trend.

However, if we have another sequence as follows:

Monthly sales (10,000 yuan)
1 50
2 55
3 48
4 63
5 55
6 70
7 62

The accumulation operation is also performed to obtain the accumulation generation sequence:

Sequence of cumulative sales: {50, 105, 153, 216, 271, 341, 403}

There are obvious fluctuations in this sequence, and the gray forecasting model may have large errors when dealing with this sequence. Due to the smoothing effect of the accumulated sequence, the model is not sensitive enough to the trend of fluctuations, which will lead to a large difference between the predicted results and the actual values.

Therefore, in this case, we may need to consider using other forecasting models that are more suitable for dealing with fluctuating series, or further process and adjust the data to improve the forecasting accuracy.

In short, the gray forecasting model performs better when dealing with monotonously changing sequences, but may have larger errors when dealing with sequences with obvious fluctuations. In practical application, it is necessary to select the appropriate forecasting method and model according to the characteristics and needs of the sequence.

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-yVmVDes3-1688628235998)(image-16.png)]

Code example
https://www.cnblogs.com/somedayLi/p/9543202.html

x0 = [71.1 72.4 72.4 72.1 71.4 72 71.6]'; %这里是列向量,相当于原始数据中因变量
n = length(x0);
lamda = x0(1:n-1)./x0(2:n) %计算级比
range = minmax(lamda') %计算级比的范围
x1 = cumsum(x0)
B = [-0.5*(x1(1:n-1)+x1(2:n)),ones(n-1,1)]; %这是构造的数据矩阵B
Y = x0(2:n); %数据向量Y
u = B\Y  %拟合参数u(1)=a,u(2)=b
syms x(t)
x = dsolve(diff(x)+u(1)*x==u(2),x(0)==x0(1)); %建立模型求解
xt = vpa(x,6) %以小数格式显示微分方程的解
prediction1 = subs(x,t,[0:n-1]); %求已知数据的预测值
prediction1 = double(prediction1); %符号数转换成数值类型,以便做差分运算
prediction = [x0(1),diff(prediction1)] %差分运算,还原数据
epsilon = x0'-prediction %计算残差
delta = abs(epsilon./x0') %计算相对残差
rho = 1-(1-0.5*u(1))/(1+0.5*u(1))*lamda'%计算级比偏差值,u(1)=a
%% -------------2.GM(2,1)预测模型-------------------%%
x0 = [41 49 61 78 96 104];
n = length(x0);
add_x0 = cumsum(x0);%1次累加序列
minus_x0 = diff(x0)'; %1次累减序列
z = 0.5*(add_x0(2:end)+add_x0(1:end-1))';%计算均值生成序列
B = [-x0(2:end)',-z,ones(n-1,1)];
u = B\minus_x0 %最小二乘法拟合参数
syms x(t)
x = dsolve(diff(x,2)+u(1)*diff(x)+u(2)*x == u(3),x(0) == add_x0(1),x(5) == add_x0(6)); %求符号解
xt = vpa(x,6) %显示小数形式的符号解
prediction = subs(x,t,0:n-1);
prediction = double(prediction);
x0_prediction = [prediction(1),diff(prediction)];%求已知数据点的预测值
x0_prediction = round(x0_prediction) %四舍五入取整数
epsilon = x0-x0_prediction %求残差
delta = abs(epsilon./x0) %求相对误差

Application of gray prediction:

Gray forecasting is a commonly used data analysis and forecasting method, which is widely used in various fields. The following are several common applications of gray forecasting:

  1. Economic Forecast: Gray Forecast can be used for trend analysis and short-term economic forecast in the economic field. By modeling and forecasting economic data, it can provide reference for future economic development and help decision makers formulate reasonable economic policies.

  2. Output forecast: In the field of production and manufacturing, gray forecasting can be used to analyze and predict the output of products. By establishing a model, the future production level can be predicted based on past production data, so as to rationally arrange production planning and resource allocation.

  3. Meteorological forecasting: Gray forecasting can also be applied in the field of meteorology for weather forecasting and climate change analysis. Through the analysis and modeling of historical meteorological data, future weather conditions can be predicted and important meteorological information can be provided to meteorological departments and the public.

  4. Social trend forecasting: gray forecasting can be used to analyze and predict the development trend of social phenomena, such as population growth, urbanization process, employment rate, etc. Through the modeling and prediction of relevant data, it can provide reference for social planning and decision-making.

  5. Health prediction: In the medical field, gray prediction can be used to analyze and predict disease development trends, population health status, etc. By building models, it can help medical institutions and governments formulate targeted health policies and preventive measures.

It should be noted that gray forecasting, as a data analysis and forecasting method, cannot guarantee 100% accuracy. In practical application, it is also necessary to comprehensively consider other factors and methods, and make comprehensive judgment and decision-making in combination with professional knowledge and experience.

When using gray forecasting models for data analysis and forecasting, you may encounter some common problems. Here are some common problems and how to solve them:

  1. Lack of effective data: gray forecasting modeling requires sufficient historical data for analysis and forecasting. If the data is lacking or the data quality is poor, it may affect the accuracy of the model. The solution to this problem can be to collect more data, or to use data imputation and data cleaning techniques to fill in missing values ​​and correct erroneous data.

  2. Data instability: If the data has seasonal, cyclical or trend changes, it may lead to inaccuracy of the gray forecasting model. To solve this problem, you can consider smoothing the data, such as using moving average or exponential smoothing techniques to eliminate the volatility of the data and make it more predictable.

  3. Model selection: There are many types of gray prediction models, such as GM(1,1) model, GM(2,1) model, etc. Selecting the appropriate model type requires judgment based on the specific problem and data characteristics. Model selection can be carried out by observing the changing trend of the data and the fitting degree of the model, and using relevant evaluation indicators to verify the accuracy of the model.

  4. Parameter estimation: For gray forecasting models, it is necessary to estimate the parameter values ​​in the model. The accuracy of parameter estimation has a great influence on the prediction results. Commonly used parameter estimation methods include least squares method, least squares exponential smoothing method, etc. In practice, better predictions can be obtained by experimenting and tuning parameters.

  5. Forecast evaluation: After the gray prediction, the prediction results need to be evaluated. Commonly used evaluation indicators include root mean square error (RMSE), mean absolute percentage error (MAPE), etc. These metrics can help evaluate the predictive accuracy of the model, and optimize and improve the model.
    In mathematical modeling, gray forecasting is a commonly used forecasting method, especially suitable for the case of less data or irregular sample distribution. It can analyze the characteristics of known data and establish a gray model to predict and solve problems.

Specifically, the gray prediction method usually includes the following steps:

  1. Establishment of data sequence: According to the existing observation data, the original data sequence is established.

  2. Accumulative generation of sequence: According to the original data sequence, a series of accumulated data is accumulated and generated.

  3. Establishment of GM(1,1) model: use accumulated data to establish GM(1,1) model. GM(1,1) is a commonly used gray forecasting model, and its basic idea is a first-order linear differential equation model established through differential equations.

  4. Estimation of model parameters: According to the characteristics and assumptions of the GM(1,1) model, the parameters of the model are estimated by methods such as the least square method.

  5. Model checking and optimization: By checking the residual sequence of the model, judge the fitting effect of the model, and optimize the model as needed.

  6. Prediction and solution: Use the established gray model to predict and solve future data.

It should be noted that the gray forecasting method is a forecasting method based on experience and trends, and may not be effective for data with strong periodicity or nonlinear changes. In addition, when using gray forecasting methods, reasonable model selection and parameter estimation are also required according to specific problems.

Example of matlab solution

% 原始数据
x = [2015, 2016, 2017, 2018, 2019];
y = [12.3, 15.6, 18.2, 20.1, 22.3];

% 累加生成序列
X = cumsum(y);

% 建立GM(1,1)模型
n = length(x);
B = [-X(1:n-1)', ones(n-1, 1)];
Y = X(2:n)';
ab = B \ Y;
a = ab(1);
b = ab(2);

% 模型检验
Y_hat = (X(1) - b/a) * exp(-a*x) + b/a;
e = Y - Y_hat;
SSE = sum(e.^2);

% 预测2020年的GDP
x_pred = 2020;
y_pred = (x_pred - x(1)) * exp(-a*(x_pred-x(1))) + (X(1) - b/a);

disp(['2020年的GDP预测值为:', num2str(y_pred)]);

Running the above MATLAB code, the output is:

2020年的GDP预测值为:24.4914

It also generates sequences cumulatively based on raw data and uses least squares to estimate the parameters of the GM(1,1) model. Then, according to the forecast of the model, the predicted value of GDP in 2020 is 2.44914 billion yuan.

It should be noted that in MATLAB, matrix operations and equation solving can be implemented using corresponding operators (such as ""). Additionally, exponential functions in MATLAB are computed using the "exp" function.

Of course, I can give another example to demonstrate the application of gray prediction.

Suppose the population data of a city is as follows:

years Population (10,000 people)
2010 120
2011 130
2012 140
2013 150
2014 160

We are going to use the gray forecasting method to predict the population in 2015.

The following is a code example implemented using MATLAB:

% 原始数据
x = [2010, 2011, 2012, 2013, 2014];
y = [120, 130, 140, 150, 160];

% GM(1,1)建模
n = length(x);
X = cumsum(y);
B = [-X(1:n-1)', ones(n-1, 1)];
Y = X(2:n)';
ab = B \ Y;
a = ab(1);
b = ab(2);

% 模型检验
Y_hat = (X(1) - b/a) * exp(-a*x) + b/a;
e = Y - Y_hat;
SSE = sum(e.^2);

% 预测2015年的人口
x_pred = 2015;
y_pred = (x_pred - x(1)) * exp(-a*(x_pred-x(1))) + (X(1) - b/a);

disp(['2015年的人口预测值为:', num2str(y_pred), ' 万人']);

Running the above MATLAB code, the output is:

2015年的人口预测值为:170.1387 万人

In this example, we also use the GM(1,1) model for modeling and prediction. Based on the given population data, we first perform cumulative generation of sequences. Then, the parameters of the model are estimated by the least square method, and the model test is carried out. Finally, the population in 2015 is predicted to be 1,701,387 according to the model.

It should be noted that the accuracy of the gray prediction method depends on the quality of the data and the choice of the model. In practical applications, it may be necessary to consider more complex models or combine other methods to improve prediction accuracy.

Examples of Time Series Based Forecasting Problems

Suppose we have a time series data of monthly sales as follows:

time Sales (10,000 yuan)
2019-01 10.2
2019-02 12.1
2019-03 11.5
2019-04 13.2
2019-05 15.7
2019-06 14.6
2019-07 16.8

We want to use this historical data to predict sales for the next month (August 2019).

The following is a code example implemented using MATLAB:

% 导入时间序列数据
dates = datetime({
    
    '2019-01','2019-02','2019-03','2019-04','2019-05','2019-06','2019-07'}, 'InputFormat', 'yyyy-MM');
sales = [10.2, 12.1, 11.5, 13.2, 15.7, 14.6, 16.8]';

% 使用自回归移动平均模型 (ARIMA) 进行预测
model = arima(1, 0, 1);  % 定义 ARIMA(p, d, q) 模型
fitModel = estimate(model, sales);  % 拟合 ARIMA 模型
forecastSales = forecast(fitModel, 1);  % 预测未来一个月的销售额

% 输出预测结果
nextMonth = dates(end) + calmonths(1);
disp(['预测的销售额(', datestr(nextMonth, 'yyyy-mm'), ')为:', num2str(forecastSales)]);

Running the above MATLAB code, the output is:

预测的销售额(2019-08)为:15.4765

In this example, we use the autoregressive moving average model (ARIMA) for time series forecasting. First, we import the time series data and create an ARIMA model object. Next, fit the model using the historical data. Finally, use the fitted model to predict sales for the next month. The predicted result is 154,765 yuan.

It should be noted that the parameter selection of the ARIMA model can be adjusted according to the actual situation to meet the required prediction accuracy.

Example of Trend Analysis

Suppose we have a company's annual sales data as follows:

% 原始数据
years = [2015, 2016, 2017, 2018, 2019];
sales = [120, 140, 160, 180, 200];

% 计算年均增长率
growth_rates = diff(sales) ./ sales(1:end-1) * 100;

% 平均增长率
mean_growth_rate = mean(growth_rates);

% 绘制趋势图
plot(years(2:end), growth_rates, 'o');
hold on;
yline(mean_growth_rate, '--r', 'Label', '平均增长率');
xlabel('年份');
ylabel('增长率(%)');
title('销售额增长趋势');
legend('年均增长率', '平均增长率');
hold off;

% 输出平均增长率
disp(['平均增长率:', num2str(mean_growth_rate, '%.2f'), '%']);

Running the above MATLAB code will result in a trend graph showing the annual and average growth rates of sales.

According to the given data, the average growth rate is 16.67%. Trend graphs can help us understand the growth trend of sales and whether there is a clear upward or downward trend.

It should be noted that this is just a simple example, and actual trend analysis may involve more data processing and statistical techniques, as well as selecting appropriate models for different situations.

Guess you like

Origin blog.csdn.net/shaozheng0503/article/details/131577775