数学建模——灰色预测模型Python代码“”
喷码机编辑器
this is a temporary script文件。
''
导入编号为NP
导入匹配
history _ data=[ 724.57,746.62,778.27,800.8,827.75,871.1,912.37,954.28,995.01,1037.2 ]
n=len(history_data ) )。
x0=NP.Array(history_data ) ) ) ) ) ) ) ) )。
#累积生成
history _ data _ agg=[ sum (history _ data [ 0: i1 ] ) forIinrange(n ) ]
x1=NP.Array(History_data_Agg ) ) ) ) ) ) ) x1=NP.Array(History_data_Agg ) ) ) ) ) ) ) x1 ) ) x1 ) x1 ) x1 ) NP )
#计算数据矩阵b和数据向量y
b=NP.zeros ([ n-1,2 ] ) ) )。
y=NP.zeros ([ n-1,1 ] ) )。
forIinrange(0,n-1 ) :
b[I][0]=-0.5*(x1[I]x1[I1] ) ) ) ) ) ) ) ) b ) ) ) ) ) b ) ) ) 652
B[i][1]=1
Y[i][0]=X0[i 1]
计算# GM (1,1 )微分方程的参数a和u
# a=NP.zeros ([ 2,1 ] ) )。
a=NP.LinaLG.inv(b.t.dot(b ) ).dot (b.t ).dot (y ) ) )。
a=A[0][0]
u=A[1][0]
建立灰色预测模型
x0=NP.Zeros(n ) ) )
XX0[0]=X0[0]
forIinrange(1,n ) :
x0[I]=(x0[0]-u/a ) (1- math.exp (a ) a ) ) math.exp(-a ) I );
#模型精度的后验差检查
求出e=0 #残差的平均值
forIinrange(0,n ) :
e=(x0[I]-xx0[I] ) ) )。
e /=n
#求出历史数据的平均值
aver=0;
forIinrange(0,n ) :
aver =X0[i]
aver /=n
#求出历史数据的方差
s12=0;
forIinrange(0,n ) :
S12=(x0[I]-aver ) **2;
s12 /=n
求残差方差
s22=0;
forIinrange(0,n ) :
S22=() x0[I]-xx0[I] )- e ) **2;
s22 /=n
求出#后验差之比
C=s22/s12
#求出小误差概率
cout=0
forIinrange(0,n ) :
ifABS(x0[I]-xx0[I]-e ) 0.6754*math.sqrt ) S12 ) :
cout=cout 1
else:
cout=出局
P=cout/n
if(c0.35andp0.95 ) :
#预测精度为一级
m=10 #请输入需要预测的年数
#print(‘()今后m年的负荷是) )
f=NP.Zeros(m ) )。
forIinrange(0,m ) :
f[I]=(x0[0]-u/a ) (math.exp(-a ) In ) ) ) ) ) ) ) ) )。
else:
print(‘“灰色预测法不适用”)