『Numpy学习指南』第四章_便捷函数

时间:2021-10-07 01:08:25

数据准备:

1 import numpy as np
2 
3 
4 # 计算简单收益率
5 bhp = np.loadtxt('BHP.csv',delimiter=',',usecols=(6,),unpack=True)
6 bhp_returns = np.diff(bhp)/bhp[:-1]
7 vale = np.loadtxt('VALE.csv',delimiter=',',usecols=(6,),unpack=True)
8 vale_returns = np.diff(vale)/vale[:-1]

np.cov(bhp_returns,vale_returns)
covariance.diagonal()
covariance.trace()
np.corrcoef(bhp_returns,vale_returns)
 1 '''相关性'''
 2 
 3 # 计算协方差
 4 covariance = np.cov(bhp_returns,vale_returns)
 5 print('协方差矩阵:\n',covariance)
 6 print('对角线元素:\n',covariance.diagonal())
 7 print('迹(对角线元素和):\n',covariance.trace())
 8 # 相关系数计算方法
 9 print('相关系数:\n',covariance/(bhp_returns.std(ddof=1)*vale_returns.std(ddof=1)))
10 print('标准相关系数求法:\n',np.corrcoef(bhp_returns,vale_returns))
11 print('测试一下两种求标准差的方法:\n',np.std(bhp_returns),bhp_returns.std())
12 
13 difference = bhp - vale
14 avg = np.mean(difference)
15 dev = np.std(difference)
16 print(np.abs(difference[-1]-avg) > 2*dev)
17 
18 import matplotlib.pyplot as plt
19 
20 t = np.arange(len(bhp_returns))
21 #plt.plot(t, bhp_returns,lw=1)
22 #plt.plot(t, vale_returns,lw=2)
23 # plt.show()
协方差矩阵:
 [[ 0.00028179  0.00019766]
 [ 0.00019766  0.00030123]]
对角线元素:
 [ 0.00028179  0.00030123]
迹(对角线元素和):
 0.00058302354992
相关系数:
 [[ 0.96719112  0.67841747]
 [ 0.67841747  1.03392182]]
标准相关系数求法:
 [[ 1.          0.67841747]
 [ 0.67841747  1.        ]]
测试一下两种求标准差的方法:
 0.0164946618219 0.0164946618219
False

 

np.polyfit(t,y,3)
np.polyval(poly,t[-1]+1)
np.poly1d(poly) # 创建poly为系数的多项式

np.roots(poly)
np.sign(change)
np.piecewise(change,[change<0,change>0],[-1,1])
 1 '''多项式'''
 2 
 3 t = np.arange(len(bhp))
 4 y = bhp - vale
 5 
 6 
 7 # 多项式拟合
 8 poly = np.polyfit(t,y,3)         # 拟合参数
 9 f3 = np.poly1d(poly)             # 生成拟合函数
10 print(poly)
11 print(f3(t[-1]+1))
12 print(np.polyval(poly,t[-1]+1))  # 预测纵坐标
13 # 对比一下scipy的多项式拟合
14 # 没错,polyfit、polyval、poly1d用法完全一样
15 import scipy as sp
16 poly = sp.polyfit(t,y,3)
17 f3 = sp.poly1d(poly)
18 print(f3(t[-1]+1))
19 print(sp.polyval(poly,t[-1]+1))
20 
21 # 计算零点
22 print(np.roots(poly))
23 print(sp.roots(poly))
24 
25 # 求导函数
26 der = np.polyder(poly)
27 print(der)
28 # 求极值点
29 print(np.roots(der))
30 # 检验
31 vals = np.polyval(poly,t)
32 print(np.argmax(vals),np.argmin(vals))
33 #plt.plot(t,y)
34 #plt.plot(t,vals)
35 # plt.show()
36 
37 # 计算OBV
38 c,v = np.loadtxt('BHP.csv',delimiter=',',usecols=(6,7),unpack=True)
39 change = np.diff(c)
40 print(np.sign(change))                                        # 判断正负号
41 print(np.piecewise(change,[change<0,change>0],[-1,1]))        # 按条件取值
[  1.11655581e-03  -5.28581762e-02   5.80684638e-01   5.79791202e+01]
57.9743076081
57.9743076081
57.9743076081
57.9743076081
[ 35.48624287+30.62717062j  35.48624287-30.62717062j -23.63210575 +0.j        ]
[ 35.48624287+30.62717062j  35.48624287-30.62717062j -23.63210575 +0.j        ]
[ 0.00334967 -0.10571635  0.58068464]
[ 24.47820054   7.08205278]
7 24
[ 1. -1. -1.  1. -1. -1.  1. -1.  1.  1. -1.  1.  1. -1. -1. -1. -1. -1.
  1. -1. -1. -1.  1.  1.  1. -1.  1.  1. -1.]
