Python 金融编程第二版(GPT 重译)(三)(4)

简介: Python 金融编程第二版(GPT 重译)(三)

Python 金融编程第二版(GPT 重译)(三)(3)https://developer.aliyun.com/article/1559335


其他绘图样式

在二维绘图方面,线条和点图可能是金融中最重要的图表之一;这是因为许多数据集包含时间序列数据,通常通过这些图表进行可视化。第八章详细讨论了金融时间序列数据。然而,目前这一节还是采用了一个随机数的二维数据集,并且展示了一些备用的、对于金融应用有用的可视化方法。

第一种是散点图,其中一个数据集的值作为另一个数据集的x值。图 7-13 展示了这样一个图。例如,此类图用于绘制一个金融时间序列的回报与另一个金融时间序列的回报。对于此示例,我们将使用一个新的二维数据集以及一些更多的数据:

In [24]: y = np.random.standard_normal((1000, 2))  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
In [25]: plt.figure(figsize=(10, 6))
         plt.plot(y[:, 0], y[:, 1], 'ro')  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         plt.xlabel('1st')
         plt.ylabel('2nd')
         plt.title('Scatter Plot');
         # plt.savefig('../../images/ch07/mpl_13')


创建一个包含随机数的较大数据集。


通过 plt.plot() 函数绘制散点图。


图 7-13. 通过 plot 函数绘制散点图

matplotlib 还提供了一个特定的函数来生成散点图。它基本上工作方式相同,但提供了一些额外的功能。图 7-14 展示了使用 plt.scatter() 函数生成的相应散点图,这次与 图 7-13 对应,:

In [26]: plt.figure(figsize=(10, 6))
         plt.scatter(y[:, 0], y[:, 1], marker='o')  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         plt.xlabel('1st')
         plt.ylabel('2nd')
         plt.title('Scatter Plot');
         # plt.savefig('../../images/ch07/mpl_14')


通过 plt.scatter() 函数绘制的散点图。


图 7-14. 通过散点函数生成的散点图

例如,plt.scatter()  绘图函数允许添加第三个维度,可以通过不同的颜色来可视化,并且可以通过使用颜色条来描述。图 7-15  展示了一个散点图,其中第三个维度通过单个点的不同颜色来说明,并且有一个颜色条作为颜色的图例。为此,以下代码生成了一个具有随机数据的第三个数据集,这次是介于  0 到 10 之间的整数:

In [27]: c = np.random.randint(0, 10, len(y))
In [28]: plt.figure(figsize=(10, 6))
         plt.scatter(y[:, 0], y[:, 1],
                     c=c,  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
                     cmap='coolwarm',  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
                     marker='o')  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
         plt.colorbar()
         plt.xlabel('1st')
         plt.ylabel('2nd')
         plt.title('Scatter Plot');
         # plt.savefig('../../images/ch07/mpl_15')


包含了第三个数据集。


选择了颜色映射。


将标记定义为粗点。


图 7-15. 具有第三维的散点图

另一种类型的图表,直方图,在金融收益的背景下也经常被使用。图 7-16 将两个数据集的频率值放在同一个图表中相邻位置:

In [29]: plt.figure(figsize=(10, 6))
         plt.hist(y, label=['1st', '2nd'], bins=25)  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         plt.legend(loc=0)
         plt.xlabel('value')
         plt.ylabel('frequency')
         plt.title('Histogram');
         # plt.savefig('../../images/ch07/mpl_16')


通过 plt.hist() 函数绘制直方图。


图 7-16. 两个数据集的直方图

由于直方图在金融应用中是如此重要的图表类型,让我们更近距离地看一下 plt.hist 的使用。以下示例说明了支持的参数:

plt.hist(x, bins=10, range=None, normed=False, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, hold=None, **kwargs)

表 7-5 提供了 plt.hist 函数的主要参数的描述。

表 7-5. plt.hist() 的参数

参数 描述
x list 对象(s),ndarray 对象
bins 柱子数量
range 柱的下限和上限
normed 规范化,使积分值为 1
weights x 中每个值的权重
cumulative 每个柱包含低位柱的计数
histtype 选项(字符串):barbarstackedstepstepfilled
align 选项(字符串):leftmidright
orientation 选项(字符串):horizontalvertical
rwidth 条柱的相对宽度
log 对数刻度
color 每个数据集的颜色(类似数组)
label 用于标签的字符串或字符串序列
stacked 堆叠多个数据集

图 7-17 展示了类似的图表;这次,两个数据集的数据在直方图中堆叠:

In [30]: plt.figure(figsize=(10, 6))
         plt.hist(y, label=['1st', '2nd'], color=['b', 'g'],
                     stacked=True, bins=20, alpha=0.5)
         plt.legend(loc=0)
         plt.xlabel('value')
         plt.ylabel('frequency')
         plt.title('Histogram');
         # plt.savefig('../../images/ch07/mpl_17')


图 7-17. 两个数据集的堆叠直方图

