2023 Mathematical Modeling National Competition: Gray Prediction Model and MATLAB Actual Case (Matlab Code)

After subscribing to the column, ideas and Matlab codes will be shared during the competition in September

Gray forecast model (Gray Forecast Model, GM) is a method based on a small amount of incomplete information to establish a mathematical model and make predictions. Scientific prediction is crucial when solving practical problems, formulating development strategies and policies, and making major decisions. This article will introduce the basic principles of the gray forecasting model, and show how to use MATLAB to implement the gray forecasting model through actual combat cases.

Basic Principles of Gray Forecasting Model

The core of the gray forecasting model (GM) is to establish a gray differential equation to predict future trends through interpolation and fitting. GM(1,1) is the most common gray prediction model, and its basic steps are as follows:

  1. Data preprocessing: cumulative generation of raw data to eliminate data fluctuations and irregularities.
  2. Establish differential equations: use accumulated data to establish gray differential equations.
  3. Solving Differential Equations: Solve the parameters of differential equations by the method of least squares.
  4. Forecast: Forecast future data based on the found parameters.
  5. Data post-processing: Carry out cumulative reduction and restoration of the forecast results to obtain the actual forecast value.

Realization of Gray Forecasting Model in MATLAB

Here is a simple MATLAB implementation for computing the GM(1,1) model:

