NumPy Beginner's Guide 2e 带注释源码 四、NumPy 便利的函数

简介: 版权声明:License CC BY-NC-SA 4.0 https://blog.csdn.net/wizardforcel/article/details/72828999 # ...
版权声明:License CC BY-NC-SA 4.0 https://blog.csdn.net/wizardforcel/article/details/72828999
# 来源:NumPy Beginner's Guide 2e ch4

交易相关偶对

import numpy as np
from matplotlib.pyplot import plot
from matplotlib.pyplot import show

# 读入 BHP 的收盘价
bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)

# 计算 BHP 的简单收益
bhp_returns = np.diff(bhp) / bhp[ : -1]

# 读入 VALE 的收盘价
vale = np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True)

# 计算 VALE 的简单收益
vale_returns = np.diff(vale) / vale[ : -1]

# 计算协方差
# cov_x_y = ((x - x.mean()) * (y - y.mean())).mean()
# cov 函数返回协方差矩阵
# [[ var_x, cov_x_y],
#  [ cov_y_x, var_y]]
covariance = np.cov(bhp_returns, vale_returns) 
print "Covariance", covariance
'''
Covariance [[ 0.00028179  0.00019766]
           [ 0.00019766  0.00030123]]
'''

# diagonal 获取对角线上的元素
print "Covariance diagonal", covariance.diagonal()
# Covariance diagonal [ 0.00028179  0.00030123]
# trace 计算矩阵的迹(对角线元素和)
print "Covariance trace", covariance.trace()
# Covariance trace 0.00058302354992

print covariance/ (bhp_returns.std() * vale_returns.std())

# corrcoef 计算相关系数
# rho_x_y = cov_x_y / (std_x * std_y)
print "Correlation coefficient", np.corrcoef(bhp_returns, vale_returns)
'''
[[ 1.00173366  0.70264666]
[ 0.70264666  1.0708476 ]]
'''

# 检查两个股票是否同步
# 如果差值最后一项,距离均值大于两个标准差,就认为是不同步的
difference = bhp - vale
avg = np.mean(difference)
dev = np.std(difference)

print "Out of sync", np.abs(difference[-1] - avg) > 2 * dev
# Out of sync False

# 绘制两个股票的收益
t = np.arange(len(bhp_returns))
plot(t, bhp_returns, lw=1)
plot(t, vale_returns, lw=2)
show()

多项式拟合

import numpy as np
import sys
from matplotlib.pyplot import plot
from matplotlib.pyplot import show

# 导入 BHP 和 VALE 的收盘价
bhp=np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)

vale=np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True)

# polyfit 用于多项式拟合
# 参数为训练集x,训练集y,最高项次数
# 返回方程的系数数组,高次在前
t = np.arange(len(bhp))
poly = np.polyfit(t, bhp - vale, int(sys.argv[1]))
print "Polynomial fit", poly
# 假设最高项次数为 3:
# Polynomial fit [  1.11655581e-03  -5.28581762e-02   5.80684638e-01   5.79791202e+01]

# polyval 使用拟合结果来预测新的值
# p[0] * x**n + p[1] * x**(n-1) + ... + p[n-1]*x + p[n]
print "Next value", np.polyval(poly, t[-1] + 1)
# Next value 57.9743076081

# 返回多项式的根
print "Roots", np.roots(poly)
# Roots [ 35.48624287+30.62717062j  35.48624287-30.62717062j -23.63210575 +0.j        ]

# 多项式求导
# (x ** n)' = n * x ** (n - 1)
# (a * u(x) + b * v(x))' = a * u'(x) + b * v'(x)
der = np.polyder(poly)
print "Derivative", der
# Derivative [ 0.00334967 -0.10571635  0.58068464]

# 导数的根(可能)是极值
print "Extremas", np.roots(der)
# Extremas [ 24.47820054   7.08205278]

# 拟合函数的最大值和最小值点
vals = np.polyval(poly, t)
print np.argmax(vals)
# 7
print np.argmin(vals)
# 24

# 绘制原始函数和拟合函数
plot(t, bhp - vale)
plot(t, vals)
show()

平衡成交量

import numpy as np

# 读入收盘价和成交量
c, v=np.loadtxt('BHP.csv', delimiter=',', usecols=(6, 7), unpack=True)

