11Assortativity
22=============
33
4- .. note ::
5- The methods in this section rely on calculating the Pearson correlation
6- coefficient between pairs of value sequences. If the network is regular (i.e.
7- all vertices have equal degree or label) or if the network has fewer than two
8- pairs of interacting vertices, the result of calculating assortativity will
9- be :cpp: `NaN `.
4+ Assortativity is a measure of preference of nodes to attach to other nodes
5+ similar to themselves. In many cases, researchers are interested in degree
6+ assortaivity, which measures the correlation between the degrees of connected
7+ vertices. However, assortativity can also be calculated for other node attributes :cite:p: `newman2003mixing `.
8+
9+ Reticula provides a set of functions to calculate assortativity in directed
10+ and undirected networks, either in terms of degree or other numerical node
11+ attributes.
12+
13+ .. note ::
14+
15+ The methods in this section rely on calculating the Pearson correlation
16+ coefficient between pairs of value sequences. If all connected vertices
17+ have equal degree or attribute, or if the network has fewer than two pairs
18+ of interacting vertices, the result of calculating assortativity will be
19+ :cpp:`NaN`.
1020
1121
1222 Degree assortativity
@@ -32,6 +42,13 @@ Calculates degree assortativity for undirected dyadic and hypergraph static
3242networks. Assortativity is calculated through Pearson correlation coefficient
3343over degrees of all pairs of interacting vertices.
3444
45+ .. code-block :: pycon
46+
47+ >>> import reticula as ret
48+ >>> gen = ret.mersenne_twister(42)
49+ >>> net = ret.random_gnp_graph[ret.int64](n=128, p=0.05, random_state=gen)
50+ >>> ret.degree_assortativity(net)
51+ 0.012174626999704952
3552
3653 Directed networks
3754^^^^^^^^^^^^^^^^^
@@ -62,6 +79,17 @@ Calculates in-/out-degree assortativity for directed dyadic and hypergraph
6279static networks. Assortativity is calculated through Pearson correlation
6380coefficient over degrees of all pairs of interacting vertices.
6481
82+ .. code-block :: pycon
83+
84+ >>> import reticula as ret
85+ >>> gen = ret.mersenne_twister(42)
86+ >>> net = ret.random_directed_gnp_graph[ret.int64](
87+ ... n=128, p=0.05, random_state=gen)
88+ >>> ret.in_in_degree_assortativity(net)
89+ -0.008986050522667814
90+ >>> ret.in_out_degree_assortativity(net)
91+ -0.05295305364798128
92+
6593 Numerical attribute assortativity
6694---------------------------------
6795
@@ -95,13 +123,25 @@ correlation coefficient over the value of the given function over all pairs of
95123interacting vertices.
96124
97125
126+ .. code-block :: pycon
127+
128+ >>> import reticula as ret
129+ >>> import math
130+ >>> gen = ret.mersenne_twister(42)
131+ >>> net = ret.random_gnp_graph[ret.int64](
132+ ... n=128, p=0.05, random_state=gen)
133+ >>> ret.attribute_assortativity(net,
134+ ... lambda v: math.log(ret.degree(net, v)))
135+ 0.018211216997337246
136+
137+
98138 .. tab-set ::
99139
100140 .. tab-item :: Python
101141 :sync: python
102142
103143 .. py :function :: attribute_assortativity(undirected_network, \
104- attribute_map: dict[network.edge_type (), float ], \
144+ attribute_map: dict[network.vertex_type (), float ], \
105145 default_value : float ) -> float
106146 :noindex:
107147
@@ -110,17 +150,32 @@ interacting vertices.
110150
111151 .. cpp :function :: template <\
112152 network_edge EdgeT, \
113- mapping<EdgeT , double> MapT> \
153+ mapping<VertT , double> MapT> \
114154 double attribute_assortativity(\
115155 const network<EdgeT>& net, \
116156 const MapT& attribute_map, \
117157 double default_value)
118158
119159Calculates assortativity of the given mapping of vertices to floating-point
120- (dictionary) for undirected dyadic and hypergraph static networks. Assortativity
121- is calculated through Pearson correlation coefficient over the value of the
122- given mapping over all pairs of interacting vertices. If a vertex is not present
123- in the mapping, the value of the argument :cpp: `default_value ` is used.
160+ (dictionary) for undirected dyadic and hypergraph static networks.
161+ Assortativity is calculated through Pearson correlation coefficient over the
162+ value of the given mapping over all pairs of interacting vertices. If a vertex
163+ is not present in the mapping, the value of the argument :cpp: `default_value `
164+ is used.
165+
166+
167+ .. code-block :: pycon
168+
169+ >>> import reticula as ret
170+ >>> gen = ret.mersenne_twister(42)
171+ >>> net = ret.random_gnp_graph[ret.int64](
172+ ... n=128, p=0.05, random_state=gen)
173+ >>> attribute_map = {
174+ ... v: math.log(ret.degree(net, v))
175+ ... for v in net.vertices()}
176+ >>> ret.attribute_assortativity(net, attribute_map, default_value=0.0)
177+ 0.018211216997337246
178+
124179
125180
126181 Directed networks
@@ -132,8 +187,8 @@ Directed networks
132187 :sync: python
133188
134189 .. py :function :: attribute_assortativity(directed_network, \
135- mutator_attribute_fun: Callable[[network.edge_type ()], float ], \
136- mutated_attribute_fun: Callable[[network.edge_type ()], float ]) -> float
190+ mutator_attribute_fun: Callable[[network.vertex_type ()], float ], \
191+ mutated_attribute_fun: Callable[[network.vertex_type ()], float ]) -> float
137192
138193 .. tab-item :: C++
139194 :sync: cpp
@@ -162,6 +217,17 @@ representation of a directed link) is calculated from the function
162217:cpp: `mutator_attribute_fun `, and the mutated vertex (head of the arrow) from
163218:cpp: `mutated_attribute_fun `.
164219
220+ .. code-block :: pycon
221+
222+ >>> import reticula as ret
223+ >>> import math
224+ >>> gen = ret.mersenne_twister(42)
225+ >>> net = ret.random_directed_gnp_graph[ret.int64](
226+ ... n=128, p=0.05, random_state=gen)
227+ >>> ret.attribute_assortativity(net,
228+ ... lambda tail: math.log(ret.in_degree(net, tail) + 1),
229+ ... lambda head: math.log(ret.out_degree(net, head) + 1))
230+ -0.05753797100756569
165231
166232 .. tab-set ::
167233
@@ -198,3 +264,19 @@ argument :cpp:`mutator_default_value` or :cpp:`mutated_attribute_fun` is used.
198264The associated value of the mutator (tail of the arrow representation of a
199265directed link) is calculated from the function :cpp: `mutator_attribute_fun `, and
200266the mutated vertex (head of the arrow) from :cpp: `mutated_attribute_fun `.
267+
268+ .. code-block :: pycon
269+
270+ >>> import reticula as ret
271+ >>> gen = ret.mersenne_twister(42)
272+ >>> net = ret.random_directed_gnp_graph[ret.int64](
273+ ... n=128, p=0.05, random_state=gen)
274+ >>> mutator_map = {
275+ ... tail: math.log(ret.in_degree(net, tail) + 1)
276+ ... for tail in net.vertices()}
277+ >>> mutated_map = {
278+ ... head: math.log(ret.out_degree(net, head) + 1)
279+ ... for head in net.vertices()}
280+ >>> ret.attribute_assortativity(net, mutator_map, mutated_map,
281+ ... mutator_default_value=0.0, mutated_default_value=0.0)
282+ -0.05753797100756569
0 commit comments