时间序列-ARIMA模型调参检验实战

1.数据格式

在这里插入图片描述

2.理论补充

关于截断与拖尾如何选择模型
参考:博客
在这里插入图片描述
剩余部分代码中都有

3.代码
#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author: liujie
@software: PyCharm
@file: ArmaModel(2,2)or(0,1).py
@time: 2020/11/6 17:33
"""
'''
模型的介绍:
AR、MA、ARMA、ARIMA模型 参考:https://blog.csdn.net/u010687164/article/details/86010154
1.自回归模型AR(p)模型:利用时间序列前期数值与后期数值的相关关系,建立一个包含前后期数值的自变量回归方程
2.移动平均MA(q)模型:t 时间点的序列值为白噪声 u_t 的加权之和
3.自回归滑动平滑ARMA(p,q)模型:移动平均方程是对自回归模型的一个补充.这种模型综合了AR与MA两种模型的优势,解决了随机变动项的求解问题。
4.ARIMA(p,q,d)模型:AR/MA/ARMA用于分析平稳时间序列,接下来所说的ARIMA通过差分可以用于处理非平稳时间序列。参数d为差分的次数。
  相比于ARMA模型,该模型需要将不平稳数据进行d次差分形成一个稳定的时间序列数据,然后采用ARMA模型
'''
import warnings
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import statsmodels as sm
from datetime import datetime
# 作qq图
from statsmodels.graphics.api import qqplot
# 作ADF单方根检验
from statsmodels.tsa.stattools import adfuller as ADF
# 自相关、偏自相关
from statsmodels.tsa.stattools import acf, pacf
# ARMA模型
from statsmodels.tsa.arima_model import ARMA
# ARIMA模型
from statsmodels.tsa.arima_model import ARIMA
# 季节性分解
from statsmodels.tsa.seasonal import seasonal_decompose
# 作自相关图与偏自相关图
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
# SciPy的stats模块提供了大约80种连续随机变量和10多种离散分布变量
from scipy import stats
# D-W检验
from statsmodels.stats.stattools import durbin_watson
from sklearn.metrics import mean_squared_error


class ArimaModelTest:
    '''
    ARIMA建模的基本步骤:
        1.获取被观测的时间序列数据
        2.对时序数据进行平稳化处理,利用差分寻找最佳的d值
        3.根据平稳化处理后的时序数据自相关、偏相关图选择合适的p,q值,并对ARIMA模型进行AIC,BIC,HQIC准则验证
        4.检查残差序列:分别用自相关、偏自相关、D-W检验,正态分布检验、qq图检验、Ljung-Box检验
        5.最后进行数据预测
    '''

    # 参数初始化
    def __init__(self, lags):
        self.lags = lags

    # 第一步:平稳性检验
    '''
    时序数据平稳性是进行时间序列分析的前提条件,为什么需要满足平稳性要求呢?在大数定理与中心极限定理中要求样本同分布
    (这里的同分布等价于时间序列中的平稳性),我们在建模过程中有很多是建立在大数定理与中心极限定理条件下的,如果它不满足,
    得到的很多结论都不可靠
    平稳性检验一般采用观察法与检验法
    python判断时序数据的稳定性
        有两种方法:1.Rolling statistic--即每个时间段内的数据均值与标准差情况
                 2.ADF单方根检验
                    Dickey-Fuller Test 在一定的置信水平下,对于时间序列数据假设Null hypothesis:非稳定 
                    如果检验值小于临界值,则拒绝null hypothesis,即数据是稳定的,反之是非稳定的
                 查看结果参考:https://blog.csdn.net/qq_18999357/article/details/85097797
                 p值越小越好,p值要求小于给定的显著水平,p值要小于0.05,等于0是最好的。
    '''

    def test_stationarity(self, timeseries, window):

        # 移动平均图
        rol_mean = timeseries.rolling(window=window).mean()
        rol_std = timeseries.rolling(window=window).std()
        fig = plt.figure(facecolor='white')
        fig.add_subplot()
        plt.plot(timeseries, 'r-', label='Original')
        plt.plot(rol_mean, 'b-', label='rolling_mean')
        plt.plot(rol_std, 'g-', label='rolling_std')
        plt.legend(loc='best')
        plt.title('Rolling Mean'
  • 15
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值