Consider the following data frame:
A=data.frame(v1=c(4,2,-3,3,-1,3,6,-2), v2=c(3,3,-1,5,-3,-2,-2,-3), v3=c(5,-2,2,2,5,5,4,-4),
v4=c(-2,-1,3,1,-1,3,2,-5), v5=c(2,-5,4,-4,3,1,1,1))
with the following optimization problem:
where a_i is the i-th row of matrix A.
I tried to solve this with the package nloptr. First the objective function:
fct <- function(p) {
return(sum((as.matrix(A)%*%p<0)*(as.matrix(A)%*%p)^2))
}
Then the constraint:
constraint <- function(p){
return(p[1]-1)
}
But all solvers that I tried demand a gradient, e.g.:
sol <- nloptr(x0=c(1,1,-0.13,-0.5,1.3), eval_f=fct, eval_g_eq=constraint,
opts=list("algorithm"="NLOPT_LD_SLSQP"))
-> A gradient for the objective function is needed by algorithm NLOPT_LD_SLSQP but was not supplied
Is it possible to calculate the gradient of this function, or are there other ways to solve this problem?
Thank you.
I suspect you can solve this as a standard QP (Quadratic Programming) problem:
min sum(i, y(i)^2 )
y(i) <= sum(j, a(i,j)*p(j))
y(i) <= 0
No need for gradients. QP Solvers like quadprog, Cplex and Gurobi can solve this: just plug the problem in.
The last constraint is just a bound which simplifies things even more.
Related
I've been able to minimize a non-linear objective with a linear constraint using quadprog, however, I haven't been able to do it the other way around...
require(quadprog)
min_var <- function(Obj,Rentabilidades,var_covar){
b <- c(Obj,1)
Betha <- var_covar
A <- t(matrix(rbind(Rentabilidades,c(1,1)),nrow=2))
Gamma <- matrix(0,nrow=2)
solve.QP(Betha,Gamma,A,b,2)
}
Now I want to maximize what previously was the constraint taking as a new constraint the former objective. Regrettably, solve.QP() only supports linear constraints. Does anyone know a package similar to quadprog that might help me?
A standard portfolio optimization model looks like:
min sum((i,j), x(i)*Q(i,j)*x(j))
sum(i,x(i)) = 1
sum(i,r(i)*x(i)) >= R
x(i) >= 0
This is a Quadratic Programming model, and can be solved with standard QP solvers.
If you turn this around (maximize return subject to a risk constraint), you can write:
max sum(i,r(i)*x(i))
sum((i,j), x(i)*Q(i,j)*x(j)) <= V
sum(i,x(i)) = 1
x(i) >= 0
This is now a Quadratically Constrained problem. Luckily this is convex so you can use solvers like Cplex, Gurobi, or Mosek to solve them (they have R interfaces). An open source candidate could be a solver like ECOSolveR, or even better a framework like cxvr.
I am going to use a very simple example to explain my problem (the real problem concerns a very complex univariate function). Consider the following univariate function
f <- function(x, p){ 10 - (x - p)^2 }
where p belongs to {-500, -499,..., -1, 0, 1,..., 499, 500}.
I would like to find the value of x that maximizes f, for each value of p. This translates into 1001 values of x.
I know that one can do that in R-software with i) a for-loop, ii) a while-loop, iii) the function apply, and iv) the function foreach (doParallel package). However, I was wondering if you could tell me whether there is a more efficient way to solve the above optimization problem in R-software, please.
I know the above optimization problem is trivial to solve. However, the question focuses on an efficient procedure to solve several optimization problems simultaneously in R.
Thank you very much for your help.
The function optimize() can be used to get the maximum value of a function.
Then, you can lapply over the values of the parameters as following:
p<-seq(-500,500,1)
fn <- function(param,p,...){return( 10 - (param - p)^2) }
ll <- lapply(pp,function(i)
optimize(f= fn,
par =c(-1000),
p=i,
interval = c(-100000000,1000000000),
maximum = TRUE))
ll[[which.max(sapply(ll,'[[','maximum'))]]
I have a multi-objective optimization problem with both equality and inequality constraints. I tried GA package in R but I didn't figure out how to set equality constraints like the GA in Matlab. Could anybody give some suggestions about which algorithm could I use in R and how to solve that. Thanks a lot. The problem is like below:
Objective functions(I would like to find a set of w1,w2,w3,w4,w5 to minimize f1, and at the same time I want to make f2 as small as possible):
f1<-(w1)^2+4*(w2)^4+3*(w3)^2+5*w4+w5
f2<-((w1)-(w2)^2+3(w3)^2-4*(w4)^3-w5)^2-8
Constraints:
w1+w2+w3+w4+w5=1
0<w1,w2,w3,w4,w5<1
Use normalization so you can drop the equality constraint:
Objectives:
w <- w / sum(w)
f1<-(w1)^2+4*(w2)^4+3*(w3)^2+5*w4+w5
f2<-((w1)-(w2)^2+3(w3)^2-4*(w4)^3-w5)^2-8
Constraints:
0 <= w1,w2,w3,w4,w5 <= 1
I am trying to solve the following inequality constraint:
Given time-series data for N stocks, I am trying to construct a portfolio weight vector to minimize the variance of the returns.
the objective function:
min w^{T}\sum w
s.t. e_{n}^{T}w=1
\left \| w \right \|\leq C
where w is the vector of weights, \sum is the covariance matrix, e_{n}^{T} is a vector of ones, C is a constant. Where the second constraint (\left \| w \right \|) is an inequality constraint (2-norm of the weights).
I tried using the nloptr() function but it gives me an error: Incorrect algorithm supplied. I'm not sure how to select the correct algorithm and I'm also not sure if this is the right method of solving this inequality constraint.
I am also open to using other functions as long as they solve this constraint.
Here is my attempted solution:
data <- replicate(4,rnorm(100))
N <- 4
fn<-function(x) {cov.Rt<-cov(data); return(as.numeric(t(x) %*%cov.Rt%*%x))}
eqn<-function(x){one.vec<-matrix(1,ncol=N,nrow=1); return(-1+as.numeric(one.vec%*%x))}
C <- 1.5
ineq<-function(x){
z1<- t(x) %*% x
return(as.numeric(z1-C))
}
uh <-rep(C^2,N)
lb<- rep(0,N)
x0 <- rep(1,N)
local_opts <- list("algorithm"="NLOPT_LN_AUGLAG,",xtol_rel=1.0e-7)
opts <- list("algorithm"="NLOPT_LN_AUGLAG,",
"xtol_rel"=1.0e-8,local_opts=local_opts)
sol1<-nloptr(x0,eval_f=fn,eval_g_eq=eqn, eval_g_ineq=ineq,ub=uh,lb=lb,opts=opts)
This looks like a simple QP (Quadratic Programming) problem. It may be easier to use a QP solver instead of a general purpose NLP (NonLinear Programming) solver (no need for derivatives, functions etc.). R has a QP solver called quadprog. It is not totally trivial to setup a problem for quadprog, but here is a very similar portfolio example with complete R code to show how to solve this. It has the same objective (minimize risk), the same budget constraint and the lower and upper-bounds. The example just has an extra constraint that specifies a minimum required portfolio return.
Actually I misread the question: the second constraint is ||x|| <= C. I think we can express the whole model as:
This actually looks like a convex model. I could solve it with "big" solvers like Cplex,Gurobi and Mosek. These solvers support convex Quadratically Constrained problems. I also believe this can be formulated as a cone programming problem, opening up more possibilities.
Here is an example where I use package cccp in R. cccp stands for
Cone Constrained Convex Problems and is a port of CVXOPT.
The 2-norm of weights doesn't make sense. It has to be the 1-norm. This is essentially a constraint on the leverage of the portfolio. 1-norm(w) <= 1.6 implies that the portfolio is at most 130/30 (Sorry for using finance language here). You want to read about quadratic cones though. w'COV w = w'L'Lw (Cholesky decomp) and hence w'Cov w = 2-Norm (Lw)^2. Hence you can introduce the linear constraint y - Lw = 0 and t >= 2-Norm(Lw) [This defines a quadratic cone). Now you minimize t. The 1-norm can also be replaced by cones as abs(x_i) = sqrt(x_i^2) = 2-norm(x_i). So introduce a quadratic cone for each element of the vector x.
I am trying to solve the following inequality constraint:
Given time-series data for N stocks, I am trying to construct a portfolio weight vector to minimize the variance of the returns.
the objective function:
min w^{T}\sum w
s.t. e_{n}^{T}w=1
\left \| w \right \|\leq C
where w is the vector of weights, \sum is the covariance matrix, e_{n}^{T} is a vector of ones, C is a constant. Where the second constraint (\left \| w \right \|) is an inequality constraint (2-norm of the weights).
I tried using the nloptr() function but it gives me an error: Incorrect algorithm supplied. I'm not sure how to select the correct algorithm and I'm also not sure if this is the right method of solving this inequality constraint.
I am also open to using other functions as long as they solve this constraint.
Here is my attempted solution:
data <- replicate(4,rnorm(100))
N <- 4
fn<-function(x) {cov.Rt<-cov(data); return(as.numeric(t(x) %*%cov.Rt%*%x))}
eqn<-function(x){one.vec<-matrix(1,ncol=N,nrow=1); return(-1+as.numeric(one.vec%*%x))}
C <- 1.5
ineq<-function(x){
z1<- t(x) %*% x
return(as.numeric(z1-C))
}
uh <-rep(C^2,N)
lb<- rep(0,N)
x0 <- rep(1,N)
local_opts <- list("algorithm"="NLOPT_LN_AUGLAG,",xtol_rel=1.0e-7)
opts <- list("algorithm"="NLOPT_LN_AUGLAG,",
"xtol_rel"=1.0e-8,local_opts=local_opts)
sol1<-nloptr(x0,eval_f=fn,eval_g_eq=eqn, eval_g_ineq=ineq,ub=uh,lb=lb,opts=opts)
This looks like a simple QP (Quadratic Programming) problem. It may be easier to use a QP solver instead of a general purpose NLP (NonLinear Programming) solver (no need for derivatives, functions etc.). R has a QP solver called quadprog. It is not totally trivial to setup a problem for quadprog, but here is a very similar portfolio example with complete R code to show how to solve this. It has the same objective (minimize risk), the same budget constraint and the lower and upper-bounds. The example just has an extra constraint that specifies a minimum required portfolio return.
Actually I misread the question: the second constraint is ||x|| <= C. I think we can express the whole model as:
This actually looks like a convex model. I could solve it with "big" solvers like Cplex,Gurobi and Mosek. These solvers support convex Quadratically Constrained problems. I also believe this can be formulated as a cone programming problem, opening up more possibilities.
Here is an example where I use package cccp in R. cccp stands for
Cone Constrained Convex Problems and is a port of CVXOPT.
The 2-norm of weights doesn't make sense. It has to be the 1-norm. This is essentially a constraint on the leverage of the portfolio. 1-norm(w) <= 1.6 implies that the portfolio is at most 130/30 (Sorry for using finance language here). You want to read about quadratic cones though. w'COV w = w'L'Lw (Cholesky decomp) and hence w'Cov w = 2-Norm (Lw)^2. Hence you can introduce the linear constraint y - Lw = 0 and t >= 2-Norm(Lw) [This defines a quadratic cone). Now you minimize t. The 1-norm can also be replaced by cones as abs(x_i) = sqrt(x_i^2) = 2-norm(x_i). So introduce a quadratic cone for each element of the vector x.