当前位置:   article > 正文

数学建模 | 灰色预测原理及python实现

灰色预测

目录

一、灰色预测的原理

二、灰色预测的应用及python实现


一、灰色预测的原理

灰色预测是以灰色模型为基础,灰色模型GM(n,h)是微分方程模型,可用于描述对象做长期、连续、动态的反应。其中,n代表微分方程式的阶数,h代表微分方程式的变化数目。在诸多的灰色模型中,以灰色系统中单序列一阶线性微分方程模型GM(1,1)最为常见。

下面说明GM(1,1)模式:

设有原始数据列X^{(0)}=\left\{x^{(0)}(1),x^{(0)}(2),...,x^{(0)}(n)\right\},n为数据个数,则可以根据以下步骤来建立GM(1,1)模型:

步骤一:

与原来统计累加以便减弱随机数据序列的波动性与 随机性,从而得到新数据序列:

X^{(1)}=\left\{x^{(1)}(1),x^{(1)}(2),...,x^{(1)}(n)\right\}

其中,x^{(1)}(k)中各数据表示前几项的相加,即:

x^{(1)}(k)=\sum_{i=1}^{k}x^{(0)}(i),k=1,2,...n

步骤二:

Z^{(1)}X^{(1)}的紧邻均值生成序列:

Z^{(1)}=\left\{z^{(1)}(2),z^{(1)}(3),...,z^{(1)}(n)\right\} 

其中,z^{(1)}(k)=0.5x^{(1)}(k)+0.5x^{(1)}(k-1) 

步骤三:

建立GM(1,1) 的灰微分方程为:

x^{(0)}(k)+az^{(1)}(k)=b

灰微分方程的白化方程为:

\tfrac{dx^{(1)}(t)}{dt}+ax^{(1)}(t)=b 

其中,a为发展系数,b为内生控制系数。

步骤四:

模型求解:构造矩阵B和向量Y:

B=\begin{bmatrix} -z^{(1)}(2) & 1\\ -z^{(1)}(3) &1 \\ ... & ...\\-z^{(1)}(n) &1 \end{bmatrix}   ,  Y=\begin{bmatrix} x^{(0)}(2)\\ x^{(0)}(3) \\... \\x^{(0)}(n) \end{bmatrix}

步骤五:

\hat{a}为待估参数向量,即:

\hat{a}=[ab]^{T}=(B^{T}B)^{-1}B^{T}Y 

 这是利用正规方程得到的闭式解

步骤六:

求解白化方程,可得到灰色预测的离散时间响应函数:

\hat{x}^{(1)}(t)=(x^{(1)}(0)-\frac{b}{a})e^{-ak}+\frac{b}{a}

那么相应的时间相应序列为:

\hat{x}^{(1)}(k+1)=(x^{(1)}(0)-\frac{b}{a})e^{-ak}+\frac{b}{a} 

x^{(1)}(0)=x^{(0)}(1),则所得的累加预测值为:

\hat{x}^{(1)}(k+1)=(x^{(0)}(1)-\frac{b}{a})e^{-ak}+\frac{b}{a},k=1,2,...,n-1 

将预测值还原为:

\hat{x}^{(0)}(k+1)=\hat{x}^{(1)}(k+1)-\hat{x}^{(1)}(k)=(x^{(0)}(1)-\frac{b}{a})(e^{-ak}-e^{-a(k-1)})=(x^{(0)}(1)-\frac{b}{a})(1-e^{a})e^{-ak} 

二、灰色预测的应用及python实现

某公司根据2015-2020年的产品的销售额,试构建GM(1,1)预测模型,并预测2021年的产品销售额。

原始数据为:X^{(0)}=(2.67,3.13,3.25,3.36,3.56,3.72) 

