Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add assortativity module #122

Merged
merged 19 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: added assortativity measures.
  • Loading branch information
nwlandry committed Jun 5, 2022
commit ee06fa94cce362827cfd8419e60fd61858a83d21
5 changes: 3 additions & 2 deletions xgi/algorithms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from xgi.algorithms import connected
from xgi.algorithms import assortativity, connected
nwlandry marked this conversation as resolved.
Show resolved Hide resolved

from .connected import *
from .assortativity import *
from .connected import *
66 changes: 66 additions & 0 deletions xgi/algorithms/assortativity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import numpy as np
import xgi
from xgi.exception import XGIError
from itertools import combinations
import random

__all__ = [
"dynamical_assortativity",
"top_2_assortativity",
"top_bottom_assortativity",
"uniform_assortativity",
]

def dynamical_assortativity(H):
""" Gets the dynamical assortativity of a uniform hypergraph.

Parameters
----------
H : xgi.Hypergraph
Hypergraph of interest

Returns
-------
_type_
_description_

Raises
------
XGIError
_description_
"""
if not xgi.is_uniform(H):
raise XGIError("Hypergraph must be uniform!")

k1 = np.mean([H.degree(n) for n in H.nodes])
k2 = np.mean([H.degree(n)**2 for n in H.nodes])

kk1 = np.mean([H.degree(n1)*H.degree(n2) for e in H.edges for n1, n2 in combinations(H.edges.members(e), 2)])
return kk1*k1**2/k2**2 - 1


def top_2_assortativity(H):
degrees = dict(H.degree())
k1 = [max((degrees[n] for n in H.edges.members(e))) for e in H.edges]
try:
k2 = [sorted((degrees[n] for n in H.edges.members(e)))[-2] for e in H.edges]
except IndexError:
raise XGIError("Hypergraph must not have singleton edges.")
return np.corrcoef(k1, k2)[0, 1]


def top_bottom_assortativity(H):
degrees = dict(H.degree())
k1 = [max((degrees[n] for n in H.edges.members(e))) for e in H.edges]
k2 = [min((degrees[n] for n in H.edges.members(e))) for e in H.edges]
return np.corrcoef(k1, k2)[0, 1]


def uniform_assortativity(H):
degrees = dict(H.degree())
try:
k1k2 = [[degrees[n] for n in random.sample(H.edges.members(e), 2)] for e in H.edges]
except IndexError:
raise XGIError("Hypergraph must not have singleton edges.")

return np.corrcoef(*k1k2)[0, 1]