Nothing
#' Psuedo-Huber Loss
#'
#' Calculate the Pseudo-Huber Loss, a smooth approximation of [huber_loss()].
#' Like [huber_loss()], this is less sensitive to outliers than [rmse()].
#'
#' @family numeric metrics
#' @family accuracy metrics
#' @seealso [All numeric metrics][numeric-metrics]
#' @templateVar fn huber_loss_pseudo
#' @template return
#'
#' @inheritParams huber_loss
#'
#' @details
#' Pseudo-Huber loss is a metric that should be
#' `r attr(huber_loss_pseudo, "direction")`d. The output ranges from
#' `r metric_range_chr(huber_loss_pseudo, 1)` to
#' `r metric_range_chr(huber_loss_pseudo, 2)`, with
#' `r metric_optimal(huber_loss_pseudo)` indicating perfect predictions.
#'
#' The formula for Pseudo-Huber loss is:
#'
#' \deqn{L_\delta = \frac{1}{n} \sum_{i=1}^{n} \delta^2 \left( \sqrt{1 + \left(\frac{\text{truth}_i - \text{estimate}_i}{\delta}\right)^2} - 1 \right)}
#'
#' @author James Blair
#'
#' @references
#'
#' Huber, P. (1964). Robust Estimation of a Location Parameter.
#' _Annals of Statistics_, 53 (1), 73-101.
#'
#' Hartley, Richard (2004). Multiple View Geometry in Computer Vision.
#' (Second Edition). Page 619.
#'
#' @template examples-numeric
#' @examples
#' # Using a different value of 'delta'... if you are adding the metric to a
#' # metric set, you can create a new metric function with the updated argument
#' # value:
#'
#' huber_loss_pseudo_2 <- metric_tweak("huber_loss_pseudo_2", huber_loss_pseudo, delta = 2)
#' multi_metrics <- metric_set(huber_loss_pseudo, huber_loss_pseudo_2)
#' multi_metrics(solubility_test, solubility, prediction)
#' @export
huber_loss_pseudo <- function(data, ...) {
UseMethod("huber_loss_pseudo")
}
huber_loss_pseudo <- new_numeric_metric(
huber_loss_pseudo,
direction = "minimize",
range = c(0, Inf)
)
#' @rdname huber_loss_pseudo
#' @export
huber_loss_pseudo.data.frame <- function(
data,
truth,
estimate,
delta = 1,
na_rm = TRUE,
case_weights = NULL,
...
) {
numeric_metric_summarizer(
name = "huber_loss_pseudo",
fn = huber_loss_pseudo_vec,
data = data,
truth = !!enquo(truth),
estimate = !!enquo(estimate),
na_rm = na_rm,
case_weights = !!enquo(case_weights),
# Extra argument for huber_loss_pseudo_impl()
fn_options = list(delta = delta)
)
}
#' @export
#' @rdname huber_loss_pseudo
huber_loss_pseudo_vec <- function(
truth,
estimate,
delta = 1,
na_rm = TRUE,
case_weights = NULL,
...
) {
check_bool(na_rm)
check_number_decimal(delta, min = 0)
check_numeric_metric(truth, estimate, case_weights)
if (na_rm) {
result <- yardstick_remove_missing(truth, estimate, case_weights)
truth <- result$truth
estimate <- result$estimate
case_weights <- result$case_weights
} else if (yardstick_any_missing(truth, estimate, case_weights)) {
return(NA_real_)
}
huber_loss_pseudo_impl(
truth = truth,
estimate = estimate,
delta = delta,
case_weights = case_weights
)
}
huber_loss_pseudo_impl <- function(
truth,
estimate,
delta,
case_weights,
call = caller_env()
) {
check_number_decimal(delta, min = 0, call = call)
a <- truth - estimate
loss <- delta^2 * (sqrt(1 + (a / delta)^2) - 1)
yardstick_mean(loss, case_weights = case_weights)
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.