Skip to content
Prev 8314 / 15274 Next

Sharpe's algorithm for portfolio improvement

Patrick Burns wrote:
Thank you, Patrick, for this suggestion.  I tried implementing it in 
DEoptim by writing the function to be minimized as shown below.  This 
code is intended to handle a long-only portfolio of 31 assets.  The 
variable x is meant to be a vector representing the shares of the assets 
in the portfolio. The Data matrix has dimensions 20x31. Each row 
corresponds a time period (six months for all but the first period, 
which is only four months long).  Each column corresponds to an asset. 
The elements of the matrix are gross returns. (The gross returns for the 
short first period are raised to the 3/2 power to make them comparable 
to those for the other periods.)  My attempt to rescale the variables to 
assure that they add up to one can be found 5 lines from the bottom of 
the code below.

Neu31<- function(x){
x1 <- x[1]
x2 <- x[2]
x3 <- x[3]
x4 <- x[4]
x5 <- x[5]
x6 <- x[6]
x7 <- x[7]
x8 <- x[8]
x9 <- x[9]
x10 <- x[10]
x11 <- x[11]
x12 <- x[12]
x13 <- x[13]
x14 <- x[14]
x15 <- x[15]
x16 <- x[16]
x17 <- x[17]
x18 <- x[18]
x19 <- x[19]
x20 <- x[20]
x21 <- x[21]
x22 <- x[22]
x23 <- x[23]
x24 <- x[24]
x25 <- x[25]
x26 <- x[26]
x27 <- x[27]
x28 <- x[28]
x29 <- x[29]
x30 <- x[30]
x31 <- x[31]
if (sum(x) == 0) {x <- x + 1e-2}
x = x/sum(x)
grp <- Data%*%x
  u <- grp/(.5 + grp)
objfun <- -sum(u)/nr
return(objfun)
}

To create initial shares summing to one, I used the following code:
npar = 31
NP = 10*npar
rum <- matrix(runif(npar*NP), ncol=npar, nrow=NP)
rowsums <- rowSums(rum)
ip <- rum/rowsums

Finally I invoked DEoptim as follows:
out <- DEoptim(Neu31, lower, upper, control=list(NP=NP, initial=ip, 
itermax=5000, F=.8))

That started the iterations, which progressed plausibly until getting 
stuck at iteration 4055.  At that point my computer became unresponsive 
and remained so until I rebooted.

Any suggestions for improving the code to obtain convergence would be 
greatly appreciated.

Best regards,
John