亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

如何在Python中用三階指數(shù)平滑模型對金融數(shù)據(jù)集進行擬合與預測

 更新時間:2023年03月22日 17:16:00   作者:二十六夜.  
這篇文章主要介紹了如何在Python中用三階指數(shù)平滑模型對金融數(shù)據(jù)集進行擬合與預測,本次實驗數(shù)據(jù)為10列金融數(shù)據(jù)集,且相互之間獨立,需要分別擬合預測,需要的朋友可以參考下

一、前期準備

本次模型的構(gòu)建與預測都是用的是python進行,其中涉及多個庫:

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

以上各個庫的作用介紹不是本文主要內(nèi)容不過多解釋,請自行了解。

本次實踐通過三階指數(shù)平滑的方法對十列金融數(shù)據(jù)集進行模型的擬合與預測。

二、數(shù)據(jù)來源與樣式

本次實驗數(shù)據(jù)為十列金融數(shù)據(jù)集,該數(shù)據(jù)集為時間序列,且相互之間獨立,因此需要對十個時間序列分別進行擬合與預測。

圖1 數(shù)據(jù)樣式

 三、數(shù)據(jù)的預處理

(一)表格處理

由于時間序列數(shù)據(jù)之間相互獨立,為了便于建模預測在導入之前我在excel表格上對數(shù)據(jù)進行了簡單的處理。

圖2 處理后表格

我將十個時間序列數(shù)據(jù)分別放入十個sheet里,并且以年份命名,以便于代碼導入數(shù)據(jù)。再者,由于原始時間序列數(shù)據(jù)沒有時間列,因此我按照個人理解添加了時間列,每一個數(shù)據(jù)都來自每月的一月一日,因此其周期可分為12、6、4、3等。

(二)數(shù)據(jù)導入

通過pandas的相關(guān)方法導入excel數(shù)據(jù)。設(shè)置好相關(guān)參數(shù),這樣在pycharm里顯示數(shù)據(jù)時就不會出現(xiàn)過多數(shù)據(jù)而省略部分數(shù)據(jù)的情況。

#處理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)

在python中導入excel的方法在基礎(chǔ)操作篇有介紹,不再贅述,詳情可看本篇文章:

如何在Python中導入EXCEL數(shù)據(jù)

(三)數(shù)據(jù)處理

為方便后續(xù)選擇數(shù)據(jù)以及代碼參數(shù)的調(diào)整,我將“日期”列設(shè)置為行索引,通過以下代碼實現(xiàn)。

#將excel中的“日期”一列設(shè)置為行索引
data=data.set_index('日期')
data.index=pd.to_datetime(data.index)

 四、模型構(gòu)建(指數(shù)平滑)

(一)數(shù)據(jù)作圖

在此以1964年金融數(shù)據(jù)集為例。首先導入數(shù)據(jù)并作出原始數(shù)據(jù)的折線圖。數(shù)據(jù)可視化的最基礎(chǔ)的知識可以看我先前寫的文章:

如何使用Python程序完成描述性統(tǒng)計分析需求

