Skip to content

Commit

Permalink
feat: support for linex loss (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke authored Aug 13, 2024
1 parent 787c584 commit 92bed7e
Show file tree
Hide file tree
Showing 56 changed files with 303 additions and 88 deletions.
4 changes: 2 additions & 2 deletions .lintr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
linters: linters_with_defaults(
# lintr defaults: https://github.com/jimhester/lintr#available-linters
# lintr defaults: https://lintr.r-lib.org/reference/default_linters.html
# the following setup changes/removes certain linters
assignment_linter = NULL, # do not force using <- for assignments
object_name_linter = object_name_linter(c("snake_case", "CamelCase")), # only allow snake case and camel case object names
cyclocomp_linter = NULL, # do not check function complexity
commented_code_linter = NULL, # allow code in comments
line_length_linter = line_length_linter(120)
line_length_linter = line_length_linter(120L)
)

1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Collate:
'regr_ape.R'
'regr_bias.R'
'regr_ktau.R'
'regr_linex.R'
'regr_mae.R'
'regr_mape.R'
'regr_maxae.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(gmean)
export(gpr)
export(jaccard)
export(ktau)
export(linex)
export(logloss)
export(mae)
export(mape)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# mlr3measures (development version)

* Added new measure `linex` (Linear-Exponential Loss).

# mlr3measures 0.6.0

* Added binary classification measures `gmean` and `gpr`.
Expand Down
11 changes: 10 additions & 1 deletion R/bibentries.R
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ bibentries = c( # nolint start
pages = "1263--1284",
year = "2009",
doi = "10.1109/TKDE.2008.239"
),
varian_1975 = bibentry("incollection",
title = "A Bayesian Approach to Real Estate Assessment",
author = "Varian, Hal R.",
booktitle = "Studies in Bayesian Econometrics and Statistics: In Honor of Leonard J. Savage",
editor = "Stephen E. Fienberg and Arnold Zellner",
year = "1975",
publisher = "North-Holland",
address = "Amsterdam",
pages = "195--208"
)

) # nolint end
2 changes: 1 addition & 1 deletion R/binary_fbeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#' @inheritParams binary_params
#' @param beta (`numeric(1)`)\cr
#' Parameter to give either precision or recall more weight.
#' Default is 1, resulting in balanced weights.
#' Default is `1`, resulting in balanced weights.
#' @template binary_example
#' @export
fbeta = function(truth, response, positive, beta = 1, na_value = NaN, ...) {
Expand Down
2 changes: 1 addition & 1 deletion R/classif_mbrier.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# 1/n * sum_i sum_j (I_ij - p_ij)^2.
#' }
#' \eqn{I_{ij}}{I_ij} is 1 if observation \eqn{x_i} has true label \eqn{j}, and 0 otherwise.
#' \eqn{p_{ij}{p_ij} is the probability that observation \eqn{x_i} belongs to class \eqn{j}.
#' \eqn{p_{ij}}{p_ij} is the probability that observation \eqn{x_i} belongs to class \eqn{j}.
#'
#' Note that there also is the more common definition of the Brier score for binary
#' classification problems in [bbrier()].
Expand Down
2 changes: 1 addition & 1 deletion R/classif_mcc.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#' `r format_bib("matthews_1975")`
#'
#' @inheritParams classif_params
#' @param positive (`character(1`) Name of the positive class in case of binary classification.
#' @param positive (`character(1)`) Name of the positive class in case of binary classification.
#'
#' @template classif_example
#' @export
Expand Down
40 changes: 40 additions & 0 deletions R/regr_linex.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' @title Linear-Exponential Loss (per observation)
#'
#' @details
#' The Linear-Exponential Loss is defined as \deqn{
#' b ( \exp (t_i - r_i) - a (t_i - r_i) - 1 ),
#' }{
# b(exp(t - r) − a(t - r) − 1),
#' },
#' where \eqn{a \neq 0, b > 0}.
#'
#' @templateVar mid linex
#' @template regr_template
#' @param a (`numeric(1)`)\cr
#' Shape parameter controlling asymmetry.
#' Negative values penalize overestimation more, positive values penalize underestimation more.
#' As `a` approaches 0, the loss resembles squared error loss. Default is `-1`.
#' @param b (`numeric(1)`)\cr
#' Positive scaling factor for the loss. Larger values increase the loss magnitude.
#' Default is `1`.
#'
#' @references
#' `r format_bib("varian_1975")`
#'
#' @inheritParams regr_params
#' @template regr_example
#' @export
linex = function(truth, response, a = -1, b = 1, ...) {
assert_regr(truth, response = response)
assert_number(a)
assert_number(b, lower = 0)
if (a == 0) {
stop("'a' can't be 0")
}
e = truth - response
we = a * e
b * (exp(we) - we - 1)
}

#' @include measures.R
add_measure(linex, "Linear-exponential Loss (per observation)", "regr", 0, Inf, TRUE, aggregated = FALSE)
10 changes: 5 additions & 5 deletions man/acc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/ae.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions man/ape.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions man/bacc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions man/bbrier.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions man/bias.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions man/ce.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion man/confusion_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fbeta.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/fn.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/fp.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gmean.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gpr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions man/ktau.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 92bed7e

Please sign in to comment.