另一种有用的绘图类型是箱线图。类似于直方图,箱线图既可以简明地概述数据集的特征,又可以轻松比较多个数据集。图 7-18 展示了我们数据集的这样一个图:

In [31]: fig, ax = plt.subplots(figsize=(10, 6))
         plt.boxplot(y)  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         plt.setp(ax, xticklabels=['1st', '2nd'])  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         plt.xlabel('data set')
         plt.ylabel('value')
         plt.title('Boxplot');
         # plt.savefig('../../images/ch07/mpl_18')


通过plt.boxplot()函数绘制箱线图。


设置各个 x 标签。

最后一个示例使用了函数plt.setp(),它为一个(组)绘图实例设置属性。例如,考虑由以下代码生成的线图:

line = plt.plot(data, 'r')

下面的代码:

plt.setp(line, linestyle='--')

将线条样式更改为“虚线”。这样,您可以在生成绘图实例(“艺术家对象”)之后轻松更改参数。


图 7-18. 两个数据集的箱线图

作为本节的最后一个示例,请考虑一个在matplotlib 画廊中也可以找到的受数学启发的绘图。它绘制了一个函数并在图形上突出显示了函数下方的区域,从下限到上限  — 换句话说,函数在下限和上限之间的积分值突出显示为一个区域。要说明的积分值是∫ a b f ( x ) d x,其中f ( x ) = 1 2  · e x + 1,a = 1 2,b = 3 2。图 7-19 显示了结果图,并演示了matplotlib如何无缝处理数学公式的LaTeX类型设置以将其包含到绘图中。首先,函数定义,积分限制作为变量以及 x 和 y 值的数据集。

In [32]: def func(x):
             return 0.5 * np.exp(x) + 1  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         a, b = 0.5, 1.5  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         x = np.linspace(0, 2)  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
         y = func(x)  ![4](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/4.png)
         Ix = np.linspace(a, b)  ![5](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/5.png)
         Iy = func(Ix) ![6](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/6.png)
         verts = [(a, 0)] + list(zip(Ix, Iy)) + [(b, 0)]  ![7](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/7.png)


函数定义。


积分限制。


用于绘制函数的 x 值。


用于绘制函数的 y 值。


积分限制内的 x 值。


积分限制内的 y 值。


包含多个表示要绘制的多边形的坐标的list对象。

其次,由于需要明确放置许多单个对象,绘图本身有点复杂。

In [33]: from matplotlib.patches import Polygon
         fig, ax = plt.subplots(figsize=(10, 6))
         plt.plot(x, y, 'b', linewidth=2)  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         plt.ylim(ymin=0)  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         poly = Polygon(verts, facecolor='0.7', edgecolor='0.5')  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
         ax.add_patch(poly)  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
         plt.text(0.5 * (a + b), 1, r'$\int_a^b f(x)\mathrm{d}x$',
                  horizontalalignment='center', fontsize=20)  ![4](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/4.png)
         plt.figtext(0.9, 0.075, '$x$')  ![5](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/5.png)
         plt.figtext(0.075, 0.9, '$f(x)$')  ![5](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/5.png)
         ax.set_xticks((a, b))  ![6](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/6.png)
         ax.set_xticklabels(('$a$', '$b$'))  ![6](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/6.png)
         ax.set_yticks([func(a), func(b)])  ![7](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/7.png)
         ax.set_yticklabels(('$f(a)$', '$f(b)$'))  ![7](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/7.png)
         # plt.savefig('../../images/ch07/mpl_19')
Out[33]: [<matplotlib.text.Text at 0x1066af438>, <matplotlib.text.Text at 0x10669ba20>]


将函数值绘制为蓝线。


定义纵坐标轴的最小 y 值。


以灰色绘制多边形(积分区域)。


将积分公式放置在图中。


放置轴标签。


放置 x 标签。


放置 y 标签。


图 7-19。指数函数、积分区域和 LaTeX 标签

静态 3D 绘图

在金融领域,确实没有太多领域真正受益于三维可视化。然而,一个应用领域是同时显示一系列到期时间和行权价的隐含波动率的波动率曲面。接下来,代码人工生成类似于波动率曲面的图形。为此,请考虑:

  • 50 到 150 之间的行权价值
  • 0.5 到 2.5 年的到期时间

这提供了一个二维坐标系。NumPynp.meshgrid()函数可以从两个一维ndarray对象生成这样的系统:

In [34]: strike = np.linspace(50, 150, 24)  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
In [35]: ttm = np.linspace(0.5, 2.5, 24)  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
In [36]: strike, ttm = np.meshgrid(strike, ttm)  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
In [37]: strike[:2].round(1)  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
Out[37]: array([[ 50. ,  54.3,  58.7,  63. ,  67.4,  71.7,  76.1,  80.4,  84.8,
                  89.1,  93.5,  97.8, 102.2, 106.5, 110.9, 115.2, 119.6, 123.9,
                 128.3, 132.6, 137. , 141.3, 145.7, 150. ],
                [ 50. ,  54.3,  58.7,  63. ,  67.4,  71.7,  76.1,  80.4,  84.8,
                  89.1,  93.5,  97.8, 102.2, 106.5, 110.9, 115.2, 119.6, 123.9,
                 128.3, 132.6, 137. , 141.3, 145.7, 150. ]])
