【Matlab数学建模】灰色预测模型

一、灰色预测的概念

  1982年我国学者邓聚龙教授发表第一篇中文论文《灰色控制系统》标志着灰色系统这一学科诞生。

  白色系统是指一个系统的内部特征已知的,即系统的信息是完全充分的。黑色系统是指一个系统的内部信息对外界来说是一无所知的,只能通过它与外界的联系来加以观测研究。

  灰色系统则介于二者之间。

二、灰色关联度与优势分析

  选取参考数列
X 0 = X 0 ( k ) ∣ k = 1 , 2 , ⋯   , n = ( X 0 ( 1 ) , X 0 ( 2 ) , ⋯   , X 0 ( n ) ) , 其 中 k 表 示 时 刻 X_0 = {X_0(k)|k = 1, 2, \cdots, n} = (X_0(1), X_0(2), \cdots, X_0(n)), 其中k表示时刻 X0=X0(k)k=1,2,,n=(X0(1),X0(2),,X0(n)),k
  假设有m个比较数列
X i = X i ( k ) ∣ k = 1 , 2 , ⋯   , n = ( X i ( 1 ) , X i ( 2 ) , ⋯   , X i ( n ) ) , ( i = 1 , 2 , ⋯   , m ) X_i = {X_i(k)|k = 1, 2, \cdots, n} = (X_i(1), X_i(2), \cdots, X_i(n)), (i = 1, 2, \cdots, m) Xi=Xi(k)k=1,2,,n=(Xi(1),Xi(2),,Xi(n)),(i=1,2,,m)
  则称:
ζ i ( k ) = min ⁡ i min ⁡ k ∣ X 0 ( k ) − X i ( k ) ∣ + ρ max ⁡ i max ⁡ k ∣ X 0 ( k ) − X i ( k ) ∣ ∣ X 0 ( k ) − X i ( k ) ∣ + ρ max ⁡ i max ⁡ k ∣ X 0 ( k ) − X i ( k ) ∣ \zeta_i(k) = \frac{\min_i\min_k|X_0(k) - X_i(k)| + \rho \max_i\max_k|X_0(k) - X_i(k)|}{|X_0(k) - X_i(k)| + \rho \max_i\max_k|X_0(k) - X_i(k)|} ζi(k)=X0(k)Xi(k)+ρmaximaxkX0(k)Xi(k)miniminkX0(k)Xi(k)+ρmaximaxkX0(k)Xi(k)
  为比较数列Xi对参考数列X0在k时刻的关联系数。其中 ρ ∈ [ 0 , + ∞ ) \rho\in [0, +\infty) ρ[0,+)分辨系数,一般情况下 ρ ∈ [ 0 , 1 ] \rho\in [0, 1] ρ[0,1]

  关联系数 ζ \zeta ζ在每一时刻都有一个,过于分散,不利于比较,为此我们给出:
r i 1 n ∑ k = 1 n ζ i ( k ) r_i \frac1n\sum\limits^n_{k = 1}\zeta_i(k) rin1k=1nζi(k)
  为比较数列Xi对参考数列X0关联度

  然而关联系数公式中的 ∣ X 0 ( k ) − X i ( k ) ∣ |X_0(k) - X_i(k)| X0(k)Xi(k)并不能去别处因素关联是正关联还是负关联,可采用以下方法解决该问题,记:
σ i = ∑ k = 1 n k X i ( k ) − ∑ k = 1 n X i ( k ) ∑ k = 1 n k n σ n = ∑ k = 1 n k 2 − ( ∑ k = 1 n k ) 2 n \sigma_i = \sum\limits^n_{k = 1}kX_i(k) - \sum\limits^n_{k = 1}X_i(k)\sum\limits^n_{k = 1}\frac k n \sigma_n = \sum\limits^n_{k = 1}k^2 - \frac{(\sum\limits^n_{k = 1}k)2}{n} σi=k=1nkXi(k)k=1nXi(k)k=1nnkσn=k=1nk2n(k=1nk)2

  则当 s i g n ( σ i σ n ) = s i g n ( σ j σ n ) sign(\frac{\sigma_i}{\sigma_n}) = sign(\frac{\sigma_j}{\sigma_n}) sign(σnσi)=sign(σnσj),则Xi和Xj正关联,否则为负关联

  代码实现:

