Commit 309ca2f9 authored by andibraimllari's avatar andibraimllari Committed by AndiBraimllari
Browse files

add tests for mean, stddev and 95% conf. interval, fix typo

parent 3537d31d
Pipeline #880812 passed with stages
in 21 minutes and 49 seconds
......@@ -101,7 +101,7 @@ namespace elsa
// Sum of product of each element
auto sqSum =
std::inner_product(std::begin(diff), std::end(diff), std::begin(diff), T());
auto stdev = std::sqrt(sqSum / mean);
auto stdev = std::sqrt(sqSum / static_cast<T>(std::size(v)));
return std::make_pair(mean, stdev);
}
......
......@@ -67,7 +67,7 @@ TEST_CASE("Math::heaviside")
TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
{
GIVEN("a DataContainer")
GIVEN("some DataContainers")
{
IndexVector_t sizeVector(2);
sizeVector << 2, 4;
......@@ -92,9 +92,9 @@ TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
InvalidArgumentError);
}
auto meanSqErr = statistics::meanSquaredError<TestType>(dataCont1, dataCont2);
THEN("it produces the correct result")
{
auto meanSqErr = statistics::meanSquaredError<TestType>(dataCont1, dataCont2);
REQUIRE_UNARY(checkApproxEq(meanSqErr, 346.01125f));
}
}
......@@ -108,9 +108,9 @@ TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
InvalidArgumentError);
}
auto relErr = statistics::relativeError<TestType>(dataCont1, dataCont2);
THEN("it produces the correct result")
{
auto relErr = statistics::relativeError<TestType>(dataCont1, dataCont2);
REQUIRE_UNARY(checkApproxEq(relErr, 1.4157718f));
}
}
......@@ -133,6 +133,67 @@ TEST_CASE_TEMPLATE("Math: Testing the statistics", TestType, float, double)
}
}
}
GIVEN("some vectors of numbers")
{
std::vector<TestType> numbers = {5, 0, 14, -4, 8};
std::vector<TestType> manyNumbers = {2, 6, 9, 4, 2, 0, 7, 9, 4, 8, 6, 6,
9, 4, 2, 15, 6, 91, 4, 22, 2, 3, 9, 4,
1, -2, 6, 9, 4, 2, 5, 6, 4, 4, 9};
WHEN("calculating the mean, standard deviation and a 95% confidence interval")
{
auto [mean, stddev] = statistics::calculateMeanStddev(numbers);
THEN("it produces the correct mean and standard deviation")
{
TestType expectedmean = 4.6f;
TestType expectedstddev = 6.2481997f;
REQUIRE_UNARY(checkApproxEq(mean, expectedmean));
REQUIRE_UNARY(checkApproxEq(stddev, expectedstddev));
}
auto [lower, upper] = statistics::confidenceInterval95(numbers.size(), mean, stddev);
THEN("it produces the expected 95% confidence interval")
{
float expectedlower = -12.74778f;
float expectedupper = 21.94778f;
REQUIRE_UNARY(checkApproxEq(lower, expectedlower));
REQUIRE_UNARY(checkApproxEq(upper, expectedupper));
}
}
WHEN("calculating the mean, standard deviation and a 95% confidence interval for more than "
"30 numbers")
{
auto [mean, stddev] = statistics::calculateMeanStddev(manyNumbers);
THEN("it produces the correct mean and standard deviation")
{
TestType expectedmean = 8.0571429f;
TestType expectedstddev = 14.851537f;
REQUIRE_UNARY(checkApproxEq(mean, expectedmean));
REQUIRE_UNARY(checkApproxEq(stddev, expectedstddev));
}
auto [lower, upper] =
statistics::confidenceInterval95(manyNumbers.size(), mean, stddev);
THEN("it produces the expected 95% confidence interval")
{
float expectedlower = -21.05187f;
float expectedupper = 37.16616f;
REQUIRE_UNARY(checkApproxEq(lower, expectedlower));
REQUIRE_UNARY(checkApproxEq(upper, expectedupper));
}
}
}
}
TEST_SUITE_END();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment