Node2vec learns graph-node vectors using biased random walks. Trace the return and outward probabilities, and distinguish exploration bias from a promised cluster structure.
Node2vec
Node2vec learns a vector for each graph node from the contexts produced by biased random walks. Its distinguishing feature is that the next step depends on both the current node and the node just visited. For a quant using a network of firms or accounts, this changes which relationships supply training examples, and therefore which aspects of the network the features can preserve.
Like DeepWalk, it treats walks as sequences of node identities and uses a Skip-gram style objective to predict nearby identities in those sequences. The original node2vec method uses negative sampling during optimisation. The walk is the sampling mechanism; the embedding is learned afterwards. Grover and Leskovec, §3.
Remember the last edge
Suppose the walk has moved from t to v and is choosing a neighbour x of v. For an undirected graph, let d(t,x) be the shortest-path distance in number of edges. A candidate is at distance 0, 1 or 2 from t: it is the previous node itself, a neighbour of that node, or reachable through v in two edges.
The denominator must be positive. In an unweighted graph every existing edge has weight 1. In a weighted graph, multiply the bias by the edge weight before normalising; the bias alone is not the transition probability. These rules are explicit in the authors’ transition-preprocessing implementation. For a directed graph, specify the permitted edge directions and adjacency convention separately rather than importing this undirected distance picture unchanged.
The dependence on the last two nodes makes the node sequence second order. Taking the ordered pair “previous node, current node” as the state gives a first-order Markov chain. The Markov-property lesson explains this state-enlargement idea (public preview; full lesson requires membership). On the very first step there is no previous node: the reference implementation samples using normalised edge weights. A walk at an isolated node stops.
One junction, three different weights
Use a synthetic, unweighted, undirected graph with exactly the edges A–B, A–C, B–C and B–D, and no self-loops. The walk has just travelled A to B. B's neighbours are A, C and D. Set p=2 and q=0.5.
Candidate from B
Relation to previous node A
Raw weight
Next-step probability
A
Return; distance 0
1/2
1/7
C
A–C exists; distance 1
1
2/7
D
No A–D edge; distance 2
2
4/7
The weights total 3.5. Equivalently, their ratio is 1:2:4, so seven equally likely tickets—one A, two C and four D—would implement the same choice. D is most likely, but a return to A remains possible.
Synthetic unweighted undirected graph with edges A–B, A–C, B–C and B–D. After moving A to B, p = 2 and q = 0.5 assign A, C and D raw weights 0.5, 1 and 2, giving next-step probabilities 1/7, 2/7 and 4/7.
The distance categories refer to the previous node A. D is favoured for this step; the diagram does not show learned embedding coordinates.
Now change only the B–D edge weight to 1/4, leaving the other edge weights at 1. The raw weights become 0.5,1,0.5 and the probabilities become 1/4,1/2,1/4. The outward bias no longer makes D the most likely choice. Edge construction and walk parameters act together.
Exploration is a bias, not a clustering promise
Holding other factors fixed, increasing p suppresses immediate backtracking. Increasing q suppresses outward candidates, producing more local, breadth-first-like exploration; decreasing q favours outward candidates, producing depth-first-like exploration. These are sampling heuristics, not literal breadth-first or depth-first traversals. Distances from the starting node need not increase on successive steps. No setting guarantees semantic clusters or a particular notion of structural similarity. With p=q=1, the walk bias disappears: on an unweighted graph the sampling reduces to uniform neighbour choices, as in basic DeepWalk. Identical walks alone do not make different training implementations identical. See node2vec, §§3.2 and 4.2.
For a hypothetical supplier network, a useful interpretation starts with what an edge means. Making supply relationships undirected removes who supplies whom. Treating a negative return correlation as a negative transition weight would violate the probability construction; any transformation to nonnegative edges changes the represented network. Tune the graph and walk choices against the downstream question, rather than assuming visually separated vectors must be useful.
For historical financial prediction, build the graph and fit the representation using only information available at the prediction cutoff. In particular, a supplier relationship disclosed later must not enter an earlier training snapshot. This is an application of information-time discipline to graph features, not a property node2vec enforces automatically.
Basic node2vec learns node-specific vectors. A new firm does not receive an embedding merely by passing its attributes through that table. GraphSAGE instead learns a neighbourhood aggregation function that can generate representations for unseen nodes with compatible features; the authors’ project page explains that distinction. Neither approach makes embedding distance a causal relationship or a calibrated financial risk measure.