該篇末尾對數(shù)據(jù)可視化作了簡單的教學,但是只是冰山一角,想要深入學習數(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軸的標簽,藍色部分字
plt.ylabel('金融數(shù)據(jù)',fontsize=35,color='blue')#設(shè)置y軸的標簽,藍色部分字
plt.title('原始數(shù)據(jù)1964',fontsize=35)#設(shè)置圖的標題
x1=data.index #將先前設(shè)置的日期行索引設(shè)置為坐標軸的橫軸
y=data.iloc[:,0] #利用提取excel列的方法,提取了金融數(shù)據(jù)集第一列的數(shù)據(jù)作為y軸
plt.plot(x1,y,linewidth=4.0,label='真實線',color='orange')#繪制坐標及數(shù)據(jù),并設(shè)置一些參數(shù)
plt.legend(fontsize=20)#設(shè)置圖例,即圖中左上角那個
plt.show()#繪圖

 圖3 1964年金融數(shù)據(jù)折線圖

由上圖可看到該年金融數(shù)據(jù)的基本趨勢。

(二)觀察季節(jié)性與趨勢

通過程序作出以下季節(jié)性及趨勢圖,我將周期設(shè)置為12,原因在開篇提及,不再復述。

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時間序列。如果是兩維的,則單個Series應該在一列中。x 必須包含 2 個完整的周期。
model{“additive”, “multiplicative”} 時間序列分解的類型(加和 or 求乘積)參數(shù)名稱縮寫是允許的
filt過濾掉季節(jié)性分量的過濾系數(shù)。 濾波中使用的具體移動平均法(單邊or兩側(cè))由 two_sided確定。(個人理解是,計算滑動平均時,滑動平均階數(shù)內(nèi)各點所乘的那個系數(shù))
period時間序列的周期。如果 x 不是 pandas 對象或 x 的索引沒有頻率,則必須使用。如果 x 是具有時間序列索引的 pandas 對象,則覆蓋 x 的默認周期。
two_sided濾波中使用的移動平均法。如果為 True(默認),則使用 filt 計算居中移動平均值。如果為 False,則濾波器系數(shù)filt僅適用于過去的值。
extrapolate_trend如果設(shè)置為 > 0,考慮到這么多 (+1) 個最近點,移動平均(卷積)產(chǎn)生的趨勢是在兩端外推的線性最小二乘法(如果 two_lateral 為 False,則為單側(cè)外推)。如果設(shè)置為“freq”,則使用最近點。設(shè)置此參數(shù)會導致趨勢或殘差組件中沒有 NaN 值。

 圖4 季節(jié)性及趨勢圖

從趨勢圖中看到,自1965到1967年期間數(shù)據(jù)有較大幅度的上漲,波動較大。而在1967到1971年間趨勢波動小,變動較為平。1971年至1972年又出現(xiàn)了較大的波動。從季節(jié)性圖來看,數(shù)據(jù)的變化在12期的情況下出現(xiàn)了周期波動的情況,說明該金融數(shù)據(jù)存在季節(jié)性趨。殘差圖則說明了數(shù)據(jù)中隨機誤差的產(chǎn)生是呈現(xiàn)正態(tài)分布的,并且期分布隨機、不可預測。因此以該數(shù)據(jù)來構(gòu)建預測模型是具有可行性的。

(三)一階指數(shù)平滑

以下是一階模型擬合數(shù)據(jù)的代碼:需要注意的是,擬合的時候需要在最后加上  .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ù)據(jù)畫折線圖
plt.legend(fontsize=20)
plt.show()

 圖5 一階平滑結(jié)果可視化

由圖可以看出一階指數(shù)平滑下模型的擬合效果很差,沒有擬合出原始數(shù)據(jù)的波動與趨勢,因此一階指數(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')#真實線圖
plt.legend(fontsize=20)
plt.show()

 圖6 二階平滑結(jié)果可視化

由圖看出,二階平滑區(qū)分為真實線、加入趨勢效應、未加入趨勢效應三條線,其中加入趨勢效應的曲線的擬合效果更加貼近真實線。但總的來看,二階指數(shù)平滑的擬合效果仍然很差,無法擬合出原始數(shù)據(jù)的波動與趨勢。

(五)三階指數(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ù)的波動及趨勢。圖中的三條線分別為原始數(shù)據(jù)線、加入季節(jié)效應線、未加入季節(jié)效應線,而加入了季節(jié)效應的曲線效果與原始數(shù)據(jù)更加接近。綜合以上,三階指數(shù)平滑的效果相對一階指數(shù)平滑以及二階指數(shù)平滑的表現(xiàn)更良好。

(六)均方誤(MSE)比較

以上我做了三個模型,分別為一階指數(shù)平滑、二階指數(shù)平滑和三階指數(shù)平滑,為了從這三者之間選擇哪一個模型效果最優(yōu),那么需要有一個標準來進行比較,而通常會選擇比較三個模型的均方誤(MSE)來進行選擇。代碼如下:

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 均方誤計算結(jié)果
均方誤(MSE)
一階指數(shù)平滑二階指數(shù)平滑三階指數(shù)平滑
140960.991155824.61447311.015

由上表可知,三界指數(shù)平滑的均方誤是最小的,且都遠小于一階與二階情況下的均方誤,說明三界指數(shù)平滑的擬合效果要優(yōu)于一階指數(shù)平滑與二階指數(shù)平滑??傮w來看,三者的均方誤都很大,在模型的擬合與預測中仍然存在較大的偏差,但相對于ARMA模型來說其擬合效果已經(jīng)有很大的提升。

 (七)正態(tài)性檢驗

正態(tài)性檢驗代碼與圖如下:

resid=model.resid#先計算出數(shù)據(jù)的殘差
#以下再進行作圖
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)性檢驗1964',fontsize=20)
fig=qqplot(resid,line='q',ax=ax,fit=True)
plt.show()

 圖8 正態(tài)性檢驗qq圖

由qq圖來看,該模型數(shù)據(jù)通過正態(tài)性檢驗,符合正態(tài)性分布,說明我先前的判斷合理。綜合以上考慮,我決定使用三階指數(shù)平滑方法來構(gòu)建模型并預測未來18期的金融數(shù)據(jù)。

