Assortativity and Mixing Patterns
Mixing asks which endpoint categories or degrees occur together on edges. Assortativity compresses a mixing matrix to a correlation-like coefficient. The compression is useful, but it can hide directional asymmetry, small cell counts, and the composition of available endpoints.
Prerequisites
Complete bipartite and multipartite networks. Keep node attributes distinct from node modes and inferred structural roles.
Learning goals
- Construct a directed normalized attribute mixing matrix with a fixed category order.
- Distinguish source out-degree from target in-degree assortativity.
- Detect the undefined zero-variance case.
- Separate observed mixing from preference, opportunity, selection, and causality.
Analysis contract
- Graph type: a directed simple graph after parallel-edge aggregation; its self-loop remains, and an illustrative two-category analysis label is declared in the example rather than claimed as fixture truth.
- Normalization: the attribute matrix entries are directed-edge proportions summing to one; attribute assortativity uses that matrix, and degree assortativity is the Pearson correlation of source out-degree with target in-degree.
- Disconnected case: isolates contribute no endpoint pair and therefore no mixing count; an all-isolate graph or constant endpoint attribute can make a coefficient undefined.
- Interpretation limit: assortativity is evidence, not preference, homophily, discrimination, mechanism, or causality; the observation process and group composition remain alternatives.
Matrix before coefficient
The example assigns Ada, Ben, and Cy to an illustrative core category and the other declared nodes to edge. This is a teaching label, not a discovered community or social identity. With mapping order core, edge, row describes the source group and column the target group.
The normalized matrix retains directional detail that the scalar coefficient discards. If every node has the same value—such as the fixture's node_type=person—there is no attribute variance, so the assortativity denominator is zero. Reporting NaN as neutral mixing would be incorrect.
Worked example
import csv
from pathlib import Path
import networkx as nx
fixture = Path("tests/fixtures/graph-data-science")
with (fixture / "interaction-nodes.csv").open(encoding="utf-8") as handle:
node_ids = [row["node_id"] for row in csv.DictReader(handle)]
with (fixture / "interaction-relationships.csv").open(encoding="utf-8") as handle:
rows = [row for row in csv.DictReader(handle) if row["record_status"] == "valid"]
graph = nx.DiGraph()
graph.add_nodes_from(node_ids)
graph.add_edges_from((row["source"], row["target"]) for row in rows)
groups = {node: ("core" if node in {"ada", "ben", "cy"} else "edge") for node in node_ids}
nx.set_node_attributes(graph, groups, "analysis_group")
mapping = {"core": 0, "edge": 1}
matrix = nx.attribute_mixing_matrix(
graph, "analysis_group", mapping=mapping, normalized=True
)
formatted = [[round(float(value), 3) for value in row] for row in matrix]
attribute_r = nx.attribute_assortativity_coefficient(graph, "analysis_group")
degree_r = nx.degree_assortativity_coefficient(graph, x="out", y="in", weight=None)
all_one_type = "undefined" if len({"person" for _ in graph}) == 1 else "defined"
print(f"groups={list(mapping)} matrix={formatted}")
print(f"attribute_assortativity={attribute_r:.3f} degree_out_in={degree_r:.3f}")
print(f"all_one_type_assortativity={all_one_type}")
Expected output:
groups=['core', 'edge'] matrix=[[0.333, 0.222], [0.111, 0.333]]
attribute_assortativity=0.341 degree_out_in=0.000
all_one_type_assortativity=undefined
The positive attribute coefficient describes this label assignment and observed edge set only. The zero degree coefficient does not imply absence of every structural pattern.
Limitations
- Small matrices are sensitive to one edge, especially a self-loop or repeated event.
- Category proportions constrain possible mixing; compare the full matrix and an appropriate null model.
- Aggregating parallel events removes event frequency from the coefficient used here.
- Missing edges and nonrandom observation can create or erase apparent mixing.
Practice
Remove the self-loop and recompute both the matrix and coefficient. Then reverse all edges and identify which matrix cells swap while explaining why the scalar may conceal that change.
Next steps
Continue to motifs, triads, and local structure for patterns involving three nodes rather than one edge at a time.
Primary sources
Checked 2026-10-18: NetworkX, assortativity reference; NetworkX, attribute_mixing_matrix; M. E. J. Newman, Mixing patterns in networks.