I want to find the maximum in one function. I'll start with one input variable in the function, but the goal is at least 4 variables.
The function is continuous (but for some variables like first variable) if you give it a decimal it will get the same return (example: 99.6,99.8102,100,100.2,100.3,100.4 = 2.28)
In below, I plot the first 500 values (just change VAR1), we can easily see the maximum is x=110 y =3.31
I've tried several approaches, but it seems like something is missing (maybe my knowledge). The goal is to use at least 4 variables in function and find the maximum (of course it is not feasible to draw all the plots and check which is the maximum).
I used optim (below with one variable just to make it easier explain what I want).
optimize(SMA_R, interval=c(1,500), maximum=TRUE,tol=0.0001)
$maximum 111.5218 #Expecting 110
$objective 3.137111
optimize(SMA_R, interval=c(1,100), maximum=TRUE,tol=0.0001)
$maximum 38.81469 #Expecting 35
$objective 2.370557 #Expecting 2.41
optim(par = c(100), fn = SMA_R,control =list(fnscale=-1))
$par 110 #It work as expected
$value 3.314204
optim(par = c(40), fn = SMA_R,control =list(fnscale=-1))
$par 39 #local maximum expected 35
$value 2.370557`
It evident for one variable I can use Brent or Nelder-Mead.
Now adding the second variable
function(x,y) {optim(par = c(x,y), fn = SMA_R,control =list(fnscale=-1))}
l <- list(VAR1 = c(35,110), VAR2 = c(1,1)) #Inputs 35 and 110 both best local points for VAR1.
par value counts convergence message
V1 36.0048828, 0.9589844 3.148596 39, NA 0 NULL
V2 110, 1 3.314204 47, NA 0 NULL
V3 36.0048828, 0.9589844 3.148596 39, NA 0 NULL
V4 110, 1 3.314204 47, NA 0 NULL
Adding this second variable slightly increases the output. I was checking manually for some results to be sure.
After several tries a find better maximum for VAR1=118, VAR2=0.81 RESULT=3.41
If Add more options to VAR2=c(0.5,0.8,1,1.2,1.5), I find the result that I want.
par value counts convergence message
V1 35.0, 0.5 1 5, NA 0 NULL
V2 121.00, -2.25 1 15, NA 0 NULL
V3 37.9257812, 0.9640625 3.140239 33, NA 0 NULL
V4 118.2875977, 0.8107422 3.415413 45, NA 0 NULL
V5 36.0048828, 0.9589844 3.148596 39, NA 0 NULL
V6 110, 1 3.314204 47, NA 0 NULL
V7 35.0, 4.7 0.9415069 7, NA 0 NULL
V8 110.4605713, 0.8374512 3.31747 49, NA 0 NULL
V9 35.0, 1.5 0.9415069 3, NA 0 NULL
V10 110.0, 12.5 0.9415069 7, NA 0 NULL
My question is how can you be sure that this is the best Maximum? Maybe there's a better one and I don't give all the combinations to find the maximum.
Given more options I have V16 with 3.8 a huge increase.
VAR1 = c(20,35,50,70,110,250)
VAR2 = c(0.5,0.8,1,1.2,1.5))
par value counts convergence message
V1 20.0, 0.5 1 5, NA 0 NULL
V2 35.0, 0.5 1 5, NA 0 NULL
V3 50.0, 0.5 1 5, NA 0 NULL
V4 70.0, 0.5 1 5, NA 0 NULL
V5 121.00, -2.25 1 15, NA 0 NULL
V6 274.7558594, 0.5976562 2.718835 43, NA 0 NULL
V7 20.0566406, 0.8957031 2.499993 45, NA 0 NULL
V8 37.9257812, 0.9640625 3.140239 33, NA 0 NULL
V9 54.8144531, 0.9367188 3.679734 55, NA 0 NULL
V10 76.7539062, 0.8546875 3.496945 45, NA 0 NULL
V11 118.2875977, 0.8107422 3.415413 45, NA 0 NULL
V12 269.4213867, 0.6291016 2.718835 47, NA 0 NULL
V13 20.8669434, 0.9204102 2.496346 35, NA 0 NULL
V14 36.0048828, 0.9589844 3.148596 39, NA 0 NULL
V15 55.1074219, 0.9414062 3.679734 43, NA 0 NULL
V16 69.1523438, 0.9453125 3.823568 41, NA 0 NULL
V17 110, 1 3.314204 47, NA 0 NULL
V18 262.0971680, 0.6337891 2.718835 45, NA 0 NULL
V19 22.0, -0.8 1 9, NA 0 NULL
V20 35.0, 4.7 0.9415069 7, NA 0 NULL
V21 54.7412109, 0.9363281 3.679734 43, NA 0 NULL
V22 76.2617188, 0.9265625 3.672373 41, NA 0 NULL
V23 110.4605713, 0.8374512 3.31747 49, NA 0 NULL
V24 266.6488528, 0.6399309 2.721007 55, NA 0 NULL
V25 20.0, 1.5 0.9415069 3, NA 0 NULL
V26 35.0, 1.5 0.9415069 3, NA 0 NULL
V27 50.0, 6.5 0.9415069 7, NA 0 NULL
V28 70.0, 8.5 0.9415069 7, NA 0 NULL
V29 110.0, 12.5 0.9415069 7, NA 0 NULL
V30 275.4394531, 0.6210938 2.718835 45, NA 0 NULL
Is there a better way to get the results I want?
Each variable adds a lot of complexity (and processing time) and the results it always depends on my inputs.