graph_tool.correlations
 Correlations¶
Summary¶
Obtain the assortativity coefficient for the given graph. 

Obtain the scalar assortativity coefficient for the given graph. 

Obtain the correlation histogram for the given graph. 

Obtain the singlevertex combined correlation histogram for the given graph. 

Obtain the average neighborneighbor correlation for the given graph. 

Obtain the singlevertex combined correlation histogram for the given graph. 
Contents¶

graph_tool.correlations.
assortativity
(g, deg, eweight=None)[source]¶ Obtain the assortativity coefficient for the given graph.
 Parameters
 g
Graph
Graph to be used.
 degstring or
VertexPropertyMap
Degree type (“in”, “out” or “total”) or vertex property map, which specifies the vertex types.
 eweight
EdgePropertyMap
(optional, default: None) If given, this will specify the edge weights, otherwise a constant value of one will be used.
 g
 Returns
 assortativity coefficienttuple of two floats
The assortativity coefficient, and its variance.
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
The assortativity coefficient [newmanmixing2003] tells in a concise fashion how vertices of different types are preferentially connected amongst themselves, and is defined by
\[r = \frac{\sum_i e_{ii}  \sum_i a_i b_i}{1\sum_i a_i b_i}\]where \(a_i=\sum_je_{ij}\) and \(b_j=\sum_ie_{ij}\), and \(e_{ij}\) is the fraction of edges from a vertex of type i to a vertex of type j.
The variance is obtained with the jackknife method.
If enabled during compilation, this algorithm runs in parallel.
References
 newmanmixing2003
M. E. J. Newman, “Mixing patterns in networks”, Phys. Rev. E 67, 026126 (2003), DOI: 10.1103/PhysRevE.67.026126 [scihub, @tor]
Examples
>>> g = gt.collection.data["pgpstrong2009"] >>> g = gt.GraphView(g, directed=False) >>> gt.assortativity(g, "out") (0.0240578552..., 0.0003272847...)

graph_tool.correlations.
scalar_assortativity
(g, deg, eweight=None)[source]¶ Obtain the scalar assortativity coefficient for the given graph.
 Parameters
 g
Graph
Graph to be used.
 degstring or
VertexPropertyMap
Degree type (“in”, “out” or “total”) or vertex property map, which specifies the vertex scalar values.
 eweight
EdgePropertyMap
(optional, default: None) If given, this will specify the edge weights, otherwise a constant value of one will be used.
 g
 Returns
 scalar assortativity coefficienttuple of two floats
The scalar assortativity coefficient, and its variance.
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
The scalar assortativity coefficient [newmanmixing2003] tells in a concise fashion how vertices of different types are preferentially connected amongst themselves, and is defined by
\[r = \frac{\sum_{xy} xy(e_{xy}  a_x b_y)}{\sigma_a\sigma_b}\]where \(a_x=\sum_ye_{xy}\) and \(b_y=\sum_xe_{xy}\), and \(e_{xy}\) is the fraction of edges from a vertex of type x to a vertex of type y.
The variance is obtained with the jackknife method.
If enabled during compilation, this algorithm runs in parallel.
References
 newmanmixing2003
M. E. J. Newman, “Mixing patterns in networks”, Phys. Rev. E 67, 026126 (2003), DOI: 10.1103/PhysRevE.67.026126 [scihub, @tor]
Examples
>>> g = gt.collection.data["pgpstrong2009"] >>> g = gt.GraphView(g, directed=False) >>> gt.scalar_assortativity(g, "out") (0.026384932418..., 0.001213444270...)

graph_tool.correlations.
corr_hist
(g, deg_source, deg_target, bins=[[0, 1], [0, 1]], weight=None, float_count=True)[source]¶ Obtain the correlation histogram for the given graph.
 Parameters
 g
Graph
Graph to be used.
 deg_sourcestring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the source vertex.
 deg_targetstring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the target vertex.
 binslist of lists (optional, default: [[0, 1], [0, 1]])