function [zeta, r] = func(x, x_0, rho)
	
	[m, ~] = size(x);

	if nargin < 3
		rho = 0.5;
	end
	
	if nargin < 2
		x_0 = max(x);
	end
	
	x_abs = abs(x_0 - x);
	mn = min(min(x_abs));
	mx = max(max(x_abs));
	
	zeta = (mn + rho * mx) ./ (abs(repmat(x_0, m, 1) - x) + rho * mx);
	r = mean(zeta, 2);
	
end

三、灰色生成数列

  累加生成
设 原 始 数 列 : x ( 0 ) = ( x ( 0 ) ( 1 ) , x ( 0 ) ( 2 ) , ⋯   , x ( 0 ) ( n ) ) x ( 0 ) 的 一 次 累 加 数 列 : x ( 1 ) ( k ) = ∑ i = 1 k x ( 0 ) ( i ) , k = 1 , 2 , ⋯   , n , x ( 1 ) = ( x ( 1 ) ( 1 ) , x ( 1 ) ( 2 ) , ⋯   , x ( 1 ) ( n ) ) x ( 0 ) 的 r 次 累 加 数 列 : x ( r ) ( k ) = ∑ i = 1 k x ( r − 1 ) ( i ) , k = 1 , 2 , ⋯   , n , r ≥ 1 设原始数列:x^{(0)} = (x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n))\\ x^{(0)}的一次累加数列:x^{(1)}(k) = \sum^k_{i = 1}x^{(0)}(i), k = 1, 2, \cdots, n, \\ x^{(1)} = (x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n))\\ x^{(0)}的r次累加数列:x^{(r)}(k) = \sum^k_{i = 1}x^{(r - 1)}(i), k = 1, 2, \cdots, n, r ≥ 1 x(0)=(x(0)(1),x(0)(2),,x(0)(n))x(0):x(1)(k)=i=1kx(0)(i),k=1,2,,n,x(1)=(x(1)(1),x(1)(2),,x(1)(n))x(0)rx(r)(k)=i=1kx(r1)(i),k=1,2,,n,r1
  累减生成
设 原 始 数 列 : x ( 1 ) = ( x ( 1 ) ( 1 ) , x ( 1 ) ( 2 ) , ⋯   , x ( 1 ) ( n ) ) x ( 1 ) 的 一 次 累 减 数 列 : x ( 0 ) ( k ) = x ( 1 ) ( k ) − x ( 1 ) ( k − 1 ) , k = 2 , 3 , ⋯   , n , 设原始数列:x^{(1)} = (x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n))\\ x^{(1)}的一次累减数列:x^{(0)}(k) = x^{(1)}(k) - x^{(1)}(k - 1), k = 2, 3, \cdots, n, x(1)=(x(1)(1),x(1)(2),,x(1)(n))x(1)x(0)(k)=x(1)(k)x(1)(k1),k=2,3,,n,

function res = func(a, r)
	% a为原始数列
	% 若r为正数,则res为a的+r次累加数列
	% 若r为负数,则res为a的-r次累减数列

	if r == 0
		res = a;
		return
	end
	
	n = length(a);
	
	if r > 0
		a = func(a, r - 1);
		res = cumsum(a);
		return
	end
	
	if r < 0
		a = func(a, r + 1);
		res = a - [0, a(1 : length(a) - 1)];
		return
	end
	
end

  加权邻值生成
设 原 始 数 列 : x ( 0 ) = ( x ( 0 ) ( 1 ) , x ( 0 ) ( 2 ) , ⋯   , x ( 0 ) ( n ) ) 设原始数列:x^{(0)} = (x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n))\\ x(0)=(x(0)(1),x(0)(2),,x(0)(n))
  称 x ( 0 ) ( k − 1 ) , x ( 0 ) ( k ) x^{(0)}(k-1), x^{(0)}(k) x(0)(k1),x(0)(k)为原始数列的任意一对邻值。 x ( 0 ) ( k − 1 ) x^{(0)}(k-1) x(0)(k1)为后邻值, x ( 0 ) ( k ) x^{(0)}(k) x(0)(k)为前邻值。对于常数 α ∈ [ 0 , 1 ] \alpha\in[0, 1] α[0,1],设有:
