#加载相应的包 import numpy as np import pandas as pd from pandas_datareader import data,wb #从互联网读入数据 goog=data.DataReader('GOOG',data_source='yahoo') goog.head() goog.tail() #实现对波动率的计算 goog['log_ret']=np.log(goog['Close']/goog['Close'].shift(1)) goog['volatility']=pd.rolling_std(goog['log_ret'],window=252)*np.sqrt(252) #绘制图形 %matplotlib inline goog[['Close','volatility']].plot(subplots=True,color='Red',figsize=(8,6))
#似乎中间有除权的行为影响了判断。我们用除权后的价格进行计算 goog['log_ret']=np.log(goog['Adj Close']/goog['Adj Close'].shift(1)) goog['volatility']=pd.rolling_std(goog['log_ret'],window=252)*np.sqrt(252) #绘制新图形 %matplotlib inline goog[['Adj Close','volatility']].plot(subplots=True,color='Red',figsize=(8,6))
#用除权后的数据避免了断崖式的缺口,但是,规律仍旧不是非常明显,我们在用收益率试一试 goog['log_ret']=np.log(goog['Adj Close']/goog['Adj Close'].shift(1)) goog['volatility']=pd.rolling_std(goog['log_ret'],window=252)*np.sqrt(252) #绘制新图形 %matplotlib inline goog[['log_ret','volatility']].plot(subplots=True,color='Red',figsize=(12,6))