A list of bin edges to be used for the source and target degrees. If any list has size 2, it is used to create an automatically generated bin range starting from the first value, and with constant bin width given by the second value.
 weightedge property map (optional, default: None)
Weight (multiplicative factor) to be used on each edge.
 float_countbool (optional, default: True)
If True, the bin counts are converted float variables, which is useful for normalization, and other processing. It False, the bin counts will be unsigned integers.
 g
 Returns
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
The correlation histogram counts, for every vertex with degree (or scalar property) ‘source_deg’, the number of outneighbors with degree (or scalar property) ‘target_deg’.
If enabled during compilation, this algorithm runs in parallel.
Examples
>>> def sample_k(max): ... accept = False ... while not accept: ... k = np.random.randint(1,max+1) ... accept = np.random.random() < 1.0/k ... return k ... >>> g = gt.random_graph(10000, lambda: sample_k(40), ... model="probabilisticconfiguration", ... edge_probs=lambda i, j: (sin(i / pi) * sin(j / pi) + 1) / 2, ... directed=False, n_iter=100) >>> h = gt.corr_hist(g, "out", "out") >>> clf() >>> xlabel("Source outdegree") Text(...) >>> ylabel("Target outdegree") Text(...) >>> imshow(h[0].T, interpolation="nearest", origin="lower") <...> >>> colorbar() <...> >>> savefig("corr.svg")

graph_tool.correlations.
combined_corr_hist
(g, deg1, deg2, bins=[[0, 1], [0, 1]], float_count=True)[source]¶ Obtain the singlevertex combined correlation histogram for the given graph.
 Parameters
 g
Graph
Graph to be used.
 deg1string or
VertexPropertyMap
first degree type (“in”, “out” or “total”) or vertex property map.
 deg2string or
VertexPropertyMap
second degree type (“in”, “out” or “total”) or vertex property map.
 binslist of lists (optional, default: [[0, 1], [0, 1]])
A list of bin edges to be used for the first and second degrees. If any list has size 2, it is used to create an automatically generated bin range starting from the first value, and with constant bin width given by the second value.
 float_countbool (optional, default: True)
If True, the bin counts are converted float variables, which is useful for normalization, and other processing. It False, the bin counts will be unsigned integers.
 g
 Returns
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
If enabled during compilation, this algorithm runs in parallel.
Examples
>>> def sample_k(max): ... accept = False ... while not accept: ... i = np.random.randint(1, max + 1) ... j = np.random.randint(1, max + 1) ... accept = np.random.random() < (sin(i / pi) * sin(j / pi) + 1) / 2 ... return i,j ... >>> g = gt.random_graph(10000, lambda: sample_k(40)) >>> h = gt.combined_corr_hist(g, "in", "out") >>> clf() >>> xlabel("Indegree") Text(...) >>> ylabel("Outdegree") Text(...) >>> imshow(h[0].T, interpolation="nearest", origin="lower") <...> >>> colorbar() <...> >>> savefig("combined_corr.svg")

graph_tool.correlations.
avg_neighbor_corr
(g, deg_source, deg_target, bins=[0, 1], weight=None)[source]¶ Obtain the average neighborneighbor correlation for the given graph.
 Parameters
 g
Graph
Graph to be used.
 deg_sourcestring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the source vertex.
 deg_targetstring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the target vertex.
 binslist (optional, default: [0, 1])
Bins to be used for the source degrees. If the list has size 2, it is used as the constant width of an automatically generated bin range, starting from the first value.
 weightedge property map (optional, default: None)
