【问题标题】:In R, given y = f(x), how do I find the global max of y/x?在 R 中,给定 y = f(x),如何找到 y/x 的全局最大值?
【发布时间】:2023-01-14 02:18:56
【问题描述】:

我创建了一条曲线,显示每个支出水平 (X) 的独特收入产出 (Y)。

曲线由以下(单调)函数定义:

calculate_abc_revenue <- function(a, b, c, spend) {

  res <- ifelse(
    a/(1+b*(spend)^c) >= 0,
    a/(1+b*(spend)^c),
    0
    )

  return(res)

}

其中 abc 是给定参数,应被视为常量:

a0 <- 1303066.36937866
b0 <- 15560519.9999999
c0 <- -1.09001859302511

现在,如果我们将 ROI 定义为:

revenue <- calculate_abc_revenue(a = a0, b = b0, c = c0, spend)
ROI <- revenue/spend

如何找到使 ROI 最大的 revenuespend 的确切值?

我目前使用长度为 n 的花费向量来帮助我找到大约最大投资回报率,但大多数时候结果并非 100% 准确真正的最大投资回报率可以落在作为输入发送的两点之间。

我想避免增加花费向量的长度,因为它会增加计算时间(并且它不能保证找到的解决方案无论如何都是全局最大值)。

【问题讨论】:

  • 使用optim .....
  • 感谢@Roland 的评论,你介意帮我设置解决方案吗?
  • 你的函数不是单调递增的吗?
  • 感谢您的评论,@MarBlo。不一定(它可能发生,但它也可以变平)但这并不重要。重要的是,即使它一直在增加,我们想要最大化的是 y 除以 x 的值。
  • 另外,我认为数值优化器不能严格保证全球的max 但这是一个数学问题而不是编程问题。

标签: r maximize


【解决方案1】:

通过将ROI设置为函数,我们可以使用optimize

ROI <- function(spend) calculate_abc_revenue(a0, b0, c0, spend)/spend

optspend <- optimize(ROI, c(0, 1e12), maximum = TRUE)$maximum
c("optimal spend" = optspend, revenue = calculate_abc_revenue(a0, b0, c0, optspend))
#> optimal spend       revenue 
#>      435274.1      107613.0

【讨论】:

    【解决方案2】:

    这是另一种方法。它使用 unitroot 应用于 ROI 函数 f 的导数。 (只对spend &gt; 0有效) 从这个函数中构建导数,从中创建一个函数。然后这个函数被输入unitroot

    上排的图显示了广泛投资及其衍生产品的投资回报率。下面一行显示了这些的放大版本。

    #| function related to investment (spend) ROI
    f = expression(1 / spend * (a0 / (1 + b0 * spend^c0)))     
    
    #| get the derivative
    ff_2 <- D(f, 'spend')
    #| uniroot
    ff_2 <- function(spend){}
    body(ff_2) <- ff
    res <- uniroot(ff_2, c(1e5, 1e6))
    c("optimal spend" = res$root, revenue = calculate_abc_revenue(spend = res$root))
    # optimal spend       revenue 
    #    435274.1      107613.0 
    


    原始数据

    a0 <- 1303066.36937866
    b0 <- 15560519.9999999
    c0 <- -1.09001859302511
    
    #| Original function
    calculate_abc_revenue <- function(a = a0,b = b0, c = c0,spend) {
      res <- ifelse(
        a/(1+b*(spend)^c) >= 0,
        a/(1+b*(spend)^c),
        0
      )
      return(res)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      相关资源
      最近更新 更多