I'm trying to solve the following quadratic programming problem:
minw wTΣw,
s.t. wTe = 1,
st. ‖w‖1 ≤ δ
Where A
is an identity matrix, Sigma
is a covariance matrix and e
is a vector of ones.
The first constraint ensures that the solution adds up to one.
The second constraint ensures that the sum of absolute values (1-norm) of the solution is less than or equal to a certain constant.
I tried to solve this the following way:
library(Rsolnp)
#Generate some sample data
N=100
sample.data <- replicate(N,rnorm(1000,0,1))
#specify optimization problem
fn<-function(x) {cov.Rt<-cov(sample.data); return(as.numeric(t(x)%*%cov.Rt%*%x))}
#specify equality constraint
eqn<-function(x){one.vec<-matrix(1,ncol=N,nrow=1);return(as.numeric(one.vec%*%x))}
constraints<-1
#specify inequality constraint
ineq<-function(x){one.vec<-matrix(1,ncol=N,nrow=1);
z1<-one.vec%*%abs(x)
return(as.numeric(z1))
}
#specify lower and upper bounds
uh<-2
lb<-1
#specify starting vector of "w"
x0<-matrix(1/N,N,1)
#solve quadratic optimization problem:
control <- list("trace"="0")
sol1<-solnp(pars=x0,fun=fn,eqfun=eqn,eqB=constraints, ineqfun=ineq,ineqUB=uh,ineqLB=lb,control=control)
I would like to know:
Is this solution correct?
Are there alternative (simpler) ways to solve it? The solution using
solnp()
takes forever for larger tasks.