Weight (multiplicative factor) to be used on each edge.
 g
 Returns
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
The average correlation is the average, for every vertex with degree (or scalar property) ‘source_deg’, the of the ‘target_deg’ degree (or scalar property) of its neighbors.
If enabled during compilation, this algorithm runs in parallel.
Examples
>>> def sample_k(max): ... accept = False ... while not accept: ... k = np.random.randint(1,max+1) ... accept = np.random.random() < 1.0 / k ... return k ... >>> g = gt.random_graph(10000, lambda: sample_k(40), ... model="probabilisticconfiguration", ... edge_probs=lambda i, j: (sin(i / pi) * sin(j / pi) + 1) / 2, ... directed=False, n_iter=100) >>> h = gt.avg_neighbor_corr(g, "out", "out") >>> clf() >>> xlabel("Source outdegree") Text(...) >>> ylabel("Target outdegree") Text(...) >>> errorbar(h[2][:1], h[0], yerr=h[1], fmt="o") <...> >>> savefig("avg_corr.svg")

graph_tool.correlations.
avg_neighbour_corr
(g, deg_source, deg_target, bins=[0, 1], weight=None)¶ Obtain the average neighborneighbor correlation for the given graph.
 Parameters
 g
Graph
Graph to be used.
 deg_sourcestring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the source vertex.
 deg_targetstring or
VertexPropertyMap
degree type (“in”, “out” or “total”) or vertex property map for the target vertex.
 binslist (optional, default: [0, 1])
Bins to be used for the source degrees. If the list has size 2, it is used as the constant width of an automatically generated bin range, starting from the first value.
 weightedge property map (optional, default: None)
Weight (multiplicative factor) to be used on each edge.
 g
 Returns
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
The average correlation is the average, for every vertex with degree (or scalar property) ‘source_deg’, the of the ‘target_deg’ degree (or scalar property) of its neighbors.
If enabled during compilation, this algorithm runs in parallel.
Examples
>>> def sample_k(max): ... accept = False ... while not accept: ... k = np.random.randint(1,max+1) ... accept = np.random.random() < 1.0 / k ... return k ... >>> g = gt.random_graph(10000, lambda: sample_k(40), ... model="probabilisticconfiguration", ... edge_probs=lambda i, j: (sin(i / pi) * sin(j / pi) + 1) / 2, ... directed=False, n_iter=100) >>> h = gt.avg_neighbor_corr(g, "out", "out") >>> clf() >>> xlabel("Source outdegree") Text(...) >>> ylabel("Target outdegree") Text(...) >>> errorbar(h[2][:1], h[0], yerr=h[1], fmt="o") <...> >>> savefig("avg_corr.svg")

graph_tool.correlations.
avg_combined_corr
(g, deg1, deg2, bins=[0, 1])[source]¶ Obtain the singlevertex combined correlation histogram for the given graph.
 Parameters
 g
Graph
Graph to be used.
 deg1string or
VertexPropertyMap
first degree type (“in”, “out” or “total”) or vertex property map.
 deg2string or
VertexPropertyMap
second degree type (“in”, “out” or “total”) or vertex property map.
 binslist (optional, default: [0, 1])
Bins to be used for the first degrees. If the list has size 2, it is used as the constant width of an automatically generated bin range, starting from the first value.
 g
 Returns
See also
assortativity
assortativity coefficient
scalar_assortativity
scalar assortativity coefficient
corr_hist
vertexvertex correlation histogram
combined_corr_hist
combined singlevertex correlation histogram
avg_neighbor_corr
average nearestneighbor correlation
avg_combined_corr
average combined singlevertex correlation
Notes
If enabled during compilation, this algorithm runs in parallel.
Examples
>>> def sample_k(max): ... accept = False ... while not accept: ... i = np.random.randint(1,max+1) ... j = np.random.randint(1,max+1) ... accept = np.random.random() < (sin(i/pi)*sin(j/pi)+1)/2 ... return i,j ... >>> g = gt.random_graph(10000, lambda: sample_k(40)) >>> h = gt.avg_combined_corr(g, "in", "out") >>> clf() >>> xlabel("Indegree") Text(...) >>> ylabel("Outdegree") Text(...) >>> errorbar(h[2][:1], h[0], yerr=h[1], fmt="o") <...> >>> savefig("combined_avg_corr.svg")