# 获取绝对收益
change = np.diff(c)
print "Change", change
'''
Change [ 1.92 -1.08 -1.26  0.63 -1.54 -0.28  0.25 -0.6   2.15 0.69 -1.33  1.16
  1.59 -0.26 -1.29 -0.13 -2.12 -3.91  1.28 -0.57 -2.07 -2.07 2.5   1.18
-0.88  1.31  1.24 -0.59]
'''

# 计算状态
signs = np.sign(change)
print "Signs", signs
'''
Signs [ 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.]
'''

# 状态也可以用 piecewise 来计算
# 参数为输入数组 arr、状态数组 condlist 和结果数组 reslist
# 如果满足 condlist[i],将 arr[i] 变为 reslist[i]
pieces = np.piecewise(change, [change < 0, change > 0], [-1, 1])
print "Pieces", pieces

print "Arrays equal?", np.array_equal(signs, pieces)
# Arrays equal? True

# 平衡成交量是状态乘以成交量
print "On balance volume", v[1:] * signs
'''
[ 2620800. -2461300. -3270900.  2650200. -4667300. -5359800. 7768400.
 -4799100.  3448300.  4719800. -3898900.  3727700.  3379400. -2463900.
 -3590900. -3805000. -3271700. -5507800.  2996800. -3434800. -5008300.
 -7809799.  3947100.  3809700.  3098200. -3500200.  4285600. 3918800.
 -3632200.]
'''

使用向量化来避免循环

# 向量化就是逐元素调用函数

import numpy as np
import sys

# 获取开盘价、最高价、最低价和收盘价
o, h, l, c = np.loadtxt('BHP.csv', delimiter=',', usecols=(3, 4, 5, 6), unpack=True)

# calc_profit 用于计算利润
def calc_profit(open, high, low, close):
   # 以稍低于开盘价的价格买入
   buy = open * float(sys.argv[1])

   if low <  buy < high:
      # 如果这个价格在当天的区间之内
      # 就以收盘价卖掉
      return (close - buy)/buy
   else:
      # 否则就没有收益,返回 0
      return 0

# 创建向量化的 calc_profit
# 也可以使用装饰器 @np.vectorize
func = np.vectorize(calc_profit)
profits = func(o, h, l, c)
print "Profits", profits

# 获取不为零的收益
real_trades = profits[profits != 0]
# 打印概率和平均利润率
print "Number of trades", len(real_trades), round(100.0 * len(real_trades)/len(c), 2), "%"
# Number of trades 28 93.33 %
print "Average profit/loss %", round(np.mean(real_trades) * 100, 2)
# Average profit/loss % 0.02

# 选择获利的交易,计算概率和平均利润率
winning_trades = profits[profits > 0]
print "Number of winning trades", len(winning_trades), round(100.0 * len(winning_trades)/len(c), 2), "%"
Number of winning trades 16 53.33 %
print "Average profit %", round(np.mean(winning_trades) * 100, 2)
# Average profit/loss % 0.72

# 选择亏损的交易,计算概率和平均利润率
losing_trades = profits[profits < 0]
print "Number of losing trades", len(losing_trades), round(100.0 * len(losing_trades)/len(c), 2), "%"
# Number of losing trades 12 40.0 %
print "Average loss %", round(np.mean(losing_trades) * 100, 2)
# Average loss % -0.92

使用海宁函数实现平滑

import numpy as np
import sys
from matplotlib.pyplot import plot
from matplotlib.pyplot import show

# 读取长度 N
N = int(sys.argv[1])

# 使用 hanning 来生成权重
weights = np.hanning(N)
print "Weights", weights
'''
Weights [ 0.          0.1882551   0.61126047  0.95048443  0.95048443  0.61126047
  0.1882551   0.        ]
'''

# 读取 BHP 收盘价
bhp = np.loadtxt('BHP.csv', delimiter=',', usecols=(6,), unpack=True)
# 计算简单收益
bhp_returns = np.diff(bhp) / bhp[ : -1]
# 使用 convolve 函数来使之平滑
smooth_bhp = np.convolve(weights/weights.sum(), bhp_returns)[N-1:-N+1]

# 读取 VALE 收盘价
vale = np.loadtxt('VALE.csv', delimiter=',', usecols=(6,), unpack=True)
# 计算简单收益
vale_returns = np.diff(vale) / vale[ : -1]
# 使用 convolve 函数来使之平滑
smooth_vale = np.convolve(weights/weights.sum(), vale_returns)[N-1:-N+1]

