如何在Python中用三階指數(shù)平滑模型對(duì)金融數(shù)據(jù)集進(jìn)行擬合與預(yù)測(cè)
一、前期準(zhǔn)備
本次模型的構(gòu)建與預(yù)測(cè)都是用的是python進(jìn)行,其中涉及多個(gè)庫(kù):
import pandas as pd import matplotlib.pyplot as plt from statsmodels.graphics.api import qqplot import warnings import os from statsmodels.tsa.seasonal import seasonal_decompose from statsmodels.tsa.holtwinters import SimpleExpSmoothing,ExponentialSmoothing from sklearn.metrics import mean_squared_error
以上各個(gè)庫(kù)的作用介紹不是本文主要內(nèi)容不過(guò)多解釋,請(qǐng)自行了解。
本次實(shí)踐通過(guò)三階指數(shù)平滑的方法對(duì)十列金融數(shù)據(jù)集進(jìn)行模型的擬合與預(yù)測(cè)。
二、數(shù)據(jù)來(lái)源與樣式
本次實(shí)驗(yàn)數(shù)據(jù)為十列金融數(shù)據(jù)集,該數(shù)據(jù)集為時(shí)間序列,且相互之間獨(dú)立,因此需要對(duì)十個(gè)時(shí)間序列分別進(jìn)行擬合與預(yù)測(cè)。
圖1 數(shù)據(jù)樣式
三、數(shù)據(jù)的預(yù)處理
(一)表格處理
由于時(shí)間序列數(shù)據(jù)之間相互獨(dú)立,為了便于建模預(yù)測(cè)在導(dǎo)入之前我在excel表格上對(duì)數(shù)據(jù)進(jìn)行了簡(jiǎn)單的處理。
圖2 處理后表格
我將十個(gè)時(shí)間序列數(shù)據(jù)分別放入十個(gè)sheet里,并且以年份命名,以便于代碼導(dǎo)入數(shù)據(jù)。再者,由于原始時(shí)間序列數(shù)據(jù)沒(méi)有時(shí)間列,因此我按照個(gè)人理解添加了時(shí)間列,每一個(gè)數(shù)據(jù)都來(lái)自每月的一月一日,因此其周期可分為12、6、4、3等。
(二)數(shù)據(jù)導(dǎo)入
通過(guò)pandas的相關(guān)方法導(dǎo)入excel數(shù)據(jù)。設(shè)置好相關(guān)參數(shù),這樣在pycharm里顯示數(shù)據(jù)時(shí)就不會(huì)出現(xiàn)過(guò)多數(shù)據(jù)而省略部分?jǐn)?shù)據(jù)的情況。
#處理warning warnings.filterwarnings("ignore") #有時(shí)候代碼處于某些原因會(huì)飄紅卻不影響正常的運(yùn)行,為了美觀使用該代碼進(jìn)行忽視處理 #作圖顯示中文字符 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False #展示所有列表文件 pd.set_option('display.max_columns',1000) pd.set_option("display.width",1000) pd.set_option('display.max_colwidth',1000) pd.set_option('display.max_rows',1000) datax=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=None)
在python中導(dǎo)入excel的方法在基礎(chǔ)操作篇有介紹,不再贅述,詳情可看本篇文章:
如何在Python中導(dǎo)入EXCEL數(shù)據(jù)
(三)數(shù)據(jù)處理
為方便后續(xù)選擇數(shù)據(jù)以及代碼參數(shù)的調(diào)整,我將“日期”列設(shè)置為行索引,通過(guò)以下代碼實(shí)現(xiàn)。
#將excel中的“日期”一列設(shè)置為行索引 data=data.set_index('日期') data.index=pd.to_datetime(data.index)
四、模型構(gòu)建(指數(shù)平滑)
(一)數(shù)據(jù)作圖
在此以1964年金融數(shù)據(jù)集為例。首先導(dǎo)入數(shù)據(jù)并作出原始數(shù)據(jù)的折線圖。數(shù)據(jù)可視化的最基礎(chǔ)的知識(shí)可以看我先前寫的文章:
如何使用Python程序完成描述性統(tǒng)計(jì)分析需求
該篇末尾對(duì)數(shù)據(jù)可視化作了簡(jiǎn)單的教學(xué),但是只是冰山一角,想要深入學(xué)習(xí)數(shù)據(jù)可視化的朋友可以在網(wǎng)上查看其他的教程
#原始數(shù)據(jù)作圖 fig=plt.figure(figsize=(15,8))#作圖面積的大小 ax1=plt.subplot(1,1,1)#作圖的位置 plt.xticks(fontsize=20)#設(shè)置x軸刻度的字體大小 plt.yticks(fontsize=20)#設(shè)置y軸刻度的字體大小 plt.xlabel('日期',fontsize=35,color='blue')#設(shè)置x軸的標(biāo)簽,藍(lán)色部分字 plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')#設(shè)置y軸的標(biāo)簽,藍(lán)色部分字 plt.title('原始數(shù)據(jù)1964',fontsize=35)#設(shè)置圖的標(biāo)題 x1=data.index #將先前設(shè)置的日期行索引設(shè)置為坐標(biāo)軸的橫軸 y=data.iloc[:,0] #利用提取excel列的方法,提取了金融數(shù)據(jù)集第一列的數(shù)據(jù)作為y軸 plt.plot(x1,y,linewidth=4.0,label='真實(shí)線',color='orange')#繪制坐標(biāo)及數(shù)據(jù),并設(shè)置一些參數(shù) plt.legend(fontsize=20)#設(shè)置圖例,即圖中左上角那個(gè) plt.show()#繪圖
圖3 1964年金融數(shù)據(jù)折線圖
由上圖可看到該年金融數(shù)據(jù)的基本趨勢(shì)。
(二)觀察季節(jié)性與趨勢(shì)
通過(guò)程序作出以下季節(jié)性及趨勢(shì)圖,我將周期設(shè)置為12,原因在開(kāi)篇提及,不再?gòu)?fù)述。
decomposition=seasonal_decompose(data.iloc[:,0],model='addictive',period=12) decomposition.plot() plt.show()
#關(guān)于seasonal_decompose()方法的參數(shù) seasonal_decompose(x,model='additive',filt=None,period=None,two_sided=True,extrapolate_trend=0)
x | 時(shí)間序列。如果是兩維的,則單個(gè)Series應(yīng)該在一列中。x 必須包含 2 個(gè)完整的周期。 |
model | {“additive”, “multiplicative”} 時(shí)間序列分解的類型(加和 or 求乘積)參數(shù)名稱縮寫是允許的 |
filt | 過(guò)濾掉季節(jié)性分量的過(guò)濾系數(shù)。 濾波中使用的具體移動(dòng)平均法(單邊or兩側(cè))由 two_sided確定。(個(gè)人理解是,計(jì)算滑動(dòng)平均時(shí),滑動(dòng)平均階數(shù)內(nèi)各點(diǎn)所乘的那個(gè)系數(shù)) |
period | 時(shí)間序列的周期。如果 x 不是 pandas 對(duì)象或 x 的索引沒(méi)有頻率,則必須使用。如果 x 是具有時(shí)間序列索引的 pandas 對(duì)象,則覆蓋 x 的默認(rèn)周期。 |
two_sided | 濾波中使用的移動(dòng)平均法。如果為 True(默認(rèn)),則使用 filt 計(jì)算居中移動(dòng)平均值。如果為 False,則濾波器系數(shù)filt僅適用于過(guò)去的值。 |
extrapolate_trend | 如果設(shè)置為 > 0,考慮到這么多 (+1) 個(gè)最近點(diǎn),移動(dòng)平均(卷積)產(chǎn)生的趨勢(shì)是在兩端外推的線性最小二乘法(如果 two_lateral 為 False,則為單側(cè)外推)。如果設(shè)置為“freq”,則使用最近點(diǎn)。設(shè)置此參數(shù)會(huì)導(dǎo)致趨勢(shì)或殘差組件中沒(méi)有 NaN 值。 |
圖4 季節(jié)性及趨勢(shì)圖
從趨勢(shì)圖中看到,自1965到1967年期間數(shù)據(jù)有較大幅度的上漲,波動(dòng)較大。而在1967到1971年間趨勢(shì)波動(dòng)小,變動(dòng)較為平。1971年至1972年又出現(xiàn)了較大的波動(dòng)。從季節(jié)性圖來(lái)看,數(shù)據(jù)的變化在12期的情況下出現(xiàn)了周期波動(dòng)的情況,說(shuō)明該金融數(shù)據(jù)存在季節(jié)性趨。殘差圖則說(shuō)明了數(shù)據(jù)中隨機(jī)誤差的產(chǎn)生是呈現(xiàn)正態(tài)分布的,并且期分布隨機(jī)、不可預(yù)測(cè)。因此以該數(shù)據(jù)來(lái)構(gòu)建預(yù)測(cè)模型是具有可行性的。
(三)一階指數(shù)平滑
以下是一階模型擬合數(shù)據(jù)的代碼:需要注意的是,擬合的時(shí)候需要在最后加上 .fittedvalues 。
datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues print(datasmooth1)
以下是一階平滑擬合的可視化代碼及圖:
plt.figure(figsize=(15,8)) plt.title('一階平滑1964',fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth1.plot(color='green',linewidth=3.0,label='擬合線') #將一階平滑的結(jié)果畫折線圖 data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')#將原始數(shù)據(jù)畫折線圖 plt.legend(fontsize=20) plt.show()
圖5 一階平滑結(jié)果可視化
由圖可以看出一階指數(shù)平滑下模型的擬合效果很差,沒(méi)有擬合出原始數(shù)據(jù)的波動(dòng)與趨勢(shì),因此一階指數(shù)平滑不可用。
(四)二階指數(shù)平滑
以下是二階模型擬合數(shù)據(jù)的代碼:
datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues#添加trend效果 datasmooth2_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal=None).fit().fittedvalues#非添加trend效果 print(datasmooth2) print(datasmooth2_2)
以下是二階平滑擬合的可視化代碼及圖:
plt.figure(figsize=(15,8)) plt.title('二階平滑1964',fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth2.plot(color='green',linewidth=3.0,label='line_add')#添加trend效果折線圖 datasmooth2_2.plot(color='red',linewidth=3.0,label='line_mul')#非添加trend效果折線圖 data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')#真實(shí)線圖 plt.legend(fontsize=20) plt.show()
圖6 二階平滑結(jié)果可視化
由圖看出,二階平滑區(qū)分為真實(shí)線、加入趨勢(shì)效應(yīng)、未加入趨勢(shì)效應(yīng)三條線,其中加入趨勢(shì)效應(yīng)的曲線的擬合效果更加貼近真實(shí)線。但總的來(lái)看,二階指數(shù)平滑的擬合效果仍然很差,無(wú)法擬合出原始數(shù)據(jù)的波動(dòng)與趨勢(shì)。
(五)三階指數(shù)平滑
以下是三階模型擬合數(shù)據(jù)的代碼:
datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues#添加seasonal效果 datasmooth3_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal="mul", seasonal_periods=12).fit().fittedvalues#非添加seasonal效果 print(datasmooth3) print(datasmooth3_2)
以下是二階平滑擬合的可視化代碼及圖:
plt.figure(figsize=(15,8)) plt.title('三階平滑'+num,fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth3.plot(color='green',linewidth=3.0,label='line_add')#添加seasonal效果折線圖 datasmooth3_2.plot(color='red',linewidth=3.0,label='line_mul')#非添加seasonal效果折線圖 data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real')#原始數(shù)據(jù)折線圖 plt.legend(fontsize=20) plt.show()
圖7 三階平滑結(jié)果可視化
由圖可以看出,三階指數(shù)平滑后的結(jié)果與原始數(shù)據(jù)更為貼近,反映出了原始數(shù)據(jù)的波動(dòng)及趨勢(shì)。圖中的三條線分別為原始數(shù)據(jù)線、加入季節(jié)效應(yīng)線、未加入季節(jié)效應(yīng)線,而加入了季節(jié)效應(yīng)的曲線效果與原始數(shù)據(jù)更加接近。綜合以上,三階指數(shù)平滑的效果相對(duì)一階指數(shù)平滑以及二階指數(shù)平滑的表現(xiàn)更良好。
(六)均方誤(MSE)比較
以上我做了三個(gè)模型,分別為一階指數(shù)平滑、二階指數(shù)平滑和三階指數(shù)平滑,為了從這三者之間選擇哪一個(gè)模型效果最優(yōu),那么需要有一個(gè)標(biāo)準(zhǔn)來(lái)進(jìn)行比較,而通常會(huì)選擇比較三個(gè)模型的均方誤(MSE)來(lái)進(jìn)行選擇。代碼如下:
from sklearn.metrics import mean_squared_error datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues#一階指數(shù)平滑擬合結(jié)果 datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues#二階指數(shù)平滑擬合結(jié)果 datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues#三階指數(shù)平滑擬合結(jié)果 mse_1 = mean_squared_error(datasmooth1,data.iloc[:,0])#一階指數(shù)平滑的均方誤 mse_2 = mean_squared_error(datasmooth2,data.iloc[:,0])#二階指數(shù)平滑的均方誤 mse_3 = mean_squared_error(datasmooth3,data.iloc[:,0])#三階指數(shù)平滑的均方誤 print(mse_1) print(mse_2) print(mse_3)
表1 均方誤計(jì)算結(jié)果 | ||
均方誤(MSE) | ||
一階指數(shù)平滑 | 二階指數(shù)平滑 | 三階指數(shù)平滑 |
140960.991 | 155824.614 | 47311.015 |
由上表可知,三界指數(shù)平滑的均方誤是最小的,且都遠(yuǎn)小于一階與二階情況下的均方誤,說(shuō)明三界指數(shù)平滑的擬合效果要優(yōu)于一階指數(shù)平滑與二階指數(shù)平滑??傮w來(lái)看,三者的均方誤都很大,在模型的擬合與預(yù)測(cè)中仍然存在較大的偏差,但相對(duì)于ARMA模型來(lái)說(shuō)其擬合效果已經(jīng)有很大的提升。
(七)正態(tài)性檢驗(yàn)
正態(tài)性檢驗(yàn)代碼與圖如下:
resid=model.resid#先計(jì)算出數(shù)據(jù)的殘差 #以下再進(jìn)行作圖 fig=plt.figure(figsize=(8,6)) ax=fig.add_subplot(1,1,1) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('Theoretical Quantiles',fontsize=35,color='blue') plt.ylabel('Sample Quantiles',fontsize=35,color='blue') plt.title('正態(tài)性檢驗(yàn)1964',fontsize=20) fig=qqplot(resid,line='q',ax=ax,fit=True) plt.show()
圖8 正態(tài)性檢驗(yàn)qq圖
由qq圖來(lái)看,該模型數(shù)據(jù)通過(guò)正態(tài)性檢驗(yàn),符合正態(tài)性分布,說(shuō)明我先前的判斷合理。綜合以上考慮,我決定使用三階指數(shù)平滑方法來(lái)構(gòu)建模型并預(yù)測(cè)未來(lái)18期的金融數(shù)據(jù)。
五、數(shù)據(jù)預(yù)測(cè)
預(yù)測(cè)數(shù)據(jù)的代碼如下:
(需要注意的是:在進(jìn)行預(yù)測(cè)時(shí),第一行代碼與擬合時(shí)不一樣,最后部分沒(méi)有 .fittedvalues 。)
通過(guò)forecast()方法進(jìn)行未來(lái)十八期數(shù)據(jù)的預(yù)測(cè),其中該方法中的數(shù)字18則是說(shuō)明要預(yù)測(cè)未來(lái)18期的數(shù)據(jù)。
model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit() pred = model.forecast(18) print(pred)
表2 未來(lái)18期預(yù)測(cè)數(shù)據(jù) | |
---|---|
日期 | 預(yù)測(cè)數(shù)據(jù) |
1973-01-01 | 7285.228568 |
1973-02-01 | 6915.147716 |
1973-03-01 | 7793.479060 |
1973-04-01 | 7143.713913 |
1973-05-01 | 7379.143971 |
1973-06-01 | 7104.064988 |
1973-07-01 | 6545.559722 |
1973-08-01 | 6991.558418 |
1973-09-01 | 6986.912414 |
1973-10-01 | 7799.023518 |
1973-11-01 | 7221.405211 |
1973-12-01 | 6921.526282 |
1974-01-01 | 7199.852791 |
1974-02-01 | 6829.771939 |
1974-03-01 | 7708.103282 |
1974-04-01 | 7058.338135 |
1974-05-01 | 7293.768194 |
1974-06-01 | 7018.689211 |
上表是由模型預(yù)測(cè)出的未來(lái)18期的金融數(shù)據(jù)。
預(yù)測(cè)圖代碼如下:
plt.figure(figsize=(15,8)) plt.title('最終預(yù)測(cè)結(jié)果1964',fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') pred.plot(color='red',linewidth=3.0,label='預(yù)測(cè)線')#預(yù)測(cè)數(shù)據(jù)折線圖 data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線')#實(shí)際數(shù)據(jù)折線圖 plt.legend(fontsize=20) plt.show()
圖9 預(yù)測(cè)結(jié)果可視化
由上圖看出,預(yù)測(cè)數(shù)據(jù)的波動(dòng)以及變化趨勢(shì)與原始數(shù)據(jù)較為吻合,認(rèn)為其具有一定的合理性。
六、總結(jié)
以上的建模與預(yù)測(cè)只以1964年金融數(shù)據(jù)集為例,而其他數(shù)據(jù)集在我進(jìn)行建模過(guò)程中表現(xiàn)出的特性與該例子的數(shù)據(jù)集相似,因此使用三階指數(shù)平滑的方法在十個(gè)數(shù)據(jù)集中都能夠行得通。
七、完整代碼
由于總體需要預(yù)測(cè)的數(shù)據(jù)有10列,而我在數(shù)據(jù)處理時(shí)將十列數(shù)據(jù)分別放入同一個(gè)表格中的不同Sheet,在本文開(kāi)頭已有說(shuō)明,因此完整代碼中我加入了一個(gè)簡(jiǎn)單的循環(huán)與函數(shù)來(lái)遍歷不同的sheet來(lái)分別預(yù)測(cè)不同的數(shù)據(jù)集·。同時(shí)也出于個(gè)人的需要,我要將代碼輸出的數(shù)據(jù)寫入一個(gè)word文件中,因此在完整代碼中可以看到print()函數(shù)里面會(huì)接一個(gè)file=wordfile參數(shù),這個(gè)就是將輸出print到我指定的word文件中,同時(shí)我還需要將作出的圖片輸出到指定的文件夾當(dāng)中,因此在作圖的最后我會(huì)添加一行plt.savefig()樣式的代碼,這個(gè)對(duì)于作圖可有可無(wú),純看個(gè)人的需求。
其次,三階指數(shù)平滑并不是該數(shù)據(jù)集的最優(yōu)的擬合與預(yù)測(cè)方法,只是相對(duì)其他模型而言實(shí)現(xiàn)相對(duì)簡(jiǎn)單,若有更高的預(yù)測(cè)精度需求可自行嘗試構(gòu)建其他模型。
import pandas as pd import matplotlib.pyplot as plt from statsmodels.graphics.api import qqplot import warnings import os #處理warning warnings.filterwarnings("ignore") #作圖顯示中文字符 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False #展示所有列表文件 pd.set_option('display.max_columns',1000) pd.set_option("display.width",1000) pd.set_option('display.max_colwidth',1000) pd.set_option('display.max_rows',1000) datax=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=None)#導(dǎo)入表格中的所有sheet datasets=['1998','2001','1996','1981','1982','1974','1976','1972','1984','1964']#待循環(huán)的十個(gè)數(shù)據(jù) def timeda_3(num): data=pd.read_excel(r'D:\雜貨\金融數(shù)據(jù)集合.xlsx',sheet_name=num) data=data.set_index('日期') data.index=pd.to_datetime(data.index) #原始數(shù)據(jù)作圖 fig=plt.figure(figsize=(15,8)) ax1=plt.subplot(1,1,1) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') plt.title('原始數(shù)據(jù)'+num,fontsize=35) x1=data.index y=data.iloc[:,0] plt.plot(x1,y,linewidth=4.0,label='真實(shí)線',color='orange') plt.legend(fontsize=20) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '原始數(shù)據(jù)圖'+num))#用于將圖片保存到指定的文件夾中 plt.show() #作季節(jié)性圖 from statsmodels.tsa.seasonal import seasonal_decompose decomposition=seasonal_decompose(data.iloc[:,0],model='addictive',period=12) decomposition.plot() plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '季節(jié)性圖' + num)) plt.show() #一階平滑 from statsmodels.tsa.holtwinters import SimpleExpSmoothing,ExponentialSmoothing datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues print('一階平滑結(jié)果:\n',datasmooth1,file=wordfile) plt.figure(figsize=(15,8)) plt.title('一階平滑'+num,fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth1.plot(color='green',linewidth=3.0,label='擬合線') data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線') plt.legend(fontsize=20) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '一階平滑圖' + num)) plt.show() #一階平滑均方誤 datasmooth1= SimpleExpSmoothing(data.iloc[:,0]).fit().fittedvalues from sklearn.metrics import mean_squared_error mse_1 = mean_squared_error(datasmooth1,data.iloc[:,0]) print('一階平滑均方誤:',mse_1,file=wordfile) #二階平滑 datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues datasmooth2_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal=None).fit().fittedvalues print('二階平滑結(jié)果(add):\n',datasmooth2,file=wordfile) print('二階平滑結(jié)果(mul):\n',datasmooth2_2,file=wordfile) plt.figure(figsize=(15,8)) plt.title('二階平滑'+num,fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth2.plot(color='green',linewidth=3.0,label='line_add') datasmooth2_2.plot(color='red',linewidth=3.0,label='line_mul') data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real') plt.legend(fontsize=20) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '二階平滑圖' + num)) plt.show() #二階平滑均方誤 datasmooth2= ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal=None).fit().fittedvalues from sklearn.metrics import mean_squared_error mse_2 = mean_squared_error(datasmooth2,data.iloc[:,0]) print('二階平滑均方誤:',mse_2,file=wordfile) #三階平滑 datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues datasmooth3_2 = ExponentialSmoothing(data.iloc[:,0], trend="mul", seasonal="mul", seasonal_periods=12).fit().fittedvalues print('三階平滑結(jié)果(add):\n',datasmooth3,file=wordfile) print('三階平滑結(jié)果(mul):\n',datasmooth3_2,file=wordfile) plt.figure(figsize=(15,8)) plt.title('三階平滑'+num,fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') datasmooth3.plot(color='green',linewidth=3.0,label='line_add') datasmooth3_2.plot(color='red',linewidth=3.0,label='line_mul') data.iloc[:,0].plot(color='black',linewidth=3.0,label='line_real') plt.legend(fontsize=20) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '三階平滑圖' + num)) plt.show() #三階平滑均方誤 datasmooth3 = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit().fittedvalues from sklearn.metrics import mean_squared_error mse_3 = mean_squared_error(datasmooth3,data.iloc[:,0]) print('三階平滑均方誤:',mse_3,file=wordfile) #預(yù)測(cè)三階平滑模型數(shù)據(jù) model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit() pred = model.forecast(18) print('三階平滑預(yù)測(cè)結(jié)果數(shù)據(jù):\n',pred,file=wordfile) #qq圖正態(tài)性檢驗(yàn) resid=model.resid fig=plt.figure(figsize=(8,6)) ax=fig.add_subplot(1,1,1) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('Theoretical Quantiles',fontsize=35,color='blue') plt.ylabel('Sample Quantiles',fontsize=35,color='blue') plt.title('正態(tài)性檢驗(yàn)'+num,fontsize=20) fig=qqplot(resid,line='q',ax=ax,fit=True) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '正態(tài)性檢驗(yàn)qq圖' + num)) plt.show() #預(yù)測(cè)圖 plt.figure(figsize=(15,8)) plt.title('最終預(yù)測(cè)結(jié)果'+num,fontsize=40) plt.xticks(fontsize=20) plt.yticks(fontsize=20) plt.xlabel('日期',fontsize=35,color='blue') plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue') pred.plot(color='red',linewidth=3.0,label='預(yù)測(cè)線') data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實(shí)線') plt.legend(fontsize=20) plt.savefig(os.path.join(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\金融數(shù)據(jù)集圖片', '預(yù)測(cè)數(shù)據(jù)圖' + num)) plt.show() print('-------------','以上為',num,'年的數(shù)據(jù)','------------',file=wordfile) if __name__=='__main__': wordfile = open(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\數(shù)據(jù)輸出.docx', 'w')#打開(kāi)一個(gè)word文檔,只有打開(kāi)了才能寫入 for num in datasets: timeda_3(num) wordfile.close()#寫完word文檔后要關(guān)閉,才能保存。 print('運(yùn)行完畢')
到此這篇關(guān)于如何在Python中用三階指數(shù)平滑模型對(duì)金融數(shù)據(jù)集進(jìn)行擬合與預(yù)測(cè)的文章就介紹到這了,更多相關(guān)python 模型 金融 人工智能內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于pytest結(jié)合csv模塊實(shí)現(xiàn)csv格式的數(shù)據(jù)驅(qū)動(dòng)問(wèn)題
這篇文章主要介紹了pytest結(jié)合csv模塊實(shí)現(xiàn)csv格式的數(shù)據(jù)驅(qū)動(dòng),使用python中的csv模塊來(lái)處理csv文件,結(jié)合pygtest的參數(shù)化處理方式來(lái)實(shí)現(xiàn)ddt,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-05-05接口自動(dòng)化多層嵌套json數(shù)據(jù)處理代碼實(shí)例
這篇文章主要介紹了接口自動(dòng)化多層嵌套json數(shù)據(jù)處理代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用
這篇文章主要為大家介紹了pytest自動(dòng)化測(cè)試中的fixture的聲明和調(diào)用,文中含有詳細(xì)示例操作有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-10-10Python實(shí)例方法與類方法和靜態(tài)方法介紹與區(qū)別分析
在 Python 中,實(shí)例方法(instance method),類方法(class method)與靜態(tài)方法(static method)經(jīng)常容易混淆。本文通過(guò)代碼例子來(lái)說(shuō)明它們的區(qū)別2022-10-10Python 實(shí)現(xiàn)判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中
今天小編就為大家分享一篇Python判斷圖片格式并轉(zhuǎn)換,將轉(zhuǎn)換的圖像存到生成的文件夾中,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01python 還原梯度下降算法實(shí)現(xiàn)一維線性回歸
這篇文章主要介紹了python 還原梯度下降算法實(shí)現(xiàn)一維線性回歸,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10