【基本工具】S02E04 NumPy简单统计方法举例

# 0.本集概览

1.csv数据的读取
2.利用常用函数获取均值、中位数、方差、标准差等统计量
3.利用常用函数分析价格的加权均值、收益率、年化波动率等常用指标
4.处理数据中的日期

# 2.csv数据的读取

import numpy as np

c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
print(c)
print(v) 

[ 178.02  178.65  178.44  179.97  181.72  179.98  176.94  175.03  176.67   176.82  176.21  175.    178.12  178.39  178.97  175.5   172.5   171.07   171.85  172.43  172.99  167.37  164.34  162.71  156.41  155.15  159.54   163.03  156.49  160.5   167.78  167.43  166.97  167.96  171.51  171.11   174.22  177.04  177.    178.46  179.26  179.1   176.19  177.09  175.28   174.29  174.33  174.35  175.    173.03  172.23  172.26  169.23  171.08   170.6   170.57  175.01  175.01  174.35  174.54  176.42]
[ 38313330.  22676520.  29334630.  31464170.  32191070.  32130360.   24518850.  31686450.  23273160.  27825140.  38426060.  48706170.   37568080.  38885510.  37353670.  33772050.  30953760.  37378070.   33690660.  40113790.  50908540.  40382890.  32483310.  60774900.   70583530.  54145930.  51467440.  68171940.  72215320.  85957050.   44453230.  32234520.  45635470.  50565420.  39075250.  41438280.   51368540.  32395870.  27052000.  31306390.  31087330.  34260230.   29512410.  25302200.  18653380.  23751690.  21532200.  20523870.   23589930.  22342650.  29461040.  25400540.  25938760.  16412270.   21477380.  33113340.  16339690.  20848660.  23451420.  27393660.   29385650.]

# 3.基本统计量计算

## 3.1.算术平均值

import numpy as np

c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
mean_c = np.mean(c) print(mean_c) 

172.614918033

## 3.2.加权平均值

import numpy as np

c, v = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1, 2), unpack=True)
vwap = np.average(c, weights=v)
print(vwap)  

170.950010035

## 3.3.最值获取

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.max(c))
print(np.min(c))
print(np.ptp(c))  

181.72
155.15
26.57

## 3.4.求取中位数

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.max(c))
print(np.min(c))
print(np.median(c))  

181.72
155.15
174.35

## 3.5.求取方差

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.var(c))  

37.5985528621

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
print(np.mean((c - c.mean())**2))  

37.5985528621

# 4.常用指标分析方法

## 4.1.收益率分析

diff函数时用数组的第N项减第N-1项，得到一个n-1项的一维数组。本例中我们注意到数组中日期越近的收盘价，数组索引越小，因此得取一个相反数，综上代码：

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
returns = -np.diff(c)/c[1:]
print(returns)  

[-0.00352645  0.00117687 -0.00850142 -0.0096302   0.00966774  0.01718097   0.01091242 -0.00928284 -0.00084832  0.00346178  0.00691429 -0.01751628  -0.00151354 -0.00324077  0.01977208  0.0173913   0.00835915 -0.00453884  -0.00336368 -0.00323718  0.0335783   0.01843739  0.01001782  0.04027875   0.00812117 -0.02751661 -0.0214071   0.04179181 -0.02498442 -0.04339015   0.00209043  0.00275499 -0.00589426 -0.0206985   0.00233768 -0.01785099  -0.0159286   0.00022599 -0.00818111 -0.00446279  0.00089336  0.01651626  -0.00508216  0.01032634  0.00568019 -0.00022945 -0.00011471 -0.00371429   0.01138531  0.00464495 -0.00017416  0.01790463 -0.01081365  0.0028136   0.00017588 -0.02536998 -0.          0.00378549 -0.00108858 -0.01065639]

import numpy as np

c = np.loadtxt('AAPL.csv', delimiter=',', usecols=(1,), unpack=True)
returns = -np.diff(c)/c[1:]
print(np.std(returns))  

0.0150780328454

`