案例: 电视广告呼入次数预测
import pandas as pd
import numpy as np
np.random.seed(1206)
import matplotlib.pyplot as plt
%matplotlib inline
# 解决坐标轴刻度负号乱码
plt.rcParams['axes.unicode_minus'] = False
# 解决中文乱码问题
plt.rcParams['font.sans-serif'] = ['Simhei']
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20, 6
import scipy.stats as stats
import statsmodels.api as sm
import statsmodels.tsa.api as smt
from statsmodels.tsa.arima_process import arma_generate_sample
from statsmodels.tsa.stattools import adfuller
from statsmodels.stats.diagnostic import acorr_ljungbox
import warnings
warnings.filterwarnings("ignore")
"""
在这儿:https://github.com/statsmodels/statsmodels/tree/master/statsmodels/tsa
下载holtwinters.py,tsamodel.py,并放在当前脚本所在路径下
"""
import sys, os
sys.path.append(os.getcwd())
from holtwinters import ExponentialSmoothing, SimpleExpSmoothing, Holt
制作ACF, PACF
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# 自相关和偏相关图,默认阶数为31阶
def draw_acf_pacf(ts, subtitle, lags=31):
print("自相关图和偏相关图,maxlags={}".format(lags))
f = plt.figure(facecolor='white', figsize=(18,4))
ax1 = f.add_subplot(121)
plot_acf(ts, lags=lags, ax=ax1, title='ACF\n{}'.format(subtitle))
ax2 = f.add_subplot(122)
plot_pacf(ts, lags=lags, ax=ax2, title='PACF\n{}'.format(subtitle))
plt.show()
from statsmodels.tsa.stattools import adfuller
def test_stationarity(timeseries):
# Perform Dickey-Fuller test:
print('Results of Dickey-Fuller Test:')
dftest = adfuller(timeseries, autolag='AIC')
dfoutput = pd.Series(dftest[0:4], index=['Test Statistic','p-value','#Lags Used','Number of Observations Used'])
for key,value in dftest[4].items():
dfoutput['Critical Value ({})'.format(key)] = value
print(dfoutput)
# 白噪声检验:Ljung-Box test
def randomness(ts, lags=31):
rdtest = acorr_ljungbox(ts,lags=lags