【问题标题】:MLE - Optimization with constraints as non-linear functions of the variablesMLE - 将约束作为变量的非线性函数进行优化
【发布时间】:2020-04-29 15:38:14
【问题描述】:

我对以下优化问题有疑问。特别是,我想在 MLE 问题中添加以下约束:(x - location)/scale > 0。如果没有这个约束,LL 是Inf 并且L-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 &lt;- 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】:

    将 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 的问题。有更好的答案吗?

    【讨论】:

      猜你喜欢
      • 2012-05-24
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-15
      • 1970-01-01
      • 2020-04-12
      • 1970-01-01
      相关资源
      最近更新 更多