Python实现代码:

  1. import numpy as np
  2. class GM:
  3. def __init__(self,N,n,data):
  4. '''
  5. :param N: the number of data
  6. :param n: the number of data that is needs to be predicted
  7. :param data: time series
  8. '''
  9. self.N=N
  10. self.n=n
  11. self.data=data
  12. def prediction(self,a,b):
  13. '''
  14. :param a: parameter a of the grey prediction model
  15. :param b: parameter b of the grey prediction model
  16. :return: a list of prediction
  17. '''
  18. # a list to save n predition
  19. pre_data=[]
  20. # calculating the prediction
  21. for i in range(self.n):
  22. pre=(self.data[0]-b/a)*(1-np.exp(a))*np.exp(-a*(self.N+i))
  23. pre_data.append(pre[0])
  24. return pre_data
  25. def residual_test(self,a,b):
  26. '''
  27. :param a: parameter a of the grey prediction model
  28. :param b: parameter b of the grey prediction model
  29. '''
  30. # prediction od raw data
  31. pre_rawdata=self.sequence_prediction(a,b)
  32. # calculating absolute residual
  33. abs_residual=[]
  34. for i in range(self.N):
  35. r=abs(pre_rawdata[i]-self.data[i])
  36. abs_residual.append(r)
  37. # calculating relative residual
  38. rel_residual=[]
  39. for i in range(self.N):
  40. rel=abs_residual[i]/abs(self.data[0])
  41. rel_residual.append(rel)
  42. # calculating average relative residual
  43. avg_residual=0
  44. for i in range(self.N):
  45. avg_residual=avg_residual+rel_residual[i]
  46. avg_residual=avg_residual/self.N
  47. print("average relative residual: {}".format(avg_residual[0]))
  48. if avg_residual<0.01:
  49. print("model accuracy: excellent(Level I)")
  50. elif avg_residual<0.05:
  51. print("model accuracy: qualified(LevelⅡ)")
  52. elif avg_residual<0.10:
  53. print("model accuracy: barely qualified(Level Ⅲ)")
  54. else:
  55. print("model accuracy: unqualified(Level Ⅳ)")
  56. def sequence_prediction(self,a,b):
  57. '''
  58. :param a: parameter a of the grey prediction model
  59. :param b: parameter b of the grey prediction model
  60. :return: prediction of raw data
  61. '''
  62. pre_rawdata=[]
  63. pre_rawdata.append(self.data[0])
  64. for i in range(1,self.N):
  65. pre_raw=(self.data[0]-b/a)*(1-np.exp(a))*np.exp(-a*(i))
  66. pre_rawdata.append(pre_raw)
  67. return pre_rawdata
  68. def GM11(self):
  69. '''
  70. :return: n prediction if the grey prediction model can be used
  71. and parameter a and parameter b
  72. '''
  73. # accumulate raw data
  74. cumdata=[]
  75. for i in range(self.N):
  76. s=0
  77. for j in range(i+1):
  78. s=s+self.data[j]
  79. cumdata.append(s)
  80. # calculating the nearest neighbor mean generation sequence
  81. Z=[] # len(Z)=N-1
  82. for i in range(1,self.N):
  83. z=0.5*cumdata[i]+0.5*cumdata[i-1]
  84. Z.append(z)
  85. # construct data matrix B and data vector Y
  86. B=np.array([[-Z[i],1] for i in range(self.N-1)])
  87. Y=np.array([[self.data[i]] for i in range(1,self.N)])
  88. # calculating parameter a and parameter b
  89. A=np.dot(np.dot(np.linalg.inv(np.dot(B.T,B)),B.T),Y)
  90. a=A[0]
  91. b=A[1]
  92. # determine whether the grey prediction model can be used
  93. if (-1)*a>1:
  94. print("the grey prediction model can not be used")
  95. else:
  96. #print("a={}".format(a[0]))
  97. #print("b={}".format(b[0]))
  98. # derive a prediction model and predict
  99. pre_data=self.prediction(a,b)
  100. return pre_data,a,b
  101. if __name__=="__main__":
  102. data=[2.67,3.13,3.25,3.36,3.56,3.72]
  103. N=6
  104. n=1
  105. # create object
  106. grey_prediction=GM(N,n,data)
  107. # get prediction
  108. pre_data,a,b=grey_prediction.GM11()
  109. # get average relative residual
  110. avg_residual=grey_prediction.residual_test(a,b)
  111. print("predicted data: {}".format(pre_data[0]))

运行结果:

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号