function [predict, a, b] = gray_forecast(data, num_forecast)
    % 数据预处理
    n = length(data);
    X0 = data;
    X1 = cumsum(X0);
    
    % 建立微分方程
    B = [-0.5 * (X1(1:n-1) + X1(2:n))', ones(n-1,1)];
    Y = X0(2:n)';
    
    % 求解微分方程
    u = (B' * B) \ B' * Y;
    a = u(1);
    b = u(2);
    
    % 预测
    X1_predict = zeros(1, n + num_forecast);
    X1_predict(1) = X1(1);
    for i = 2:(n + num_forecast)
        X1_predict(i) = (X0(1) - b / a) * exp(-a * (i - 1)) + b / a;
    end
    
    % 数据后处理
    predict = diff([0, X1_predict]);
end

Practical Case: House Price Forecast

Suppose we have house price data for the past 10 years, and we want to use the gray forecasting model to predict house prices for the next 3 years. The data are as follows (unit: ten thousand yuan):

data = [5000, 5400, 5800, 6300, 6700, 7200, 7600, 8100, 8700, 9200];

Next, we will use the function implemented above gray_forecastto predict house prices for the next 3 years:

num_forecast = 3;
[predict, a, b] = gray_forecast(data, num_forecast);

% 输出预测结果
disp('未来3年的房价预测(单位:万元):');
disp(predict(end - num_forecast + 1:end));

aIn this example, MATLAB will calculate the parameter sum of the gray forecasting model based on the given data b, and predict the house price in the next 3 years. Suppose we get the following predictions:

未来3年的房价预测(单位:万元):
   9744.57
  10298.16
  10861.35

This means that according to the gray prediction model, the housing prices in the next three years are expected to be 97.4457 million yuan, 102.9816 million yuan and 108.6135 million yuan respectively.

Note that gray forecasting models (GM) are generally suitable for short-term forecasting and when there is less data. For long-term forecasts or situations with large amounts of data, other forecasting models (such as time series models, machine learning models, etc.) may be more appropriate. At the same time, the gray forecasting model is also very important for data preprocessing and postprocessing to ensure the accuracy of the forecasting results.

Practical Case: Stock Price Prediction

In this case, we will use the gray forecasting model to predict the stock price. First, we need to obtain historical stock price data. The following is a simple MATLAB code to get the historical closing price of a stock using Yahoo Finance API:

function prices = get_stock_prices(ticker, start_date, end_date)
    url = sprintf("https://query1.finance.yahoo.com/v7/finance/download/%s?period1=%d&period2=%d&interval=1d&events=history&includeAdjustedClose=true", ticker, posixtime(datetime(start_date,'ConvertFrom','yyyymmdd')), posixtime(datetime(end_date,'ConvertFrom','yyyymmdd')));
    data = webread(url);
    prices = str2double(regexp(data, '(?<=")[^"]*(?=")', 'match'));
    prices = prices(~isnan(prices));
end

Taking Apple (AAPL) as an example, get the closing price data from January 1, 2018 to December 31, 2021:

ticker = 'AAPL';
start_date = '20180101';
end_date = '20211231';
prices = get_stock_prices(ticker, start_date, end_date);

Next, we will use the gray forecasting model to predict the stock price for the next 30 days:

num_forecast = 30;
[predict, a, b] = gray_forecast(prices, num_forecast);

% 输出预测结果
disp(sprintf('未来30天%s的股票价格预测:', ticker));
disp(predict(end - num_forecast + 1:end));

Please note that this case is only used to demonstrate the application of the gray forecasting model in stock price forecasting. The actual stock price is affected by many factors, such as market sentiment, company fundamentals, policy factors, etc. The gray forecasting model may not be enough to accurately predict the stock price . In practical applications, you can try to use other forecasting models, such as time series models, machine learning models, etc., and combine multiple factors for comprehensive analysis.

Practical Case: Power Demand Forecasting

Power demand forecasting is of great significance to power companies and government departments, and can guide power production and supply planning. In this case, we will use the gray forecasting model to predict the monthly electricity demand for the coming year.

Suppose we have monthly electricity demand data (unit: million kWh) for the past 5 years:

data = [2000, 2100, 2200, 2300, 2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100, ...
        3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900, 4000, 4100, 4200, 4300, ...
        4400, 4500, 4600, 4700, 4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500, ...
        5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300, 6400, 6500, 6600, 6700, ...
        6800, 6900, 7000, 7100, 7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900];

Next, we will use the gray forecasting model to predict the monthly electricity demand for the coming year:

num_forecast = 12;
[predict, a, b] = gray_forecast(data, num_forecast);

% 输出预测结果
disp('未来一年的月度电力需求预测(单位:百万千瓦时):');
disp(predict(end - num_forecast + 1:end));

Suppose we get the following predictions:

未来一年的月度电力需求预测(单位:百万千瓦时):
   8022.36
   8135.93
   8249.49
   8363.06
   8476.62
   8590.19
   8703.75
   8817.32
   8930.88
   9044.45
   9158.01
   9271.58

This means that according to the gray forecast model, the monthly electricity demand in the coming year is expected to be 8022.36 million kWh, 8135.93 million kWh, ... , and 9271.58 million kWh respectively.

Please note that the gray forecasting model may be affected by factors such as seasonality and emergencies in power demand forecasting. In practical applications, you can consider using other forecasting models, such as time series models, machine learning models, etc., and combine multiple factors for comprehensive analysis. For example, in electricity demand forecasting, the following factors can be considered:

  1. Seasonality: Electricity demand is often affected by seasonal changes, such as in summer due to increased use of air conditioning, electricity demand may rise. Therefore, seasonal factors can be considered in the forecast.

  2. Weather conditions: Extreme weather (such as high temperature, low temperature, heavy rain, etc.) may cause a sudden increase or decrease in power demand. Weather factors can be considered in the forecast.

  3. Economic Development: As the economy develops, electricity demand may gradually increase. Economic development indicators could be considered in the forecasts.

  4. Energy Conservation Policies: Governments and businesses may implement energy conservation policies to reduce electricity demand. Energy conservation policy factors can be considered in the forecast.

  5. Population change: An increase or decrease in the population can affect electricity demand. Population changes could be factored into the projections.

In practical applications, you can try to incorporate these factors into the forecasting model, and use multiple forecasting methods for comparison and verification to improve the forecasting accuracy. At the same time, the prediction results are constantly revised and optimized to adapt to the changing actual situation.

Application of Gray Prediction Model in Other Fields

In addition to being used in house price forecasting, the gray forecasting model can also be widely used in other fields, such as economic forecasting, energy demand forecasting, environmental protection, transportation, etc. Here are some practical application examples:

  1. Economic growth forecast: Governments and enterprises can use gray forecasting models to analyze economic growth trends, so as to formulate targeted economic policies and development strategies.

  2. Energy demand forecasting: Energy companies and government departments can use gray forecasting models to predict future energy demand to guide energy production and supply planning.

  3. Environmental Protection Prediction: Environmental protection departments can use the gray forecasting model to predict pollutant discharge, environmental quality and other indicators to evaluate the effectiveness of environmental protection policies and formulate new environmental protection measures.

  4. Transportation Demand Forecast: The transportation department can use the gray forecasting model to predict future transportation demand to guide transportation infrastructure construction and transportation planning.

It should be noted that the gray forecasting model is mainly suitable for short-term forecasting and situations with less data. For long-term forecasting or situations with more data, other forecasting models, such as time series models and machine learning models, can be considered. In practical applications, the appropriate prediction method can be selected according to the specific situation, while paying attention to the pre-processing and post-processing of the data to ensure the accuracy of the prediction results.

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/130478737