@Channelchan
2017-03-08T11:21:41.000000Z
字数 1244
阅读 26572
未分类
Confidence Interval:(区间估计)
品种收益置信区间
import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy import stats
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')
per = data['close'].pct_change()[1:]
p_mu = np.mean(per)
p_std = np.std(per)
cil, cir = stats.norm.interval(0.95, loc=p_mu, scale=p_std)
print(cil, cir)
输出结果:说明用一年的数据计算出这只股票一天的涨跌幅95%的概率在(-2.27%,2.27%)之间
(-0.022749357724022465, 0.022726966268070206)
x = np.linspace(per.min(), per.max(), len(per))
y = stats.norm.pdf(x,p_mu, p_std)
plt.plot(x, y)
plt.vlines(cil,0, 35, colors='r', linestyles='dashed')
plt.vlines(cir, 0, 35, colors='r', linestyles='dashed')
plt.show()
Sample Mean vs. Population Mean
sample_size = 100
sample = np.random.normal(p_mu, p_std, size=sample_size)
s_mu = np.mean(sample)
s_std = np.std(sample)
se = s_std/np.sqrt(sample_size)
cil, cir = stats.t.interval(0.95, len(sample)-1, loc=s_mu, scale=se)
print(cil, cir)
x = np.linspace(sample.min(), sample.max(), len(sample)-1)
y = stats.norm.pdf(x, np.mean(sample), np.std(sample))
plt.plot(x, y)
plt.vlines(cil,0, 20, colors='r', linestyles='dashed')
plt.vlines(cir, 0, 20, colors='r', linestyles='dashed')
plt.show()
策略收益置信区间