In R, I am using the function optim() to find the minimum of an objective function of two variables. The real objective functions I'm working with are quite complex, so I tried to familiarize myself with the a simpler objective function. The simplest way to run optim() is optim(par,function) where par is a vector of initial values for the algorithm. I find that the answer I get depends heavily on the initial values I input. However, the function I used is so simple, I'm worried that I am misunderstanding either the input or output of optim().
The objective function I am using is:
f <- function(x){
abs(x[1])*abs(x[2])
}
When I run optim(c(-1.2,1),f)
and optim(c(-1.2,10),f)
I get drastically different output for the optimal arguments (par) and the minimum (value). Does anyone have an idea why this would be so?
In this case your objective function has infinitely many optimal points (not necessarily just different local maxima). Anywhere one of the parameters is zero 0 is just as good as any other point where a parameter is near 0. I'm not sure if you were expecting (0,0), but (0,34) has the same value and can also be considered optimal.
![](https://www.manongdao.com/static/images/pcload.jpg)
An associated function is:
g <- function(x, y) abs(x)*abs(y)
We can visualize the levels of the graph with contour
and plot the points given:
A reasonable field given your initial and final conditions (noted from running optim
):
x <- seq(-1.5, 0, by=.1)
y <- seq(0, 11, by=1)
A matrix of the values in g
:
m <- outer(x, y, g)
Plot the results, including the results of optim
. Note that the values at x==0
or y==0
are optimal.
contour(x, y, m)
o1 <- optim(c(-1.2,1),f)$par
o2 <- optim(c(-1.2,10),f)$par
segments(-1.2, 1, o1[1], o1[2], col='red')
segments(-1.2, 10, o2[1], o2[2], col='red')
# Add zero lines as contour does not draw them
segments(0, 11, 0, 0)
segments(-1.5, 0, 0, 0)
This shows a straight line from the initial condition (left side) to the zero of the function (right side). Note that the optimization does not follow a straight line, but this shows that it is reasonable that quite different results will be achieved.
![](https://www.manongdao.com/static/images/pcload.jpg)