python - 如何在 Python 中计算和绘制 CCDF(互补累积分布函数)?
问题描述
我有以下熊猫数据框
Date Price pct_change cumsum ccdf
1927-12-30 17.660000 NaN NaN NaN
1928-01-03 17.760000 0.005663 0.005663 0.994337
1928-01-04 17.719999 -0.002252 0.003410 0.996590
1928-01-05 17.549999 -0.009594 -0.006183 1.006183
1928-01-06 17.660000 0.006268 0.000084 0.999916
1928-01-09 17.500000 -0.009060 -0.008976 1.008976
1928-01-10 17.370001 -0.007429 -0.016404 1.016404
1928-01-11 17.350000 -0.001151 -0.017556 1.017556
1928-01-12 17.469999 0.006916 -0.010639 1.010639
1928-01-13 17.580000 0.006297 -0.004343 1.004343
1928-01-16 17.290001 -0.016496 -0.020839 1.020839
1928-01-17 17.299999 0.000578 -0.020260 1.020260
1928-01-18 17.260000 -0.002312 -0.022572 1.022572
1928-01-19 17.379999 0.006952 -0.015620 1.015620
1928-01-20 17.480000 0.005754 -0.009866 1.009866
1928-01-23 17.639999 0.009153 -0.000713 1.000713
1928-01-24 17.709999 0.003968 0.003255 0.996745
1928-01-25 17.520000 -0.010728 -0.007473 1.007473
1928-01-26 17.629999 0.006278 -0.001195 1.001195
1928-01-27 17.690001 0.003403 0.002209 0.997791
1928-01-30 17.490000 -0.011306 -0.009097 1.009097
1928-01-31 17.570000 0.004574 -0.004523 1.004523
1928-02-01 17.530001 -0.002277 -0.006800 1.006800
1928-02-02 17.629999 0.005704 -0.001095 1.001095
1928-02-03 17.400000 -0.013046 -0.014141 1.014141
1928-02-06 17.450001 0.002874 -0.011267 1.011267
1928-02-07 17.440001 -0.000573 -0.011841 1.011841
1928-02-08 17.490000 0.002867 -0.008974 1.008974
1928-02-09 17.549999 0.003431 -0.005543 1.005543
1928-02-10 17.540001 -0.000570 -0.006113 1.006113
为了计算 ccdf,我使用了 1 - cumsum,我不确定这部分是否正确完成。
我想在 y 轴上绘制 ccdf,从 0 到 1 排序,在 X 轴上,
我希望 pct_change 从 0 排序到负无穷大。我不想要任何正数。
我使用以下代码删除正 pct_change 数字,
for cols in df.columns.tolist()[1:]:
data = df.ix[df[cols] < 0]
我尝试了 df.plot('cumsum','ccdf') 和其他一些命令,但这些图没有意义。
这家伙似乎已经在 R How to plot CCDF graph on a logarithmic scale 中做到了?但我在 python 中找不到类似的东西。
有任何想法吗?
如果我没记错的话,形状应该是这样的。
解决方案
可以是这样的好东西:
import matplotlib.pyplot as plt
plt.scatter(x=df['pct_change'].values, y=df['ccdf'].values)
plt.xlim(right=0) # This limits the x axis from negative inf to 0
plt.ylim(bottom=0, top=1) # This clip the y axis from 0 to 1
plt.xlabel('pct_change')
plt.ylabel('ccdf')
plt.show()
推荐阅读
- arrays - 如何使用蓝色棱镜将文本中的 fieldinfo 参数用于列代码
- google-sheets - 如何创建引用来自多个工作表的数据的数据透视表?
- c++ - 更改调用者传递的结构?(从前序构造二叉搜索树)
- c++ - 为什么嵌套类时不能从内部类访问外部类的私有成员?
- c# - 将一个类列表中的数据分配给另一个类列表
- android - 设备启动时不发送 FCM 消息且有密码
- c++ - 如何在 CLion 项目中使用共享的 .clang 格式
- java - collectors.tomap 和 HeaderEelement 的 getValue 和 getNmae 存在问题
- python - 为线性总和分配构建成本矩阵的最有效方法?
- bash - 使用脚本 ( mac ) 让普通用户成为管理员