Commit 3bfc3612 authored by Davis King's avatar Davis King

Fixed a bug in running_scalar_covariance that caused the covariance() and

correlation() methods to output the wrong answer if the covariance was
negative.
parent b9f45f37
......@@ -345,13 +345,7 @@ namespace dlib
<< "\n\tthis: " << this
);
T temp = 1/(n-1) * (sum_xy - sum_y*sum_x/n);
// make sure the variance is never negative. This might
// happen due to numerical errors.
if (temp >= 0)
return temp;
else
return 0;
return 1/(n-1) * (sum_xy - sum_y*sum_x/n);
}
T correlation (
......
......@@ -350,6 +350,15 @@ namespace
DLIB_TEST(std::abs(rs2.variance() - rsc1.variance_y()) < 1e-10);
DLIB_TEST(rs2.current_n() == rsc1.current_n());
rsc1.clear();
rsc1.add(1, -1);
rsc1.add(0, 0);
rsc1.add(1, -1);
rsc1.add(0, 0);
rsc1.add(1, -1);
rsc1.add(0, 0);
DLIB_TEST(std::abs(rsc1.covariance() - -0.3) < 1e-10);
}
void test_skewness_and_kurtosis_1()
......
Markdown is supported
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