In [38]: iv = (strike - 100) ** 2 / (100 * strike) / ttm  ![4](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/4.png)
In [39]: iv[:5, :3]  ![4](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/4.png)
Out[39]: array([[1.        , 0.76695652, 0.58132045],
                [0.85185185, 0.65333333, 0.4951989 ],
                [0.74193548, 0.56903226, 0.43130227],
                [0.65714286, 0.504     , 0.38201058],
                [0.58974359, 0.45230769, 0.34283001]])


ndarray对象中的行权价值。


ndarray 对象中的时间至到期值。


创建的两个二维ndarray对象(网格)。


虚拟的隐含波动率值。

由以下代码生成的图形显示在图 7-20 中:

In [40]: from mpl_toolkits.mplot3d import Axes3D  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         fig = plt.figure(figsize=(10, 6))
         ax = fig.gca(projection='3d')  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         surf = ax.plot_surface(strike, ttm, iv, rstride=2, cstride=2,
                                cmap=plt.cm.coolwarm, linewidth=0.5,
                                antialiased=True)  ![3](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/3.png)
         ax.set_xlabel('strike')  ![4](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/4.png)
         ax.set_ylabel('time-to-maturity')  ![5](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/5.png)
         ax.set_zlabel('implied volatility')  ![6](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/6.png)
         fig.colorbar(surf, shrink=0.5, aspect=5);  ![7](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/7.png)
         # plt.savefig('../../images/ch07/mpl_20')


导入相关的 3D 绘图特性。


为 3D 绘图设置画布。


创建 3D 图。


设置 x 标签。


设置 y 标签。


设置 z 标签。


这将创建一个色标。


图 7-20。 (虚拟) 隐含波动率的三维曲面图

表 7-6 提供了plt.plot_surface()函数可以接受的不同参数的描述。

表 7-6。plot_surface的参数

参数 描述
X, Y, Z 数据值为 2D 数组
rstride 数组行跨度(步长)
cstride 数组列跨度(步长)
color 表面补丁的颜色
cmap 表面补丁的颜色映射
facecolors 各个补丁的面颜色
norm 用于将值映射到颜色的 Normalize 的实例
vmin 要映射的最小值
vmax 要映射的最大值
shade 是否着色面颜色

与二维图相似,线型可以由单个点或如下所示的单个三角形替代。图 7-21 将相同的数据绘制为 3D 散点图,但现在还使用 view_init() 方法设置了不同的视角:

In [41]: fig = plt.figure(figsize=(10, 6))
         ax = fig.add_subplot(111, projection='3d')
         ax.view_init(30, 60)  ![1](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/1.png)
         ax.scatter(strike, ttm, iv, zdir='z', s=25,
                    c='b', marker='^')  ![2](https://gitee.com/OpenDocCN/ibooker-quant-zh/raw/master/docs/py-fin-2e/img/2.png)
         ax.set_xlabel('strike')
         ax.set_ylabel('time-to-maturity')
         ax.set_zlabel('implied volatility');
         # plt.savefig('../../images/ch07/mpl_21')


设置视角。


创建 3D 散点图。


图 7-21. (虚拟的) 隐含波动率的 3D 散点图


Python 金融编程第二版(GPT 重译)(三)(5)https://developer.aliyun.com/article/1559341

相关文章
|
2天前
|
数据可视化 Python
Python 金融编程第二版(三)(4)
Python 金融编程第二版(三)
12 2
|
2天前
|
存储 数据可视化 API
Python 金融编程第二版(三)(5)
Python 金融编程第二版(三)
8 1
|
2天前
|
存储 机器学习/深度学习 关系型数据库
Python 金融编程第二版(四)(5)
Python 金融编程第二版(四)
8 0
|
2天前
|
存储 SQL 数据库
Python 金融编程第二版(四)(4)
Python 金融编程第二版(四)
8 0
|
2天前
|
SQL 存储 数据库
Python 金融编程第二版(四)(3)
Python 金融编程第二版(四)
8 0
|
2天前
|
存储 分布式计算 数据可视化
Python 金融编程第二版(四)(2)
Python 金融编程第二版(四)
10 0
|
2天前
|
存储 SQL 数据可视化
Python 金融编程第二版(四)(1)
Python 金融编程第二版(四)
9 0
|
2天前
|
存储 SQL 数据库
Python 金融编程第二版(GPT 重译)(四)(4)
Python 金融编程第二版(GPT 重译)(四)
13 3
|
2天前
|
存储 NoSQL 索引
Python 金融编程第二版(GPT 重译)(一)(4)
Python 金融编程第二版(GPT 重译)(一)
11 2
|
2天前
|
存储 机器学习/深度学习 关系型数据库
Python 金融编程第二版(GPT 重译)(四)(5)
Python 金融编程第二版(GPT 重译)(四)
10 2