hrg_fit

Contents

hrg_fit#

graph_tool.inference.hrg_fit(g, niter=1000, start=None, beta=1.0)[git][source]#

Fit a hierarchical random graph to a graph.

Parameters:
gGraph

Graph to be used. Edge directions, self-loops, and parallel edges are ignored.

niterint (optional, default: 1000)

Number of sweeps of the Markov chain, each with \(V - 1\) steps.

startGraph (optional, default: None)

Initial dendrogram, as returned by this function. If not given, a random one is used.

betafloat (optional, default: 1.)

Inverse temperature of the Markov chain.

Returns:
treeGraph

The dendrogram of largest likelihood found, as a directed tree with \(2V - 1\) vertices, whose leaves \(0, \dots, V - 1\) are the vertices of g (in the order of get_vertices()), and whose edges point from parents to children.

pVertexPropertyMap

Connection probability of every internal vertex of the tree (zero for the leaves).

Lfloat

Log-likelihood of the dendrogram.

See also

hrg_predict

Prediction of missing edges with the hierarchical random graph

hrg_consensus

Consensus tree of the hierarchical random graph

hrg_generate

Sampling of graphs from a hierarchical random graph

NestedBlockState

Nested stochastic block model

Notes

In the hierarchical random graph of Clauset, Moore, and Newman [clauset-hierarchical-2008], every pair of vertices is connected independently with the probability \(p_r\) of their lowest common ancestor \(r\) in a binary dendrogram. With the maximum likelihood \(p_r = E_r / (L_rR_r)\), where \(E_r\) is the number of edges between the left and right subtrees of \(r\), which have \(L_r\) and \(R_r\) leaves, the log-likelihood is

\[\ln\mathcal{L} = \sum_r \left[E_r\ln p_r + (L_rR_r - E_r)\ln(1 - p_r)\right],\]

with \(0\ln 0 = 0\), where the sum runs over the \(V - 1\) internal nodes. Dendrograms are sampled with probability proportional to \(\mathcal{L}^\beta\) by the Markov chain Monte Carlo algorithm of the paper, which rearranges the three subtrees below an internal node and its internal child. In every step, an internal node \(r\) and one of its two children \(s\) are chosen uniformly at random (repeating the choice until \(s\) is an internal node); if \(u\) and \(v\) are the children of \(s\) and \(t\) is the other child of \(r\), one of the two other arrangements, in which \(s\) has the children \((u, t)\) and \(r\) the children \((s, v)\), or \(s\) has \((v, t)\) and \(r\) has \((s, u)\), is proposed with equal probability, and accepted with probability \(\min(1, e^{\beta\Delta\ln\mathcal{L}})\). The initial dendrogram, if start is not given, is obtained by merging random pairs of subtrees, starting from the single vertices, until one tree remains. The dendrogram of largest likelihood visited is returned. Each step takes time proportional to the size of the rearranged subtrees times their degrees.

This is a descriptive model with a single, non-nested level of hierarchy per split; inference of hierarchical structure with the nested stochastic block model (see minimize_nested_blockmodel_dl()) is usually preferable.

References

[clauset-hierarchical-2008]

Aaron Clauset, Cristopher Moore and M. E. J. Newman, “Hierarchical structure and the prediction of missing links in networks”, Nature 453, 98-101 (2008), DOI: 10.1038/nature06830 [sci-hub, @tor], arXiv: 0811.0484

Examples

>>> g = gt.collection.data["karate"]
>>> tree, p, L = gt.hrg_fit(g, niter=500)
>>> print(tree.num_vertices(), L)
67 -87.2...

The dendrogram, shown in the figure below, can be drawn with tree_layout(), starting from its root, as follows:

>>> from matplotlib.colors import LinearSegmentedColormap
>>> cmap = LinearSegmentedColormap.from_list("p", ["#d9d9d9",
...                                                  "#ed6a6b"])
>>> leaf = tree.get_out_degrees(tree.get_vertices()) == 0
>>> vs = tree.get_vertices()
>>> root = np.flatnonzero(tree.get_in_degrees(vs) == 0)[0]
>>> shape = tree.new_vp("string",
...                     vals=np.where(leaf, "square", "circle"))
>>> label = tree.new_vp("string", vals=[str(v) if l else ""
...                                     for v, l in enumerate(leaf)])
>>> gt.graph_draw(tree, pos=gt.tree_layout(tree, root),
...               vertex_fill_color=p, vcmap=cmap, vertex_shape=shape,
...               vertex_text=label, vertex_font_size=8,
...               edge_end_marker="none", output_size=(600, 500),
...               output="hrg-fit-karate.pdf")
<...>
../_images/hrg-fit-karate.png

Dendrogram of the hierarchical random graph fitted to the karate club network, with the root at the top. The leaves (squares) are labeled with the vertex indices, and the internal vertices are colored by their connection probabilities \(p_r\), from gray (zero) to red (the largest value).#