Abby Spurdle
2020-Mar-13 02:25 UTC
[R] Relatively Simple Maximization Using Optim Doesnt Optimize
> There is nothing in that plot to indicate that the result given by > optim() should be accepted as optimal. The numerical approximation to > the derivative is 0.055851 everywhere in your graphThat wasn't how I intended the plot to be interpreted. By default, the step size (in x) is 1e-5, which seems like a moderate step size. However, at that level, the numerical approximation is very badly behaved. And if the step size is decreased, things get worse. I haven't checked all the technical details of the optim function. But any reliance on numerical approximations of the derivative, have a high chance of running into problems using a function like this.
Duncan Murdoch
2020-Mar-13 03:13 UTC
[R] Relatively Simple Maximization Using Optim Doesnt Optimize
On 12/03/2020 7:25 p.m., Abby Spurdle wrote:>> There is nothing in that plot to indicate that the result given by >> optim() should be accepted as optimal. The numerical approximation to >> the derivative is 0.055851 everywhere in your graph > > That wasn't how I intended the plot to be interpreted. > By default, the step size (in x) is 1e-5, which seems like a moderate step size.optim() uses a much larger one. You can see it if you run this code after yours: production2 <- function(L){ abline(v=L);cat("L=", L, "\n") # Add this in to see L values budget=100000 Lcost=12 Kcost=15 K=(budget-L*Lcost)/Kcost machines=0.05*L^(2/3)*K^(1/3) return(machines) } optim.sol <- optim (1001, production2 ,method="CG", control = list(fnscale=-1) ) You'll get just 3 evaluations within the scale of your plot. They are at L= 1006.536 L= 1006.537 L= 1006.535 It appears to have chosen step size 0.001, not 0.00001. It should be getting adequate accuracy in both 1st and 2nd derivatives. Those little ripples you see in the plot are not relevant.> However, at that level, the numerical approximation is very badly behaved. > And if the step size is decreased, things get worse. > > I haven't checked all the technical details of the optim function. > But any reliance on numerical approximations of the derivative, have a > high chance of running into problems using a function like this. >
Abby Spurdle
2020-Mar-13 03:52 UTC
[R] Relatively Simple Maximization Using Optim Doesnt Optimize
> L= 1006.536 > L= 1006.537 > L= 1006.535 > It appears to have chosen step size 0.001, not 0.00001. It should be > getting adequate accuracy in both 1st and 2nd derivatives. > Those little ripples you see in the plot are not relevant.I'm impressed. But you're still wrong. Try this: --------- #not good R code! v = numeric () production3 <- function(L){ #store in vector v <<- c (v, L) budget=100000 Lcost=12 Kcost=15 K=(budget-L*Lcost)/Kcost machines=0.05*L^(2/3)*K^(1/3) return(machines) } optim.sol <- optim (1001, production3 ,method="CG", control = list(fnscale=-1) ) n = length (v) print (n) plot (1:n ,v, type="l") --------- After 401 iterations (on my computer), the algorithm hasn't converged. And I note it's converging extremely slowly, so I don't see any argument for increasing the number of iterations. And try this: (The first 30 steps). --------- plot (1:30 ,v [1:30], type="l") --------- Little ripples aren't going anywhere...