pandas:

pandas.DataFrame.rolling

pandas.DataFrame.ewm

pandas.DataFrame.mean

其中rolling可以指定窗口类型win_type,比如boxcar, boxcar, triang, blackman, hanning, bartlett

以hanning window为例,其窗口形状为钟型,曲线函数为:

python代码:

import matplotlib.pyplot as plt
import statsmodels.api as smdata_loader = sm.datasets.sunspots.load_pandas()
df = data_loader.dataprint("df length is %d" %len(df))
print("inital df head:")
print(df.head(20))
print("SMA head:")
print(df["SUNACTIVITY"].rolling(window=10).mean().head(20))
print("EMA head:")
print(df["SUNACTIVITY"].ewm(span=10,min_periods=10).mean().head(20))year_range = df["YEAR"].values
plt.plot(year_range, df["SUNACTIVITY"].values, label="Original")
plt.plot(year_range, df["SUNACTIVITY"].rolling(window=10).mean(), label="SMA wave")
plt.plot(year_range, df["SUNACTIVITY"].rolling(window=10, win_type='hanning').mean(), label="SMA wave with Hanning window")
plt.plot(year_range, df["SUNACTIVITY"].ewm(span=10,min_periods=10).mean(), label="EMA wave")
plt.legend()
plt.show()

输出结果:

df length is 309
inital df head:YEAR  SUNACTIVITY
0   1700.0          5.0
1   1701.0         11.0
2   1702.0         16.0
3   1703.0         23.0
4   1704.0         36.0
5   1705.0         58.0
6   1706.0         29.0
7   1707.0         20.0
8   1708.0         10.0
9   1709.0          8.0
10  1710.0          3.0
11  1711.0          0.0
12  1712.0          0.0
13  1713.0          2.0
14  1714.0         11.0
15  1715.0         27.0
16  1716.0         47.0
17  1717.0         63.0
18  1718.0         60.0
19  1719.0         39.0
SMA head:
0      NaN
1      NaN
2      NaN
3      NaN
4      NaN
5      NaN
6      NaN
7      NaN
8      NaN
9     21.6
10    21.4
11    20.3
12    18.7
13    16.6
14    14.1
15    11.0
16    12.8
17    17.1
18    22.1
19    25.2
Name: SUNACTIVITY, dtype: float64
EMA head:
0           NaN
1           NaN
2           NaN
3           NaN
4           NaN
5           NaN
6           NaN
7           NaN
8           NaN
9     20.690866
10    17.076843
11    13.664921
12    10.982917
13     9.244962
14     9.580603
15    12.880856
16    19.296004
17    27.462651
18    33.512151
19    34.528305
Name: SUNACTIVITY, dtype: float64

参考:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html

http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.ewm.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.mean.html

时间序列分析 - 移动平均SMA, EMA(EWMA) 之python相关推荐

  1. 时间序列分析 - 移动平均SMA, WMA, EMA(EWMA) 之理论公式

    原文: https://zh.wikipedia.org/w/index.php?title=%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D%87&variant=zh-c ...

  2. 金融时间序列分析:3. First Demo By Python

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

  3. 时间序列分析建模及相关算法的python实现

    纯随机性检验 原理: 纯随机序列:平稳序列值之间没有任何相关性的序称为纯随机序列,这意味着该序列过去的行为对将来的发展没有丝毫影响. 从统计分析的角度而言, 纯随机序列没有任何分析价值.纯随机序列也称 ...

  4. 金融时间序列分析:8. MA模型实例(Python)

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

  5. 【Python数据分析】时间序列分析——AR/MA/ARMA/ARIMA

    目录 一.时间序列的平稳性与差分法 1.时间序列的平稳性: 2.平稳性检验 3.纯随机性检验 4.差分法 二.平稳时间序列模型 1.AR模型 2.MR模型 3.ARMA模型 4.平稳时间序列建模步骤 ...

  6. 金融时间序列分析:5. AR模型实例(Python)

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

  7. 金融时间序列分析: 10. ARMA模型实例(R,Python)

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

  8. 金融时间序列分析:1. 基础知识

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

  9. 金融时间序列分析:7. MA滑动平均模型

    0. 目录 金融时间序列分析:9. ARMA自回归移动平均模型 金融时间序列分析:8. MA模型实例(Python) 金融时间序列分析:7. MA滑动平均模型 金融时间序列分析:6. AR模型实例 金 ...

最新文章

  1. 一文带你重温去年最难忘的10个数据泄露事件
  2. 干货丨八大基础概念带你入门机器学习!
  3. c# 多线程排队队列实现的源码
  4. C#中Trim()、TrimStart()、TrimEnd()的错误认识
  5. ???????????? no permissions的解决办法 解决网上方法行不通的问题
  6. imagesLoaded-检测图片是否正确加载的js插件
  7. 实战突击:PHP项目开发案例整合(第2版)
  8. 空行的符号 计算机,word中空白行的回车符号怎么消除 消除word中的回车符号
  9. GD32f103介绍第一章
  10. “互联网+”服务产业现状和个人信息保护特征分析
  11. 谈一谈工程中最为常用的概率图模型
  12. drop index mysql_MySQL修改和删除索引(DROP INDEX)
  13. divmod在python中是什么意思_python中divmod是什么
  14. 编译原理:LL(1)文法 语法分析器(预测分析表法)
  15. Kaggle---Toxic Comment Classification Challenge
  16. 配置 Rails 应用程序
  17. 物联网设备数据流转之实时数据从哪里来、如何转发:Node.js, MQTT, EMQX的WebHook
  18. ol xyz 加载天地图_Openlayers3 加载百度地图,天地图
  19. 【总结】放电管知识大全,看过这篇你都了解了
  20. finder刷新文件夹_如何在OS X Finder中自定义文件夹视图

热门文章

  1. jq给元素添加或删除类名
  2. High Dynamic Range Imaging
  3. java crc计算_Java对文件CRC32值计算
  4. 团队间合作的教训总结
  5. 计算机职称考试网络模块试题,计算机职称考试Excel2003模块试题
  6. 疫情过后,人工智能和机器人会迎来哪些机遇?
  7. Intel Complier
  8. 有哪些程序员才懂的梗?
  9. mysql 5.6 开启gtid_MySQL 5.6 GTID 原理以及使用
  10. 3d建模师需要经常加班吗?一分钱干一分事