rmaximize

In R, given y = f(x), how do I find the global max of y/x?


I have created a curve that shows for each level of spend (X) a unique output in revenue (Y).

The curve is defined by the following (monotone) function:

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

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

  return(res)

}

Where a, b and c are given parameters and should be treated as constant:

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

Now, if we define ROI as:

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

How do I find the exact values of revenue and spend that make ROI max?

I currently use a spend vector of length n that helps me finding approximately the max ROI, but most of the times the result is not 100% exact as the real max ROI can fall between two points sent as input.

I would like to avoid to increase the length of the spend vector as it would increase the calculation time (and it wouldn't guarantee that the solution found is a global max anyway).


Solution

  • By setting ROI as a function, we can use 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