欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

時間序列分析之ARIMA模型預(yù)測餐廳銷量

 更新時間:2022年11月02日 10:10:07   作者:Eureka丶  
這篇文章主要介紹了時間序列分析之ARIMA模型預(yù)測餐廳銷量,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

ARIMA模型預(yù)測餐廳銷量

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
from matplotlib.pylab import style ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 自定義圖表風(fēng)格
style.use('ggplot')
# 解決中文的顯示問題
plt.rcParams["font.sans-serif"] = ["SimHei"]
plt.rcParams["axes.unicode_minus"] = False
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf ? ? ? ? # 自相關(guān)圖、偏自相關(guān)圖
from statsmodels.tsa.stattools import adfuller as ADF ? ? ? ? ? ? ? ?# 平穩(wěn)性檢驗(yàn)
from statsmodels.stats.diagnostic import acorr_ljungbox ? ? ? ? ? ? ?# 白噪聲檢驗(yàn)
import statsmodels.api as sm ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # D-W檢驗(yàn),一階自相關(guān)檢驗(yàn)
from statsmodels.graphics.api import qqplot ? ? ? ? ? ? ? ? ? ? ? ? ?# 畫QQ圖,檢驗(yàn)一組數(shù)據(jù)是否服從正態(tài)分布
from statsmodels.tsa.arima_model import ARIMA

1、導(dǎo)入數(shù)據(jù) 

sale = pd.read_excel('./arima_data.xls', index_col='日期')
sale.head()

sale.info()
print('-----')
sale.銷量 = sale.銷量.astype('float')
sale.info()

2、原始序列檢驗(yàn)

· 時序圖

plt.figure(figsize=(10,5))
sale.plot()
plt.show()
 
#解讀:具有單調(diào)遞增趨勢,則是非平穩(wěn)序列。

· 自相關(guān)圖

plot_acf(sale, lags=35).show()
 
#解讀:自相關(guān)系數(shù)長期大于零,沒有趨向于零,說明序列間具有很強(qiáng)的長期相關(guān)性。

· 平穩(wěn)性檢驗(yàn)

#方法:單位根檢驗(yàn)
 
print('原始序列的ADF檢驗(yàn)結(jié)果為:',ADF(sale.銷量))
 
#解讀:P值大于顯著性水平α(0.05),接受原假設(shè)(非平穩(wěn)序列),說明原始序列是非平穩(wěn)序列。

第一個是adf檢驗(yàn)的結(jié)果。 第二個是統(tǒng)計(jì)量的P值。 第三個是計(jì)算過程中用到的延遲階數(shù)。 第四個是用于ADF回歸和計(jì)算的觀測值的個數(shù)。 第五個是配合第一個一起看的,是在99%,95%,90%置信區(qū)間下的臨界的ADF檢驗(yàn)的值。

原文鏈接:adfuller函數(shù)返回值的參數(shù)說明與記錄

3、一階差分序列檢驗(yàn)

d1_sale = sale.diff(periods=1, axis=0).dropna()
 
#時序圖
plt.figure(figsize=(10,5))
d1_sale.plot()
plt.show()
#解讀:在均值附件比較平穩(wěn)波動
 
#自相關(guān)圖
plot_acf(d1_sale, lags=34).show()
#解讀:有短期相關(guān)性,但趨向于零。
 
#平穩(wěn)性檢驗(yàn)
print('原始序列的ADF檢驗(yàn)結(jié)果為:',ADF(d1_sale.銷量))
 
#解讀:P值小于顯著性水平α(0.05),拒絕原假設(shè)(非平穩(wěn)序列),說明一階差分序列是平穩(wěn)序列。

· 白噪聲檢驗(yàn)

print('一階差分序列的白噪聲檢驗(yàn)結(jié)果為:',acorr_ljungbox(d1_sale, lags=1))#返回統(tǒng)計(jì)量、P值
 
#解讀:p值小于0.05,拒絕原假設(shè)(純隨機(jī)序列),說明一階差分序列是非白噪聲序列。

4、定階

· 參數(shù)調(diào)優(yōu):人工判別

d1_sale = sale.diff(periods=1, axis=0).dropna()
 
#自相關(guān)圖
plot_acf(d1_sale, lags=34).show()
 
#解讀:有短期相關(guān)性,但趨向于零。
 
#偏自相關(guān)圖
plot_pacf(d1_sale, lags=10).show()
 
 
#偏自相關(guān)圖
plot_pacf(d1_sale, lags=17).show()
 
#解讀:自相關(guān)圖,1階截尾;偏自相關(guān)圖,拖尾。則ARIMA(p,d,q)=ARIMA(0,1,1)

· 參數(shù)調(diào)優(yōu):BIC

pmax = int(len(d1_sale) / 10) #一般階數(shù)不超過length/10
qmax = int(len(d1_sale) / 10) #一般階數(shù)不超過length/10
pmax
qmax

bic_matrix = []
for p in range(pmax + 1):
    tmp = []
    for q in range(qmax + 1):
        try:
            tmp.append(ARIMA(tuple(sale), (p, 1, q)).fit().bic)
        except:
            tmp.append(None)
    bic_matrix.append(tmp)
