Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Assortativity  #272

@ghost

Description

Hi,

As promised, here are the functions I wrote to compute the assortativity of graphs. I orientated myself on the respective functions in the igraph package (https://github.com/igraph/igraph/blob/master/src/mixing.c) and also used them as a benchmark. In the link you also find the references for the methods. A function to compute node-level assortativity will follow, hopefully in the course of today. The vertex categories/values have to supplied in the form of dictionaries.

###nominal assortativity
function assortativity_nominal(g,cat)
uc  = unique(values(cat))
nue = ne(g)
ai  = Dict()
bi  = Dict()
eii = Dict()
sumaibi = 0
sumeii  = 0

for (u,v) in edges(g)
ai[cat[u]]  = get(ai,cat[u],0) + 1
bi[cat[v]]  = get(bi,cat[v],0) + 1
cat[u] != cat[v] && continue
eii[cat[u]] = get(eii,cat[u],0) + 1
end

for c in uc
sumaibi += (ai[c]/nue) * (bi[c]/nue);
sumeii  += try (eii[c]/nue) catch 0 end;
end

if typeof(g)==LightGraphs.Graph
  sumaibi /= 4.0;
  sumeii  /= 2.0;
end

res = (sumeii - sumaibi) / (1.0 - sumaibi);

return res
end

###degree assortativity
function assortativity_degree(g)
nue  = ne(g)
sjk = 0
sj  = 0
sk  = 0
sjs = 0
sks = 0

for (u,v) in edges(g)
j   = outdegree(g,u) - 1;
k   = indegree(g,v)  - 1;
sjk += j*k
sj  += j
sk  += k
sjs += j^2
sks += k^2
end

if typeof(g)==LightGraphs.DiGraph
res = (sjk - sj*sk/nue)/sqrt((sjs - sj^2/nue)*(sks - sk^2/nue))
end

if typeof(g)==LightGraphs.Graph
res = (sjk/nue - ((sj + sk)/(2*nue))^2)/((sjs + sks)/(2*nue) - ((sj + sk)/(2*nue))^2)
end

return res
end

###assortativity
function assortativity(g, cat1, cat2 = "foo")
nue  = ne(g)
sjk = 0
sj  = 0
sk  = 0
sjs = 0
sks = 0

if cat2 == "foo"
cat2 = cat1
end

for (u,v) in edges(g)
j   = cat1[u];
k   = cat2[v];
sjk += j*k
sj  += j
sk  += k
sjs += j^2
sks += k^2
end

if typeof(g)==LightGraphs.DiGraph
res = (sjk - sj*sk/nue)/sqrt((sjs - sj^2/nue)*(sks - sk^2/nue))
end

if typeof(g)==LightGraphs.Graph
res = (sjk/nue - ((sj + sk)/(2*nue))^2)/((sjs + sks)/(2*nue) - ((sj + sk)/(2*nue))^2)
end

return res
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions