MLE - 将约束条件作为变量的非线性函数进行优化。

问题描述 投票:0回答:1

我有一个关于以下优化问题的问题。特别是,我想在MLE问题中加入以下约束条件。(x - location)/scale > 0. 如果没有这个约束条件,则LL是 InfL-BGFS-B 优化后给出以下错误

library(PearsonDS)
x <- rpearsonIII(n=1000, shape = 5, location = 6, scale = 7)

dpearson3 <- function (x, shape, location, scale, log = FALSE) 
{
  gscale <- abs(scale)
  ssgn <- sign(scale)
  density <- dgamma(ssgn * (x - location), shape = shape, scale = gscale, log = log)

  return(density)

}

LL3 <- function(theta, x, display)
{
        shape <- as.numeric(theta[1])
        location <- as.numeric(theta[2])
        scale <- as.numeric(theta[3])

        tmp <- -sum(log(dpearson3(x, shape, location, scale, log = FALSE)))

        if (is.na(tmp)) +Inf else tmp
        if(display == 1){print(c(tmp, theta))}
        return(sum(tmp))
}

control.list <- list(maxit = 100000, factr=1e-12, fnscale = 1)

fit <- optim(par = param, 
               fn = LL3, 
               hessian = TRUE, 
               method = "L-BFGS-B",
               lower = c(0,-Inf,-Inf),
               upper = c(Inf,Inf,Inf),
               control = control.list,
               x = x, display = 1)

假设我从 param <- c(100,1000,10)我得到以下错误信息

Error in optim(par = param, fn = LL3, hessian = TRUE, method = "L-BFGS-B", : L-BFGS-B needs finite values of 'fn'

如何解决这个问题?

r optimization constraints mle
1个回答
0
投票

将MLE函数改为

LL3 <- function(theta, x, display){
  shape <- as.numeric(theta[1])
  location <- as.numeric(theta[2])
  scale <- as.numeric(theta[3])

  tmp <- -sum(log(dpearson3(x, shape, location, scale, log = FALSE)))

  if(min((x-location)/scale) < 0) tmp = + 100000000000 # I added this line
  if (is.na(tmp)) +Inf else tmp
  if(display == 1){print(c(tmp, theta))}
  return(tmp)
}

是我能找到的最聪明的办法。这样一来,我就避免了 Inf 问题。有更好的答案吗?

© www.soinside.com 2019 - 2024. All rights reserved.