bic_matrix = pd.DataFrame(bic_matrix)
bic_matrix

bic_matrix.stack()

p,q=bic_matrix.stack().idxmin() #最小值的索引
print('用BIC方法得到最優(yōu)的p值是%d,q值是%d'%(p,q))

· 參數(shù)調(diào)優(yōu):AIC

pmax = int(len(d1_sale )/ 10) #一般階數(shù)不超過length/10
qmax = int(len(d1_sale) / 10) #一般階數(shù)不超過length/10
 
aic_matrix = []
for p in range(pmax + 1):
    tmp = []
    for q in range(qmax + 1):
        try:
            tmp.append(ARIMA(tuple(sale), (p, 1, q)).fit().aic)
        except:
            tmp.append(None)
    aic_matrix.append(tmp)
aic_matrix = pd.DataFrame(aic_matrix)
p,q = aic_matrix.stack().idxmin() #最小值的索引
print('用AIC方法得到最優(yōu)的p值是%d,q值是%d'%(p,q))

5、建模及預(yù)測

· 建模

#創(chuàng)建模型
model = ARIMA(tuple(sale), (0, 1, 1)).fit()
#查看模型報(bào)告
model.summary2()

· 殘差檢驗(yàn)

resid = model.resid
 
#自相關(guān)圖
plot_acf(resid, lags=35).show()
 
#解讀:有短期相關(guān)性,但趨向于零。
 
#偏自相關(guān)圖
plot_pacf(resid, lags=10).show()
 
#偏自相關(guān)圖
plot_pacf(resid, lags=17).show()

· QQ圖

qqplot(resid, line='q', fit=True).show() 
 
#解讀:殘差服從正態(tài)分布,均值為零,方差為常數(shù)

· D-W檢驗(yàn)

德賓-沃森檢驗(yàn),簡稱D-W檢驗(yàn),是目前檢驗(yàn)自相關(guān)性最常用的方法,但它只適用于檢驗(yàn)一階自相關(guān)性。 因?yàn)樽韵嚓P(guān)系數(shù)ρ的值介于-1和1之間,所以 0≤DW≤4。

  • 并且DW=O <=> ρ=1  即存在正自相關(guān)性
  • DW=4 <=> ρ=-1 即存在負(fù)自相關(guān)性
  • DW=2 <=> ρ=0  即不存在(一階)自相關(guān)性

因此,當(dāng)DW值顯著的接近于O或4時,則存在自相關(guān)性,而接近于2時,則不存在(一階)自相關(guān)性。

print('D-W檢驗(yàn)的結(jié)果為:',sm.stats.durbin_watson(resid.values))  
 
#解讀:不存在一階自相關(guān)

· Ljung-Box檢驗(yàn)

Ljung-Box test是對randomness的檢驗(yàn),或者說是對時間序列是否存在滯后相關(guān)的一種統(tǒng)計(jì)檢驗(yàn)。對于滯后相關(guān)的檢驗(yàn),我們常常采用的方法還包括計(jì)算ACF和PCAF并觀察其圖像,但是無論是ACF還是PACF都僅僅考慮是否存在某一特定滯后階數(shù)的相關(guān)。LB檢驗(yàn)則是基于一系列滯后階數(shù),判斷序列總體的相關(guān)性或者說隨機(jī)性是否存在。

時間序列中一個最基本的模型就是高斯白噪聲序列。而對于ARIMA模型,其殘差被假定為高斯白噪聲序列,所以當(dāng)我們用ARIMA模型去擬合數(shù)據(jù)時,擬合后我們要對殘差的估計(jì)序列進(jìn)行LB檢驗(yàn),判斷其是否是高斯白噪聲,如果不是,那么就說明ARIMA模型也許并不是一個適合樣本的模型。

檢驗(yàn)的結(jié)果就是看最后一列前十二行的檢驗(yàn)概率(一般觀察滯后1~12階),如果檢驗(yàn)概率小于給定的顯著性水平,比如0.05、0.10等就拒絕原假設(shè),其原假設(shè)是相關(guān)系數(shù)為零。

# 方法一
print('殘差序列的白噪聲檢驗(yàn)結(jié)果為:',acorr_ljungbox(resid,lags=1))#返回統(tǒng)計(jì)量、P值
 
#解讀:殘差是白噪聲

# 方法二
confint,qstat,pvalues = sm.tsa.acf(resid.values, qstat=True) #qstat is Ljung-Box Q-Statistic. confint is  Confidence intervals for the ACF
data = np.c_[range(1,36), confint[1:], qstat, pvalues]
table = pd.DataFrame(data, columns=['lag', "confint", "qstat", "pvalues(>Q)"])
print(table.set_index('lag'))

· 預(yù)測

#預(yù)測
print('未來7天的銷量預(yù)測:')
model.forecast(7) #預(yù)測、標(biāo)準(zhǔn)差、置信區(qū)間

forecast = pd.Series(model.forecast(7)[0], index=pd.date_range('2015-2-7', periods=7, freq='D'))
forecast

data = pd.concat((sale, forecast), axis=0)
data.columns = ['銷量', '未來7天銷量']
plt.figure(figsize = (10,5))
data.plot()
plt.show()

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論