From d001dc74ef8b51fb00c7aafc8ef48d6f69d538b0 Mon Sep 17 00:00:00 2001 From: Alexander Lalejini Date: Tue, 20 Oct 2020 15:43:47 -0400 Subject: [PATCH] Add standard error to stats.h --- source/tools/stats.h | 9 +++++++++ tests/tools/stats.cc | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/source/tools/stats.h b/source/tools/stats.h index b09a4be575..e865142fd5 100644 --- a/source/tools/stats.h +++ b/source/tools/stats.h @@ -111,6 +111,15 @@ namespace emp { return sqrt(Variance(elements)); } + /// Calculate the standard error of the values in a container + /// If values are pointers, they will be automatically de-referenced + /// Values must be numeric. + template + emp::sfinae_decoy + StandardError(C & elements) { + return StandardDeviation(elements) / sqrt(elements.size()); + } + /// Count the number of unique elements in a container template typename std::enable_if::value, int>::type diff --git a/tests/tools/stats.cc b/tests/tools/stats.cc index d86d3a6fec..9ea1985565 100644 --- a/tests/tools/stats.cc +++ b/tests/tools/stats.cc @@ -33,6 +33,10 @@ TEST_CASE("Test stats", "[tools]") { REQUIRE(emp::StandardDeviation(vec2) == Approx(0.5).epsilon(0.001)); REQUIRE(emp::StandardDeviation(deque1) == Approx(1.0488).epsilon(0.001)); + REQUIRE(emp::StandardError(vec1) == Approx(0.3333).epsilon(0.001)); + REQUIRE(emp::StandardError(vec2) == Approx(0.25).epsilon(0.001)); + REQUIRE(emp::StandardError(deque1) == Approx(0.4281).epsilon(0.001)); + REQUIRE(emp::Sum(vec1) == 10); REQUIRE(emp::Sum(vec2) == 5); REQUIRE(emp::Sum(deque1) == 27);