Skip to content

Commit

Permalink
Fix 149 (#155)
Browse files Browse the repository at this point in the history
* implement moment method for stats
* from PR discussion, the default should be to compute the raw/uncentered moments, which makes moment2 unnecessary.
  • Loading branch information
leotrs authored Aug 30, 2022
1 parent a5d9c70 commit 30bc50a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
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

0 comments on commit 30bc50a

Please sign in to comment.