【问题标题】:Can't run LBFGS on R无法在 R 上运行 LBFGS
【发布时间】:2020-06-04 13:43:45
【问题描述】:

我正在尝试在R 上运行lbfgs-algorithm,但我总是得到这样的结果:

L-BFGS optimization terminated with status code = -1001 fx = -0.0119691 

我尝试了不同的设置,但始终得到相同的结果。

TI <- read.csv("Alphabet01-2000.csv", header = TRUE)
TIn <- TI[9:28]
relPreisBew <- TI[8]
x <- vector(mode = "numeric", length = 20L)
i <- integer()
d=0
b=rep.int(0,20)
grad <- function(x, j = 90, n = 100){for(i in j:n)
{b <- b-as.vector(t(TIn[i,]), mode = "numeric")*relPreisBew[i,]/(cosh(sum(x*as.vector(t(TIn[i,]), mode="numeric"))))*cosh(sum(x*as.vector(t(TIn[i,]), mode = "numeric")))}; return(b)}
Profit <- function(x, j = 90, n = 100){for(i in j:n)
{d <- d-tanh(sum(x*as.vector(t(TIn[i,]), mode = "numeric")))*relPreisBew[i,]}; return(d)}
lbfgs.out <- lbfgs(Profit, grad, rep.int(0, 20))

我猜问题是 grad 函数针对某些值返回 NaNs。 我试图通过限制stepsizemax_iterations 来解决这个问题,但没有成功。 relPreisBewTIn 中的值介于 0 和 1 之间,这些 data.frames 的长度为 2956。我尝试重新创建以下算法:https://minerva-access.unimelb.edu.au/bitstream/handle/11343/51750/bitvai_cohn_day_trading.pdf?sequence=1 请参见第 4-7 页。

问题

这是我的实现问题还是因为功能问题?

【问题讨论】:

  • "gradfunction 返回特定值的 nans" 你希望优化器如何处理这个问题?您可能需要使用约束(请参阅包 optimx)。

标签: r mathematical-optimization


【解决方案1】:

我认为你的代码可以重写为

set.seed(1)
TIn         <- as.data.frame(matrix(runif(2956 * 20), 2956, 20))
relPreisBew <- as.data.frame(matrix(runif(2956 *  1), 2956,  1))

grad <- function(x, j = 90, n = 100){
  b <- numeric(20)
  for(i in j:n)
    b <- b - as.numeric(TIn[i,]) * relPreisBew[i,] / 
      cosh(sum(x * as.numeric(TIn[i,])))^2
  b
}
Profit <- function(x, j = 90, n = 100){
  d <- numeric(1)
  for(i in j:n)
    d <- d - tanh(sum(x * as.numeric(TIn[i,]))) * relPreisBew[i,]
  d
}

library(lbfgs)
lbfgs.out <- lbfgs(Profit, grad, numeric(20), invisible = 1)
lbfgs.out
#R> $value
#R> [1] -3.95898
#R> 
#R> $par
#R> [1] 0.6454434 0.4368204 0.8539061 0.7784060 1.2161234 0.9605967 1.1229975 0.3174475 0.5408638 0.9498033 0.5697073 0.7133112 0.8297062 0.5387137
#R> [15] 0.3503997 0.4985034 0.8430027 0.3826048 0.9294685 0.9222503
#R> 
#R> $convergence
#R> [1] 0

这似乎适用于 0 到 1 之间的数据。不过,如果没有实际数据,要查看我是否得到相同的数据或问题是什么并不容易。

我猜问题是梯度函数返回 NaNs 的某些值。

除非它与数据相关,否则我看不出这是如何发生的。如果我没记错的话,cosh 不能返回小于 1 的值,因此梯度中的分子永远不会变为零。

我试图通过限制stepsizemax_iterations 来解决这个问题,但没有成功。

虽然更改 stepsize 可能会有所帮助,但我不确定您对更改 max_iterations 有何期望。

这是我的实现问题还是因为功能问题?

我猜的数据集。但是没有实际数据很难知道。

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多