数学建模——灰色预测模型Python代码
數學建?!疑A測模型Python代碼
“”"
Spyder Editor
This is a temporary script file.
“”"
import numpy as np
import math
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:i+1]) for i in range(n)]
X1 = np.array(history_data_agg)
#計算數據矩陣B和數據向量Y
B = np.zeros([n-1,2])
Y = np.zeros([n-1,1])
for i in range(0,n-1):
B[i][0] = -0.5*(X1[i] + X1[i+1])
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]
#建立灰色預測模型
XX0 = np.zeros(n)
XX0[0] = X0[0]
for i in range(1,n):
XX0[i] = (X0[0] - u/a)*(1-math.exp(a))math.exp(-a(i));
#模型精度的后驗差檢驗
e = 0 #求殘差平均值
for i in range(0,n):
e += (X0[i] - XX0[i])
e /= n
#求歷史數據平均值
aver = 0;
for i in range(0,n):
aver += X0[i]
aver /= n
#求歷史數據方差
s12 = 0;
for i in range(0,n):
s12 += (X0[i]-aver)**2;
s12 /= n
#求殘差方差
s22 = 0;
for i in range(0,n):
s22 += ((X0[i] - XX0[i]) - e)**2;
s22 /= n
#求后驗差比值
C = s22 / s12
#求小誤差概率
cout = 0
for i in range(0,n):
if abs((X0[i] - XX0[i]) - e) < 0.6754*math.sqrt(s12):
cout = cout+1
else:
cout = cout
P = cout / n
if (C < 0.35 and P > 0.95):
#預測精度為一級
m = 10 #請輸入需要預測的年數
#print(‘往后m各年負荷為:’)
f = np.zeros(m)
for i in range(0,m):
f[i] = (X0[0] - u/a)*(1-math.exp(a))math.exp(-a(i+n))
else:
print(‘灰色預測法不適用’)
總結
以上是生活随笔為你收集整理的数学建模——灰色预测模型Python代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数学建模——层次分析法Python代码
- 下一篇: 数学建模——逻辑回归模型Python代码