[关闭]
@Channelchan 2017-03-08T11:24:43.000000Z 字数 1735 阅读 26289

StatsModels 变量相关性

未分类


np.cov(X,Y)
np.corrcoef(X,Y)
np.cov(X,Y)[0,1]/(np.std(X)*np.std(Y))

计算000001、000005股票与深圳指数的相关性

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import tushare as ts
  5. asset1 = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
  6. asset1.index = pd.to_datetime(asset1['date'], format='%Y-%m-%d')
  7. asset1 = asset1['close']
  8. asset2 = ts.get_k_data('000005', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
  9. asset2.index = pd.to_datetime(asset2['date'], format='%Y-%m-%d')
  10. asset2 = asset2['close']
  11. benchmark = ts.get_hist_data('sh', start='2016-01-01', end='2016-12-31', ktype='D')[::-1]
  12. benchmark = benchmark['close']
  13. new = pd.concat([asset1, asset2, benchmark],join='inner', axis=1)
  14. new.columns = ['asset1', 'asset2', 'benchmark']
  1. print "Correlation coefficients"
  2. print "000001 and benchmark: ", np.corrcoef(new['asset1'],new['benchmark'])[0,1]
  3. print "000005 and benchmark: ", np.corrcoef(new['asset2'],new['benchmark'])[0,1]
  4. print "000001 and 000005: ", np.corrcoef(new['asset1'],new['asset2'])[0,1]
  5. print "000001 and 000005: ", np.cov(new['asset1'],new['asset2'])[0,1]/(np.std(new['asset1'])*np.std(new['asset2']))
  1. Correlation coefficients
  2. 000001 and benchmark: 0.904350480115
  3. 000005 and benchmark: 0.329516731028
  4. # 由于degree of freedom 结果会有不同
  5. 000001 and 000005: 0.138377116304
  6. 000001 and 000005: 0.138946569458

高相关性图表

  1. plt.scatter(new['asset1'], new['benchmark'])
  2. plt.show()

由于相关性会随着时间的变化而变化,目前计算出来的相关性不代表未来,因此我们需要通过调整不同周期来计算动态的相关性系数,并且计算相关系数的分布情况,以便对未来做区间估计。

周期为60天的动态相关性计算

  1. rolling_correlation = new['asset1'].rolling(window=60).corr(new['benchmark'])
  2. plt.subplot(2,1,1)
  3. plt.plot(rolling_correlation)
  4. plt.xlabel('Day')
  5. plt.ylabel('60day Rolling Correlation')
  6. plt.subplot(2,1,2)
  7. plt.hist(rolling_correlation.dropna())
  8. plt.show()

Determining related Strategies

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注