通常經常使用到的指數平滑法爲一次指數平滑、二次指數平滑和三次指數平滑,高次指數平滑通常比較難見到,所以本文着重介紹了一次、二次和三次指數平滑的特色與不一樣。html
一次指數平滑通常應用於直線型數據,且一次指數平滑具備滯後性,能夠說明有明顯的時間性、季節性。數組
二次指數平滑通常也應用於直線型,可是效果會比一次指數平滑好不少,也就至關於增強版的一次指數平滑。app
三次指數平滑能夠應用於拋物線型的數據,由於數據在二次平滑事後仍是具備斜率,那麼能夠繼續使用三次指數平滑。函數
初值:無論什麼指數平滑都會有個初值,假如數據大於20項,那麼初值就能夠認定爲第一個數據,或者利用下列公式計算也行;假如數據小於20項,則初始值爲:post
低於20項通常取3,大於20的看着取就好了。spa
指數平滑係數α的肯定3d
(1)經驗判斷code
一、當時間序列呈現較穩定的水平趨勢時,應選較小的α,通常可在0.05~0.20之間取值‘htm
二、當時間序列有波動,但長期趨勢變化不大時,可選稍大的α值,常在0.1~0.4之間取值;blog
三、當時間序列波動很大,長期趨勢變化幅度較大,呈現明顯且迅速的上升或降低趨勢時,宜選擇較大的α值,如可在0.6~0.8間選值。以使預測模型靈敏度高些,能迅速跟上數據的變化。
四、當時間序列數據是上升(或降低)的發展趨勢類型,α應取較大的值,在0.6~1之間。
一次指數平滑:
一次指數平滑須要滯後一期,給定平滑係數,那麼一次指數平滑的計算公式爲:
預測第期的數值則是上一期的實際值與預測值的加權平均,預測公式爲:
二次指數平滑:
給定平滑係數,那麼二次指數平滑的計算公式爲:
預測將來期的值的計算公式爲:
其中:
三次指數平滑:
給定平滑係數,那麼三次指數平滑的計算公式爲:
預測將來期的值的計算公式爲:
其中:
下面舉例說明,數據以下:
253993 |
275396.2 |
315229.5 |
356949.6 |
400158.2 |
442431.7 |
495102.9 |
570164.8 |
640993.1 |
704250.4 |
767455.4 |
781807.8 |
776332.3 |
794161.7 |
834177.7 |
931651.5 |
1028390 |
1114914 |
133 |
88 |
150 |
123 |
404 |
107 |
674 |
403 |
243 |
257 |
900 |
1043 |
1156 |
895 |
1200 |
1038 |
1024 |
1283 |
引入均方偏差概念來判斷平滑係數是否準確:
要使最小則構成了一個關於的函數,由此能夠獲得最優的平滑係數,這裏能夠引入線性規劃的思想來求得最優解
一次指數平滑代碼:
S1_1 = [] for m in range(0, len(info_data_id)): S1_1_empty = [] x = 0 for n in range(0, 3): x = x + int(info_data_sales[m][n]) x = x / 3 S1_1_empty.append(x) S1_1.append(S1_1_empty) # print(S1_1) a = [] ##這是用來存放阿爾法的數組 info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): v = input('請輸入第' + str(i + 1) + '組數據的a:') a.append(v) for i in range(0, len(info_data_sales)): MSE = 0 for j in range(0, len(info_data_sales[i])): S1_1[i].append( float(a[i]) * int(info_data_sales[i][j]) + (1 - float(a[i])) * int(S1_1[i][j])) ##計算預估值 MSE = (int(S1_1[i][j]) - int(info_data_sales[i][j])) ** 2 + MSE # print(info_data_sales[i][j], S1_1[i][j]) MSE = (MSE ** (1 / 2)) / int(len(info_data_sales[i])) ##獲得均方偏差 info_MSE.append(MSE) # print(info_MSE) # print(S1_1) for i in range(0, len(S1_1)): print('第' + str(i + 1) + '組的一次平滑預估值爲:' + str(S1_1[i][len(S1_1[i]) - 1]) + ';均方偏差爲:' + str(info_MSE[i]))
二次指數平滑代碼:
S2_1 = [] S2_2 = [] for m in range(0, len(info_data_id)): S2_1_empty = [] x = 0 for n in range(0, 3): x = x + float(info_data_sales[m][n]) x = x / 3 S2_1_empty.append(x) S2_1.append(S2_1_empty) S2_2.append(S2_1_empty) # print(S2_2) a = [] ##這是用來存放阿爾法的數組 info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): v = float(input('請輸入第' + str(i + 1) + '組數據的a:')) a.append(v) ##下面是計算一次指數平滑的值 S2_1_new1 = [] for i in range(0, len(info_data_sales)): S2_1_new = [[]] * len(info_data_id) for j in range(0, len(info_data_sales[i])): if j == 0: S2_1_new[i].append( float(a[i]) * float(info_data_sales[i][j]) + (1 - float(a[i])) * float(S2_1[i][j])) else: S2_1_new[i].append(float(a[i]) * float(info_data_sales[i][j]) + (1 - float(a[i])) * float( S2_1_new[i][j - 1])) ##計算一次指數的值 S2_1_new1.append(S2_1_new[i]) # print(S2_1_new1) # print(len(S2_1_new1[i])) ##下面是計算二次指數平滑的值 S2_2_new1 = [] info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): S2_2_new = [[]] * len(info_data_id) MSE = 0 for j in range(0, len(info_data_sales[i])): if j == 0: S2_2_new[i].append(float(a[i]) * float(S2_1_new1[i][j]) + (1 - float(a[i])) * float(S2_2[i][j])) else: S2_2_new[i].append(float(a[i]) * float(S2_1_new1[i][j]) + (1 - float(a[i])) * float( S2_2_new[i][j - 1])) ##計算二次指數的值 MSE = (int(S2_2_new[i][j]) - int(info_data_sales[i][j])) ** 2 + MSE MSE = (MSE ** (1 / 2)) / int(len(info_data_sales[i])) info_MSE.append(MSE) S2_2_new1.append(S2_2_new[i]) # print(S2_2_new1) # print(len(S2_2_new1[i])) ##下面是計算At、Bt以及每一個預估值Xt的值,直接計算預估值,不一一列舉Xt的值了 u = input('你要預估多少期?') Xt = [] for i in range(0, len(info_data_sales)): At = (float(S2_1_new1[i][len(S2_1_new1[i]) - 1]) * 2 - float(S2_2_new1[i][len(S2_2_new1[i]) - 1])) Bt = (float(a[i]) / (1 - float(a[i])) * ( float(S2_1_new1[i][len(S2_1_new1[i]) - 1]) - float(S2_2_new1[i][len(S2_2_new1[i]) - 1]))) Xt.append(At + Bt * int(u)) print('第' + str(i + 1) + '組的二次平滑預估值爲:' + str(Xt[i]) + ';均方偏差爲:' + str(info_MSE[i]))
三次指數平滑代碼
S3_1 = [] S3_2 = [] S3_3 = [] for m in range(0, len(info_data_id)): S3_1_empty = [] x = 0 for n in range(0, 3): x = x + float(info_data_sales[m][n]) x = x / 3 S3_1_empty.append(x) S3_1.append(S3_1_empty) S3_2.append(S3_1_empty) S3_3.append(S3_1_empty) # print(S3_1) a = [] ##這是用來存放阿爾法的數組 info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): v = float(input('請輸入第' + str(i + 1) + '組數據的a:')) a.append(v) ##下面是計算一次指數平滑的值 S3_1_new1 = [] for i in range(0, len(info_data_sales)): S3_1_new = [[]] * len(info_data_id) for j in range(0, len(info_data_sales[i])): if j == 0: S3_1_new[i].append( float(a[i]) * float(info_data_sales[i][j]) + (1 - float(a[i])) * float(S3_1[i][j])) else: S3_1_new[i].append(float(a[i]) * float(info_data_sales[i][j]) + (1 - float(a[i])) * float( S3_1_new[i][j - 1])) ##計算一次指數的值 S3_1_new1.append(S3_1_new[i]) ##下面是計算二次指數平滑的值 S3_2_new1 = [] info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): S3_2_new = [[]] * len(info_data_id) for j in range(0, len(info_data_sales[i])): if j == 0: S3_2_new[i].append(float(a[i]) * float(S3_1_new1[i][j]) + (1 - float(a[i])) * float(S3_2[i][j])) else: S3_2_new[i].append(float(a[i]) * float(S3_1_new1[i][j]) + (1 - float(a[i])) * float( S3_2_new[i][j - 1])) ##計算二次指數的值 S3_2_new1.append(S3_2_new[i]) ##下面是計算二次指數平滑的值 S3_3_new1 = [] info_MSE = [] ##計算均方偏差來獲得最優的a(阿爾法) for i in range(0, len(info_data_sales)): S3_3_new = [[]] * len(info_data_id) MSE = 0 for j in range(0, len(info_data_sales[i])): if j == 0: S3_3_new[i].append(float(a[i]) * float(S3_2_new1[i][j]) + (1 - float(a[i])) * float(S3_3[i][j])) else: S3_3_new[i].append(float(a[i]) * float(S3_2_new1[i][j]) + (1 - float(a[i])) * float( S3_3_new[i][j - 1])) ##計算三次指數的值 MSE = (int(S3_3_new[i][j]) - int(info_data_sales[i][j])) ** 2 + MSE MSE = (MSE ** (1 / 2)) / int(len(info_data_sales[i])) info_MSE.append(MSE) S3_3_new1.append(S3_3_new[i]) # print(S3_3_new1) ##下面是計算At、Bt、Ct以及每一個預估值Xt的值,直接計算預估值,不一一列舉Xt的值了 u = input('你要預估多少期?') Xt = [] for i in range(0, len(info_data_sales)): At = ( float(S3_1_new1[i][len(S3_1_new1[i]) - 1]) * 3 - float(S3_2_new1[i][len(S3_2_new1[i]) - 1]) * 3 + float( S3_3_new1[i][len(S3_3_new1[i]) - 1])) Bt = ((float(a[i]) / (2 * ((1 - float(a[i])) ** 2))) * ((6 - 5 * float(a[i])) * ( float(S3_1_new1[i][len(S3_1_new1[i]) - 1]) - 2 * (5 - 4 * float(a[i])) * float( S3_2_new1[i][len(S3_2_new1[i]) - 1]) + (4 - 3 * float(a[i])) * float( S3_3_new1[i][len(S3_3_new1[i]) - 1])))) Ct = (((float(a[i])) ** 2) / (2 * ((1 - float(a[i])) ** 2))) * ( float(S3_1_new1[i][len(S3_1_new1[i]) - 1]) - float(S3_2_new1[i][len(S3_2_new1[i]) - 1])*2 + float( S3_3_new1[i][len(S3_3_new1[i]) - 1])) Xt.append(At + Bt * int(u) + Ct * (int(u) ** 2)) print('第' + str(i + 1) + '組的三次平滑預估值爲:' + str(Xt[i]) + ';均方偏差爲:' + str(info_MSE[i]))