@Channelchan
2017-03-08T19:44:52.000000Z
字数 2784
阅读 26172
未分类
一个对称的分布偏峰(skewness)的值为 0.
正倾斜偏峰分布意味 mean > median > mode。
负倾斜偏峰分布意味 mean < median < mode.
import tushare as ts
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
s是指shape=sigma, loc是mu。
lognorm.pdf(x, s, loc, scale)
# Generate xvalues for which we will plot the distribution
#Percent point function (inverse of cdf - percentiles)
x = np.linspace(stats.lognorm.ppf(0.01, .7, loc=-.1), stats.lognorm.ppf(0.99, .7, loc=-.1), 150)
# Negatively skewed distribution
lognormal = stats.lognorm.pdf(x, .7)
plt.plot(x, lognormal, label='Skew < 0')
# Positively skewed distribution
plt.plot(x, lognormal[::-1], label='Skew > 0')
plt.legend()
plt.show()
读取000001股票的skewnes值
data = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
data.index = pd.to_datetime(data['date'],format='%Y-%m-%d')
data['returns'] = data['close'].pct_change()[1:]
returns = data['returns'].dropna()
print 'Skew:', stats.skew(returns)
print 'Mean:', np.mean(returns)
print 'Median:', np.median(returns)
plt.hist(returns, 30)
plt.show()
Skew: -0.589166660049
Mean: -1.11957279761e-05
Median: 0.000939739222366
xs = np.linspace(-6,6,300)
normal = stats.norm.pdf(xs)
# Plot some example distributions
plt.plot(xs, stats.laplace.pdf(xs), label='Leptokurtic')
print 'Excess kurtosis of leptokurtic distribution:', (stats.laplace.stats(moments='k'))
plt.plot(xs, normal, label='Mesokurtic (normal)')
print 'Excess kurtosis of mesokurtic distribution:', (stats.norm.stats(moments='k'))
plt.plot(xs,stats.cosine.pdf(xs), label='Platykurtic')
print 'Excess kurtosis of platykurtic distribution:', (stats.cosine.stats(moments='k'))
plt.legend()
plt.show()
Excess kurtosis of leptokurtic distribution: 3.0
Excess kurtosis of mesokurtic distribution: 0.0
Excess kurtosis of platykurtic distribution: -0.593762875598
获取000001股票的超值峰度
print "Excess kurtosis of returns: ", stats.kurtosis(returns)
Excess kurtosis of returns: 3.95204544689
'''大于三属于尖顶峰度'''
return (JB, JBpv, skew, kurtosis)
data = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
data.index = pd.to_datetime(data['date'],format='%Y-%m-%d')
data['percentage']=data['close'].pct_change()
returns = data['percentage'][1:]
_, pvalue, _, _ = jarque_bera(returns)
if pvalue > 0.05:
print 'The returns are likely normal.'
else:
print 'The returns are likely not normal.'
The returns are likely not normal.
例子对000001股票从12年到16年的15分钟数据,用均线交叉的策略,获取了收益数据。
数据可从微云下载:
import pandas as pd
import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
r = pd.read_excel('performance.xls')
r.index = r[u'最后成交时间']
r = r[r[u'买卖'] == u'卖']
r['equity'] = r[u'累积盈利']+100000
returns = r[u'盈利']/r['equity']
print(returns)
plt.hist(returns)
print 'Skew:', stats.skew(returns)
print 'Mean:', np.mean(returns)
print 'Median:', np.median(returns)
print 'kurtosis:', stats.kurtosis(returns)
plt.show()
Skew: 2.98409620906
Mean: 0.00258093135289
Median: -0.00318133586001
kurtosis: 12.7743488626