# 读取最高项系数
K = int(sys.argv[1])
# 多项式拟合 BHP 和 VALE 的平滑收益
t = np.arange(N - 1, len(bhp_returns))
poly_bhp = np.polyfit(t, smooth_bhp, K)
poly_vale = np.polyfit(t, smooth_vale, K)

# np.polysub 用于求多项式的差
poly_sub = np.polysub(poly_bhp, poly_vale)
# 差值为 0 的点就是多项式的交点
xpoints = np.roots(poly_sub)
print "Intersection points", xpoints
'''
Intersection points [ 27.73321597+0.j          27.51284094+0.j          24.32064343+0.j
  18.86423973+0.j          12.43797190+1.73218179j  12.43797190-1.73218179j
   6.34613053+0.62519463j   6.34613053-0.62519463j]
'''

# 检查是否是实数值
reals = np.isreal(xpoints)
print "Real number?", reals
# Real number? [ True  True  True  True False False False False]

# 过滤实数值,转成实数
# 其实可以直接使用 xpoints[np.isreal(xpoints)].real
xpoints = np.select([reals], [xpoints])
xpoints = xpoints.real
print "Real intersection points", xpoints
# Real intersection points [ 27.73321597  27.51284094 24.32064343  18.86423973   0.           0.   0.  0.]
# trim_zeros 去除首尾的零元素
print "Sans 0s", np.trim_zeros(xpoints)
# Sans 0s [ 27.73321597  27.51284094  24.32064343  18.86423973]

# 绘制简单收益,以及 N 天的平滑收益
plot(t, bhp_returns[N-1:], lw=1.0)
plot(t, smooth_bhp, lw=2.0)

plot(t, vale_returns[N-1:], lw=1.0)
plot(t, smooth_vale, lw=2.0)
show()

相关文章
|
7月前
|
机器学习/深度学习 数据处理 C语言
numpy通用函数:快速的逐元素数组函数
numpy通用函数:快速的逐元素数组函数
numpy通用函数:快速的逐元素数组函数
|
3月前
|
存储 测试技术 数据库
NumPy 秘籍中文第二版:六、特殊数组和通用函数
NumPy 秘籍中文第二版:六、特殊数组和通用函数
|
4月前
|
数据处理 Python
NumPy 中级教程——通用函数(ufuncs)
NumPy 中级教程——通用函数(ufuncs)
92 0
|
4月前
|
存储 并行计算 数据挖掘
利用Numpy和Pandas对地铁客量数据进行实战分析(附源码)
利用Numpy和Pandas对地铁客量数据进行实战分析(附源码)
32 0
|
4月前
|
Python
关于Python的Numpy库reshape()函数的用法
1.介绍 更改数组的形状,不改变原数组 2.语法 a = np.reshape(mat, newshape, order = ‘C’) a : newshape形状的新数组 mat : 原数组
52 0
|
8月前
|
Python
Python map() 函数 和 numpy mean()函数
Python map() 函数 和 numpy mean()函数
88 0
|
10月前
|
Python
【Numpy】flatnonzero函数的用法
【Numpy】flatnonzero函数的用法
52 0
|
10月前
|
机器学习/深度学习 索引 Python
这8个NumPy函数可以解决90%的常见问题
NumPy是一个用于科学计算和数据分析的Python库,也是机器学习的支柱。可以说NumPy奠定了Python在机器学习中的地位。NumPy提供了一个强大的多维数组对象,以及广泛的数学函数,可以对大型数据集进行有效的操作。这里的“大”是指数百万行。
11252 0
|
29天前
|
机器学习/深度学习 存储 算法
Python中的NumPy库:数值计算与科学计算的基石
【2月更文挑战第29天】NumPy是Python科学计算的核心库,专注于高效处理大型多维数组和矩阵。其核心是ndarray对象,提供快速数组操作和数学运算,支持线性代数、随机数生成等功能。NumPy广泛应用于数据处理、科学计算和机器学习,简化了矩阵运算、统计分析和算法实现,是数据科学和AI领域的重要工具。
|
1月前
|
存储 索引 Python
请解释Python中的NumPy库以及它的主要用途。
【2月更文挑战第27天】【2月更文挑战第97篇】请解释Python中的NumPy库以及它的主要用途。