Skip to content

Commit

Permalink
Add TTestStatistic.
Browse files Browse the repository at this point in the history
  • Loading branch information
ielis committed Sep 4, 2024
1 parent 532e611 commit a9f66b2
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/gpsea/analysis/pscore/stats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ._stats import PhenotypeScoreStatistic
from ._stats import MannWhitneyStatistic
from ._stats import MannWhitneyStatistic, TTestStatistic

__all__ = [
'PhenotypeScoreStatistic',
'MannWhitneyStatistic',
'MannWhitneyStatistic', 'TTestStatistic',
]
26 changes: 25 additions & 1 deletion src/gpsea/analysis/pscore/stats/_stats.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import abc
import typing

from scipy.stats import mannwhitneyu
from scipy.stats import mannwhitneyu, ttest_ind


class PhenotypeScoreStatistic(metaclass=abc.ABCMeta):
Expand Down Expand Up @@ -42,3 +42,27 @@ def compute_pval(
)

return pval


class TTestStatistic(PhenotypeScoreStatistic):
"""
`TTestStatistic` is a wrapper around SciPy's
:func:`~scipy.stats.ttest_ind` function to apply
T test on 2 phenotype scores.
See :ref:`phenotype-score-stats` for an example usage.
"""

def compute_pval(
self,
scores: typing.Collection[typing.Sequence[float]],
) -> float:
assert len(scores) == 2, 'T test only supports 2 categories at this time'

x, y = scores
res = ttest_ind(
a=x, b=y,
alternative='two-sided',
)

return res.pvalue
54 changes: 54 additions & 0 deletions tests/analysis/pscore/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import typing
import pytest

from gpsea.analysis.pscore.stats import MannWhitneyStatistic, TTestStatistic


class TestMannWhitneyStatistic:

@pytest.fixture(scope='class')
def statistic(self) -> MannWhitneyStatistic:
return MannWhitneyStatistic()

@pytest.mark.parametrize(
'x, y, expected',
[
((1., 2., 3., ), (1., 2., 3., ), 1.),
((11., 15, 8., 12.,), (4., 2., 3., 3.5, 4.,), 0.01945103333136247),
]
)
def test_compute_pval(
self,
statistic: MannWhitneyStatistic,
x: typing.Sequence[float],
y: typing.Sequence[float],
expected: float,
):
actual = statistic.compute_pval((x, y))

assert actual == pytest.approx(expected)


class TestTTestStatistic:

@pytest.fixture(scope='class')
def statistic(self) -> TTestStatistic:
return TTestStatistic()

@pytest.mark.parametrize(
'x, y, expected',
[
((1., 2., 3., ), (1., 2., 3., ), 1.),
((11., 15, 8., 12.,), (4., 2., 3., 3.5, 4.,), 0.0004749950471148506),
]
)
def test_compute_pval(
self,
statistic: TTestStatistic,
x: typing.Sequence[float],
y: typing.Sequence[float],
expected: float,
):
actual = statistic.compute_pval((x, y))

assert actual == pytest.approx(expected)

0 comments on commit a9f66b2

Please sign in to comment.