1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| import numpy as np import math import matplotlib.pyplot as plt from copy import deepcopy import plotly.graph_objects as go plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False np.set_printoptions(threshold=np.inf) np.set_printoptions(suppress=True)
X = np.array([-18, 0.34, 4.68, 8.49, 29.84, 50.21, 77.65, 109.36]) x = np.array([[83.0, 79.8, 78.1, 85.1, 86.6, 88.2, 90.3, 86.7, 93.3, 92.5, 90.9, 96.9], [101.7, 85.1, 87.8, 91.6, 93.4, 94.5, 97.4, 99.5, 104.2, 102.3, 101.0, 123.5], [92.2, 114.0, 93.3, 101.0, 103.5, 105.2, 109.5, 109.2, 109.6, 111.2, 121.7, 131.3], [105.0, 125.7, 106.6, 116.0, 117.6, 118.0, 121.7, 118.7, 120.2, 127.8, 121.8, 121.9], [139.3, 129.5, 122.5, 124.5, 135.7, 130.8, 138.7, 133.7, 136.8, 138.9, 129.6, 133.7], [137.5, 135.3, 133.0, 133.4, 142.8, 141.6, 142.9, 147.3, 159.6, 162.1, 153.5, 155.9]]) def grey_model(X,form,y): x_0 = deepcopy(X) x_n = [X[i] / X[i + 1] for i in range(len(X) - 1)] if any(n <= math.exp(-2 / (len(X) + 1)) for n in x_n)==True or any(n >= math.exp(-2 / (len(X) + 2)) for n in x_n)==True: print('______未通过级比检验______') i = 0 while True: X += 10 x_n_new = [X[i] / X[i + 1] for i in range(len(X) - 1)] if any(n <= np.exp(-2 / (len(X) + 1)) for n in x_n_new)==True or any(n >= np.exp(2 / (len(X) + 1)) for n in x_n_new)==True: i += 1 continue else: print('修正数列为:\n',X) sc = 10*(i+1) print('修正值为:\n',sc) break else: print("_______通过级比检验______")
X_sum = X.cumsum()
z_n = [(X_sum[i] + X_sum[i + 1]) / 2 for i in range(len(X_sum)-1)]
Y = [X[i] for i in range(1,len(X))] Y = np.array(Y) Y = Y.reshape(-1,1)
B = [-z_n[i] for i in range(len(z_n))] B = np.array(B) B = B.reshape(-1,1) c = np.ones((len(B),1)) B = np.hstack((B,c))
parameters = np.linalg.inv(B.T.dot(B)).dot(B.T).dot(Y) a = parameters[0,0] b = parameters[1,0] print("a=",a) print("b=",b)
b_a = b/a model_predict = [(X[0] - b_a) * np.exp(-a * k) + b_a for k in range(len(X)+form)]
list_predict = np.concatenate(([model_predict[0]], np.diff(model_predict))) - sc
print("预测数列为:\n",list_predict) list_predict = [float(format(i,'.4f')) for i in list_predict] if y == 1: fig = go.Figure() fig.add_trace(go.Scatter(x=list(range(len(X))),y=x_0, mode="markers+lines+text",text=x_0,textposition="top center", name='原数列')) fig.add_trace(go.Scatter(x=list(range(len(X)+form)),y=list_predict, mode="markers+lines+text",text=list_predict,textposition="top center", name='预测数列')) fig.update_layout(title="灰色预测", xaxis=dict(title="时间序号"), yaxis=dict(title="值"), title_x=0.5, title_y=0.94, template="plotly") fig.show()
G = np.array(list_predict[:len(list_predict)-form]) e = x_0 - G q = abs(e / x_0) print("相对误差数列为:\n",q) q_q = q.sum()/(len(q)-1) print(f"平均相对误差为:\n{q_q:.4f}")
S0 = np.sqrt(np.var(x_0)) S1 = np.sqrt(np.var(e)) print('后验差比值C为:\n',S1 / S0)
E = e.sum()/(len(e)-1) yu_zhi = 0.6745*S0 g = 0 for i in range(len(e)): if abs(e[i]-E) < yu_zhi: g += 1 p = g/len(e) print('小概率误差为:\n',p)
list_p = list_predict[-form:] return list_p
def grey_models(x): av = np.array([np.mean(x[i]) for i in range(len(x))]) av_predict = grey_model(av,1,0) sum_predict = len(x[0]) * av_predict[0] x_x = x.T
pre = np.array([grey_model(x_x[i],1,0) for i in range(len(x_x))]) pre = pre.ravel()
c_predict = np.array(pre/sum(pre)) c_predict = c_predict.ravel() predict = c_predict * sum_predict return predict
list_p = grey_model(X,3,1) print(list_p)
|