五、數(shù)據(jù)預測

預測數(shù)據(jù)的代碼如下:

(需要注意的是:在進行預測時,第一行代碼與擬合時不一樣,最后部分沒有  .fittedvalues  。)

通過forecast()方法進行未來十八期數(shù)據(jù)的預測,其中該方法中的數(shù)字18則是說明要預測未來18期的數(shù)據(jù)。

model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit()
pred = model.forecast(18)
print(pred)
表2 未來18期預測數(shù)據(jù)
日期預測數(shù)據(jù)
1973-01-017285.228568
1973-02-016915.147716
1973-03-017793.479060
1973-04-017143.713913
1973-05-017379.143971
1973-06-017104.064988
1973-07-016545.559722
1973-08-016991.558418
1973-09-016986.912414
1973-10-017799.023518
1973-11-017221.405211
1973-12-016921.526282
1974-01-017199.852791
1974-02-016829.771939
1974-03-017708.103282
1974-04-017058.338135
1974-05-017293.768194
1974-06-017018.689211

上表是由模型預測出的未來18期的金融數(shù)據(jù)。

預測圖代碼如下:

plt.figure(figsize=(15,8))
plt.title('最終預測結(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='預測線')#預測數(shù)據(jù)折線圖
data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實線')#實際數(shù)據(jù)折線圖
plt.legend(fontsize=20)
plt.show()

 圖9 預測結(jié)果可視化

 由上圖看出,預測數(shù)據(jù)的波動以及變化趨勢與原始數(shù)據(jù)較為吻合,認為其具有一定的合理性。

六、總結(jié)

 以上的建模與預測只以1964年金融數(shù)據(jù)集為例,而其他數(shù)據(jù)集在我進行建模過程中表現(xiàn)出的特性與該例子的數(shù)據(jù)集相似,因此使用三階指數(shù)平滑的方法在十個數(shù)據(jù)集中都能夠行得通。

七、完整代碼

由于總體需要預測的數(shù)據(jù)有10列,而我在數(shù)據(jù)處理時將十列數(shù)據(jù)分別放入同一個表格中的不同Sheet,在本文開頭已有說明,因此完整代碼中我加入了一個簡單的循環(huán)與函數(shù)來遍歷不同的sheet來分別預測不同的數(shù)據(jù)集·。同時也出于個人的需要,我要將代碼輸出的數(shù)據(jù)寫入一個word文件中,因此在完整代碼中可以看到print()函數(shù)里面會接一個file=wordfile參數(shù),這個就是將輸出print到我指定的word文件中,同時我還需要將作出的圖片輸出到指定的文件夾當中,因此在作圖的最后我會添加一行plt.savefig()樣式的代碼,這個對于作圖可有可無,純看個人的需求。

其次,三階指數(shù)平滑并不是該數(shù)據(jù)集的最優(yōu)的擬合與預測方法,只是相對其他模型而言實現(xiàn)相對簡單,若有更高的預測精度需求可自行嘗試構(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)#導入表格中的所有sheet
datasets=['1998','2001','1996','1981','1982','1974','1976','1972','1984','1964']#待循環(huán)的十個數(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='真實線',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='真實線')
    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)
    #預測三階平滑模型數(shù)據(jù)
    model = ExponentialSmoothing(data.iloc[:,0], trend="add", seasonal="add", seasonal_periods=12).fit()
    pred = model.forecast(18)
    print('三階平滑預測結(jié)果數(shù)據(jù):\n',pred,file=wordfile)
    #qq圖正態(tài)性檢驗
    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)性檢驗'+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)性檢驗qq圖' + num))
    plt.show()
    #預測圖
    plt.figure(figsize=(15,8))
    plt.title('最終預測結(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='預測線')
    data.iloc[:,0].plot(color='black',linewidth=3.0,label='真實線')
    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()
    print('-------------','以上為',num,'年的數(shù)據(jù)','------------',file=wordfile)
 
if __name__=='__main__':
    wordfile = open(r'C:\Users\Lenovo\Desktop\商業(yè)數(shù)據(jù)挖掘大作業(yè)\數(shù)據(jù)輸出.docx', 'w')#打開一個word文檔,只有打開了才能寫入
    for num in datasets:
        timeda_3(num)
    wordfile.close()#寫完word文檔后要關(guān)閉,才能保存。
    print('運行完畢')

到此這篇關(guān)于如何在Python中用三階指數(shù)平滑模型對金融數(shù)據(jù)集進行擬合與預測的文章就介紹到這了,更多相關(guān)python 模型 金融 人工智能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論