What is the difference between ** optimize ** and ** uniroot **?

I looked at the descriptions for uniroot and optimized them, and they are slightly different from each other, but the link to the book is the same, and I wonder if there are reasons to choose one after the other?

+5
source share
2 answers

Two functions have completely different goals:

  • optimize used to find the minimum (or maximum) of a function.

  • uniroot used to find the root (zero) of a function.

+6
source

For reasons, choose one over the other. In (possibly) in most cases, the person will be more natural. Because @shadow says optimise is for minimizing (or maximizing), and uniroot is for finding zero points.

In some cases, however, you can use either to solve your problem. This is often due to the fact that you can take derivatives, but there may be another way to restructure your problem. The rest of this answer will talk about these cases when you really have a choice and you need to choose between two cases.

For a simple example, we can find the minimum of the function:

 Func = function(x) { (2*x-pi)^2 + exp(1)*x - 18 } 

One way to do this is to use optimize as:

 OResult = optimize(Func, lower = 0, upper = 5) OResult $minimum [1] 1.231011 $objective [1] -14.19195 

Another way is to transform the function by taking the derivative. Since the optimal point is that the derivative is zero, we need a root search algorithm such as uniroot. Thus, the function becomes:

 DerivFunction = function(x) { 4*(2*x-pi) + exp(1) } 

What is optimized:

 UResult = uniroot(DerivFunction , interval = c(0,5)) UResult $root [1] 1.231011 $f.root [1] 4.440892e-16 $iter [1] 2 $init.it [1] NA $estim.prec [1] 6.103516e-05 

Speed

If you choose between the above approaches, you will probably go with optimize for simplicity. There are times when uniroot can be faster.

Using the above example, the optimize function, called the function 7 times, while the unirot only calls the derivative function 5 times. (This can be found by setting the counters in the above functions). This is logical, optimization does not know how minimal the lows are, while uniroot knows to aim for a function value of zero. Thus, uniroot will often know which direction to go along the x axis, while optimization will need to look around more.

If you optimize the benchmark, it is much faster. Thus, in general (if you look at speed), use optimization if it is not a very intensive function, when function calls are more expensive than what the optimization algorithm does (of course, in these cases it is often very difficult to get the form of using uniroot on).

 library(microbenchmark) microbenchmark( optimize(Func, lower = 0, upper = 5), uniroot(DerivFunction, interval = c(0,5)) ) Unit: microseconds expr min lq mean median uq max neval optimize(Func, lower = 0, upper = 5) 18.264 19.785 23.50664 20.7370 23.781 129.365 100 uniroot(DerivFunction, interval = c(0, 5)) 63.161 67.346 74.10322 69.8195 76.858 143.062 100 

Multiple Local Optimization

The optimization function may not find global optimization, but simply local optimization. The same is true for uniroot , which can find local zero, but not others that exist. uniroot is different from optimize , however, because while you are setting the optimization to find the maximum or minimum, uniroot will find the point intersecting the y axis. If you are uniroot for a derivative, then this point can be a minimum or a maximum.

For example, if we have the following function and derivative:

 TwoMinFunction = function(x){ ((x)^4)/4 + x^3 - 3*x^2 - 8*x + 16 } TwoZeroDerivFunction = function(x){ (x^3 + 3*x^2 βˆ’ 6*x βˆ’ 8) } 

This function is shown in the following figure: TwoMinimumFunction

 OResult = optimize(TwoMinFunction, lower = -6, upper = 4) OResult $minimum [1] -4.000001 $objective [1] 8.007817e-12 UResult = uniroot(TwoZeroDerivFunction , interval = c(-6,4)) UResult $root [1] -1 $f.root [1] 0 $iter [1] 1 $init.it [1] NA $estim.prec [1] 5 

What happened here is that the optimizer found one of two lows. On the other hand, the uniroot function detected maxima (since uniroot cannot distinguish between minima and maxima).

+6
source

Source: https://habr.com/ru/post/1213511/


All Articles