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.
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: