[Returnanalytics-commits] r2224 - in pkg/MPO: R examples
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Aug 9 01:52:10 CEST 2012
Author: jamesleehobbs
Date: 2012-08-09 01:52:10 +0200 (Thu, 09 Aug 2012)
New Revision: 2224
Modified:
pkg/MPO/R/ProportionalCostOpt.R
pkg/MPO/R/TransactionCostOpt.R
pkg/MPO/R/TurnoverOpt.R
pkg/MPO/examples/BackTestingExamples.R
Log:
-backtesting examples
Modified: pkg/MPO/R/ProportionalCostOpt.R
===================================================================
--- pkg/MPO/R/ProportionalCostOpt.R 2012-08-07 08:33:54 UTC (rev 2223)
+++ pkg/MPO/R/ProportionalCostOpt.R 2012-08-08 23:52:10 UTC (rev 2224)
@@ -56,12 +56,16 @@
#need to flip sign for w_sell
constraint.weights.positive[temp.index,]<-
constraint.weights.positive[temp.index,]*-1
- #put left hand side of constraints into constraint matrix
- Amat <- cbind(constraint.sum, constraint.mu.target, constraint.weights.initial,
- constraint.weights.positive)
+
- #right hand side of constraints in this vector
- bvec <- c(1,(1+mu.target),w.initial,rep(0,2*nassets))
+ #put left hand side of constraints into constraint matrix
+ Amat <- cbind(constraint.sum, constraint.mu.target, constraint.weights.initial,
+ constraint.weights.positive)
+
+ #right hand side of constraints in this vector
+ bvec <- c(1,(1+mu.target),w.initial,rep(0,2*nassets))
+ n.eq = 2+ nassets
+
#optional long only constraint
if(long.only == TRUE){
@@ -73,15 +77,17 @@
bvec <- c(bvec,rep(0,nassets))
}
- solution <- solve.QP(Dmat,dvec,Amat,bvec,meq=(2+nassets))
+
+ solution <- solve.QP(Dmat,dvec,Amat,bvec,meq=n.eq)
+
port.var <- solution$value
w.buy <- solution$solution[(nassets+1):(2*nassets)]
w.sell <- solution$solution[(2*nassets+1):(3*nassets)]
w.total <- w.initial + w.buy + w.sell
port.mu <- w.total%*%(mu[1:nassets])
list(w.initial = w.initial, w.buy = w.buy,w.sell=w.sell,
- w.total=w.total, port.var=port.var,port.mu=port.mu)
+ w=w.total, port.var=port.var,port.mu=port.mu)
}
Modified: pkg/MPO/R/TransactionCostOpt.R
===================================================================
--- pkg/MPO/R/TransactionCostOpt.R 2012-08-07 08:33:54 UTC (rev 2223)
+++ pkg/MPO/R/TransactionCostOpt.R 2012-08-08 23:52:10 UTC (rev 2224)
@@ -61,6 +61,7 @@
#step 1: optimize without constraints to determine buys vs sells
#if w*>w.initial c = c
#if w* < w.initial c = -c
+
unconstrained <- UtilityMaximization(returns,lambda,long.only)
w.unconstrained <- unconstrained$w
diff <- w.unconstrained-w.initial
Modified: pkg/MPO/R/TurnoverOpt.R
===================================================================
--- pkg/MPO/R/TurnoverOpt.R 2012-08-07 08:33:54 UTC (rev 2223)
+++ pkg/MPO/R/TurnoverOpt.R 2012-08-08 23:52:10 UTC (rev 2224)
@@ -59,11 +59,13 @@
constraint.turnover, constraint.weights.positive)
#right hand side of constraints in this vector
bvec <- c(1,mu.target,w.initial,-turnover,rep(0,2*nassets))
+ n.eq <- 2+nassets
} else {
#min variance, no target mu
Amat <- cbind(constraint.sum, constraint.weights.initial,
constraint.turnover, constraint.weights.positive)
bvec <- c(1,w.initial,-turnover,rep(0,2*nassets))
+ n.eq <- 1 + nassets
}
#optional long only constraint
@@ -76,11 +78,7 @@
bvec <- c(bvec,rep(0,nassets))
}
- if(!is.null(mu.target)){
- n.eq = 2+nassets
- } else {
- n.eq = 1 + nassets
- }
+
#Note that the first 5 constraints are equality constraints
#The rest are >= constraints, so if you want <= you have to flip
#signs as done above
Modified: pkg/MPO/examples/BackTestingExamples.R
===================================================================
--- pkg/MPO/examples/BackTestingExamples.R 2012-08-07 08:33:54 UTC (rev 2223)
+++ pkg/MPO/examples/BackTestingExamples.R 2012-08-08 23:52:10 UTC (rev 2224)
@@ -1,7 +1,23 @@
+###only 149 data points, so may want to use less than 100 assets
+returns <- cbind(large.cap.returns[,1:5],mid.cap.returns[,1:5])
##backtesting turnover example
-wt.to <- BackTestWeights(large.cap.returns,12,36,FUN=TurnoverOpt,w.initial=rep(1/100,100),turnover=2)
-class(wt.to)
-ret.to <- Return.rebalancing(large.cap.returns, wt.to2)
-charts.PerformanceSummary(ret.to2,main="MV Portfolio, turnover constrained")
+wt.to <- BackTestWeights(returns,1,36,FUN=TurnoverOpt,w.initial=rep(1/10,10),turnover=.75)
+ret.to <- Return.rebalancing(returns, wt.to)
+charts.PerformanceSummary(ret.to,main="MV Portfolio, turnover constrained")
+##backtesting proportional cost example
+wt.pc <- BackTestWeights(returns,1,36,FUN=ProportionalCostOpt,w.initial=rep(1/10,10),tc=.001,mu.target=0.001)
+ret.pc <- Return.rebalancing(returns,wt.pc)
+charts.PerformanceSummary(ret.pc,main="MV Portfolio, Proportional Transaction Costs")
+
+##backtesting fixed transaction cost
+wt.tc <- BackTestWeights(returns,1,36,FUN=TransactionCostOpt,w.initial=rep(1/10,10),c=.01,lambda=1000)
+ret.tc <- Return.rebalancing(returns,wt.tc)
+charts.PerformanceSummary(ret.pc,main="MV Portfolio, Fixed Transaction Costs")
+
+combined <- cbind(ret.to,ret.pc,ret.tc)
+colnames(combined) <- c("Turnover","Proportional Cost","Fixed Cost")
+charts.PerformanceSummary(combined)
+
+
More information about the Returnanalytics-commits
mailing list