[R-SIG-Finance] fPortfolio target risk optimization?

Hugh Whelan whelanh at comcast.net
Tue Nov 25 13:50:26 CET 2008


Enrique,

Rather than computing the entire efficient frontier, you could do a binary 
search around your target risk.  This would involve many fewer optimizations 
than computing the entire range.  I have used this approach using solve.QP for 
portfolio optimization problems (but I would think you could adapt it to 
fPortfolio).  See code fragments below:

# Functions we use for optimization
# This function can be used to obtain two values: opt(.....)$solQPw and/or 
opt(.....)$Psd
opt <- function(K, DMAT, DVEC, AMATRIX,BOVEC,MEQ) {
	solQP<-solve.QP(DMAT,K*DVEC,AMATRIX,bvec=BOVEC,meq=MEQ)
	solQPw<-as.array(solQP$solution)
	#Variance of portfolio
	Psd <- (t(solQPw)%*%Dmat%*%(solQPw))^(0.5)
	return(solQPw, Psd)
}

# Very fast way to home in on right k to hit vol target
BinarySrch <- function(STRT, END, TARG, DMAT, DVEC, AMATRIX, BOVEC, MEQ) {
	if (STRT > END) {
		return (-1); # Couldn't find k that meets target
	} else {
		Mid = (STRT + END)/2
		sDiff = opt(Mid, DMAT, DVEC, AMATRIX,BOVEC,MEQ)$Psd - TARG
		Diff = abs(sDiff)
		
		if (Diff < 0.0001) {
			return(Mid);
		} else if (sDiff < 0) {   # add/subtract a constant to Mid to make sure we 
don't do this forever!
		   return(BinarySrch(Mid + 0.00001, END, TARG, DMAT, DVEC, AMATRIX, BOVEC, 
MEQ))
		} else if (sDiff > 0) {
		   return(BinarySrch(STRT, Mid - 0.00001, TARG, DMAT, DVEC, AMATRIX, BOVEC, 
MEQ))
		}
	}
}

# How we use the functions.......
if (opt(500, Dmat, dvec, Amatrix, boVec, meq)$Psd < volTarg) { #if will never 
exceed volTarg
		sol <- rbind(sol, opt(500, Dmat, dvec, Amatrix, boVec, meq)$solQPw)
	} else if (opt(0, Dmat, dvec, Amatrix, boVec, meq)$Psd > volTarg) { #if will 
always exceed volTarg
		sol <- rbind(sol, opt(0, Dmat, dvec, Amatrix, boVec, meq)$solQPw)
	} else {   # else find the right k that gets us to the volTarg
		k = BinarySrch(0, 500, volTarg, Dmat, dvec, Amatrix, boVec, meq)
		sol <- rbind(sol, opt(k, Dmat, dvec, Amatrix, boVec, meq)$solQPw)
	}


Hope that is helpful.

Best,
Hugh
>Hi,
 
>Does anybody know whether is it possible to optimize with fPortfolio (I'm 
>using v260.72 under R 2.7.2) defining the target risk? I know it is possible 
>to set the target return and obtain the minimum variance portfolio, but is it 
>possible to solve the dual problem of setting the target (volatility) risk 
>and getting the point on the efficient frontier that maximizes the return given 
>that risk?
 
>The only way I've found is an indirect one: compute the whole efficient 
>frontier, then select the point with the risk closest to the target. But this 
>seems inefficient and depends on the actual search grid used... 
 
>Hints appreciated. Session info copied below.
 
>Best,
 
>Enrique



More information about the R-SIG-Finance mailing list