[Returnanalytics-commits] r2554 - in pkg/PortfolioAnalytics: R sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jul 12 05:12:18 CEST 2013
Author: rossbennett34
Date: 2013-07-12 05:12:18 +0200 (Fri, 12 Jul 2013)
New Revision: 2554
Modified:
pkg/PortfolioAnalytics/R/optimize.portfolio.R
pkg/PortfolioAnalytics/sandbox/testing_DEoptim_cardinality_constraint.R
Log:
modifying optimze.portfolio_v2 for method=DEoptim to pass fn_map to optional fnMap function and force normalize=FALSE in call to constrained_objective
Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R
===================================================================
--- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-12 03:02:10 UTC (rev 2553)
+++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2013-07-12 03:12:18 UTC (rev 2554)
@@ -667,8 +667,9 @@
}
controlDE <- do.call(DEoptim.control, DEcformals)
- # need to modify constrained_objective to accept a portfolio object
- minw = try(DEoptim( constrained_objective_v2, lower=lower[1:N], upper=upper[1:N], control=controlDE, R=R, portfolio=portfolio, nargs = dotargs , ...=...)) # add ,silent=TRUE here?
+ # We are passing fn_map to the optional fnMap function to do the
+ # transformation so we need to force normalize=FALSE in call to constrained_objective
+ minw = try(DEoptim( constrained_objective_v2, lower=lower[1:N], upper=upper[1:N], control=controlDE, R=R, portfolio=portfolio, nargs = dotargs , ...=..., normalize=FALSE, fnMap=function(x) fn_map(x, portfolio=portfolio))) # add ,silent=TRUE here?
if(inherits(minw, "try-error")) { minw=NULL }
if(is.null(minw)){
@@ -679,10 +680,11 @@
if(isTRUE(tmptrace)) trace <- tmptrace
weights <- as.vector(minw$optim$bestmem)
+ # is it necessary to normalize the weights here?
weights <- normalize_weights(weights)
names(weights) <- colnames(R)
- out <- list(weights=weights, objective_measures=constrained_objective_v2(w=weights, R=R, portfolio, trace=TRUE)$objective_measures, out=minw$optim$bestval, call=call)
+ out <- list(weights=weights, objective_measures=constrained_objective_v2(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures, out=minw$optim$bestval, call=call)
if (isTRUE(trace)){
out$DEoutput <- minw
out$DEoptim_objective_results <- try(get('.objectivestorage',pos='.GlobalEnv'),silent=TRUE)
Modified: pkg/PortfolioAnalytics/sandbox/testing_DEoptim_cardinality_constraint.R
===================================================================
--- pkg/PortfolioAnalytics/sandbox/testing_DEoptim_cardinality_constraint.R 2013-07-12 03:02:10 UTC (rev 2553)
+++ pkg/PortfolioAnalytics/sandbox/testing_DEoptim_cardinality_constraint.R 2013-07-12 03:12:18 UTC (rev 2554)
@@ -58,7 +58,7 @@
# Implement a cardinality constraint for max positions with DEoptim
# http://grokbase.com/t/r/r-help/126fsz99gh/r-deoptim-example-illustrating-use-of-fnmap-parameter-for-enforcement-of-cardinality-constraints
-mappingFun <- function(x, max.pos=10) {
+mappingFun <- function(x, max.pos) {
N <- length(x)
num <- N - max.pos
# Two smallest weights are given a value of 0
@@ -66,7 +66,7 @@
x / sum(x)
}
-out2 <- DEoptim(fn = obj, lower=lower, upper=upper, control=controlDE, fnMap=mappingFun)
+out2 <- DEoptim(fn = obj, lower=lower, upper=upper, control=controlDE, fnMap=function(x) mappingFun(x, max.pos=10))
weights2 <- out2$optim$bestmem
weights2 <- weights2 / sum(weights2)
out2$optim$bestval
More information about the Returnanalytics-commits
mailing list