optim()中的错误:搜索单变量函数的全局最小值 [英] Error in optim(): searching for global minimum for a univariate function

查看:146
本文介绍了optim()中的错误:搜索单变量函数的全局最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试优化R中的功能

I am trying to optmize a function in R

该函数是仅估计mu参数时负二项式的似然函数.这应该不成问题,因为函数显然只有一个最大值.但是,我无法达到理想的结果.

The function is the Likelihood function of negative binominal when estimating only mu parameter. This should not be a problem since the function clearly has just one point of maximum. But, I am not being able to reach the desirable result.

要优化的功能是:

EMV <- function(data, par) {

    Mi  <- par
    Phi <- 2
    N   <- NROW(data)

    Resultado <- log(Mi/(Mi + Phi))*sum(data) + N*Phi*log(Phi/(Mi + Phi))
    return(Resultado)
}

数据是带有参数2和2的负二项式变量的向量

Data is a vector of negative binomial variables with parameters 2 and 2

data <- rnegbin(10000, mu = 2, theta = 2)

当我使用以下代码绘制具有mu作为变量的函数时:

When I plot the function having mu as variable with the following code:

x <- seq(0.1, 100, 0.02)
z <- EMV(data,0.1)
for (aux in x) {z <- rbind(z, EMV(data,aux))}
z <- z[2:NROW(z)]
plot(x,z)

我得到以下曲线:

z的最大值接近参数值-> 2

And the maximum value of z is close to parameter value --> 2

x[which.max(z)]

但是优化不适用于BFGS

But the optimization is not working with BFGS

Error in optim(par = theta, fn = EMV, data = data, method = "BFGS") : 
non-finite finite-difference value [1]

例如,使用SANN并不能达到正确的价值

And is not going to right value using SANN, for example:

$par
[1] 5.19767e-05

$value
[1] -211981.8

$counts
function gradient 
   10000       NA 

$convergence
[1] 0

$message
NULL

问题是:

  1. 我在做什么错?
  2. 是否有办法告诉optim参数应该大于0?
  3. 是否可以告诉optim我要最大化该功能? (恐怕optim会尝试将其最小化,并会在函数返回最小值的情况下将其设置为一个很小的值)
  1. What am I doing wrong?
  2. Is there a way to tell optim that the param should be bigger than 0?
  3. Is there a way to tell optim that I want to maximize the function? (I am afraid the optim is trying to minimize and is going to a very small value where function returns smallest values)

推荐答案

最小化还是最大化?

尽管?optim表示它可以最大化,但这在方括号中,因此最小化是默认设置:

Although ?optim says it can do maximization, but that is in a bracket, so minimization is default:

fn: A function to be minimized (or maximized) ...

因此,如果要最大化目标函数,需要将-1乘以它,然后将其最小化.这是很常见的情况.在统计数据中,我们通常希望找到最大的对数可能性,因此使用optim()别无选择,只能将负的对数可能性最小化.

Thus, if we want to maximize an objective function, we need to multiply an -1 to it, and then minimize it. This is quite a common situation. In statistics we often want to find maximum log likelihood, so to use optim(), we have no choice but to minimize the negative log likelihood.

使用哪种方法?

如果仅执行一维最小化,则应使用方法"Brent".此方法使我们可以指定搜索区域的下限和上限.搜索将从一个边界开始,然后向另一个边界搜索,直到达到最小值或到达边界.这样的规范可以帮助您约束参数.例如,您不希望mu小于0,而只需设置lower = 0.

If we only do 1D minimization, we should use method "Brent". This method allows us to specify a lower bound and an upper bound of search region. Searching will start from one bound, and search toward the other, until it hit the minimum, or it reach the boundary. Such specification can help you to constrain your parameters. For example, you don't want mu to be smaller than 0, then just set lower = 0.

当我们移至2D或更高尺寸时,应诉诸"BFGS".在这种情况下,如果要将一个参数(例如a)约束为正数,则需要采用对数变换log_a = log(a),并使用log_a重新设置目标函数的参数.现在,log_a没有约束.当我们希望将多个参数约束为正数时,情况也是如此.

When we move to 2D or higher dimension, we should resort to "BFGS". In this case, if we want to constrain one of our parameters, say a, to be positive, we need to take log transform log_a = log(a), and reparameterize our objective function using log_a. Now, log_a is free of constraint. The same goes when we want constrain multiple parameters to be positive.

如何更改代码?

EMV <- function(data, par) {

    Mi  <- par
    Phi <- 2
    N   <- NROW(data)

    Resultado <- log(Mi/(Mi + Phi))*sum(data) + N*Phi*log(Phi/(Mi + Phi))
    return(-1 * Resultado)
}

optim(par = theta, fn = EMV, data = data, method = "Brent", lower = 0, upper = 1E5)

这篇关于optim()中的错误:搜索单变量函数的全局最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