z ( 0 ) ( k ) = α x ( 0 ) ( k ) + ( 1 − α ) x ( 0 ) ( k − 1 ) , k = 2 , 3 , ⋯   , n , z^{(0)}(k) = \alpha x^{(0)}(k) + (1 - \alpha)x^{(0)}(k - 1), k = 2, 3, \cdots, n, z(0)(k)=αx(0)(k)+(1α)x(0)(k1),k=2,3,,n,
  将数列 z ( 0 ) z^{(0)} z(0)称为数列 x ( 0 ) x^{(0)} x(0)在权 α \alpha α下的邻值生成数 α \alpha α被称为生成系数

  特别地,若 α = 0.5 \alpha = 0.5 α=0.5,则称:
z ( 0 ) ( k ) = 0.5 x ( 0 ) ( k ) + 0.5 x ( 0 ) ( k − 1 ) , k = 2 , 3 , ⋯   , n , z^{(0)}(k) = 0.5x^{(0)}(k) + 0.5x^{(0)}(k - 1), k = 2, 3, \cdots, n, z(0)(k)=0.5x(0)(k)+0.5x(0)(k1),k=2,3,,n,
  为均值生成数,也称等权邻值生成数

function z = func(x, alpha)
	
	if nargin < 2
		alpha = 0.5;
	end
	
	a = alpha * x;
	b = [0, (1 - alpha) * x(1 : length(x) - 1)];
	z = a + b;
	
end

四、灰色模型GM(1, 1)

① 灰色预测的相关概念

  G代表grey(灰色),M代表model(模型)

  设原始数列:
