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

Commit

Permalink
modification following review from sbromberger
Browse files Browse the repository at this point in the history
  • Loading branch information
vboussange committed Mar 30, 2021
1 parent 68b038b commit a07a60c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/community/assortativity.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
assortativity(g)
assortativity(g,attributes)
assortativity(g, attributes)
Return the [assortativity coefficient](https://en.wikipedia.org/wiki/Assortativity)
of graph `g`, defined as the Pearson correlation of excess degree between
Expand Down Expand Up @@ -48,9 +48,10 @@ function assortativity(g::AbstractGraph{T}) where T
return assortativity_coefficient(g, sjk, sj, sk, sjs, sks, nue)
end

function assortativity(g::AbstractGraph{T},attributes::Vector{N}) where {T,N<:Number}
function assortativity(g::AbstractGraph{T}, attributes::Vector{N}) where {T,N<:Number}
P = promote_type(Int64, N) # at least Int64 to reduce risk of overflow
nue = ne(g)
sjk = sj = sk = sjs = sks = zero(N)
sjk = sj = sk = sjs = sks = zero(P)
for d in edges(g)
j = attributes[src(d)]
k = attributes[dst(d)]
Expand Down
10 changes: 5 additions & 5 deletions test/community/assortativity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ using Random, Statistics
# Test definition of assortativity as Pearson correlation coefficient
# between some scalar attributes characterising each vertex
@testset "Small graphs with attributes" for n = 5:2:11
g = grid([1,n])
attributes = [(-1.)^v for v in vertices(g)]
@test @inferred assortativity(g,attributes) -1.
g = grid([1, n])
attributes = [(-1.)^v for v in 1:nv(g)]
@test @inferred assortativity(g, attributes) -1.
end
@testset "Directed ($seed) with attributes" for seed in [1, 2, 3], (_n, _ne) in [(14, 18), (10, 22), (7, 16)]
g = erdos_renyi(_n, _ne; is_directed=true, seed=seed)
attributes = randn(_n)
assort = assortativity(g,attributes)
assort = assortativity(g, attributes)
x = [attributes[src(d)] for d in edges(g)]
y = [attributes[dst(d)] for d in edges(g)]
@test @inferred assort cor(x, y)
end
@testset "Undirected ($seed) with attributes" for seed in [1, 2, 3], (_n, _ne) in [(14, 18), (10, 22), (7, 16)]
g = erdos_renyi(_n, _ne; is_directed=false, seed=seed)
attributes = randn(_n)
assort = assortativity(g,attributes)
assort = assortativity(g, attributes)
x = [attributes[src(d)] for d in edges(g)]
y = [attributes[dst(d)] for d in edges(g)]
@test @inferred assort cor([x;y], [y;x])
Expand Down

0 comments on commit a07a60c

Please sign in to comment.