Skip to content

Commit 15e3604

Browse files
authored
fix(bias): use correct definition
1 parent 14b3dcb commit 15e3604

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# mlr3measures (development version)
2+
* fix: Define bias measures correctly.
23

34
# mlr3measures 1.0.0
45

R/regr_bias.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#'
33
#' @details
44
#' The Bias is defined as \deqn{
5-
#' \frac{1}{n} \sum_{i=1}^n w_i \left( t_i - r_i \right),
5+
#' \frac{1}{n} \sum_{i=1}^n w_i \left( r_i - t_i \right),
66
#' }{
7-
#' weighted.mean(t - r, w),
7+
#' weighted.mean(r - t, w),
88
#' }
99
#' where \eqn{w_i} are normalized sample weights.
1010
#' Good predictions score close to 0.
@@ -17,7 +17,7 @@
1717
#' @export
1818
bias = function(truth, response, sample_weights = NULL, ...) {
1919
assert_regr(truth, response = response)
20-
wmean(truth - response, sample_weights)
20+
wmean(response - truth, sample_weights)
2121
}
2222

2323
#' @include measures.R

R/regr_pbias.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#'
33
#' @details
44
#' The Percent Bias is defined as \deqn{
5-
#' \frac{1}{n} \sum_{i=1}^n w_i \frac{\left( t_i - r_i \right)}{\left| t_i \right|},
5+
#' \frac{1}{n} \sum_{i=1}^n w_i \frac{\left( r_i - t_i \right)}{\left| t_i \right|},
66
#' }{
7-
#' weighted.mean((t - r) / abs(t), w),
7+
#' weighted.mean((r - t) / abs(t), w),
88
#' }
99
#' where \eqn{w_i} are normalized sample weights.
1010
#' Good predictions score close to 0.
@@ -20,7 +20,7 @@ pbias = function(truth, response, sample_weights = NULL, na_value = NaN, ...) {
2020
if (any(abs(truth) < TOL)) {
2121
return(na_value)
2222
}
23-
wmean((truth - response) / abs(truth), sample_weights)
23+
wmean((response - truth) / abs(truth), sample_weights)
2424
}
2525

2626
#' @include measures.R

man/bias.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pbias.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test_regr.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ test_that("trigger all", {
2727

2828
test_that("tests from Metrics", {
2929
expect_equal(bias(1, 1), 0)
30-
expect_equal(bias(c(-1, -100, 17.5), c(0, 0, 0)), mean(c(-1, -100, 17.5)))
30+
expect_equal(bias(c(-1, -100, 17.5), c(0, 0, 0)), mean(c(1, 100, -17.5)))
3131

32-
expect_equal(pbias(c(1, 2, 3), c(1, 3, 2)), mean(c(0, -1 / 2, 1 / 3)))
32+
expect_equal(pbias(c(1, 2, 3), c(1, 3, 2)), mean(c(0, 1 / 2, -1 / 3)))
3333
expect_equal(pbias(c(1, 2, 0), c(1, 2, 1)), NaN)
3434
expect_equal(pbias(0, 0), NaN)
3535
expect_equal(pbias(c(-1.1, 1.1), c(-1, 1)), 0)

0 commit comments

Comments
 (0)