Using Vector Parameters for Multivariable Optimization in R
Summary
The document shows how to extend an R optimization setup from one parameter to several parameters while continuing to use the optimx package and the L-BFGS-B method. The key change is to represent the starting values as a vector and write the objective function to accept that vector, retrieving each parameter by its position. The lower and upper bounds must also be supplied as vectors, which allows different limits for individual parameters.
The example comes from optimizing a portfolio Sharpe ratio, but the proposed change is about function inputs and optimizer configuration, not a new portfolio method. The answer gives a practical pattern for adapting an existing single-parameter implementation. It does not compare alternative optimization packages, address parameter scaling or local optima, or provide evidence that a multivariable run will find a globally optimal solution; those issues depend on the objective and data.
Key ideas
- The optimx starting values can be supplied as a vector of parameters.
- The objective function must read each parameter from the vector it receives.
- Lower and upper bounds should be vectors aligned with the parameter positions.
- Vector bounds allow parameter-specific constraints in a multivariable optimization.
Tags
Full text
# Multivariable objective function optimization similar to optimx in R
# Multivariable objective function optimization similar to optimx in R
I have an optimization model in R that utilizes a single variable in my objective function. See below:
```
library(optimx)
startx <- 1.25
anstestoptimx<-optimx(startx,fn=testfunc,gr=NULL, hess=NULL, lower=1, upper=1.5, method="L-BFGS-B", itnmax = 50, hessian=FALSE,
control=list(save.failures=TRUE, maximize=TRUE, ndeps= 0.1, factr=0.01, kkt=FALSE, trace=1))
```
I'm not including the code for the objective function 'testfunc' as it is rather long. But it uses one input variable, contains several filtering routines, calculates period returns, and returns a single output (a Sharpe Ratio for a portfolio). As you can see, it utilizs the optimx package and the "L-BFGS-B" method. This code works and optimizes to a reasonable solution.
I would like to expand this objective function to include more than one variable, but do not know what packages exist for multivariable objective functions that are similar to optimx.
Can anyone recommend a package for this need? I believe that "MCO" may be a feasible option, but the documentation for MCO isn't as comprehensive as optimx so I'm not sure it will function in a similar manner.
## Answer by JKupzig (score 1)
https://quant.stackexchange.com/a/68891
Just because others may experience the same problem, here is a short answer to this problem: To optimize a multi-variate problem with optimx (i.e. more than one parameter is optimized) you can create a vector that is passed to the function. So in your case, it would be something like:
```
startVals <- c(1.25, 1.0, ...)
anstestoptimx<-optimx(startVals ,fn=testfunc,gr=NULL, hess=NULL,
lower=c(1,1,...), upper=c(1.5,1.5,...),
method="L-BFGS-B", itnmax = 50, hessian=FALSE,
control=list(save.failures=TRUE, maximize=TRUE,
ndeps= 0.1, factr=0.01, kkt=FALSE, trace=1))
```
where the `testfunc` is a function that uses a vector instead of unique variables, so for example in the function x and y should be referred to like this: `X[1]` (= "x") `X[2]` (= "y").
You have to specify `lower` and `upper` in the same way as vector, e.g. `lower=c(0,0,0,...)` and `upper = c(2,2,2,...)` this enables you also to define parameter-specific bounds.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.