nwsimindex
Common-neighbor similarity indices between all node pairs
Syntax
nwsimindex
[netname]
[,
measure(string)
name(newnetname)
xvars
replace
force]
measure(common\|jaccard\|dice\|cosine\|adamicadar) |
Which similarity index to compute; default = jaccard |
name(newnetname) |
Name of the new similarity network; default = simindex |
xvars |
Generate Stata variables for the new network |
replace |
Replace an existing network of the same name; without it a free name is used and a warning is shown |
force |
Run the command although its matrices would not fit in memory (it stops with r(484) above about 17,300 nodes otherwise) |
Description
nwsimindex computes a common-neighbor similarity index (Liben-Nowell and Kleinberg 2007) for every pair of nodes and stores the result as a new, valued, undirected network newnetname (default: simindex). These indices measure how much two nodes’ neighborhoods overlap - a standard building block for link prediction, structural-equivalence/role analysis, and as an input to blockmodeling or one-mode projection (see nw2project).
All calculations use the undirected neighbor sense: for directed networks, a node’s neighbor set is the union of its out- and in-neighbors (the same convention nwkcore uses), since neighborhood overlap is a direction-agnostic question. measure is one of:
- common
-
the raw count of shared neighbors, * N(i) intersect N(j) * - jaccard (default)
-
-
N(i) intersect N(j) / N(i) union N(j) *
-
- dice
-
the Sorensen-Dice coefficient, *2 N(i) intersect N(j) / ( N(i) + N(j) )* - cosine
-
the Salton cosine similarity, * N(i) intersect N(j) / sqrt( N(i) * N(j) )* - adamicadar
- Adamic-Adar, sum over shared neighbors k of 1/log(degree(k)) - weights rare
- (low-degree) shared neighbors more heavily than common ones
The similarity of a node with itself is not defined and is set to missing, as are any pairs where the underlying formula is undefined - most notably cosine between two isolate nodes (0/0). This mirrors how nwgeodesic reports an undefined diameter/radius rather than silently coercing an undefined value to 0.
Network types
Directed: the neighbours of a node are its out- and in-neighbours. Valued: tie values are ignored (every tie counts as present; the command prints a note). Two-mode: same as for the bipartite graph read as a one-mode network. Signed: not applicable. See nwnetworktypes.
Limits
The result is a dense n-by-n valued network. Measured in Stata/BE, random undirected network with 10 ties per node: 5,000 nodes 0.8 seconds and 1.3 GB.
The result is a dense n-by-n valued network. About 5 matrices of that size are held at once (10,000 nodes: 2.8 seconds and 4 GB). Above about 17,300 nodes the command stops at once with r(484) and a message with the memory needed (16 GB at 20,000 nodes); option force runs it anyway.
Examples
. nwwebuse florentine, nwclear
. nwsimindex flomarriage, measure(jaccard)
. nwsummarize simindex, matonly
Stored results
- Scalars
-
r(nodes) — number of nodes
- Macros
- r(measure) — the measure used
- r(netname) — name of the new similarity network
References
Liben-Nowell, D., Kleinberg, J. (2007). The link-prediction problem for social networks. Journal of the American Society for Information Science and Technology 58(7), 1019-1031.
Adamic, L.A., Adar, E. (2003). Friends and neighbors on the Web. Social Networks 25(3), 211-230.
See also
nwcommands version 3.25.0, 14 November 2026 (check your installed version)