[关闭]
@Channelchan 2017-03-08T19:55:40.000000Z 字数 4326 阅读 26124

StatsModels 描述性统计

Distribution


描述性统计

描述性统计是用来概括、表述事物整体状况以及事物间关联、类属关系的统计方法。通过统计处理可以简洁地用几个统计值来表示一组数据地集中性和离散型(波动性大小)。

类型 描述 例子 结果
算术均值 Arithmetic Mean 数据的和除以数据的数量 (1+2+2+3+4+7+9) / 7 4
中值 Median 中间的那个值,把数据分成大小两半 1, 2, 2, 3, 4, 7, 9 3
众数 Mode 频度最大的那个数 1, 2, 2, 3, 4, 7, 9 2

百分位数 Percentile:
例子: 如第五百分位,它表示在所有测量数据中,测量值的累计频次达5%。以回报率为例,回报率分布的第五百分位表示有5%的回报率小于此测量值,95%的回报率大于此测量值。

0


方差与标准差:
各数据偏离平均数的距离(离均差)的平均数。


用000001的股票收益率来做实例:

  1. import tushare as ts
  2. import pandas as pd
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  1. data = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D', autype='qfq')
  2. data.index = pd.to_datetime(data['date'], format='%Y-%m-%d')
  3. data['returns'] = data['close'].pct_change()
  4. r = data.returns.dropna()
  5. mean = r.mean()
  6. median = r.median()
  7. mode = r.mode()
  8. std = r.std()
  9. print('mean', mean)
  10. print('median', median)
  11. print('mode', mode)
  12. print('std', std)

('mean', -1.1195727976130874e-05)
('median', 0.0009397392223660095)
('mode', 0 0.0
dtype: float64)
('std', 0.011625261186742808)


随机变量

离散随机变量(Discrete Random Variables)
摇一颗骰子100次,k次摇到6,k是随机变量,k的取值只能是自然数0,1,2,…,6而不能取小数3.5,因而k是离散型随机变量。

  1. #np.random.random_integers(low, high, numberOfSamples)
  2. DiscreteRandomVariable = np.random.random_integers(1, 6, 100)
  3. plt.hist(DiscreteRandomVariable , bins = [1,2,3,4,5,6,7])
  4. plt.xlabel('Value')
  5. plt.ylabel('Occurences')
  6. plt.legend(['DiscreteRandomVariable'])
  7. plt.show()

1


连续随机变量(Continuous Random Variables)
比如,公共汽车每15分钟一班,有100个人在站台等车时间x是个随机变量,x的取值范围是[0,15],它是一个区间,从理论上说在这个区间内可取任一实数3.5、√20等,因而称这随机变量是连续型随机变量。

  1. ##np.random.uniform(low, high, numberOfSamples)
  2. CRV = np.random.uniform(0, 15, 100)
  3. plt.plot(CRV)
  4. plt.xlabel('Time')
  5. plt.ylabel('People')
  6. plt.legend(['ContinuousRandomVariable'])
  7. plt.show()

2


分布

二项分布(Binomial Distribution)
投一个硬币,0.5概率向上,投了5次,重复50次,每一轮向上的次数有多少?

  1. #np.random.binomial(numberOfTrials, probabilityOfSuccess, numberOfSamples)
  2. BinomialRandomVariable = np.random.binomial(5, 0.50,50)
  3. plt.hist(BinomialRandomVariable, bins = [0, 1, 2, 3, 4, 5, 6],align='left')
  4. plt.xlabel('Value')
  5. plt.ylabel('Occurences')
  6. plt.legend(['BinomialRandomVariable'])
  7. plt.show()

3


均匀分布(Uniform Distribution)
从一副牌里抽到黑桃,PDF:

  1. a = 0.0
  2. b = 13.0
  3. x = np.linspace(a, b, 14)
  4. y = [1/(b-a) for i in x]
  5. plt.plot(x, y)
  6. plt.xlabel('Value')
  7. plt.ylabel('Probability')
  8. plt.show()

