ARIMA差分自回归模型python代码
来源:互联网 发布:安卓与php服务器端 编辑:程序博客网 时间:2024/05/17 07:23
1.差分自回归模型的基本思想:将预测对象随时间推移而形成的数据序列视为一个随机序列,用一定的数学模型来近似描述这个序列。这个模型一旦被识别后就可以从时间序列的过去值以及现在值来预测未来值。不能对时间序列直接采取线性回归算法是因为时间序列所对应的值并不是独立的。2.要对非平稳的序列进行平稳处理,因为非平稳的序列不能进行预测。3.代码:
#!/usr/bin/pythonimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom statsmodels.tsa.arima_model import ARIMAdef date_parse(date): return pd.datetime.strptime(date, '%Y-%m')if __name__ == '__main__': data = pd.read_csv('AirPassengers.csv', header = 0, parse_dates = ['Month'], date_parser = date_parse, index_col = ['Month']) p,d,q = 2, 1, 2 data.rename(columns = {'#Passengers':'Passengers'}, inplace = True) passengersNums = data['Passengers'].astype(np.float) logNums = np.log(passengersNums) subtractionNums = logNums - logNums.shift(periods = d) rollMeanNums = logNums.rolling(window = q).mean() logMRoll = logNums - rollMeanNums plt.plot(logNums, 'g-', lw = 2, label = u'log of original') plt.plot(subtractionNums, 'y-', lw = 2, label = u'subtractionNums') plt.plot(logMRoll, 'r-', lw = 2, label = u'log of original - log of rollingMean') plt.legend(loc = 'best') plt.show() arima = ARIMA(endog = logNums, order = (p,d,q)) proArima = arima.fit(disp = -1) fittedArima = proArima.fittedvalues.cumsum() + logNums[0] fittedNums = np.exp(fittedArima) plt.plot(passengersNums, 'g-', lw = 2, label = u'orignal') plt.plot(fittedNums, 'r-', lw = 2, label = u'fitted') plt.legend(loc = 'best') plt.show()
解释:
(1)首先对原始时间序列数据进行对数处理,差分处理,滑动平均处理。其中差分处理与滑动平均处理是对非平稳序列进行平稳处理,结果如图:
很明显观察到用差分与原始数据减去滑动平均的结果可以对非平稳序列进行平稳处理。
(2)然后用差分自回归模型拟合原始数据。其中参数p为自回顾函数的参数,d为差分的参数,q为滑动平均的参数。结果如图:
阅读全文
0 0
- ARIMA差分自回归模型python代码
- 自回归模型(AR)、移动平均模型(MA)、自回归移动平均模型(ARMA)以及差分自回归移动平均模型(ARIMA)辨析
- 时间序列之差分自回归移动平均法(ARIMA)
- 时间序列(四)ARIMA模型与差分
- 残差自回归模型
- AutoRegressive Integrated Moving Average(差分自回归移动平均模型)
- Python----股票ARIMA模型拟合
- ARIMA模型
- ARIMA模型
- ARIMA模型
- ARIMA模型
- ARIMA模型
- 自回归纹理模型
- 基于自回归模型(AR)的自适应阈值的残差比异常检测
- 【DE算法】差分进化算法原理及python代码
- SAS ARIMA 模型
- ARIMA模型-R语言
- ARIMA模型简介
- Android bitmap讲解与实践
- 初学Web:计算机气质个人主页
- 开发一个自动点击工具
- R语言使用Apriori规则完成关联挖掘
- Poco库Timer定时器
- ARIMA差分自回归模型python代码
- EffectiveC++学习笔记-条款3
- 【每天一点linux】进程间通信--信号
- Ubuntu16.04 安装 Adouble Falsh Player
- 学习kotlin第13天_具体化的类型参数、内联属性、解构声明、集合
- 在Android Studio 和 Eclipse 的 git 插件操作 "代码提交"以及"代码冲突"
- python实现多线程post方法进行压测脚本模板
- Java基础教程23-static举例
- 湖北民族学院oj 1692(贪心) 之 木棒