如何统计投资品种波动率（python）？

volatility(510050vs510900).png

图中蓝色为50ETF，可以看出，过去三年来它的日波动性明显强于H股ETF。

下面就以50ETF和H股ETF为例，统计这两个投资品种的日波动率，其他品种、其他周期类似处理即可。附上代码：

# –
– coding: utf-8 –

“””

Created on Sat Jul 09 12:03:03 2016

``````@author: forestgumpgg（雪球ID、聚宽ID），欢迎关注，共同讨论量化分析之道

"""
import pandas as pd
import matplotlib.pyplot as plt
import tushare as ts
import math

ts.set_token('********') #此次隐去了自己用的token

start_date = '2013-07-01'
end_date = '2016-06-30'

info510050 = ts.get_hist_data('510050', start= start_date, end=end_date)
info510050 = info510050.sort_index(axis = 'index', ascending = True)
info510050['day_volatility'] = (pow(info510050['high'] -info510050['close'] , 2) +
pow(info510050['low'] -info510050['close'] , 2) )
for i in range(0,len(info510050.index)):
info510050['day_volatility'][i] = math.sqrt(info510050['day_volatility'][i])

info510900 = ts.get_hist_data('510900', start=start_date, end=end_date)
info510900 = info510900.sort_index(axis = 'index', ascending = True)
info510900['day_volatility'] = (pow(info510900['high'] -info510900['close'] , 2) +
pow(info510900['low'] -info510900['close'] , 2) )
for i in range(0,len(info510900.index)):
info510900['day_volatility'][i] = math.sqrt(info510900['day_volatility'][i])

plt_title = 'volatility(50ETF vs H_ETF)'
plt_figsize = (16, 9)  #unit is inch
plt.figure()
info510050['day_volatility'].plot(figsize = plt_figsize, title = plt_title, grid = True, legend =True)
info510900['day_volatility'].plot(figsize = plt_figsize, title = plt_title, grid = True, legend =True)
plt.legend(['50ETF','H_ETF'])

plt.savefig("volatility(510050vs510900).png")``````
原文作者：阿甘数量化
原文地址: https://www.jianshu.com/p/10643554afc6
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。

Scroll Up