[ 1. -1. -1.  1. -1. -1.  1. -1.  1.  1. -1.  1.  1. -1. -1. -1. -1. -1.
  1. -1. -1. -1.  1.  1.  1. -1.  1.  1. -1.]

 

np.vectorize(calc_profit)

list(map(calc_profit,o,h,l,c))

 1 '''矢量化函数-取代循环提升效率'''
 2 
 3 o,h,l,c = np.loadtxt('BHP.csv',delimiter=',',usecols=(3,4,5,6),unpack=True)
 4 
 5 def calc_profit(o,h,l,c):
 6     buy = o * float(1.01)
 7     #print(l,buy,h)
 8     if l<buy<h:
 9         return (c - buy)/buy
10     else:
11         return 0
12 
13 func = np.vectorize(calc_profit)        # 矢量化函数
14 profit = func(o,h,l,c)
15 print(profit)
16 print(list(map(calc_profit,o,h,l,c)))   # 其实使用map也行
[-0.00341446  0.00130136 -0.00874788  0.          0.00377766  0.          0.
  0.          0.          0.          0.          0.         -0.00420652
  0.          0.          0.          0.          0.          0.
  0.00524672 -0.00035597  0.01784238 -0.02630513 -0.00922067  0.          0.
  0.          0.          0.          0.        ]
[-0.0034144645266803693, 0.0013013567403055747, -0.0087478837878494625, 0, 0.0037776578521316441, 0, 
0, 0, 0, 0, 0, 0, -0.0042065235387102199,
0, 0, 0, 0, 0, 0,
0.0052467168747700292, -0.00035597299097042201, 0.017842384853287971, -0.026305133120194297, -0.009220665538839672, 0, 0,
0, 0, 0, 0]

 

np.hanning(N)
np.polysub(np.polyfit(t,smooth_bhp,k), np.polyfit(t,smooth_vale,k))
np.isreal(xpoint)
 1 '''汉宁窗平滑数据'''
 2 
 3 N = int(8)
 4 weights = np.hanning(N)
 5 
 6 print('汉宁窗权重数据:',weights)
 7 smooth_bhp = np.convolve(weights/weights.sum(),bhp_returns)[N-1:-N+1]  # 卷积操作平滑曲线
 8 smooth_vale = np.convolve(weights/weights.sum(),vale_returns)[N-1:-N+1]  # 卷积操作平滑曲线
 9 t = np.arange(N-1,len(bhp_returns))
10 plt.plot(t,bhp_returns[N-1:])
11 plt.plot(t,vale_returns[N-1:])
12 plt.plot(t,smooth_vale)
13 plt.plot(t,smooth_bhp)
14 #plt.show()
汉宁窗权重数据: [ 0.          0.1882551   0.61126047  0.95048443  0.95048443  0.61126047
  0.1882551   0.        ]
[ 27.73321598+0.j  27.51284093+0.j  24.32064343+0.j  18.86423973+0.j]