I am trying to optimize the parameters of a function with a bound using R's optim() function. Here is the function: Function, because mathjax does not work The code snippet below is my implementation of its Log-Likelihood function, which is here
likelihood_levy <- function(params, x){
mu <- params[1]
sigma <- params[2]
n <- length(x)
ans <- ((1.5 * n) * log(sigma)) +
sum(log(1 / (x - mu))) -
(n * log(sqrt(2*pi) * sigma)) -
(0.5 * sigma * sum(1 / (x - mu)))
}
return(ans)
}
optim(c(-10, 2), likelihood_levy, x = sample1, method="L-BFGS-B",
lower = c(min(sample1) - 0.001, 0))
How do I add parameter bounds to the function definition? For example, the first parameter must be lower than x and the second shouldn't be lower than 0.
Note: If I use the "L-BFGS-B" method of optim() function, I get an error that says "L-BFGS-B needs finite values of 'fn'" Any solutions to this problem would be great too!
Thanks!
There are multiple problems:
Fixing all these we have
likelihood_levy <- function(params, x){
mu <- params[1]
sigma <- params[2]
n <- length(x)
ans <- if (mu >= min(x)) -Inf
else ((1.5 * n) * log(sigma)) +
sum(log(1 / (x - mu))) -
(n * log(sqrt(2*pi) * sigma)) -
(0.5 * sigma * sum(1 / (x - mu)))
return(ans)
}
set.seed(123)
sample1 <- rnorm(25)
st <- c(min(sample1) - 0.001, 1)
res <- optim(st, likelihood_levy, x = sample1, control = list(fnscale = -1))
str(res)
giving
List of 5
$ par : num [1:2] -2.25 1.61
$ value : num -46.6
$ counts : Named int [1:2] 45 NA
..- attr(*, "names")= chr [1:2] "function" "gradient"
$ convergence: int 0
$ message : NULL