Skip to content

Commit

Permalink
Tests for regression metrics with weights. (#6729)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roffild authored Feb 25, 2021
1 parent 9da2287 commit 1fa6793
Showing 1 changed file with 111 additions and 10 deletions.
121 changes: 111 additions & 10 deletions tests/cpp/metric/test_elementwise_metric.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ TEST(Metric, DeclareUnifiedTest(RMSE)) {
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 0, 1, 2, 3}),
{ 0, 0, 1, 1}),
0.6403f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
2.8284f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.6708f, 0.001f);
delete metric;
}

Expand All @@ -27,7 +36,18 @@ TEST(Metric, DeclareUnifiedTest(RMSLE)) {
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f},
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}), 0.40632, 1e-4);
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f}),
0.4063f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f},
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f},
{ 0, -1, 1, -9, 9}),
0.6212f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.2f, 0.4f, 0.8f, 1.6f},
{1.0f, 1.0f, 1.0f, 1.0f, 1.0f},
{ 0, 1, 2, 9, 8}),
0.2415f, 1e-4);
delete metric;
}

Expand All @@ -41,6 +61,16 @@ TEST(Metric, DeclareUnifiedTest(MAE)) {
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
0.5f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
8.0f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.54f, 0.001f);
delete metric;
}

Expand All @@ -50,8 +80,20 @@ TEST(Metric, DeclareUnifiedTest(MAPE)) {
metric->Configure({});
ASSERT_STREQ(metric->Name(), "mape");
EXPECT_NEAR(GetMetricEval(metric, {150, 300}, {100, 200}), 0.5f, 1e-10);
EXPECT_NEAR(GetMetricEval(metric, {50, 400, 500, 4000}, {100, 200, 500, 1000}),
EXPECT_NEAR(GetMetricEval(metric,
{50, 400, 500, 4000},
{100, 200, 500, 1000}),
1.125f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{50, 400, 500, 4000},
{100, 200, 500, 1000},
{ -1, 1, 9, -9}),
-26.5f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{50, 400, 500, 4000},
{100, 200, 500, 1000},
{ 1, 2, 9, 8}),
1.3250f, 0.001f);
delete metric;
}

Expand All @@ -63,7 +105,18 @@ TEST(Metric, DeclareUnifiedTest(MPHE)) {
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}), 0.17517f, 1e-4);
{ 0, 0, 1, 1}),
0.1751f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
3.4037f, 1e-4);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.1922f, 1e-4);
delete metric;
}

Expand All @@ -73,10 +126,24 @@ TEST(Metric, DeclareUnifiedTest(LogLoss)) {
metric->Configure({});
ASSERT_STREQ(metric->Name(), "logloss");
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.5f, 1e-17f, 1.0f+1e-17f, 0.9f},
{ 0, 0, 1, 1}),
0.1996f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
1.2039f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
21.9722f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
1.3138f, 0.001f);
delete metric;
}

Expand All @@ -90,6 +157,16 @@ TEST(Metric, DeclareUnifiedTest(Error)) {
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
0.5f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
10.0f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.55f, 0.001f);

EXPECT_ANY_THROW(xgboost::Metric::Create("error@abc", &lparam));
delete metric;
Expand All @@ -106,9 +183,19 @@ TEST(Metric, DeclareUnifiedTest(Error)) {
EXPECT_STREQ(metric->Name(), "error@0.1");
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.2f, 0.1f, 0.2f},
{ 0, 0, 1, 1}),
0.5f, 0.001f);
{-0.1f, -0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
0.25f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{-0.1f, -0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
9.0f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{-0.1f, -0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
0.45f, 0.001f);
delete metric;
}

Expand All @@ -119,8 +206,22 @@ TEST(Metric, DeclareUnifiedTest(PoissionNegLogLik)) {
ASSERT_STREQ(metric->Name(), "poisson-nloglik");
EXPECT_NEAR(GetMetricEval(metric, {0, 1}, {0, 1}), 0.5f, 1e-10);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.2f, 0.1f, 0.2f},
{0.5f, 1e-17f, 1.0f+1e-17f, 0.9f},
{ 0, 0, 1, 1}),
0.6263f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1}),
1.1280f, 0.001f);
1.1019f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ -1, 1, 9, -9}),
13.3750f, 0.001f);
EXPECT_NEAR(GetMetricEval(metric,
{0.1f, 0.9f, 0.1f, 0.9f},
{ 0, 0, 1, 1},
{ 1, 2, 9, 8}),
1.5783f, 0.001f);
delete metric;
}

0 comments on commit 1fa6793

Please sign in to comment.