石家庄网站建设网站建设,网站的制作哪家好,凡客建站登录,企业所得税最新政策目录
一、灰色预测的原理
二、灰色预测的应用及python实现 一、灰色预测的原理
灰色预测是以灰色模型为基础#xff0c;灰色模型GM(n,h)是微分方程模型#xff0c;可用于描述对象做长期、连续、动态的反应。其中#xff0c;n代表微分方程式的阶数#xff0c;h代表微分方…目录
一、灰色预测的原理
二、灰色预测的应用及python实现 一、灰色预测的原理
灰色预测是以灰色模型为基础灰色模型GM(n,h)是微分方程模型可用于描述对象做长期、连续、动态的反应。其中n代表微分方程式的阶数h代表微分方程式的变化数目。在诸多的灰色模型中以灰色系统中单序列一阶线性微分方程模型GM(1,1)最为常见。
下面说明GM(1,1)模式
设有原始数据列n为数据个数则可以根据以下步骤来建立GM(1,1)模型
步骤一
与原来统计累加以便减弱随机数据序列的波动性与 随机性从而得到新数据序列 其中中各数据表示前几项的相加即 步骤二
令为的紧邻均值生成序列 其中
步骤三
建立GM(1,1) 的灰微分方程为 灰微分方程的白化方程为 其中a为发展系数b为内生控制系数。
步骤四
模型求解构造矩阵B和向量Y 步骤五
设为待估参数向量即 这是利用正规方程得到的闭式解。
步骤六
求解白化方程可得到灰色预测的离散时间响应函数 那么相应的时间相应序列为 取则所得的累加预测值为 将预测值还原为 二、灰色预测的应用及python实现
某公司根据2015-2020年的产品的销售额试构建GM(1,1)预测模型并预测2021年的产品销售额。
原始数据为
Python实现代码
import numpy as npclass GM:def __init__(self,N,n,data)::param N: the number of data:param n: the number of data that is needs to be predicted:param data: time seriesself.NNself.nnself.datadatadef prediction(self,a,b)::param a: parameter a of the grey prediction model:param b: parameter b of the grey prediction model:return: a list of prediction# a list to save n preditionpre_data[]# calculating the predictionfor i in range(self.n):pre(self.data[0]-b/a)*(1-np.exp(a))*np.exp(-a*(self.Ni))pre_data.append(pre[0])return pre_datadef residual_test(self,a,b)::param a: parameter a of the grey prediction model:param b: parameter b of the grey prediction model# prediction od raw datapre_rawdataself.sequence_prediction(a,b)# calculating absolute residualabs_residual[]for i in range(self.N):rabs(pre_rawdata[i]-self.data[i])abs_residual.append(r)# calculating relative residualrel_residual[]for i in range(self.N):relabs_residual[i]/abs(self.data[0])rel_residual.append(rel)# calculating average relative residualavg_residual0for i in range(self.N):avg_residualavg_residualrel_residual[i]avg_residualavg_residual/self.Nprint(average relative residual: {}.format(avg_residual[0]))if avg_residual0.01:print(model accuracy: excellent(Level I))elif avg_residual0.05:print(model accuracy: qualified(LevelⅡ))elif avg_residual0.10:print(model accuracy: barely qualified(Level Ⅲ))else:print(model accuracy: unqualified(Level Ⅳ))def sequence_prediction(self,a,b)::param a: parameter a of the grey prediction model:param b: parameter b of the grey prediction model:return: prediction of raw datapre_rawdata[]pre_rawdata.append(self.data[0])for i in range(1,self.N):pre_raw(self.data[0]-b/a)*(1-np.exp(a))*np.exp(-a*(i))pre_rawdata.append(pre_raw)return pre_rawdatadef GM11(self)::return: n prediction if the grey prediction model can be usedand parameter a and parameter b# accumulate raw datacumdata[]for i in range(self.N):s0for j in range(i1):ssself.data[j]cumdata.append(s)# calculating the nearest neighbor mean generation sequenceZ[] # len(Z)N-1for i in range(1,self.N):z0.5*cumdata[i]0.5*cumdata[i-1]Z.append(z)# construct data matrix B and data vector YBnp.array([[-Z[i],1] for i in range(self.N-1)])Ynp.array([[self.data[i]] for i in range(1,self.N)])# calculating parameter a and parameter bAnp.dot(np.dot(np.linalg.inv(np.dot(B.T,B)),B.T),Y)aA[0]bA[1]# determine whether the grey prediction model can be usedif (-1)*a1:print(the grey prediction model can not be used)else:#print(a{}.format(a[0]))#print(b{}.format(b[0]))# derive a prediction model and predictpre_dataself.prediction(a,b)return pre_data,a,bif __name____main__:data[2.67,3.13,3.25,3.36,3.56,3.72]N6n1# create objectgrey_predictionGM(N,n,data)# get predictionpre_data,a,bgrey_prediction.GM11()# get average relative residualavg_residualgrey_prediction.residual_test(a,b)print(predicted data: {}.format(pre_data[0]))
运行结果