Skip to content

Commit

Permalink
Merge pull request #541 from linsword13/cv-stats
Browse files Browse the repository at this point in the history
Add coefficient of variance to summary stats
  • Loading branch information
dapomeroy committed Jun 27, 2024
2 parents d2ba74e + 71fd306 commit 64c1581
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/ramble/ramble/test/util/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
"s",
(4, "repeats", "summary::n_successful_repeats"),
),
(ramble.util.stats.StatsCoefficientOfVariation(), [3], "s", ("NA", "", "summary::cv")),
(ramble.util.stats.StatsCoefficientOfVariation(), [3, -3], "s", ("NA", "", "summary::cv")),
(
ramble.util.stats.StatsCoefficientOfVariation(),
[6.79, 5.6, 4.31, 5.5],
"s",
(0.18, "", "summary::cv"),
),
],
)
def test_stats_for_repeat_foms(statistic, input_values, input_units, output):
Expand Down
22 changes: 22 additions & 0 deletions lib/ramble/ramble/util/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,27 @@ def compute(self, values):
return round(statistics.stdev(values), max_decimal_places(values))


class StatsCoefficientOfVariation(StatsBase):
name = "cv"
min_count = 2

def compute(self, values):
mean = statistics.mean(values)
# Only guard against zero mean.
# While CV isn't particularly meaningful when negative values are present,
# calculate anyway and leave the interpretation to individual experiments.
if not mean:
return "NA"
return round(
statistics.stdev(values) / statistics.mean(values), max_decimal_places(values)
)

def get_unit(self, unit):
# `unit` unused
del unit
return ""


class StatsCountValues(StatsBase):
name = "n_successful_repeats"

Expand All @@ -109,5 +130,6 @@ def get_unit(self, unit):
StatsMedian(),
StatsVar(),
StatsStdev(),
StatsCoefficientOfVariation(),
StatsCountValues(),
]

0 comments on commit 64c1581

Please sign in to comment.