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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
| import copy from math import log import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error np.set_printoptions(threshold=np.inf) np.set_printoptions(suppress=True) import warnings warnings.filterwarnings("ignore") plt.rcParams['axes.unicode_minus'] = False plt.rcParams['font.family'] = ['sans-serif'] plt.rcParams['font.sans-serif'] = ['SimHei']
def information(df,df0,p): n = len(df) mse = mean_squared_error(df,df0) aicc = log(mse) + (n+p)/(n-p-2) bic = n * log(mse) + p * log(n) return aicc,bic
def AR_prediction(x, p, predict_x_n = 0): df = np.array(x).ravel() df0 = df.copy() Y = df[p:].copy() h = len(df0)-p X = np.zeros((h,p)) for i in range(h): for v in range(1,p+1): X[i,-v] = df[i+v-1] sigma = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Y.T) for i in range(h): df0[p+i] = sum(np.multiply(sigma , X[i])) df00 = df.copy() df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = sum(np.multiply(sigma , df00[-p:][::-1])) df00 = np.append(df00,df1[i]) return df0,df1,sigma def SAR(x,p,s,predict_x_n=0): x = np.array(x).ravel() df0 = x.copy() Y = x[s*p:].copy() h = len(x) - p*s X = np.zeros((h,p)) for t in range(h): for i in range(1,p+1): X[t][-i] = x[p*s+t-i*s] sigma = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Y.T) for i in range(h): df0[p*s+i] = sum(sigma * X[i]) df00 = x.copy() df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = sum(np.multiply(sigma , df00[-p:][::-1])) df00 = np.append(df00,df1[i]) return df0,df1,sigma
def p_finally(n): jieguo = [] for i in range(1,n): df0,df1 = AR_prediction(data,i) aicc,bic = information(data,df0,i) jieguo.append([i,aicc,bic]) jieguo_aicc = sorted(jieguo,reverse=False, key=lambda x: x[1]) jieguo_bic = sorted(jieguo,reverse=False, key=lambda x: x[2]) return jieguo_aicc[0][0],jieguo_bic[0][0]
def MA_prediction(data, q, predict_x_n=0): def sma(x): return np.mean(x) def wma(x): x = np.array(x).ravel() w = np.arange(1,len(x)+1) return np.sum(x*w)/(len(x)*(len(x)+1)/2) def ema(x,i): x = np.array(x).ravel() if i < 1 and i > 0: l = len(x) w = np.logspace(l, 0, num=l, base=(1-i)) else: print('平滑因子范围错误') return np.sum(x*w)/np.sum(w) df = np.array(data).ravel() df0 = df.copy() for i in range(len(df0)-q+1): df[q+i-1] = ema(df0[i:i+q],0.5)
df_wu = df0 - df
df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = ema(df0[-q:],0.5) df0 = np.append(df0,df1[i]) def MA_AR_prediction(x, p, predict_x_n = 0): df = np.array(x[p:]).ravel() df0 = df.copy() Y = df[p:].copy() h = len(df0)-p X = np.zeros((h,p)) for i in range(h): for v in range(1,p+1): X[i,-v] = df[i+v-1] sigma = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Y.T) for i in range(h): df0[p+i] = sum(np.multiply(sigma , X[i])) df00 = df.copy() df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = sum(np.multiply(sigma , df00[-p:][::-1])) df00 = np.append(df00,df1[i]) return df0,df1,sigma wucha,wucha_predict,sigma = MA_AR_prediction(df_wu,q,predict_x_n) return wucha,wucha_predict,sigma,df_wu
def SMA(data,q,s,predict_x_n=0): def sma(x): return np.mean(x) def wma(x): x = np.array(x).ravel() w = np.arange(1,len(x)+1) return np.sum(x*w)/(len(x)*(len(x)+1)/2) def ema(x,i): x = np.array(x).ravel() if i < 1 and i > 0: l = len(x) w = np.logspace(l, 0, num=l, base=(1-i)) else: print('平滑因子范围错误') return np.sum(x*w)/np.sum(w) df = np.array(data).ravel() df0 = df.copy() h = len(data) - q*s X = np.zeros((h,q)) for t in range(h): for i in range(1,q+1): X[t][-i] = df[q*s+t-i*s] for i in range(h): df[q*s+i] = ema(X[i],0.5)
df_wu = df0 - df
df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = ema(df0[-q:],0.5) df0 = np.append(df0,df1[i]) def SAR(x,p,s,predict_x_n=0): x = np.array(x[p*s:]).ravel() df0 = x.copy() Y = x[s*p:].copy() h = len(x) - p*s X = np.zeros((h,p)) for t in range(h): for i in range(1,p+1): X[t][-i] = x[p*s+t-i*s] sigma = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(Y.T) for i in range(h): df0[p*s+i] = sum(np.multiply(sigma , X[i])) df00 = x.copy() df1 = np.zeros(predict_x_n) for i in range(predict_x_n): df1[i] = sum(np.multiply(sigma , df00[-p:][::-1])) df00 = np.append(df00,df1[i]) return df0,df1,sigma wucha,wucha_predict,sigma = SAR(df_wu,q,s,predict_x_n) return wucha,wucha_predict,sigma def SARIMA(p,q,P,Q,data,m): df0,df1,sigma = AR_prediction(data,p,0) wucha,wucha_predict,sigma1,df_wu = MA_prediction(data, q, 0) a,b,sigma2 = SAR(data,P,m,0) c,d,sigma3 = SMA(data,Q,m,0) sigma = sigma[::-1] sigma1 = sigma1[::-1] sigma2 = sigma2[::-1] sigma3 = sigma3[::-1] sigma22 = np.append(np.array([1]),sigma2) sigma33 = np.append(np.array([1]),sigma3) result = [] for t in range(30,len(data)+1): sar = 0 for p in range(p): for P in range(P+1): sar += sigma[p] * data[t-p-1-P*m] * sigma22[P] sar += sum(sigma2 * np.array([data[t-i*m] for i in range(1,P+1)])) sma = 0 for q in range(q): for Q in range(Q+1): sma += sigma1[q] * df_wu[t-q-1-Q*m] * sigma33[Q] sma += sum(sigma3 * np.array([df_wu[t-i*m] for i in range(1,Q+1)])) result.append(sar + sma) return result
data0 = pd.read_csv('时间序列预测数据集.csv',parse_dates=['Date']) data0.set_index('Date',inplace=True) data=np.array(copy.deepcopy(data0)).ravel()
result = SARIMA(6,7,2,2,data,365) plt.figure(figsize=(15, 7.5)) plt.plot(range(500),data[-500:],c = 'black',label='actual') plt.plot(range(500),result[-500:],c='red',label='model') plt.show()
print('mse:',mean_squared_error(result,data[-len(result):]))
|