Skip to content

Safeguard if computation of (numerical) gradient or Hessian fails #5

@loelschlaeger

Description

@loelschlaeger

Implement a safeguard if computation of (numerical) gradient or Hessian fails or does not fit within expectations. For example corresponding structure with NAs. Example case:

R> normal_mixture_llk <- function(mu, sigma, lambda, data) {
+   sigma <- exp(sigma)
+   lambda <- plogis(lambda)
+   sum(log(lambda * dnorm(data, mu[1], sigma[1]) +
+             (1 - lambda) * dnorm(data, mu[2], sigma[2])))
+ }
R> objective_object <- Objective$new(
+   f = normal_mixture_llk, 
+   target = c("mu", "sigma", "lambda"),
+   npar = c(2, 2, 1),
+   data = faithful$eruptions
+ )
R> bad <- c(-2L, -3L, -5L, -3L, -1L)
R> objective_object$evaluate_gradient_numeric(.at = bad)
Error in grad.default(func = function (.at, .negate = FALSE, .gradient_as_attribute = FALSE,  : 
  function returns NA at 2e-043e-045e-043e-041e-04 distance from x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions