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

Fix 149 #155

Merged
merged 4 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 11 additions & 10 deletions docs/source/api/stats/xgi.stats.EdgeStat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
.. autosummary::
:nosignatures:

~NodeStat.asdict
~NodeStat.aslist
~NodeStat.asnumpy
~NodeStat.aspandas
~NodeStat.max
~NodeStat.mean
~NodeStat.median
~NodeStat.min
~NodeStat.std
~NodeStat.var
~EdgeStat.asdict
~EdgeStat.aslist
~EdgeStat.asnumpy
~EdgeStat.aspandas
~EdgeStat.max
~EdgeStat.mean
~EdgeStat.median
~EdgeStat.min
~EdgeStat.std
~EdgeStat.var
~EdgeStat.moment
1 change: 1 addition & 0 deletions docs/source/api/stats/xgi.stats.NodeStat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
~NodeStat.min
~NodeStat.std
~NodeStat.var
~NodeStat.moment
18 changes: 18 additions & 0 deletions tests/stats/test_nodestats.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,21 @@ def test_view_val(edgelist1, edgelist2):
H = xgi.Hypergraph(edgelist2)
assert H.nodes.degree._val == {1: 1, 2: 1, 3: 1, 4: 2, 5: 1, 6: 1}
assert H.nodes([4, 5, 6]).degree._val == {4: 2, 5: 1, 6: 1}


def test_moment(edgelist1, edgelist6):
H = xgi.Hypergraph(edgelist1)
deg = H.nodes.degree
assert round(deg.moment(), 3) == 1.375
assert round(deg.moment(2, center=False), 3) == 1.375
assert round(deg.moment(2, center=True), 3) == 0.109
assert round(deg.moment(3, center=False), 3) == 1.875
assert round(deg.moment(3, center=True), 3) == 0.082

H = xgi.Hypergraph(edgelist6)
deg = H.edges.size
assert round(deg.moment(), 3) == 9.0
assert round(deg.moment(2, center=False), 3) == 9.0
assert round(deg.moment(2, center=True), 3) == 0.0
assert round(deg.moment(3, center=False), 3) == 27.0
assert round(deg.moment(3, center=True), 3) == 0.0
15 changes: 15 additions & 0 deletions xgi/stats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

import numpy as np
import pandas as pd
from scipy.stats import moment as spmoment

from xgi.exception import IDNotFound

Expand Down Expand Up @@ -246,6 +247,20 @@ def var(self):
"""The variance of this stat."""
return self.asnumpy().var(axis=0)

def moment(self, order=2, center=False):
"""The statistical moments of this stat.

Parameters
----------
order : int (default 2)
The order of the moment.
center : bool (default False)
Whether to compute the centered (False) or uncentered/raw (True) moment.

"""
arr = self.asnumpy()
return spmoment(arr, moment=order) if center else np.mean(arr ** order)

def dist(self):
return np.histogram(self.asnumpy(), density=True)

Expand Down