def degree_pearson_correlation_coefficient(G, x="out", y="in", weight=None, nodes=None): """Compute degree assortativity of graph. Assortativity measures the similarity of connections in the graph with respect to the node degree. This is the same as degree_assortativity_coefficient but uses the potentially faster scipy.stats.pearsonr function. Parameters ---------- G : NetworkX graph x: string ('in','out') The degree type for source node (directed graphs only). y: string ('in','out') The degree type for target node (directed graphs only). weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. nodes: list or iterable (optional) Compute pearson correlation of degrees only for specified nodes. The default is all nodes. Returns ------- r : float Assortativity of graph by degree. Examples -------- >>> G = nx.path_graph(4) >>> r = nx.degree_pearson_correlation_coefficient(G) >>> print(f"{r:3.1f}") -0.5 Notes ----- This calls scipy.stats.pearsonr. References ---------- .. [1] M. E. J. Newman, Mixing patterns in networks Physical Review E, 67 026126, 2003 .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M. Edge direction and the structure of networks, PNAS 107, 10815-20 (2010). """ from scipy import stats xy = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight) x, y = zip(*xy) return stats.pearsonr(x, y)[0]
def degree_mixing_dict(G, x='out', y='in', weight=None, nodes=None, normalized=False): """Returns dictionary representation of mixing matrix for degree. Parameters ---------- G : graph NetworkX graph object. x: string ('in','out') The degree type for source node (directed graphs only). y: string ('in','out') The degree type for target node (directed graphs only). weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- d: dictionary Counts or joint probability of occurrence of degree pairs. """ xy_iter = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight) return mixing_dict(xy_iter, normalized=normalized)
def degree_pearson_correlation_coefficient(G, x='out', y='in', weight=None, nodes=None): """Compute degree assortativity of graph. Assortativity measures the similarity of connections in the graph with respect to the node degree. This is the same as degree_assortativity_coefficient but uses the potentially faster scipy.stats.pearsonr function. Parameters ---------- G : NetworkX graph x: string ('in','out') The degree type for source node (directed graphs only). y: string ('in','out') The degree type for target node (directed graphs only). weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. nodes: list or iterable (optional) Compute pearson correlation of degrees only for specified nodes. The default is all nodes. Returns ------- r : float Assortativity of graph by degree. Examples -------- >>> G=nx.path_graph(4) >>> r=nx.degree_pearson_correlation_coefficient(G) >>> print("%3.1f"%r) -0.5 Notes ----- This calls scipy.stats.pearsonr. References ---------- .. [1] M. E. J. Newman, Mixing patterns in networks Physical Review E, 67 026126, 2003 .. [2] Foster, J.G., Foster, D.V., Grassberger, P. & Paczuski, M. Edge direction and the structure of networks, PNAS 107, 10815-20 (2010). """ try: import scipy.stats as stats except ImportError: raise ImportError( "Assortativity requires SciPy: http://scipy.org/ ") xy=node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight) x,y=zip(*xy) return stats.pearsonr(x,y)[0]
def degree_mixing_dict(G, x='out', y='in', weight=None, nodes=None, normalized=False): """Return dictionary representation of mixing matrix for degree. Parameters ---------- G : graph NetworkX graph object. x: string ('in','out') The degree type for source node (directed graphs only). y: string ('in','out') The degree type for target node (directed graphs only). weight: string or None, optional (default=None) The edge attribute that holds the numerical value used as a weight. If None, then each edge has weight 1. The degree is the sum of the edge weights adjacent to the node. normalized : bool (default=False) Return counts if False or probabilities if True. Returns ------- d: dictionary Counts or joint probability of occurrence of degree pairs. """ xy_iter = node_degree_xy(G, x=x, y=y, nodes=nodes, weight=weight) return mixing_dict(xy_iter, normalized=normalized)