Skip to content

solve.QP

1 message · Christian Prinoth

#
This is a "not-so-easy" problem, since it involves non-linear constraints. I tried using langrangian multipliers, but even for very simple problems (eg linear objective function) it does not work so well. See an example below, where I try to obtain a long-short portfolio with a maximum exposure to a some "score". The optimal result should give weight 1/(maxL/2) to stocks in the long portfolio, and the opposite of that to the short names, but the procedure only gets somewhat close to that result (see plot).

Probably a genetic-algorithm approach ? la Burns works best in these cases...



nd<-100
scores<-sample(1:nd)
w<-array(0,nd)
mw<-0.2
maxL<-2
NetP<-0.0001

fnc<-function(x,score,l1,l2) {
	sum(x*score)-l1*0.5*sum(x)^2 -l2*0.5*(sum(abs(x))-maxL)^2
}

l1<-0
l2<-0
w<-optim(w,fnc,NULL,method="L-BFGS-B",lower=-mw,upper=mw,control=list(fnscale=-1,maxit=10000),hessian=F,scores,l1,l2)$par

chk<-T
l1<-0
l2<-0
while(chk){
	l1<-l1+200
	out<-optim(w,fnc,NULL,method="L-BFGS-B",lower=-mw,upper=mw,control=list(fnscale=-1,maxit=10000),hessian=F,scores,l1,l2)
	chk<-abs(sum(out$par))>NetP
	w<-out$par
}

chk<-T
l2<-0
while(chk){
	l2<-l2+200
	out<-optim(w,fnc,NULL,method="L-BFGS-B",lower=-mw,upper=mw,control=list(fnscale=-1,maxit=10000),hessian=F,scores,l1,l2)
	chk<-sum(abs(out$par))>maxL | abs(sum(out$par))>NetP
	w<-out$par
}

round(out$par,4)
c(out$value,sum(out$par*scores))
c(l1,l2,out$counts)
c(out$convergence,out$message)
round(sum(out$par),6)
round(sum(abs(out$par)),6)
plot(sort(out$par))



Christian Prinoth
cp at epsilonsgr.it
+39-0288102355