4


正态分布(Normal Distribution)
用公式与模块代码方法生成不同标准差的正态分布图形,展示对比了解图形。
Formula:

  1. from scipy import stats
  2. mu_1 = 0
  3. mu_2 = 0
  4. sigma_1 = 1
  5. sigma_2 = 2
  6. x = np.linspace(-8, 8, 200)
  7. y = (1/(sigma_1 * np.sqrt(2 * 3.14159))) * np.exp(-(x-mu_1)*(x-mu_1) / (2 * sigma_1 * sigma_1))
  8. z = stats.norm.pdf(x,mu_2,sigma_2)
  9. plt.plot(x, y, x, z)
  10. plt.xlabel('Value')
  11. plt.ylabel('Probability')
  12. plt.show()

5

以正态分布的均值和标准差随机生成一定数量的样本。

  1. y=np.random.normal(mu_1, sigma_1, numberOfSamples)
  2. z=np.random.normal(mu_2, sigma_2, numberOfSamples)

检验分布
检查股票000001的回报率是否满足正态分布,得出回报率的峰态值,最后把回报率的分布度显示出来。

  1. from statsmodels.stats.stattools import jarque_bera
  2. prices = ts.get_k_data('000001', start='2016-01-01', end='2016-12-31', ktype='D',autype='qfq')
  3. prices.index = pd.to_datetime(prices['date'],format='%Y-%m-%d')
  4. # Take the daily returns
  5. returns = prices['close'].pct_change()[1:]
  6. #Set a cutoff
  7. cutoff = 0.01
  8. # Get the pvalue of the JB test
  9. _, p_value, skewness, kurtosis = jarque_bera(returns)
  10. print "The JB test pvalueis: ", p_value
  11. print "We reject the hypothesis that the data are normally distributed ", p_value < cutoff
  12. print "The skewness of the returns is: ", skewness
  13. print "The kurtosis of the returns is: ", kurtosis
  14. plt.hist(returns, bins = 20)
  15. plt.xlabel('Value')
  16. plt.ylabel('Occurrences')
  17. plt.show()

6

The JB test pvalue is: 4.05367795768e-38
We reject the hypothesis that the data are normally distributed True
The skewness of the returns is: -0.589166660049
The kurtosis of the returns is: 6.95204544689


抽取样本的均值与标准差,画出正态分布图。

  1. # Take the sample mean and standard deviation of the returns
  2. sample_mean = np.mean(returns)
  3. sample_std_dev = np.std(returns)
  4. x = np.linspace(-(sample_mean + 4 * sample_std_dev), (sample_mean + 4 * sample_std_dev), len(returns))
  5. sample_distribution = ((1/(sample_std_dev * 2 * np.pi)) *
  6. np.exp(-(x-sample_mean) * (x-sample_mean) / (2 * sample_std_dev * sample_std_dev)))
  7. plt.hist(returns, bins = 20, normed = True)
  8. plt.plot(x, sample_distribution)
  9. plt.xlabel('Value')
  10. plt.ylabel('Probability')
  11. plt.show()

7


t分布
t分布概率密度曲线以0为中心,左右对称的单峰分布;期形态变化与自由度大小有关,自由度越小,分布越散,而自由度与样本量和变量数量有关。当自由度为30是,t分布已经接近标准的正太分布曲线,当自由度为120时,等于正太分布。一般自由度为1-30,40,50,...,120。与标准的正态分布相比,t分布呈现‘尖峰厚尾’的特点,更加符合收益分布的特点,因此在推断统计分析中常常会使用t分布。
显示对比不同自由度的t分布。

  1. x = np.arange(-4,4.004,0.004)
  2. plt.plot(x,stats.t.pdf(x, 5),'r')
  3. plt.plot(x,stats.t.pdf(x, 30))
  4. plt.plot(x,stats.t.pdf(x, 120),'y')

8

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