r - 如何向 nlm 提供梯度(或 hessian)?

标签 r mathematical-optimization model-fitting

在非线性最小化器函数 stats::nlm 的帮助文件中,它指出参数之一 f 是(强调我的):

the function to be minimized, returning a single numeric value. This should be a function with first argument a vector of the length of p followed by any other arguments specified by the ... argument. If the function value has an attribute called gradient or both gradient and hessian attributes, these will be used in the calculation of updated parameter values.

这是否意味着渐变和粗麻布是由如下命令提供的:

attr(f, 'gradient') <- function(...){...}

这个函数的输入和输出应该是什么?

将此与一般优化器 stats::optim 进行对比:

optim(par, fn, gr = NULL, ...,
  method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN",
             "Brent"), ...)

其中梯度被显式指定为 optim 的参数,示例如下:how to propery specify a gradient function for use in optim() or other optimizer

检查 nlm 的代码内部没有帮助。

最佳答案

这是一个简单的一维示例:

f <- function(x) {
  out <- -exp(-0.5 * x^2)
  attr(out, 'gradient') <- -x * out 
  attr(out, 'hessian') <-  (x^2 - 1) * out
  return(out)
}

nlm(f, 1.3, hessian = TRUE, check.analyticals = TRUE)

这给出:

# $minimum
# [1] -1
# 
# $estimate
# [1] 4.23687e-14
# 
# $gradient
# [1] 4.23687e-14
# 
# $hessian
# [,1]
# [1,]    1
# 
# $code
# [1] 1
# 
# $iterations
# [1] 3

关于r - 如何向 nlm 提供梯度(或 hessian)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42497186/

相关文章:

r - R 中的更高级别函数 - 是否有官方的 compose 运算符或 curry 函数?

R创建没有for循环的新列

r - 结合 R Markdown 和动画包

c# - C# 中的免费优化库

python - 如何建立 RF(随机森林)和 PSO(粒子群优化器)的混合模型以找到产品的最佳折扣?

python - 层序的输入 0 与预期的 ndim=3 层不兼容,发现 ndim=2。收到完整形状 : [None, 1]

r - 标记绘图区域外的线尾

c# - 对于 R# 对精度损失的提示,这是一个很好的解决方案吗?

45度线的R最佳拟合

python - 是否可以在 sklearn 中组合多个部分拟合估计量?