x ( 0 ) = ( x ( 0 ) ( 1 ) , x ( 0 ) ( 2 ) , ⋯   , x ( 0 ) ( n ) x^{(0)} = (x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n) x(0)=(x(0)(1),x(0)(2),,x(0)(n)
   x ( 0 ) x^{(0)} x(0)的一次累加数列:
x ( 1 ) ( k ) = ∑ i = 1 k x ( 0 ) ( i ) , k = 1 , 2 , ⋯   , n , x ( 1 ) = ( x ( 1 ) ( 1 ) , x ( 1 ) ( 2 ) , ⋯   , x ( 1 ) ( n ) ) x^{(1)}(k) = \sum^k_{i = 1}x^{(0)}(i), k = 1, 2, \cdots, n, \\ x^{(1)} = (x^{(1)}(1), x^{(1)}(2), \cdots, x^{(1)}(n))\\ x(1)(k)=i=1kx(0)(i),k=1,2,,n,x(1)=(x(1)(1),x(1)(2),,x(1)(n))
  定义 x ( 1 ) x^{(1)} x(1)灰导数为:
d ( k ) = x ( 0 ) ( k ) = x ( 1 ) ( k ) − x ( 1 ) ( k − 1 ) d(k) = x^{(0)}(k) = x^{(1)}(k) - x^{(1)}(k - 1) d(k)=x(0)(k)=x(1)(k)x(1)(k1)
  令 z ( 1 ) z^{(1)} z(1)为数列 x ( 1 ) x^{(1)} x(1)的邻值生成数列,即:
z ( 1 ) ( k ) = α x ( 1 ) ( k ) + ( 1 − α ) x ( 1 ) ( k − 1 ) z^{(1)}(k) = \alpha x^{(1)}(k) + (1 - \alpha)x^{(1)}(k - 1) z(1)(k)=αx(1)(k)+(1α)x(1)(k1)
  于是定义GM(1, 1)的灰微分方程模型为:
d ( k ) + a z ( 1 ) ( k ) = b x ( 0 ) ( k ) + a z ( 1 ) ( k ) = b d(k) + az^{(1)}(k) = b\\ x^{(0)}(k) + az^{(1)}(k) = b d(k)+az(1)(k)=bx(0)(k)+az(1)(k)=b
  其中 x ( 0 ) ( k ) x^{(0)}(k) x(0)(k)称为灰导数,a称为发展系数 z ( 1 ) ( k ) z^{(1)}(k) z(1)(k)称为白化背景值,a称为灰作用量

  取 k = 2 , 3 , ⋯   , n , k = 2, 3, \cdots, n, k=2,3,,n,由灰微分方程可得:
{ d ( 2 ) + a z ( 1 ) ( 2 ) = b d ( 3 ) + a z ( 1 ) ( 3 ) = b ⋯ ⋯ ⋯ ⋯ ⋯ ⋯ d ( n ) + a z ( 1 ) ( n ) = b \left\{ \begin{aligned} d(2) + az^{(1)}(2) = b\\ d(3) + az^{(1)}(3) = b\\ \cdots\cdots\cdots\cdots\cdots\cdots\\ d(n) + az^{(1)}(n) = b\\ \end{aligned} \right. d(2)+az(1)(2)=bd(3)+az(1)(3)=bd(n)+az(1)(n)=b
  引入矩阵:
u = ( a b ) u = \begin{pmatrix} a\\ b\\ \end{pmatrix} u=(ab)
Y = ( x ( 0 ) ( 2 ) x ( 0 ) ( 3 ) ⋮ x ( 0 ) ( n ) ) Y = \begin{pmatrix} x^{(0)}(2)\\ x^{(0)}(3)\\ \vdots\\ x^{(0)}(n)\\ \end{pmatrix} Y=x(0)(2)x(0)(3)x(0)(n)
B = ( − z ( 1 ) 1 − z ( 2 ) 1 ⋮ ⋮ − z ( n ) 1 ) B = \begin{pmatrix} -z^{(1)} & 1\\ -z^{(2)} & 1\\ \vdots&\vdots\\ -z^{(n)} & 1\\ \end{pmatrix} B=z(1)z(2)z(n)111
  于是GM(1, 1)模型可以表示为:
Y = B u Y = Bu Y=Bu
u ^ = ( a ^ b ^ ) = ( B T B ) − 1 B T Y \hat{u} = \begin{pmatrix} \hat{a}\\ \hat{b}\\ \end{pmatrix}= (B^TB)^{-1}B^TY u^=(a^b^)=(BTB)1BTY
  对于GM(1, 1)的灰微分方程,如果将 x ( 0 ) ( k ) x^{(0)}(k) x(0)(k)的时刻k换成连续变量t,则对应的白微分方程即是:
d x ( 1 ) ( t ) d t + a x ( 1 ) ( t ) = b \frac{dx^{(1)}(t)}{dt} + ax^{(1)}(t) = b dtdx(1)(t)+ax(1)(t)=b
【Matlab数学建模】灰色预测模型_第1张图片

② 灰色预测的步骤

第一步:级比检验

  设原始数列为:
x ( 0 ) = ( x ( 0 ) ( 1 ) , x ( 0 ) ( 2 ) , ⋯   , x ( 0 ) ( n ) x^{(0)} = (x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n) x(0)=(x(0)(1),x(0)(2),,x(0)(n)
  计算数列的级比
λ ( k ) = x ( 0 ) ( k − 1 ) x ( 0 ) ( k ) \lambda(k) = \frac{x^{(0)}(k - 1)}{x^{(0)}(k)} λ(k)=x(0)(k)x(0)(k1)
  如果所有的级比都落在可覆盖区间 X = ( e − 2 n + 1 , e 2 n + 1 ) X = (e^{\frac{-2}{n + 1}}, e^{\frac{2}{n + 1}}) X=(en+12,en+12)内,则原始数列 x ( 0 ) x^{(0)} x(0)可以建立GM(1, 1)模型并进行灰色预测,否则需要对数据进行变化处理,如平移变化:
取 c 使 得 数 列 : y ( 0 ) ( k ) = x ( 0 ) + c , k = 1 , 2 , ⋯   , n , ∀ k ∈ Z + , y ( 0 ) ( k ) ∈ ( e − 2 n + 1 , e 2 n + 1 ) 取c使得数列:\\ y^{(0)}(k) = x^{(0)} + c, k = 1, 2, \cdots, n, \\ \forall k \in Z_+, y^{(0)}(k) \in (e^{\frac{-2}{n + 1}}, e^{\frac{2}{n + 1}}) c使y(0)(k)=x(0)+c,k=1,2,,n,kZ+,y(0)(k)(en+12,en+12)

第二步:建立GM(1, 1)模型

  原始数列为:
x ( 0 ) = ( x ( 0 ) ( 1 ) , x ( 0 ) ( 2 ) , ⋯   , x ( 0 ) ( n ) x^{(0)} = (x^{(0)}(1), x^{(0)}(2), \cdots, x^{(0)}(n) x(0)=(x(0)(1),x(0)(2),,x(0)(n)
  为它建立GM(1, 1)模型:
x ( 0 ) ( k ) + a z ( 1 ) ( k ) = b x^{(0)}(k) + az^{(1)}(k) = b x(0)(k)+az(1)(k)=b
  对应的白化模型是:
d x ( 1 ) ( t ) d t + a x ( 1 ) ( t ) = b \frac{dx^{(1)}(t)}{dt} + ax^{(1)}(t) = b dtdx(1)(t)+ax(1)(t)=b
  该微分方程的解是:
x ( 1 ) ( t ) = ( x ( 0 ) ( 1 ) − b a ) e − a ( t − 1 ) + b a x^{(1)}(t) = (x^{(0)}(1) - \frac b a)e^{-a(t - 1)} + \frac b a x(1)(t)=(x(0)(1)ab)ea(t1)+ab
  于是便能得到预测值:
x ^ ( 1 ) ( k + 1 ) = ( x ( 0 ) ( 1 ) − b a ) e − a k + b a , k = 1 , 2 , ⋯   , n − 1 x ^ ( 0 ) ( k + 1 ) = x ^ ( 1 ) ( k + 1 ) − x ^ ( 1 ) ( k ) , k = 1 , 2 , ⋯   , n − 1 \hat{x}^{(1)}(k + 1) = (x^{(0)}(1) - \frac b a)e^{-ak} + \frac b a, k = 1, 2, \cdots, n - 1\\ \hat{x}^{(0)}(k + 1) = \hat{x}^{(1)}(k + 1) - \hat{x}^{(1)}(k), k = 1, 2, \cdots, n - 1 x^(1)(k+1)=(x(0)(1)ab)eak+ab,k=1,2,,n1x^(0)(k+1)=x^(1)(k+1)x^(1)(k),k=1,2,,n1

function [u, f] = func(x, alpha)
	% x是原始数列
	% alpha是生成系数,默认为0.5
	% f是预测数列的一次生成数列的函数

	if nargin < 2
		alpha = 0.5;
	end
	
	x_1 = cumsum(x);
	z = alpha * x_1(2 : end) + (1 - alpha) * x_1(1 : end - 1);
	u = [-z; ones(1, length(x) - 1)]' \ x(2 : end)';
	f = @(k) (x(1) - u(2) /u(1)) * exp(-u(1) * k + u(1)) + u(2) / u(1);
	
end
第三步:检验预测值

  ① 残差检验:

  称:
ε ( k ) = x ( 0 ) ( k ) − x ^ ( 0 ) ( k ) x ( 0 ) ( k ) , k = 1 , 2 , ⋯   , n ε(k) = \frac{x^{(0)}(k) - \hat{x}^{(0)}(k)}{x^{(0)}(k)}, k = 1, 2, \cdots, n ε(k)=x(0)(k)x(0)(k)x^(0)(k),k=1,2,,n
  为相对残差

  如果 对 ∀ k = 1 , 2 , ⋯   , n , ∣ ε ( k ) < 0.1 ∣ 对\forall k = 1, 2, \cdots, n, |ε(k) < 0.1| k=1,2,,n,ε(k)<0.1,则认为达到较高的要求。否则,若 对 ∀ k = 1 , 2 , ⋯   , n , ∣ ε ( k ) < 0.2 ∣ 对\forall k = 1, 2, \cdots, n, |ε(k) < 0.2| k=1,2,,n,ε(k)<0.2,则认为达到一般要求。

  ② 级比偏差值检验

  计算:
ρ ( k ) = 1 − 1 − 0.5 a 1 + 0.5 a λ ( k ) \rho(k) = 1 - \frac{1 - 0.5a}{1 + 0.5a}\lambda(k) ρ(k)=11+0.5a10.5aλ(k)

  如果 对 ∀ k = 2 , 3 , ⋯   , n , ∣ ρ ( k ) < 0.1 ∣ 对\forall k = 2, 3, \cdots, n, |\rho(k) < 0.1| k=2,3,,n,ρ(k)<0.1,则认为达到较高的要求。否则,若 对 ∀ k = 2 , 3 , ⋯   , n , ∣ ρ ( k ) < 0.2 ∣ 对\forall k = 2, 3, \cdots, n, |\rho(k) < 0.2| k=2,3,,n,ρ(k)<0.2,则认为达到一般要求。

你可能感兴趣的:(数学建模,matlab,线性代数,机器学习)