Skip to content

Commit

Permalink
Add standard error to stats.h
Browse files Browse the repository at this point in the history
  • Loading branch information
amlalejini committed Oct 20, 2020
1 parent 157a3e5 commit d001dc7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
9 changes: 9 additions & 0 deletions source/tools/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename C>
emp::sfinae_decoy<double, typename C::value_type>
StandardError(C & elements) {
return StandardDeviation(elements) / sqrt(elements.size());
}

/// Count the number of unique elements in a container
template <typename C>
typename std::enable_if<!emp::is_ptr_type<typename C::value_type>::value, int>::type
Expand Down
4 changes: 4 additions & 0 deletions tests/tools/stats.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d001dc7

Please sign in to comment.