时间序列分析基础
# !pip install jupyterthemes
https://github.com/dunovank/jupyter-themes
修改jupyter notebook 主题和界面宽度,字体
在cmd中,键入jt -t grade3 -cellw 90% -fs 14 -ofs 14
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'] = 18, 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
第一部分:
白噪声及其检验,AR,MA,ARMA,差分
白噪声
# 正太分布,白噪声就是一列独立分布的正态序列
def norm(mu, sigma, size, seed):
np.random.seed(seed)
s = np.random.normal(mu,sigma,size)
return s
import matplotlib.mlab as mlab
x = norm(0, 1, 1000, 1206)
fig,axs = plt.subplots(1,2,figsize=(20,4))
axs[0].plot(x)
axs[0].set_ylabel('Frequency')
n, bins, patches = axs[1].hist(x, 50, width=.6, normed=1)
y = mlab.normpdf(bins, 0, 1)
plt.plot(bins, y, '--');
[外链图片转存(img-sHu9wKbC-1562728996727)(output_8_0.png)]
白噪声检验
# stats.shapiro?
Perform the Shapiro-Wilk test for normality.The Shapiro-Wilk test tests the null hypothesis that the data was drawn from a normal distribution.
Returns
W : float - The test statistic.
p-value : float - The p-value for the hypothesis test.
W检验全称Shapiro-Wilk检验,是一种基于相关性的算法。计算可得到一个相关系数,它越接近1就越表明数据和正态分布拟合得越好。
print('Shapiro-Wilk test:', stats.shapiro(x))
Shapiro-Wilk test: (0.998481035232544, 0.5423095226287842)
# stats.jarque_bera?
Perform the Jarque-Bera goodness of fit test on sample data.The Jarque-Bera test tests whether the sample data has the skewness and kurtosis matching a normal distribution.
Jarque-Bera 检验是一种拟合优度检验,检验是否样本和正态分布比较有偏度和峰度
print('Jarque-Bera test:', stats.jarque_bera(x))
Jarque-Bera test: (3.5855535668417966, 0.1664972005159684)
# qq 图
sm.qqplot(x,line='45')
plt.title('qq 图');
[外链图片转存(img-Ny2PjGmC-1562728996733)(output_16_0.png)]
制作ACF, PACF
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
plot_acf(x,lags=31)
[外链图片转存(img-YiZ2xXYV-1562728996734)(output_19_0.png)]
[外链图片转存(img-WPpcnZkF-1562728996734)(output_19_1.png)]
# 自相关和偏相关图,默认阶数为31阶
def draw_acf_pacf(ts, subtitle, lags=31):
print("自相关图和偏相关图,maxlags={}".format(lags))
f = plt.figure(facecolor='white', figsize=(18,4