给定系数列表计算互补 CDF

问题描述

我有一个图表中节点的聚类系数列表,这是我从 NetworkX 获得的:

coefficients = nx.clustering(G)

现在我想绘制这些系数的互补 CDF,以便在 X 轴上我有系数值 x,在 Y 轴上是聚类系数大于或等于 x,即 P(X >= x)

如何在 Python 中执行此操作?我应该使用 scipy.stats.rv_discrete.cdf 吗? https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.rv_discrete.cdf.html

解决方法

使用 scipy.stats.rv_discrete.cdf 似乎有点矫枉过正。只需对系数列表进行排序并将它们与范围 [1,0] 作图,就可以得到所需的互补 CDF。

这是一些示例代码,显示了不同随机图的互补 CDF:

import networkx as nx
import numpy as np
from matplotlib import pyplot as plt
from matplotlib.ticker import PercentFormatter

for edges in range(200,2001,200):
    G = nx.gnm_random_graph(100,edges)
    coefficients = nx.clustering(G)
    x = np.concatenate([[0],np.sort(list(coefficients.values())),[1]])
    plt.plot(x,np.linspace(1,len(x)),label=f'100 nodes,{edges} edges')
plt.xlabel('Clustering coefficient')
plt.ylabel('P(X ≥ x)')
plt.margins(x=0.02)
plt.gca().yaxis.set_major_formatter(PercentFormatter(1))
plt.legend(title='Random graphs')
plt.tight_layout()
plt.show()

example plot

使用 np.linspace(0,1,len(x)) 你会得到 P(X ≤ x)