In this long-overdue post, I will show how to optimize a Leverage Space Portfolio with the LSPM package. Please use the comments to let me know what you would like to see next.
Some copious notes before we get to the code:
These examples are based on revision
These examples were run using
The first two examples are taken from Vince, Ralph (2009). The Leverage Space Trading Model. New York: John Wiley & Sons, Inc.
# Load the LSPM package
library(LSPM)
# Multiple strategy example (data found on pp. 84-87)
trades <- cbind(
c(-150,-45.33,-45.33,rep(13,5),rep(79.67,3),136),
c(253,-1000,rep(-64.43,3),253,253,448,rep(-64.43,3),253),
c(533,220.14,220.14,-500,533,220.14,799,220.14,-325,220.14,533,220.14) )
probs <- c(rep(0.076923077,2),0.153846154,rep(0.076923077,9))
# Multiple strategy example (data found on pp. 84-87)
trades <- cbind(
c(-150,-45.33,-45.33,rep(13,5),rep(79.67,3),136),
c(253,-1000,rep(-64.43,3),253,253,448,rep(-64.43,3),253),
c(533,220.14,220.14,-500,533,220.14,799,220.14,-325,220.14,533,220.14) )
probs <- c(rep(0.076923077,2),0.153846154,rep(0.076923077,9))
# Create a Leverage Space Portfolio object
port <- lsp(trades,probs)
# DEoptim parameters (see ?DEoptim)
# NP=30 (10 * number of strategies)
# itermax=100 (maximum number of iterations)
DEctrl <- list(NP=30,itermax=100)
# Unconstrainted Optimal f (results on p. 87)
res <- optimalf(port,control=DEctrl)
# Drawdown-constrained Optimal f (results on p. 137)
# Since horizon=12, this optimization will take about an hour
res <- optimalf(port,probDrawdown,0.1,DD=0.2,horizon=12,calc.max=4,control=DEctrl)
# Ruin-constrained Optimal f
res <- optimalf(port,probRuin,0.1,DD=0.2,horizon=4,control=DEctrl)
# Drawdown-constrained Optimal f
res <- optimalf(port,probDrawdown,0.1,DD=0.2,horizon=4,control=DEctrl)
# DEoptim parameters (see ?DEoptim)
# NP=30 (10 * number of strategies)
# itermax=100 (maximum number of iterations)
DEctrl <- list(NP=30,itermax=100)
# Unconstrainted Optimal f (results on p. 87)
res <- optimalf(port,control=DEctrl)
# Drawdown-constrained Optimal f (results on p. 137)
# Since horizon=12, this optimization will take about an hour
res <- optimalf(port,probDrawdown,0.1,DD=0.2,horizon=12,calc.max=4,control=DEctrl)
# Ruin-constrained Optimal f
res <- optimalf(port,probRuin,0.1,DD=0.2,horizon=4,control=DEctrl)
# Drawdown-constrained Optimal f
res <- optimalf(port,probDrawdown,0.1,DD=0.2,horizon=4,control=DEctrl)
5 comments:
Hi Josh
I am trying to replicate the results on page 87 (as mentioned in your post above) of the LSPM book. I do the following:
> data(port)
> DEctrl <- list(NP=30, itermax=100)
> res <- optimalf(port, control=DEctrl)
The result is :
Iteration: 100 bestvalit: -1.293917 bestmemit: 1.000000 0.000000 0.857813
Page 87 reads:
MktSysA 0.307
MktSysB 0
MktSysC 0.693
Hi Chris,
I mention that in the post; in the last paragraph of text before the code. In short, the optimization algorithms are different and the algorithm in my post is able to find a set of f values that produce a higher GHPR.
Josh,
I am looking to accomplish two things with your R implementation of LSPM:
1. The ability to pickup the f values from a previous search and then continue the search with those values.
2. The ability to stop a search that is in process yet retain the current f values for use.
I have managed to accomplish the former with some rather inefficient code (coding is not my strong point) yet am at a loss as to how one could accomplish the latter:
I accomplish the former by writing the f values to file at the end of a run:
write.csv(results$f, file="OUTPUT [f].csv")
and then pick them up again prior to the next run by repopulating the initialpop with the following (here N=47):
f <- read.csv("OUTPUT [f].csv", header=TRUE, as.is=TRUE, sep=",", dec=".")
initialpop=cbind(rep(f$x[1],NP),rep(f$x[2],NP),rep(f$x[3],NP),rep(f$x[4],NP),rep(f$x[5],NP),rep(f$x[6],NP),rep(f$x[7],NP),rep(f$x[8],NP),rep(f$x[9],NP),rep(f$x[10],NP),
rep(f$x[11],NP),rep(f$x[12],NP),rep(f$x[13],NP),rep(f$x[14],NP),rep(f$x[15],NP),rep(f$x[16],NP),rep(f$x[17],NP),rep(f$x[18],NP),rep(f$x[19],NP),rep(f$x[20],NP),
rep(f$x[21],NP),rep(f$x[22],NP),rep(f$x[23],NP),rep(f$x[24],NP),rep(f$x[25],NP),rep(f$x[26],NP),rep(f$x[27],NP),rep(f$x[28],NP),rep(f$x[29],NP),rep(f$x[30],NP),
rep(f$x[31],NP),rep(f$x[32],NP),rep(f$x[33],NP),rep(f$x[34],NP),rep(f$x[35],NP),rep(f$x[36],NP),rep(f$x[37],NP),rep(f$x[38],NP),rep(f$x[39],NP),rep(f$x[40],NP),
rep(f$x[41],NP),rep(f$x[42],NP),rep(f$x[43],NP),rep(f$x[44],NP),rep(f$x[45],NP),rep(f$x[46],NP),rep(f$x[47],NP))
However would you know how I could accomplish stoping a process that is running yet retain the f values so that they could be used or the process can be restarted again at a latter time if required?
Grant
I created a more elegant solution to (1):
f <- read.csv("OUTPUT [f].csv", header=TRUE, as.is=TRUE, sep=",", dec=".")
for(i in 1:N){initialpop[(i-1)*NP+1] <- f$x[i]}
I am still however wondering how to achieve (2):
2. The ability to stop a search that is in process yet retain the current f values for use.
Hi Grant,
I don't currently know to accomplish your second request. Stopping the optimization requires you interrupt the code. There's probably a way to catch the interrupt and return the current iteration's f values to the caller, but I'm not sure how to do it.
That said, you can set DEoptim.control$trace=TRUE, manually copy/paste the last iteration's f values prior to interrupt, then manually restart the job like you do to solve your first request.
Post a Comment