4.2 股票相关性分析
covariance = np.cov(a,b)
获取对角元素
covariance.diagonal()
4.4 多项式拟合
bhp=np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)
vale=np.loadtxt('VALE.csv', delimiter=',', usecols=(6,),unpack=True)
t = np.arange(len(bhp))
poly = np.polyfit(t, bhp - vale, int(sys.argv[1])) # sys.argv[1]为3,即用3阶多项式拟合数据
print "Polynomial fit", poly
#output
Polynomial fit [ 1.11655581e-03 -5.28581762e-02 5.80684638e-01 5.79791202e+01]
#预测下个值
print "Next value", np.polyval(poly, t[-1] + 1)
使用polyder函数对多项式函数求导(以求极值)
der = np.polyder(poly)
print "Derivative", der
#output
Derivative [ 0.00334967 -0.10571635 0.58068464]
求出导数函数的根,即找出原多项式函数的极值点
print "Extremas", np.roots(der)
#output
Extremas [ 24.47820054 7.08205278]
注:书中提示,3阶多项式拟合数据的结果并不好,可尝试更高阶的多项式拟合。
4.6 计算OBV(On-Balance Volume)净额成交量
diff函数可以计算数组中两个连续元素的差值,并返回一个由这些差值组成的数组。
change = np.diff(c)
sign函数可以返回数组中每个元素的正负符号,数组元素为负时返回-1,为正时返回1,否则返回0
np.sign(change)
使用piecewise(分段的)函数来获取数组元素的正负。使用合适的返回值和对应的条件调用该函数:
pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1])
print "Pieces", pieces
检查一致性
np.array_equal(a, b)
np.vectorize 替代循环
>>> def myfunc(a, b):
... "Return a-b if a>b, otherwise return a+b"
... if a > b:
... return a - b
... else:
... return a + b
>>> vfunc = np.vectorize(myfunc)
>>> vfunc([1, 2, 3, 4], 2)
array([3, 4, 1, 2])
The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop.
4.10 使用hanning 函数平滑数据
(1) 调用hanning函数计算权重,生成一个长度为N的窗口(在这个示例中N取8)
N = int(sys.argv[1])
weights = np.hanning(N)
print "Weights", weights
#output
Weights [ 0. 0.1882551 0.61126047 0.95048443 0.95048443 0.61126047 0.1882551 0. ]
bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,),unpack=True) #某股票数据
bhp_returns = np.diff(bhp) / bhp[ : -1] #股票收益率计算
smooth_bhp = np.convolve(weights/weights.sum(), bhp_returns) [N-1:-N+1] #使用weights平滑股票收益率
#绘图
t = np.arange(N - 1, len(bhp_returns))
plot(t, bhp_returns[N-1:], lw=1.0)
plot(t, smooth_bhp, lw=2.0)
show()
两个多项式做差运算
poly_sub = np.polysub(a, b)
选择函数
numpy.select(condlist, choicelist, default=0)
>>> x = np.arange(10)
>>> condlist = [x<3, x>5]
#输出两个array [true...false...],[false,...true]
>>> choicelist = [x, x**2]
>>> np.select(condlist, choicelist)
array([ 0, 1, 2, 0, 0, 0, 36, 49, 64, 81])
trim_zeros函数可以去掉一维数组中开头和末尾为0的元素:
np.trim_zeros(a)