From noreply at r-forge.r-project.org Fri Jan 3 08:09:59 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Fri, 3 Jan 2014 08:09:59 +0100 (CET) Subject: [Returnanalytics-commits] r3296 - pkg/PerformanceAnalytics/R Message-ID: <20140103070959.646311839DE@r-forge.r-project.org> Author: matthieu_lestel Date: 2014-01-03 08:09:58 +0100 (Fri, 03 Jan 2014) New Revision: 3296 Modified: pkg/PerformanceAnalytics/R/MSquared.R Log: correction of a bug in the computation of MSquared Modified: pkg/PerformanceAnalytics/R/MSquared.R =================================================================== --- pkg/PerformanceAnalytics/R/MSquared.R 2013-12-23 06:14:36 UTC (rev 3295) +++ pkg/PerformanceAnalytics/R/MSquared.R 2014-01-03 07:09:58 UTC (rev 3296) @@ -23,7 +23,7 @@ #' @examples #' #' data(portfolio_bacon) -#' print(MSquared(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 0.1068 +#' print(MSquared(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 0.10062 #' #' data(managers) #' print(MSquared(managers['1996',1], managers['1996',8])) @@ -49,7 +49,7 @@ Rp = (prod(1 + Ra))^(Period / length(Ra)) - 1 sigp = sqrt(var(Ra)*(length(Ra)-1)/length(Ra))*sqrt(Period) sigm = sqrt(var(Rb)*(length(Rb)-1)/length(Rb))*sqrt(Period) - result = (Rp - Rf) * sigp / sigm + Rf + result = (Rp - Rf) * sigm / sigp + Rf } else { result = NA From noreply at r-forge.r-project.org Mon Jan 6 15:37:34 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Mon, 6 Jan 2014 15:37:34 +0100 (CET) Subject: [Returnanalytics-commits] r3297 - pkg/PerformanceAnalytics/R Message-ID: <20140106143734.EF845186AAA@r-forge.r-project.org> Author: peter_carl Date: 2014-01-06 15:37:34 +0100 (Mon, 06 Jan 2014) New Revision: 3297 Modified: pkg/PerformanceAnalytics/R/chart.Correlation.R Log: - now passing method into cor.test Modified: pkg/PerformanceAnalytics/R/chart.Correlation.R =================================================================== --- pkg/PerformanceAnalytics/R/chart.Correlation.R 2014-01-03 07:09:58 UTC (rev 3296) +++ pkg/PerformanceAnalytics/R/chart.Correlation.R 2014-01-06 14:37:34 UTC (rev 3297) @@ -46,7 +46,7 @@ txt <- paste(prefix, txt, sep="") if(missing(cex.cor)) cex <- 0.8/strwidth(txt) - test <- cor.test(x,y) + test <- cor.test(x,y, method=method) # borrowed from printCoefmat Signif <- symnum(test$p.value, corr = FALSE, na = FALSE, cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1), From noreply at r-forge.r-project.org Sat Jan 11 00:43:32 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sat, 11 Jan 2014 00:43:32 +0100 (CET) Subject: [Returnanalytics-commits] r3298 - pkg/PortfolioAnalytics/R Message-ID: <20140110234332.B103F1860CF@r-forge.r-project.org> Author: rossbennett34 Date: 2014-01-11 00:43:31 +0100 (Sat, 11 Jan 2014) New Revision: 3298 Modified: pkg/PortfolioAnalytics/R/optFUN.R Log: fixing bug in gmv_opt_toc to get objective value from ROI output. Modified: pkg/PortfolioAnalytics/R/optFUN.R =================================================================== --- pkg/PortfolioAnalytics/R/optFUN.R 2014-01-06 14:37:34 UTC (rev 3297) +++ pkg/PortfolioAnalytics/R/optFUN.R 2014-01-10 23:43:31 UTC (rev 3298) @@ -759,7 +759,7 @@ names(weights) <- colnames(R) out <- list() out$weights <- weights - out$out <- roi.result$value + out$out <- roi.result$objval obj_vals <- list() # Calculate the objective values here so that we can use the moments$mean # and moments$var that might be passed in by the user. From noreply at r-forge.r-project.org Sat Jan 11 22:23:39 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sat, 11 Jan 2014 22:23:39 +0100 (CET) Subject: [Returnanalytics-commits] r3299 - in pkg/PortfolioAnalytics: . R demo Message-ID: <20140111212340.0122C186812@r-forge.r-project.org> Author: rossbennett34 Date: 2014-01-11 22:23:39 +0100 (Sat, 11 Jan 2014) New Revision: 3299 Added: pkg/PortfolioAnalytics/demo/demo_roi_solvers.R Modified: pkg/PortfolioAnalytics/DESCRIPTION pkg/PortfolioAnalytics/R/optFUN.R pkg/PortfolioAnalytics/R/optimize.portfolio.R pkg/PortfolioAnalytics/demo/00Index Log: Minor modifications to optFUN and optimize.portfolio for easy way to specify solver for ROI per discussion with Doug. Modified: pkg/PortfolioAnalytics/DESCRIPTION =================================================================== --- pkg/PortfolioAnalytics/DESCRIPTION 2014-01-10 23:43:31 UTC (rev 3298) +++ pkg/PortfolioAnalytics/DESCRIPTION 2014-01-11 21:23:39 UTC (rev 3299) @@ -24,6 +24,7 @@ ROI (>= 0.1.0), ROI.plugin.glpk (>= 0.0.2), ROI.plugin.quadprog (>= 0.0.2), + ROI.plugin.symphony (>= 0.0.2), pso, GenSA, corpcor Modified: pkg/PortfolioAnalytics/R/optFUN.R =================================================================== --- pkg/PortfolioAnalytics/R/optFUN.R 2014-01-10 23:43:31 UTC (rev 3298) +++ pkg/PortfolioAnalytics/R/optFUN.R 2014-01-11 21:23:39 UTC (rev 3299) @@ -1,8 +1,9 @@ ##### GMV and QU QP Function ##### -#' Optimization function to solve minimum variance or maximum quadratic utility problems +#' GMV/QU QP Optimization #' -#' This function is called by optimize.portfolio to solve minimum variance or maximum quadratic utility problems +#' This function is called by optimize.portfolio to solve minimum variance or +#' maximum quadratic utility problems #' #' @param R xts object of asset returns #' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} @@ -10,9 +11,10 @@ #' @param lambda risk_aversion parameter #' @param target target return value #' @param lambda_hhi concentration aversion parameter -#' @param conc_groups list of vectors specifying the groups of the assets. +#' @param conc_groups list of vectors specifying the groups of the assets. +#' @param solver solver to use #' @author Ross Bennett -gmv_opt <- function(R, constraints, moments, lambda, target, lambda_hhi, conc_groups){ +gmv_opt <- function(R, constraints, moments, lambda, target, lambda_hhi, conc_groups, solver="quadprog"){ stopifnot("package:ROI" %in% search() || require("ROI", quietly = TRUE)) stopifnot("package:ROI.plugin.quadprog" %in% search() || require("ROI.plugin.quadprog", quietly = TRUE)) @@ -119,7 +121,7 @@ # set up the optimization problem and solve opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir.vec, rhs=rhs.vec)) - result <- ROI_solve(x=opt.prob, solver="quadprog") + result <- ROI_solve(x=opt.prob, solver=solver) # result <- try(solve.QP(Dmat=Dmat, dvec=dvec, Amat=t(Amat), bvec=rhs.vec, meq=meq), silent=TRUE) if(inherits(x=result, "try-error")) stop(paste("No solution found:", result)) @@ -152,16 +154,17 @@ } ##### Maximize Return LP Function ##### -#' Optimization function to solve minimum variance or maximum quadratic utility problems +#' Maximum Return LP Optimization #' -#' This function is called by optimize.portfolio to solve minimum variance or maximum quadratic utility problems +#' This function is called by optimize.portfolio to solve maximum return #' #' @param R xts object of asset returns #' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} #' @param moments object of moments computed based on objective functions #' @param target target return value +#' @param solver solver to use #' @author Ross Bennett -maxret_opt <- function(R, moments, constraints, target){ +maxret_opt <- function(R, moments, constraints, target, solver="glpk"){ stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE)) stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE)) @@ -224,7 +227,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir.vec, rhs=rhs.vec), bounds=bnds) - roi.result <- ROI_solve(x=opt.prob, solver="glpk") + roi.result <- ROI_solve(x=opt.prob, solver=solver) # roi.result <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir.vec, rhs=rhs.vec, bounds=bnds) @@ -255,16 +258,18 @@ } ##### Maximize Return MILP Function ##### -#' Optimization function to solve maximum return problems +#' Maximum Return MILP Optimization #' -#' This function is called by optimize.portfolio to solve maximum return problems via mixed integer linear programming. +#' This function is called by optimize.portfolio to solve maximum return +#' problems via mixed integer linear programming. #' #' @param R xts object of asset returns #' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} #' @param moments object of moments computed based on objective functions #' @param target target return value +#' @param solver solver to use #' @author Ross Bennett -maxret_milp_opt <- function(R, constraints, moments, target){ +maxret_milp_opt <- function(R, constraints, moments, target, solver="glpk"){ stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE)) stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE)) @@ -356,7 +361,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir, rhs=rhs), bounds=bnds, types=types) - roi.result <- try(ROI_solve(x=opt.prob, solver="glpk"), silent=TRUE) + roi.result <- try(ROI_solve(x=opt.prob, solver=solver), silent=TRUE) if(inherits(roi.result, "try-error")) stop(paste("No solution found:", roi.result)) # Weights @@ -377,7 +382,7 @@ } ##### Minimize ETL LP Function ##### -#' Optimization function to solve minimum ETL problems +#' Minimum ETL LP Optimization #' #' This function is called by optimize.portfolio to solve minimum ETL problems. #' @@ -386,8 +391,9 @@ #' @param moments object of moments computed based on objective functions #' @param target target return value #' @param alpha alpha value for ETL/ES/CVaR +#' @param solver solver to use #' @author Ross Bennett -etl_opt <- function(R, constraints, moments, target, alpha){ +etl_opt <- function(R, constraints, moments, target, alpha, solver="glpk"){ stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE)) stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE)) @@ -442,7 +448,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir.vec, rhs=rhs.vec), bounds=bnds) - roi.result <- try(ROI_solve(x=opt.prob, solver="glpk"), silent=TRUE) + roi.result <- try(ROI_solve(x=opt.prob, solver=solver), silent=TRUE) if(inherits(x=roi.result, "try-error")) stop(paste("No solution found:", roi.result)) weights <- roi.result$solution[1:N] @@ -473,17 +479,19 @@ } ##### Minimize ETL MILP Function ##### -#' Optimization function to solve minimum ETL problems +#' Minimum ETL MILP Optimization #' -#' This function is called by optimize.portfolio to solve minimum ETL problems via mixed integer linear programming. +#' This function is called by optimize.portfolio to solve minimum ETL problems +#' via mixed integer linear programming. #' #' @param R xts object of asset returns #' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} #' @param moments object of moments computed based on objective functions #' @param target target return value #' @param alpha alpha value for ETL/ES/CVaR +#' @param solver solver to use #' @author Ross Bennett -etl_milp_opt <- function(R, constraints, moments, target, alpha){ +etl_milp_opt <- function(R, constraints, moments, target, alpha, solver="glpk"){ stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE)) stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE)) @@ -589,7 +597,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=tmpAmat, dir=dir, rhs=rhs), bounds=bnds, types=types) - roi.result <- ROI_solve(x=opt.prob, solver="glpk") + roi.result <- ROI_solve(x=opt.prob, solver=solver) # The Rglpk solvers status returns an an integer with status information # about the solution returned: 0 if the optimal solution was found, a @@ -626,9 +634,10 @@ } ##### minimize variance or maximize quadratic utility with turnover constraints ##### -#' Optimization function to solve minimum variance or maximum quadratic utility problems with turnover constraint +#' GMV/QU QP Optimization with Turnover Constraint #' -#' This function is called by optimize.portfolio to solve minimum variance or maximum quadratic utility problems +#' This function is called by optimize.portfolio to solve minimum variance or +#' maximum quadratic utility problems with turnover constraint #' #' @param R xts object of asset returns #' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} @@ -636,8 +645,9 @@ #' @param lambda risk_aversion parameter #' @param target target return value #' @param init_weights initial weights to compute turnover +#' @param solver solver to use #' @author Ross Bennett -gmv_opt_toc <- function(R, constraints, moments, lambda, target, init_weights){ +gmv_opt_toc <- function(R, constraints, moments, lambda, target, init_weights, solver="quadprog"){ # function for minimum variance or max quadratic utility problems stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE)) stopifnot("package:ROI" %in% search() || require("ROI", quietly = TRUE)) @@ -748,7 +758,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir, rhs=rhs)) - roi.result <- try(ROI_solve(x=opt.prob, solver="quadprog"), silent=TRUE) + roi.result <- try(ROI_solve(x=opt.prob, solver=solver), silent=TRUE) if(inherits(roi.result, "try-error")) stop(paste("No solution found:", roi.result)) @@ -779,8 +789,21 @@ return(out) } -# proportional transaction cost constraint -gmv_opt_ptc <- function(R, constraints, moments, lambda, target, init_weights){ +##### minimize variance or maximize quadratic utility with proportional transactioncosts constraints ##### +#' GMV/QU QP Optimization with Proportional Transaction Cost Constraint +#' +#' This function is called by optimize.portfolio to solve minimum variance or +#' maximum quadratic utility problems with proportional transaction cost constraint +#' +#' @param R xts object of asset returns +#' @param constraints object of constraints in the portfolio object extracted with \code{get_constraints} +#' @param moments object of moments computed based on objective functions +#' @param lambda risk_aversion parameter +#' @param target target return value +#' @param init_weights initial weights to compute turnover +#' @param solver solver to use +#' @author Ross Bennett +gmv_opt_ptc <- function(R, constraints, moments, lambda, target, init_weights, solver="quadprog"){ # function for minimum variance or max quadratic utility problems # modifying ProportionalCostOpt function from MPO package stopifnot("package:corpcor" %in% search() || require("corpcor", quietly = TRUE)) @@ -880,7 +903,7 @@ opt.prob <- OP(objective=ROI_objective, constraints=L_constraint(L=Amat, dir=dir, rhs=rhs)) - roi.result <- try(ROI_solve(x=opt.prob, solver="quadprog"), silent=TRUE) + roi.result <- try(ROI_solve(x=opt.prob, solver=solver), silent=TRUE) if(inherits(roi.result, "try-error")) stop(paste("No solution found:", roi.result)) @@ -915,7 +938,7 @@ } -mean_etl_opt <- function(R, constraints, moments, target, alpha, tol=.Machine$double.eps^0.5, maxit=50){ +mean_etl_opt <- function(R, constraints, moments, target, alpha, solver="glpk", tol=.Machine$double.eps^0.5, maxit=50){ # This function returns the target mean return that maximizes mean / etl (i.e. starr) # if all(moments$mean == 0) then the user did not specify mean as an objective, @@ -929,17 +952,17 @@ # Find the maximum return if(!is.null(constraints$max_pos)){ - max_ret <- maxret_milp_opt(R=R, constraints=constraints, moments=moments, target=NA) + max_ret <- maxret_milp_opt(R=R, constraints=constraints, moments=moments, target=NA, solver=solver) } else { - max_ret <- maxret_opt(R=R, moments=moments, constraints=constraints, target=NA) + max_ret <- maxret_opt(R=R, moments=moments, constraints=constraints, target=NA, solver=solver) } max_mean <- as.numeric(-max_ret$out) # Find the starr at the maximum etl portfolio if(!is.null(constraints$max_pos)){ - ub_etl <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=max_mean, alpha=alpha) + ub_etl <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=max_mean, alpha=alpha, solver=solver) } else { - ub_etl <- etl_opt(R=R, constraints=constraints, moments=moments, target=max_mean, alpha=alpha) + ub_etl <- etl_opt(R=R, constraints=constraints, moments=moments, target=max_mean, alpha=alpha, solver=solver) } ub_weights <- matrix(ub_etl$weights, ncol=1) ub_mean <- as.numeric(t(ub_weights) %*% fmean) @@ -954,9 +977,9 @@ # Find the starr at the minimum etl portfolio if(!is.null(constraints$max_pos)){ - lb_etl <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=NA, alpha=alpha) + lb_etl <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=NA, alpha=alpha, solver=solver) } else { - lb_etl <- etl_opt(R=R, constraints=constraints, moments=moments, target=NA, alpha=alpha) + lb_etl <- etl_opt(R=R, constraints=constraints, moments=moments, target=NA, alpha=alpha, solver=solver) } lb_weights <- matrix(lb_etl$weights) lb_mean <- as.numeric(t(lb_weights) %*% fmean) @@ -986,9 +1009,9 @@ # Find the starr at the mean return midpoint new_ret <- (lb_mean + ub_mean) / 2 if(!is.null(constraints$max_pos)){ - mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } else { - mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } # print(mid) mid_weights <- matrix(mid$weights, ncol=1) @@ -1009,9 +1032,9 @@ ub_starr <- mid_starr new_ret <- (lb_mean + ub_mean) / 2 if(!is.null(constraints$max_pos)){ - mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } else { - mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } mid_weights <- matrix(mid$weights, ncol=1) mid_mean <- as.numeric(t(mid_weights) %*% fmean) @@ -1025,9 +1048,9 @@ lb_starr <- mid_starr new_ret <- (lb_mean + ub_mean) / 2 if(!is.null(constraints$max_pos)){ - mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } else { - mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha) + mid <- etl_opt(R=R, constraints=constraints, moments=moments, target=new_ret, alpha=alpha, solver=solver) } mid_weights <- matrix(mid$weights, ncol=1) mid_mean <- as.numeric(t(mid_weights) %*% fmean) @@ -1041,7 +1064,7 @@ return(new_ret) } -max_sr_opt <- function(R, constraints, moments, lambda, target, lambda_hhi, conc_groups, tol=.Machine$double.eps^0.5, maxit=50){ +max_sr_opt <- function(R, constraints, moments, lambda, target, lambda_hhi, conc_groups, solver="quadprog", tol=.Machine$double.eps^0.5, maxit=50){ # This function returns the target mean return that maximizes mean / sd (i.e. sharpe ratio) # get the forecast mean from moments @@ -1061,7 +1084,7 @@ # Calculate the sr at the miminum var portfolio tmpmoments <- moments tmpmoments$mean <- rep(0, length(moments$mean)) - lb_sr <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=NA, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + lb_sr <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=NA, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) lb_weights <- matrix(lb_sr$weights) lb_mean <- as.numeric(t(lb_weights) %*% fmean) lb_sd <- as.numeric(sqrt(t(lb_weights) %*% moments$var %*% lb_weights)) @@ -1079,7 +1102,7 @@ # Find the starr at the mean return midpoint new_ret <- (lb_mean + ub_mean) / 2 - mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) mid_weights <- matrix(mid$weights, ncol=1) mid_mean <- as.numeric(t(mid_weights) %*% fmean) mid_sd <- as.numeric(sqrt(t(mid_weights) %*% moments$var %*% mid_weights)) @@ -1096,7 +1119,7 @@ ub_mean <- mid_mean ub_sr <- mid_sr new_ret <- (lb_mean + ub_mean) / 2 - mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) mid_weights <- matrix(mid$weights, ncol=1) mid_mean <- as.numeric(t(mid_weights) %*% fmean) mid_sd <- as.numeric(sqrt(t(mid_weights) %*% moments$var %*% mid_weights)) @@ -1106,7 +1129,7 @@ lb_mean <- mid_mean lb_sr <- mid_sr new_ret <- (lb_mean + ub_mean) / 2 - mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + mid <- gmv_opt(R=R, constraints=constraints, moments=tmpmoments, lambda=1, target=new_ret, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) mid_weights <- matrix(mid$weights, ncol=1) mid_mean <- as.numeric(t(mid_weights) %*% fmean) mid_sd <- as.numeric(sqrt(t(mid_weights) %*% moments$var %*% mid_weights)) Modified: pkg/PortfolioAnalytics/R/optimize.portfolio.R =================================================================== --- pkg/PortfolioAnalytics/R/optimize.portfolio.R 2014-01-10 23:43:31 UTC (rev 3298) +++ pkg/PortfolioAnalytics/R/optimize.portfolio.R 2014-01-11 21:23:39 UTC (rev 3299) @@ -438,7 +438,7 @@ portfolio=NULL, constraints=NULL, objectives=NULL, - optimize_method=c("DEoptim","random","ROI","ROI_old","pso","GenSA"), + optimize_method=c("DEoptim","random","ROI","pso","GenSA"), search_size=20000, trace=FALSE, ..., rp=NULL, @@ -725,11 +725,11 @@ } ## end case for random - if(optimize_method == "ROI"){ - # This takes in a regular portfolio object and extracts the desired business objectives - # and converts them to matrix form to be inputed into a closed form solver - # retrieve the objectives to minimize, these should either be "var" and/or "mean" - # we can either miniminze variance or maximize quiadratic utility (we will be minimizing the neg. quad. utility) + roi_solvers <- c("ROI", "quadprog", "glpk", "symphony", "ipop", "cplex") + if(optimize_method %in% roi_solvers){ + # This takes in a regular portfolio object and extracts the constraints and + # objectives and converts them for input. to a closed form solver using + # ROI as an interface. moments <- list(mean=rep(0, N)) alpha <- 0.05 if(!is.null(constraints$return_target)){ @@ -737,13 +737,6 @@ } else { target <- NA } - # comment out so concentration aversion can only be specified as an objective - # because it is added to the quadratic objective term for QP problems (minvar and qu) - # if(!is.null(constraints$conc_aversion)){ - # lambda_hhi <- constraints$conc_aversion - #} else { - # lambda_hhi <- 0 - #} lambda <- 1 # list of valid objective names for ROI solvers @@ -802,6 +795,13 @@ } if("var" %in% names(moments)){ + # Set a default solver if optimize_method == "ROI", otherwise assume the + # optimize_method specified by the user is the solver for ROI + if(optimize_method == "ROI"){ + solver <- "quadprog" + } else { + solver <- optimize_method + } # Minimize variance if the only objective specified is variance # Maximize Quadratic Utility if var and mean are specified as objectives if(!is.null(constraints$turnover_target) | !is.null(constraints$ptc)){ @@ -810,14 +810,14 @@ constraints$ptc <- NULL } if(!is.null(constraints$turnover_target) & is.null(constraints$ptc)){ - qp_result <- gmv_opt_toc(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, init_weights=portfolio$assets) + qp_result <- gmv_opt_toc(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, init_weights=portfolio$assets, solver=solver) weights <- qp_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures obj_vals <- qp_result$obj_vals out <- list(weights=weights, objective_measures=obj_vals, opt_values=obj_vals, out=qp_result$out, call=call) } if(!is.null(constraints$ptc) & is.null(constraints$turnover_target)){ - qp_result <- gmv_opt_ptc(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, init_weights=portfolio$assets) + qp_result <- gmv_opt_ptc(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, init_weights=portfolio$assets, solver=solver) weights <- qp_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures obj_vals <- qp_result$obj_vals @@ -827,12 +827,12 @@ # if(hasArg(ef)) ef=match.call(expand.dots=TRUE)$ef else ef=FALSE if(hasArg(maxSR)) maxSR=match.call(expand.dots=TRUE)$maxSR else maxSR=FALSE if(maxSR){ - target <- max_sr_opt(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + target <- max_sr_opt(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) # need to set moments$mean=0 here because quadratic utility and target return is sensitive to returning no solution tmp_moments_mean <- moments$mean moments$mean <- rep(0, length(moments$mean)) } - roi_result <- gmv_opt(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, lambda_hhi=lambda_hhi, conc_groups=conc_groups) + roi_result <- gmv_opt(R=R, constraints=constraints, moments=moments, lambda=lambda, target=target, lambda_hhi=lambda_hhi, conc_groups=conc_groups, solver=solver) weights <- roi_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures obj_vals <- roi_result$obj_vals @@ -846,17 +846,25 @@ } } if(length(names(moments)) == 1 & "mean" %in% names(moments)) { + # Set a default solver if optimize_method == "ROI", otherwise assume the + # optimize_method specified by the user is the solver for ROI + if(optimize_method == "ROI"){ + solver <- "glpk" + } else { + solver <- optimize_method + } + # Maximize return if the only objective specified is mean if(!is.null(constraints$max_pos) | !is.null(constraints$leverage)) { # This is an MILP problem if max_pos is specified as a constraint - roi_result <- maxret_milp_opt(R=R, constraints=constraints, moments=moments, target=target) + roi_result <- maxret_milp_opt(R=R, constraints=constraints, moments=moments, target=target, solver=solver) weights <- roi_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures obj_vals <- roi_result$obj_vals out <- list(weights=weights, objective_measures=obj_vals, opt_values=obj_vals, out=roi_result$out, call=call) } else { # Maximize return LP problem - roi_result <- maxret_opt(R=R, constraints=constraints, moments=moments, target=target) + roi_result <- maxret_opt(R=R, constraints=constraints, moments=moments, target=target, solver=solver) weights <- roi_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures obj_vals <- roi_result$obj_vals @@ -864,6 +872,14 @@ } } if( any(c("CVaR", "ES", "ETL") %in% names(moments)) ) { + # Set a default solver if optimize_method == "ROI", otherwise assume the + # optimize_method specified by the user is the solver for ROI + if(optimize_method == "ROI"){ + solver <- "glpk" + } else { + solver <- optimize_method + } + if(hasArg(ef)) ef=match.call(expand.dots=TRUE)$ef else ef=FALSE if(hasArg(maxSTARR)) maxSTARR=match.call(expand.dots=TRUE)$maxSTARR else maxSTARR=TRUE if(ef) meanetl <- TRUE else meanetl <- FALSE @@ -872,12 +888,12 @@ # Minimize sample ETL/ES/CVaR if CVaR, ETL, or ES is specified as an objective if(length(moments) == 2 & all(moments$mean != 0) & ef==FALSE & maxSTARR){ # This is called by meanetl.efficient.frontier and we do not want that for efficient frontiers, need to have ef==FALSE - target <- mean_etl_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha) + target <- mean_etl_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha, solver=solver) meanetl <- TRUE } if(!is.null(constraints$max_pos)) { # This is an MILP problem if max_pos is specified as a constraint - roi_result <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha) + roi_result <- etl_milp_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha, solver=solver) weights <- roi_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures # obj_vals <- roi_result$obj_vals @@ -888,7 +904,7 @@ out <- list(weights=weights, objective_measures=obj_vals, opt_values=obj_vals, out=roi_result$out, call=call) } else { # Minimize sample ETL/ES/CVaR LP Problem - roi_result <- etl_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha) + roi_result <- etl_opt(R=R, constraints=constraints, moments=moments, target=target, alpha=alpha, solver=solver) weights <- roi_result$weights # obj_vals <- constrained_objective(w=weights, R=R, portfolio, trace=TRUE, normalize=FALSE)$objective_measures # obj_vals <- roi_result$obj_vals @@ -898,6 +914,8 @@ out <- list(weights=weights, objective_measures=obj_vals, opt_values=obj_vals, out=roi_result$out, call=call) } } + # Set here at the end so we get optimize.portfolio.ROI and not optimize.portfolio.{solver} classes + optimize_method <- "ROI" } ## end case for ROI ## case if method=pso---particle swarm @@ -1032,6 +1050,13 @@ #' #' When using GenSA and want to set \code{verbose=TRUE}, instead use \code{trace}. #' +#' If \code{optimize_method="ROI"} is specified, a default solver will be +#' selected based on the optimization problem. The \code{glpk} solver is the +#' default solver for LP and MILP optimization problems. The \code{quadprog} +#' solver is the default solver for QP optimization problems. For example, +#' \code{optimize_method = "quadprog"} can be specified and the optimization +#' problem will be solved via ROI using the quadprog solver. +#' #' The extension to ROI solves a limited type of convex optimization problems: #' \itemize{ #' \item{Maxmimize portfolio return subject leverage, box, group, position limit, target mean return, and/or factor exposure constraints on weights.} @@ -1063,7 +1088,8 @@ #' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization #' @param constraints default=NULL, a list of constraint objects. An object of class v1_constraint' can be passed in here. #' @param objectives default=NULL, a list of objective objects. -#' @param optimize_method one of "DEoptim", "random", "ROI","ROI_old", "pso", "GenSA". For using \code{ROI_old}, need to use a constraint_ROI object in constraints. For using \code{ROI}, pass standard \code{constratint} object in \code{constraints} argument. Presently, ROI has plugins for \code{quadprog} and \code{Rglpk}. +#' @param optimize_method one of "DEoptim", "random", "ROI", "pso", "GenSA". A solver +#' for ROI can also be specified and will be solved using ROI. See Details. #' @param search_size integer, how many portfolios to test, default 20,000 #' @param trace TRUE/FALSE if TRUE will attempt to return additional information on the path or portfolios searched #' @param \dots any other passthru parameters Modified: pkg/PortfolioAnalytics/demo/00Index =================================================================== --- pkg/PortfolioAnalytics/demo/00Index 2014-01-10 23:43:31 UTC (rev 3298) +++ pkg/PortfolioAnalytics/demo/00Index 2014-01-11 21:23:39 UTC (rev 3299) @@ -24,4 +24,5 @@ demo_min_StdDev Demonstrate objective to minimize portfolio standard deviation. demo_min_expected_shortfall Demonstrate objective to minimize expected shortfall. demo_risk_budgets Demonstrate using risk budget objectives. +demo_roi_solvers Demonstrate specifying a solver using ROI Added: pkg/PortfolioAnalytics/demo/demo_roi_solvers.R =================================================================== --- pkg/PortfolioAnalytics/demo/demo_roi_solvers.R (rev 0) +++ pkg/PortfolioAnalytics/demo/demo_roi_solvers.R 2014-01-11 21:23:39 UTC (rev 3299) @@ -0,0 +1,53 @@ + +library(PortfolioAnalytics) +library(ROI) +library(ROI.plugin.quadprog) +library(ROI.plugin.glpk) +library(ROI.plugin.symphony) + +data(edhec) +R <- edhec[, 1:4] +funds <- colnames(R) + +# Set up initial portfolio with basic constraints +init.portf <- portfolio.spec(assets=funds) +init.portf <- add.constraint(portfolio=init.portf, type="full_investment") +init.portf <- add.constraint(portfolio=init.portf, type="long_only") + +# Add objectives +maxret.portf <- add.objective(portfolio=init.portf, type="return", name="mean") +mines.portf <- add.objective(portfolio=init.portf, type="risk", name="ES") +minsd.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev") +qu.portf <- add.objective(portfolio=init.portf, type="risk", name="StdDev", + risk_aversion=0.25) +qu.portf <- add.objective(portfolio=qu.portf, type="return", name="mean") + +# Maximize return +opt.maxret.roi <- optimize.portfolio(R, maxret.portf, optimize_method="ROI") +opt.maxret.glpk <- optimize.portfolio(R, maxret.portf, optimize_method="glpk") +opt.maxret.symphony <- optimize.portfolio(R, maxret.portf, optimize_method="symphony") +all.equal(extractStats(opt.maxret.roi), extractStats(opt.maxret.glpk)) +all.equal(extractStats(opt.maxret.roi), extractStats(opt.maxret.symphony)) +# This fails because an optimization problem with a linear objective cannot +# be solved with a quadratic programming solver +# opt.maxret.qp <- optimize.portfolio(R, maxret.portf, optimize_method="quadprog") + +# Minimize ES +opt.mines.roi <- optimize.portfolio(R, mines.portf, optimize_method="ROI") +opt.mines.glpk <- optimize.portfolio(R, mines.portf, optimize_method="glpk") +opt.mines.symphony <- optimize.portfolio(R, mines.portf, optimize_method="symphony") +all.equal(extractStats(opt.mines.roi), extractStats(opt.mines.glpk)) +all.equal(extractStats(opt.mines.roi), extractStats(opt.mines.symphony)) + +# Minimize standard deviation +opt.minsd.roi <- optimize.portfolio(R, minsd.portf, optimize_method="ROI") +opt.minsd.qp <- optimize.portfolio(R, minsd.portf, optimize_method="quadprog") +all.equal(extractStats(opt.minsd.roi), extractStats(opt.minsd.qp)) +# This fails because an optimization problem with a quadratic objective cannot +# be solved with a linear programming solver +# opt.minsd.glpk <- optimize.portfolio(R, minsd.portf, optimize_method="glpk") + +# Maximize quadratic utility [TRUNCATED] To get the complete diff run: svnlook diff /svnroot/returnanalytics -r 3299 From noreply at r-forge.r-project.org Sat Jan 11 22:26:56 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sat, 11 Jan 2014 22:26:56 +0100 (CET) Subject: [Returnanalytics-commits] r3300 - pkg/PortfolioAnalytics/man Message-ID: <20140111212656.8B54718699C@r-forge.r-project.org> Author: rossbennett34 Date: 2014-01-11 22:26:56 +0100 (Sat, 11 Jan 2014) New Revision: 3300 Added: pkg/PortfolioAnalytics/man/gmv_opt_ptc.Rd Modified: pkg/PortfolioAnalytics/man/etl_milp_opt.Rd pkg/PortfolioAnalytics/man/etl_opt.Rd pkg/PortfolioAnalytics/man/gmv_opt.Rd pkg/PortfolioAnalytics/man/gmv_opt_toc.Rd pkg/PortfolioAnalytics/man/maxret_milp_opt.Rd pkg/PortfolioAnalytics/man/maxret_opt.Rd pkg/PortfolioAnalytics/man/optimize.portfolio.Rd Log: Updating documentation for optimization sub-functions and optimize.portfolio Modified: pkg/PortfolioAnalytics/man/etl_milp_opt.Rd =================================================================== --- pkg/PortfolioAnalytics/man/etl_milp_opt.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/etl_milp_opt.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,8 +1,9 @@ \name{etl_milp_opt} \alias{etl_milp_opt} -\title{Optimization function to solve minimum ETL problems} +\title{Minimum ETL MILP Optimization} \usage{ - etl_milp_opt(R, constraints, moments, target, alpha) + etl_milp_opt(R, constraints, moments, target, alpha, + solver = "glpk") } \arguments{ \item{R}{xts object of asset returns} @@ -16,6 +17,8 @@ \item{target}{target return value} \item{alpha}{alpha value for ETL/ES/CVaR} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve Modified: pkg/PortfolioAnalytics/man/etl_opt.Rd =================================================================== --- pkg/PortfolioAnalytics/man/etl_opt.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/etl_opt.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,8 +1,9 @@ \name{etl_opt} \alias{etl_opt} -\title{Optimization function to solve minimum ETL problems} +\title{Minimum ETL LP Optimization} \usage{ - etl_opt(R, constraints, moments, target, alpha) + etl_opt(R, constraints, moments, target, alpha, + solver = "glpk") } \arguments{ \item{R}{xts object of asset returns} @@ -16,6 +17,8 @@ \item{target}{target return value} \item{alpha}{alpha value for ETL/ES/CVaR} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve Modified: pkg/PortfolioAnalytics/man/gmv_opt.Rd =================================================================== --- pkg/PortfolioAnalytics/man/gmv_opt.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/gmv_opt.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,9 +1,9 @@ \name{gmv_opt} \alias{gmv_opt} -\title{Optimization function to solve minimum variance or maximum quadratic utility problems} +\title{GMV/QU QP Optimization} \usage{ gmv_opt(R, constraints, moments, lambda, target, - lambda_hhi, conc_groups) + lambda_hhi, conc_groups, solver = "quadprog") } \arguments{ \item{R}{xts object of asset returns} @@ -22,6 +22,8 @@ \item{conc_groups}{list of vectors specifying the groups of the assets.} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve Added: pkg/PortfolioAnalytics/man/gmv_opt_ptc.Rd =================================================================== --- pkg/PortfolioAnalytics/man/gmv_opt_ptc.Rd (rev 0) +++ pkg/PortfolioAnalytics/man/gmv_opt_ptc.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -0,0 +1,33 @@ +\name{gmv_opt_ptc} +\alias{gmv_opt_ptc} +\title{GMV/QU QP Optimization with Proportional Transaction Cost Constraint} +\usage{ + gmv_opt_ptc(R, constraints, moments, lambda, target, + init_weights, solver = "quadprog") +} +\arguments{ + \item{R}{xts object of asset returns} + + \item{constraints}{object of constraints in the portfolio + object extracted with \code{get_constraints}} + + \item{moments}{object of moments computed based on + objective functions} + + \item{lambda}{risk_aversion parameter} + + \item{target}{target return value} + + \item{init_weights}{initial weights to compute turnover} + + \item{solver}{solver to use} +} +\description{ + This function is called by optimize.portfolio to solve + minimum variance or maximum quadratic utility problems + with proportional transaction cost constraint +} +\author{ + Ross Bennett +} + Modified: pkg/PortfolioAnalytics/man/gmv_opt_toc.Rd =================================================================== --- pkg/PortfolioAnalytics/man/gmv_opt_toc.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/gmv_opt_toc.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,9 +1,9 @@ \name{gmv_opt_toc} \alias{gmv_opt_toc} -\title{Optimization function to solve minimum variance or maximum quadratic utility problems with turnover constraint} +\title{GMV/QU QP Optimization with Turnover Constraint} \usage{ gmv_opt_toc(R, constraints, moments, lambda, target, - init_weights) + init_weights, solver = "quadprog") } \arguments{ \item{R}{xts object of asset returns} @@ -19,10 +19,13 @@ \item{target}{target return value} \item{init_weights}{initial weights to compute turnover} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve minimum variance or maximum quadratic utility problems + with turnover constraint } \author{ Ross Bennett Modified: pkg/PortfolioAnalytics/man/maxret_milp_opt.Rd =================================================================== --- pkg/PortfolioAnalytics/man/maxret_milp_opt.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/maxret_milp_opt.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,8 +1,9 @@ \name{maxret_milp_opt} \alias{maxret_milp_opt} -\title{Optimization function to solve maximum return problems} +\title{Maximum Return MILP Optimization} \usage{ - maxret_milp_opt(R, constraints, moments, target) + maxret_milp_opt(R, constraints, moments, target, + solver = "glpk") } \arguments{ \item{R}{xts object of asset returns} @@ -14,6 +15,8 @@ objective functions} \item{target}{target return value} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve Modified: pkg/PortfolioAnalytics/man/maxret_opt.Rd =================================================================== --- pkg/PortfolioAnalytics/man/maxret_opt.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/maxret_opt.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -1,8 +1,9 @@ \name{maxret_opt} \alias{maxret_opt} -\title{Optimization function to solve minimum variance or maximum quadratic utility problems} +\title{Maximum Return LP Optimization} \usage{ - maxret_opt(R, moments, constraints, target) + maxret_opt(R, moments, constraints, target, + solver = "glpk") } \arguments{ \item{R}{xts object of asset returns} @@ -14,10 +15,12 @@ objective functions} \item{target}{target return value} + + \item{solver}{solver to use} } \description{ This function is called by optimize.portfolio to solve - minimum variance or maximum quadratic utility problems + maximum return } \author{ Ross Bennett Modified: pkg/PortfolioAnalytics/man/optimize.portfolio.Rd =================================================================== --- pkg/PortfolioAnalytics/man/optimize.portfolio.Rd 2014-01-11 21:23:39 UTC (rev 3299) +++ pkg/PortfolioAnalytics/man/optimize.portfolio.Rd 2014-01-11 21:26:56 UTC (rev 3300) @@ -11,13 +11,13 @@ optimize.portfolio_v2(R, portfolio = NULL, constraints = NULL, objectives = NULL, - optimize_method = c("DEoptim", "random", "ROI", "ROI_old", "pso", "GenSA"), + optimize_method = c("DEoptim", "random", "ROI", "pso", "GenSA"), search_size = 20000, trace = FALSE, ..., rp = NULL, momentFUN = "set.portfolio.moments", message = FALSE) optimize.portfolio(R, portfolio = NULL, constraints = NULL, objectives = NULL, - optimize_method = c("DEoptim", "random", "ROI", "ROI_old", "pso", "GenSA"), + optimize_method = c("DEoptim", "random", "ROI", "pso", "GenSA"), search_size = 20000, trace = FALSE, ..., rp = NULL, momentFUN = "set.portfolio.moments", message = FALSE) } @@ -35,13 +35,9 @@ \item{objectives}{default=NULL, a list of objective objects.} - \item{optimize_method}{one of "DEoptim", "random", - "ROI","ROI_old", "pso", "GenSA". For using - \code{ROI_old}, need to use a constraint_ROI object in - constraints. For using \code{ROI}, pass standard - \code{constratint} object in \code{constraints} argument. - Presently, ROI has plugins for \code{quadprog} and - \code{Rglpk}.} + \item{optimize_method}{one of "DEoptim", "random", "ROI", + "pso", "GenSA". A solver for ROI can also be specified + and will be solved using ROI. See Details.} \item{search_size}{integer, how many portfolios to test, default 20,000} @@ -151,6 +147,15 @@ When using GenSA and want to set \code{verbose=TRUE}, instead use \code{trace}. + If \code{optimize_method="ROI"} is specified, a default + solver will be selected based on the optimization + problem. The \code{glpk} solver is the default solver for + LP and MILP optimization problems. The \code{quadprog} + solver is the default solver for QP optimization + problems. For example, \code{optimize_method = + "quadprog"} can be specified and the optimization problem + will be solved via ROI using the quadprog solver. + The extension to ROI solves a limited type of convex optimization problems: \itemize{ \item{Maxmimize portfolio return subject leverage, box, group, position From noreply at r-forge.r-project.org Sat Jan 18 16:26:12 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sat, 18 Jan 2014 16:26:12 +0100 (CET) Subject: [Returnanalytics-commits] r3301 - in pkg/PerformanceAnalytics: . R sandbox/Shubhankit/noniid.sm/R sandbox/pulkit/R Message-ID: <20140118152613.0D5F9186C4B@r-forge.r-project.org> Author: braverock Date: 2014-01-18 16:26:12 +0100 (Sat, 18 Jan 2014) New Revision: 3301 Added: pkg/PerformanceAnalytics/R/lpm.R Modified: pkg/PerformanceAnalytics/DESCRIPTION pkg/PerformanceAnalytics/R/ActivePremium.R pkg/PerformanceAnalytics/R/AdjustedSharpeRatio.R pkg/PerformanceAnalytics/R/AppraisalRatio.R pkg/PerformanceAnalytics/R/BernadoLedoitratio.R pkg/PerformanceAnalytics/R/BurkeRatio.R pkg/PerformanceAnalytics/R/CAPM.alpha.R pkg/PerformanceAnalytics/R/CAPM.beta.R pkg/PerformanceAnalytics/R/CAPM.epsilon.R pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R pkg/PerformanceAnalytics/R/CAPM.utils.R pkg/PerformanceAnalytics/R/CalmarRatio.R pkg/PerformanceAnalytics/R/CoMoments.R pkg/PerformanceAnalytics/R/DRatio.R pkg/PerformanceAnalytics/R/DownsideDeviation.R pkg/PerformanceAnalytics/R/DownsideFrequency.R pkg/PerformanceAnalytics/R/DrawdownPeak.R pkg/PerformanceAnalytics/R/Drawdowns.R pkg/PerformanceAnalytics/R/ES.R pkg/PerformanceAnalytics/R/FamaBeta.R pkg/PerformanceAnalytics/R/Frequency.R pkg/PerformanceAnalytics/R/HerfindahlIndex.R pkg/PerformanceAnalytics/R/HurstIndex.R pkg/PerformanceAnalytics/R/InformationRatio.R pkg/PerformanceAnalytics/R/Kappa.R pkg/PerformanceAnalytics/R/KellyRatio.R pkg/PerformanceAnalytics/R/M2Sortino.R pkg/PerformanceAnalytics/R/MSquared.R pkg/PerformanceAnalytics/R/MSquaredExcess.R pkg/PerformanceAnalytics/R/MartinRatio.R pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R pkg/PerformanceAnalytics/R/MultivariateMoments.R pkg/PerformanceAnalytics/R/NetSelectivity.R pkg/PerformanceAnalytics/R/Omega.R pkg/PerformanceAnalytics/R/OmegaExcessReturn.R pkg/PerformanceAnalytics/R/OmegaSharpeRatio.R pkg/PerformanceAnalytics/R/PainIndex.R pkg/PerformanceAnalytics/R/PainRatio.R pkg/PerformanceAnalytics/R/PortfolioRisk.R pkg/PerformanceAnalytics/R/ProspectRatio.R pkg/PerformanceAnalytics/R/Return.Geltner.R pkg/PerformanceAnalytics/R/Return.annualized.R pkg/PerformanceAnalytics/R/Return.calculate.R pkg/PerformanceAnalytics/R/Return.clean.R pkg/PerformanceAnalytics/R/Return.cumulative.R pkg/PerformanceAnalytics/R/Return.excess.R pkg/PerformanceAnalytics/R/Return.index.R pkg/PerformanceAnalytics/R/Return.portfolio.R pkg/PerformanceAnalytics/R/Return.read.R pkg/PerformanceAnalytics/R/Return.relative.R pkg/PerformanceAnalytics/R/Return.wealthindex.R pkg/PerformanceAnalytics/R/Selectivity.R pkg/PerformanceAnalytics/R/SemiDeviation.R pkg/PerformanceAnalytics/R/SharpeRatio.R pkg/PerformanceAnalytics/R/SharpeRatio.annualized.R pkg/PerformanceAnalytics/R/SkewnessKurtosisRatio.R pkg/PerformanceAnalytics/R/SmoothingIndex.R pkg/PerformanceAnalytics/R/SortinoRatio.R pkg/PerformanceAnalytics/R/SpecificRisk.R pkg/PerformanceAnalytics/R/StdDev.R pkg/PerformanceAnalytics/R/StdDev.annualized.R pkg/PerformanceAnalytics/R/SystematicRisk.R pkg/PerformanceAnalytics/R/TotalRisk.R pkg/PerformanceAnalytics/R/TrackingError.R pkg/PerformanceAnalytics/R/TreynorRatio.R pkg/PerformanceAnalytics/R/UlcerIndex.R pkg/PerformanceAnalytics/R/UpDownRatios.R pkg/PerformanceAnalytics/R/UpsideFrequency.R pkg/PerformanceAnalytics/R/UpsidePotentialRatio.R pkg/PerformanceAnalytics/R/UpsideRisk.R pkg/PerformanceAnalytics/R/VaR.Marginal.R pkg/PerformanceAnalytics/R/VaR.R pkg/PerformanceAnalytics/R/VolatilitySkewness.R pkg/PerformanceAnalytics/R/apply.fromstart.R pkg/PerformanceAnalytics/R/apply.rolling.R pkg/PerformanceAnalytics/R/chart.ACF.R pkg/PerformanceAnalytics/R/chart.ACFplus.R pkg/PerformanceAnalytics/R/chart.Bar.R pkg/PerformanceAnalytics/R/chart.BarVaR.R pkg/PerformanceAnalytics/R/chart.Boxplot.R pkg/PerformanceAnalytics/R/chart.CaptureRatios.R pkg/PerformanceAnalytics/R/chart.Correlation.R pkg/PerformanceAnalytics/R/chart.CumReturns.R pkg/PerformanceAnalytics/R/chart.Drawdown.R pkg/PerformanceAnalytics/R/chart.ECDF.R pkg/PerformanceAnalytics/R/chart.Events.R pkg/PerformanceAnalytics/R/chart.Histogram.R pkg/PerformanceAnalytics/R/chart.QQPlot.R pkg/PerformanceAnalytics/R/chart.Regression.R pkg/PerformanceAnalytics/R/chart.RelativePerformance.R pkg/PerformanceAnalytics/R/chart.RiskReturnScatter.R pkg/PerformanceAnalytics/R/chart.RollingCorrelation.R pkg/PerformanceAnalytics/R/chart.RollingMean.R pkg/PerformanceAnalytics/R/chart.RollingPerformance.R pkg/PerformanceAnalytics/R/chart.RollingQuantileRegression.R pkg/PerformanceAnalytics/R/chart.RollingRegression.R pkg/PerformanceAnalytics/R/chart.Scatter.R pkg/PerformanceAnalytics/R/chart.SnailTrail.R pkg/PerformanceAnalytics/R/chart.StackedBar.R pkg/PerformanceAnalytics/R/chart.TimeSeries.R pkg/PerformanceAnalytics/R/chart.VaRSensitivity.R pkg/PerformanceAnalytics/R/charts.Bar.R pkg/PerformanceAnalytics/R/charts.BarVaR.R pkg/PerformanceAnalytics/R/charts.PerformanceSummary.R pkg/PerformanceAnalytics/R/charts.RollingPerformance.R pkg/PerformanceAnalytics/R/charts.RollingRegression.R pkg/PerformanceAnalytics/R/charts.TimeSeries.R pkg/PerformanceAnalytics/R/checkData.R pkg/PerformanceAnalytics/R/decomposeMVaR.R pkg/PerformanceAnalytics/R/findDrawdowns.R pkg/PerformanceAnalytics/R/kurtosis.R pkg/PerformanceAnalytics/R/legend.R pkg/PerformanceAnalytics/R/maxDrawdown.R pkg/PerformanceAnalytics/R/mean.utils.R pkg/PerformanceAnalytics/R/na.skip.R pkg/PerformanceAnalytics/R/rCornishFisher.r pkg/PerformanceAnalytics/R/replaceTabs.R pkg/PerformanceAnalytics/R/skewness.R pkg/PerformanceAnalytics/R/sortDrawdowns.R pkg/PerformanceAnalytics/R/table.AnnualizedReturns.R pkg/PerformanceAnalytics/R/table.Arbitrary.R pkg/PerformanceAnalytics/R/table.Autocorrelation.R pkg/PerformanceAnalytics/R/table.CAPM.R pkg/PerformanceAnalytics/R/table.CalendarReturns.R pkg/PerformanceAnalytics/R/table.CaptureRatios.R pkg/PerformanceAnalytics/R/table.Correlation.R pkg/PerformanceAnalytics/R/table.Distributions.R pkg/PerformanceAnalytics/R/table.DownsideRisk.R pkg/PerformanceAnalytics/R/table.DownsideRiskRatio.R pkg/PerformanceAnalytics/R/table.Drawdowns.R pkg/PerformanceAnalytics/R/table.DrawdownsRatio.R pkg/PerformanceAnalytics/R/table.HigherMoments.R pkg/PerformanceAnalytics/R/table.InformationRatio.R pkg/PerformanceAnalytics/R/table.MonthlyReturns.R pkg/PerformanceAnalytics/R/table.RollingPeriods.R pkg/PerformanceAnalytics/R/table.SpecificRisk.R pkg/PerformanceAnalytics/R/table.UpDownRatios.R pkg/PerformanceAnalytics/R/table.Variability.R pkg/PerformanceAnalytics/R/textplot.R pkg/PerformanceAnalytics/R/valueAtRiskFunctions.r pkg/PerformanceAnalytics/R/zerofill.R pkg/PerformanceAnalytics/R/zzz.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/CDrawdown.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/GLMSmoothIndex.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/Return.GLM.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/Return.Okunev.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/chart.AcarSim.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/chart.Autocorrelation.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/na.skip.R pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/table.EMaxDDGBM.R pkg/PerformanceAnalytics/sandbox/pulkit/R/EDDCOPS.R pkg/PerformanceAnalytics/sandbox/pulkit/R/Edd.R pkg/PerformanceAnalytics/sandbox/pulkit/R/MinTRL.R pkg/PerformanceAnalytics/sandbox/pulkit/R/ProbSharpeRatio.R pkg/PerformanceAnalytics/sandbox/pulkit/R/REDDCOPS.R pkg/PerformanceAnalytics/sandbox/pulkit/R/REM.R pkg/PerformanceAnalytics/sandbox/pulkit/R/na.skip.R pkg/PerformanceAnalytics/sandbox/pulkit/R/redd.R Log: - update copyright to 2014 Modified: pkg/PerformanceAnalytics/DESCRIPTION =================================================================== --- pkg/PerformanceAnalytics/DESCRIPTION 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/DESCRIPTION 2014-01-18 15:26:12 UTC (rev 3301) @@ -30,7 +30,7 @@ ff License: GPL URL: http://r-forge.r-project.org/projects/returnanalytics/ -Copyright: (c) 2004-2012 +Copyright: (c) 2004-2014 Contributors: Kris Boudt, Diethelm Wuertz, Eric Zivot, Matthieu Lestel Thanks: A special thanks for additional contributions from Stefan Albrecht, Khahn Nygyen, Jeff Ryan, Modified: pkg/PerformanceAnalytics/R/ActivePremium.R =================================================================== --- pkg/PerformanceAnalytics/R/ActivePremium.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/ActivePremium.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -77,7 +77,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/AdjustedSharpeRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/AdjustedSharpeRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/AdjustedSharpeRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -67,7 +67,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/AppraisalRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/AppraisalRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/AppraisalRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -94,7 +94,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/BernadoLedoitratio.R =================================================================== --- pkg/PerformanceAnalytics/R/BernadoLedoitratio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/BernadoLedoitratio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -49,7 +49,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/BurkeRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/BurkeRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/BurkeRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -129,7 +129,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CAPM.alpha.R =================================================================== --- pkg/PerformanceAnalytics/R/CAPM.alpha.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CAPM.alpha.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -98,7 +98,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CAPM.beta.R =================================================================== --- pkg/PerformanceAnalytics/R/CAPM.beta.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CAPM.beta.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -264,7 +264,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CAPM.epsilon.R =================================================================== --- pkg/PerformanceAnalytics/R/CAPM.epsilon.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CAPM.epsilon.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -71,7 +71,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R =================================================================== --- pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CAPM.jensenAlpha.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -70,7 +70,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CAPM.utils.R =================================================================== --- pkg/PerformanceAnalytics/R/CAPM.utils.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CAPM.utils.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -156,7 +156,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CalmarRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/CalmarRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CalmarRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -119,7 +119,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/CoMoments.R =================================================================== --- pkg/PerformanceAnalytics/R/CoMoments.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/CoMoments.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -509,7 +509,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/DRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/DRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/DRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -60,7 +60,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/DownsideDeviation.R =================================================================== --- pkg/PerformanceAnalytics/R/DownsideDeviation.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/DownsideDeviation.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -186,7 +186,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/DownsideFrequency.R =================================================================== --- pkg/PerformanceAnalytics/R/DownsideFrequency.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/DownsideFrequency.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -64,7 +64,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/DrawdownPeak.R =================================================================== --- pkg/PerformanceAnalytics/R/DrawdownPeak.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/DrawdownPeak.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -63,7 +63,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Drawdowns.R =================================================================== --- pkg/PerformanceAnalytics/R/Drawdowns.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Drawdowns.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -39,7 +39,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/ES.R =================================================================== --- pkg/PerformanceAnalytics/R/ES.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/ES.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -236,7 +236,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/FamaBeta.R =================================================================== --- pkg/PerformanceAnalytics/R/FamaBeta.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/FamaBeta.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -68,7 +68,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Frequency.R =================================================================== --- pkg/PerformanceAnalytics/R/Frequency.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Frequency.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -60,7 +60,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/HerfindahlIndex.R =================================================================== --- pkg/PerformanceAnalytics/R/HerfindahlIndex.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/HerfindahlIndex.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -28,7 +28,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/HurstIndex.R =================================================================== --- pkg/PerformanceAnalytics/R/HurstIndex.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/HurstIndex.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -58,7 +58,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/InformationRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/InformationRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/InformationRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -81,7 +81,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Kappa.R =================================================================== --- pkg/PerformanceAnalytics/R/Kappa.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Kappa.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -73,7 +73,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/KellyRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/KellyRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/KellyRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -74,7 +74,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/M2Sortino.R =================================================================== --- pkg/PerformanceAnalytics/R/M2Sortino.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/M2Sortino.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -72,7 +72,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/MSquared.R =================================================================== --- pkg/PerformanceAnalytics/R/MSquared.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/MSquared.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -69,7 +69,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/MSquaredExcess.R =================================================================== --- pkg/PerformanceAnalytics/R/MSquaredExcess.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/MSquaredExcess.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -74,7 +74,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/MartinRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/MartinRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/MartinRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -66,7 +66,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R =================================================================== --- pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/MeanAbsoluteDeviation.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -47,7 +47,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/MultivariateMoments.R =================================================================== --- pkg/PerformanceAnalytics/R/MultivariateMoments.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/MultivariateMoments.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -8,7 +8,7 @@ # efficient when running against very large numbers of instruments or portfolios. # # Copyright (c) 2008 Kris Boudt and Brian G. Peterson -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson for PerformanceAnalytics +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson for PerformanceAnalytics # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING ############################################################################### @@ -142,7 +142,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson and Kris Boudt +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson and Kris Boudt # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/NetSelectivity.R =================================================================== --- pkg/PerformanceAnalytics/R/NetSelectivity.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/NetSelectivity.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -71,7 +71,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Omega.R =================================================================== --- pkg/PerformanceAnalytics/R/Omega.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Omega.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -184,7 +184,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/OmegaExcessReturn.R =================================================================== --- pkg/PerformanceAnalytics/R/OmegaExcessReturn.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/OmegaExcessReturn.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -73,7 +73,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/OmegaSharpeRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/OmegaSharpeRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/OmegaSharpeRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -78,7 +78,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/PainIndex.R =================================================================== --- pkg/PerformanceAnalytics/R/PainIndex.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/PainIndex.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -64,7 +64,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/PainRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/PainRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/PainRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -66,7 +66,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/PortfolioRisk.R =================================================================== --- pkg/PerformanceAnalytics/R/PortfolioRisk.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/PortfolioRisk.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -606,7 +606,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson and Kris Boudt +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson and Kris Boudt # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/ProspectRatio.R =================================================================== --- pkg/PerformanceAnalytics/R/ProspectRatio.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/ProspectRatio.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -66,7 +66,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.Geltner.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.Geltner.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.Geltner.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -84,7 +84,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.annualized.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.annualized.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.annualized.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -97,7 +97,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.calculate.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.calculate.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.calculate.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -91,7 +91,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.clean.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.clean.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.clean.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -258,7 +258,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.cumulative.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.cumulative.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.cumulative.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -52,7 +52,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.excess.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.excess.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.excess.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -90,7 +90,7 @@ ############################################################################### # R (http://r-project.org/) Econometrics for Performance and Risk Analysis # -# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson +# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson # # This R package is distributed under the terms of the GNU Public License (GPL) # for full details see the file COPYING Modified: pkg/PerformanceAnalytics/R/Return.index.R =================================================================== --- pkg/PerformanceAnalytics/R/Return.index.R 2014-01-11 21:26:56 UTC (rev 3300) +++ pkg/PerformanceAnalytics/R/Return.index.R 2014-01-18 15:26:12 UTC (rev 3301) @@ -44,7 +44,7 @@ ############################################################################### [TRUNCATED] To get the complete diff run: svnlook diff /svnroot/returnanalytics -r 3301 From noreply at r-forge.r-project.org Sun Jan 19 20:52:42 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sun, 19 Jan 2014 20:52:42 +0100 (CET) Subject: [Returnanalytics-commits] r3302 - in pkg/PortfolioAnalytics: . R sandbox Message-ID: <20140119195242.9236A186907@r-forge.r-project.org> Author: braverock Date: 2014-01-19 20:52:42 +0100 (Sun, 19 Jan 2014) New Revision: 3302 Modified: pkg/PortfolioAnalytics/DESCRIPTION pkg/PortfolioAnalytics/R/applyFUN.R pkg/PortfolioAnalytics/R/chart.RiskReward.R pkg/PortfolioAnalytics/R/chart.Weights.R pkg/PortfolioAnalytics/R/charts.DE.R pkg/PortfolioAnalytics/R/charts.GenSA.R pkg/PortfolioAnalytics/R/charts.PSO.R pkg/PortfolioAnalytics/R/charts.ROI.R pkg/PortfolioAnalytics/R/charts.RP.R pkg/PortfolioAnalytics/R/charts.efficient.frontier.R pkg/PortfolioAnalytics/R/charts.groups.R pkg/PortfolioAnalytics/R/charts.multiple.R pkg/PortfolioAnalytics/R/charts.risk.R pkg/PortfolioAnalytics/R/constrained_objective.R pkg/PortfolioAnalytics/R/constraint_fn_map.R pkg/PortfolioAnalytics/R/constraints.R pkg/PortfolioAnalytics/R/constraintsFUN.R pkg/PortfolioAnalytics/R/constraints_ROI.R pkg/PortfolioAnalytics/R/equal.weight.R pkg/PortfolioAnalytics/R/extract.efficient.frontier.R pkg/PortfolioAnalytics/R/extractstats.R pkg/PortfolioAnalytics/R/generics.R pkg/PortfolioAnalytics/R/inverse.volatility.weight.R pkg/PortfolioAnalytics/R/moment.functions.R pkg/PortfolioAnalytics/R/objective.R pkg/PortfolioAnalytics/R/objectiveFUN.R pkg/PortfolioAnalytics/R/optFUN.R pkg/PortfolioAnalytics/R/optimize.portfolio.R pkg/PortfolioAnalytics/R/portfolio.R pkg/PortfolioAnalytics/R/random_portfolios.R pkg/PortfolioAnalytics/R/trailingFUN.R pkg/PortfolioAnalytics/R/utility.combine.R pkg/PortfolioAnalytics/R/utils.R pkg/PortfolioAnalytics/sandbox/TAA.R pkg/PortfolioAnalytics/sandbox/applylocalsearch.R pkg/PortfolioAnalytics/sandbox/localsearch.R pkg/PortfolioAnalytics/sandbox/optimizer.R Log: - update Copyright to 2014, bump version Modified: pkg/PortfolioAnalytics/DESCRIPTION =================================================================== --- pkg/PortfolioAnalytics/DESCRIPTION 2014-01-18 15:26:12 UTC (rev 3301) +++ pkg/PortfolioAnalytics/DESCRIPTION 2014-01-19 19:52:42 UTC (rev 3302) @@ -1,8 +1,8 @@ Package: PortfolioAnalytics Type: Package -Title: Portfolio Analysis, including Numeric Methods for Optimization +Title: Portfolio Analysis, including Numerical Methods for Optimization of Portfolios -Version: 0.8.3 +Version: 0.9.0 Date: $Date$ Author: Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt Contributors: R. Douglas Martin, Guy Yollin, Hezky Varon @@ -27,9 +27,10 @@ ROI.plugin.symphony (>= 0.0.2), pso, GenSA, - corpcor + corpcor, + testthat License: GPL -Copyright: (c) 2004-2012 +Copyright: (c) 2004-2014 Collate: 'charts.DE.R' 'charts.RP.R' Modified: pkg/PortfolioAnalytics/R/applyFUN.R =================================================================== --- pkg/PortfolioAnalytics/R/applyFUN.R 2014-01-18 15:26:12 UTC (rev 3301) +++ pkg/PortfolioAnalytics/R/applyFUN.R 2014-01-19 19:52:42 UTC (rev 3302) @@ -154,3 +154,4116 @@ } return(out) } + +#' classic risk reward scatter +#' +#' This function charts the \code{optimize.portfolio} object in risk-return space. +#' +#' @details +#' \code{neighbors} may be specified in three ways. +#' The first is as a single number of neighbors. This will extract the \code{neighbors} closest +#' portfolios in terms of the \code{out} numerical statistic. +#' The second method consists of a numeric vector for \code{neighbors}. +#' This will extract the \code{neighbors} with portfolio index numbers that correspond to the vector contents. +#' The third method for specifying \code{neighbors} is to pass in a matrix. +#' This matrix should look like the output of \code{\link{extractStats}}, and should contain +#' \code{risk.col},\code{return.col}, and weights columns all properly named. +#' +#' @param object optimal portfolio created by \code{\link{optimize.portfolio}}. +#' @param neighbors set of 'neighbor' portfolios to overplot, see Details. +#' @param \dots any other passthru parameters. +#' @param return.col string matching the objective of a 'return' objective, on vertical axis. +#' @param risk.col string matching the objective of a 'risk' objective, on horizontal axis. +#' @param chart.assets TRUE/FALSE. Includes a risk reward scatter of the assets in the chart. +#' @param element.color color for the default plot scatter points. +#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}. +#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. +#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. +#' @param rp TRUE/FALSE to generate random portfolios to plot the feasible space +#' @param main a main title for the plot. +#' @param labels.assets TRUE/FALSE to include the names in the plot. +#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}} +#' @param cex.assets numerical value giving the amount by which the asset points should be magnified relative to the default. +#' @param cex.lab numerical value giving the amount by which the labels should be magnified relative to the default. +#' @param colorset color palette or vector of colors to use. +#' @seealso \code{\link{optimize.portfolio}} +#' @export +chart.RiskReward <- function(object, ...){ + UseMethod("chart.RiskReward") +} + + +#' Chart the efficient frontier and risk-return scatter +#' +#' Chart the efficient frontier and risk-return scatter of the assets for +#' \code{optimize.portfolio} or \code{efficient.frontier} objects +#' +#' @details +#' For objects created by optimize.portfolio with 'DEoptim', 'random', or 'pso' +#' specified as the optimize_method: +#' \itemize{ +#' \item The efficient frontier plotted is based on the the trace information (sets of +#' portfolios tested by the solver at each iteration) in objects created by +#' \code{optimize.portfolio}. +#' } +#' +#' For objects created by optimize.portfolio with 'ROI' specified as the +#' optimize_method: +#' \itemize{ +#' \item The mean-StdDev or mean-ETL efficient frontier can be plotted for optimal +#' portfolio objects created by \code{optimize.portfolio}. +#' +#' \item If \code{match.col="StdDev"}, the mean-StdDev efficient frontier is plotted. +#' +#' \item If \code{match.col="ETL"} (also "ES" or "CVaR"), the mean-ETL efficient frontier is plotted. +#' } +#' +#' Note that \code{trace=TRUE} must be specified in \code{\link{optimize.portfolio}} +#' +#' GenSA does not return any useable trace information for portfolios tested at +#' each iteration, therfore we cannot extract and chart an efficient frontier. +#' +#' By default, the tangency portfolio (maximum Sharpe Ratio or modified Sharpe Ratio) +#' will be plotted using a risk free rate of 0. Set \code{rf=NULL} to omit +#' this from the plot. +#' +#' @param object object to chart. +#' @param \dots passthru parameters to \code{\link{plot}} +#' @param match.col string name of column to use for risk (horizontal axis). +#' \code{match.col} must match the name of an objective measure in the +#' \code{objective_measures} or \code{opt_values} slot in the object created +#' by \code{\link{optimize.portfolio}}. +#' @param n.portfolios number of portfolios to use to plot the efficient frontier. +#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. +#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. +#' @param cex.axis numerical value giving the amount by which the axis should be magnified relative to the default. +#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. +#' @param main a main title for the plot. +#' @param RAR.text string name for risk adjusted return text to plot in the legend. +#' @param rf risk free rate. If \code{rf} is not null, the maximum Sharpe Ratio or modified Sharpe Ratio tangency portfolio will be plotted. +#' @param tangent.line TRUE/FALSE to plot the tangent line. +#' @param cex.legend numerical value giving the amount by which the legend should be magnified relative to the default. +#' @param chart.assets TRUE/FALSE to include the assets. +#' @param labels.assets TRUE/FALSE to include the asset names in the plot. +#' \code{chart.assets} must be \code{TRUE} to plot asset names. +#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}}. +#' @param cex.assets numerical value giving the amount by which the asset points and labels should be magnified relative to the default. +#' @author Ross Bennett +#' @rdname chart.EfficientFrontier +#' @export +chart.EfficientFrontier <- function(object, ...){ + UseMethod("chart.EfficientFrontier") +} + +#' @rdname chart.EfficientFrontier +#' @method chart.EfficientFrontier optimize.portfolio.ROI +#' @S3method chart.EfficientFrontier optimize.portfolio.ROI +chart.EfficientFrontier.optimize.portfolio.ROI <- function(object, ..., match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ + if(!inherits(object, "optimize.portfolio.ROI")) stop("object must be of class optimize.portfolio.ROI") + + portf <- object$portfolio + R <- object$R + if(is.null(R)) stop(paste("Not able to get asset returns from", object)) + wts <- object$weights + objectclass <- class(object)[1] + + # objnames <- unlist(lapply(portf$objectives, function(x) x$name)) + # if(!(match.col %in% objnames)){ + # stop("match.col must match an objective name") + # } + + # get the optimal return and risk metrics + xtract <- extractStats(object=object) + columnames <- names(xtract) + if(!(("mean") %in% columnames)){ + # we need to calculate the mean given the optimal weights + opt_ret <- applyFUN(R=R, weights=wts, FUN="mean") + } else { + opt_ret <- xtract["mean"] + } + # get the match.col column + mtc <- pmatch(match.col, columnames) + if(is.na(mtc)) { + mtc <- pmatch(paste(match.col,match.col,sep='.'), columnames) + } + if(is.na(mtc)){ + # if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") + opt_risk <- applyFUN(R=R, weights=wts, FUN=match.col) + } else { + opt_risk <- xtract[mtc] + } + + # get the data to plot scatter of asset returns + asset_ret <- scatterFUN(R=R, FUN="mean") + asset_risk <- scatterFUN(R=R, FUN=match.col) + rnames <- colnames(R) + + if(match.col %in% c("ETL", "ES", "CVaR")){ + frontier <- meanetl.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios) + rar <- "STARR" + } + if(match.col == "StdDev"){ + frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios) + rar <- "SR" + } + # data points to plot the frontier + x.f <- frontier[, match.col] + y.f <- frontier[, "mean"] + + # Points for the Sharpe Ratio ((mu - rf) / StdDev) or STARR ((mu - rf) / ETL) + if(!is.null(rf)){ + sr <- (y.f - rf) / (x.f) + idx.maxsr <- which.max(sr) + srmax <- sr[idx.maxsr] + } + + # set the x and y limits + if(is.null(xlim)){ + xlim <- range(c(x.f, asset_risk)) + # xlim[1] <- xlim[1] * 0.8 + xlim[1] <- 0 + xlim[2] <- xlim[2] * 1.15 + } + if(is.null(ylim)){ + ylim <- range(c(y.f, asset_ret)) + # ylim[1] <- ylim[1] * 0.9 + ylim[1] <- 0 + ylim[2] <- ylim[2] * 1.1 + } + + # plot the efficient frontier line + plot(x=x.f, y=y.f, ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) + + # Add the global minimum variance or global minimum ETL portfolio + points(x=x.f[1], y=y.f[1], pch=16) + + if(chart.assets){ + # risk-return scatter of the assets + points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) + if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) + } + + # plot the optimal portfolio + points(opt_risk, opt_ret, col="blue", pch=16) # optimal + text(x=opt_risk, y=opt_ret, labels="Optimal",col="blue", pos=4, cex=0.8) + if(!is.null(rf)){ + # Plot tangency line and points at risk-free rate and tangency portfolio + if(tangent.line) abline(rf, srmax, lty=2) + points(0, rf, pch=16) + points(x.f[idx.maxsr], y.f[idx.maxsr], pch=16) + # text(x=x.f[idx.maxsr], y=y.f[idx.maxsr], labels="T", pos=4, cex=0.8) + # Add lengend with max Sharpe Ratio and risk-free rate + legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) + legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) + } + axis(1, cex.axis = cex.axis, col = element.color) + axis(2, cex.axis = cex.axis, col = element.color) + box(col = element.color) +} + +#' @rdname chart.EfficientFrontier +#' @method chart.EfficientFrontier optimize.portfolio +#' @S3method chart.EfficientFrontier optimize.portfolio +chart.EfficientFrontier.optimize.portfolio <- function(object, ..., match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ + # This function will work with objects of class optimize.portfolio.DEoptim, + # optimize.portfolio.random, and optimize.portfolio.pso + + if(inherits(object, "optimize.portfolio.GenSA")){ + stop("GenSA does not return any useable trace information for portfolios tested, thus we cannot extract an efficient frontier.") + } + + if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio") + + portf <- object$portfolio + R <- object$R + if(is.null(R)) stop(paste("Not able to get asset returns from", object)) + wts <- object$weights + + # get the stats from the object + xtract <- extractStats(object=object) + columnames <- colnames(xtract) + + # Check if match.col is in extractStats output + if(!(match.col %in% columnames)){ + stop(paste(match.col, "is not a column in extractStats output")) + } + + # check if 'mean' is in extractStats output + if(!("mean" %in% columnames)){ + stop("mean is not a column in extractStats output") + } + + # get the stats of the optimal portfolio + optstats <- xtract[which.min(xtract[, "out"]), ] + opt_ret <- optstats["mean"] + opt_risk <- optstats[match.col] + + # get the data to plot scatter of asset returns + asset_ret <- scatterFUN(R=R, FUN="mean") + asset_risk <- scatterFUN(R=R, FUN=match.col) + rnames <- colnames(R) + + # get the data of the efficient frontier + frontier <- extract.efficient.frontier(object=object, match.col=match.col, n.portfolios=n.portfolios) + + # data points to plot the frontier + x.f <- frontier[, match.col] + y.f <- frontier[, "mean"] + + # Points for the Sharpe or Modified Sharpe Ratio + if(!is.null(rf)){ + sr <- (y.f - rf) / (x.f) + idx.maxsr <- which.max(sr) + srmax <- sr[idx.maxsr] + } + + # set the x and y limits + if(is.null(xlim)){ + xlim <- range(c(x.f, asset_risk)) + # xlim[1] <- xlim[1] * 0.8 + xlim[1] <- 0 + xlim[2] <- xlim[2] * 1.15 + } + if(is.null(ylim)){ + ylim <- range(c(y.f, asset_ret)) + # ylim[1] <- ylim[1] * 0.9 + ylim[1] <- 0 + ylim[2] <- ylim[2] * 1.1 + } + + # plot the efficient frontier line + plot(x=x.f, y=y.f, ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) + + # Add the global minimum variance or global minimum ETL portfolio + points(x=x.f[1], y=y.f[1], pch=16) + + if(chart.assets){ + # risk-return scatter of the assets + points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) + if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) + } + + # plot the optimal portfolio + points(opt_risk, opt_ret, col="blue", pch=16) # optimal + text(x=opt_risk, y=opt_ret, labels="Optimal",col="blue", pos=4, cex=0.8) + if(!is.null(rf)){ + # Plot tangency line and points at risk-free rate and tangency portfolio + if(tangent.line) abline(rf, srmax, lty=2) + points(0, rf, pch=16) + points(x.f[idx.maxsr], y.f[idx.maxsr], pch=16) + # text(x=x.f[idx.maxsr], y=y.f[idx.maxsr], labels="T", pos=4, cex=0.8) + # Add lengend with max Sharpe Ratio and risk-free rate + legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) + legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) + } + axis(1, cex.axis = cex.axis, col = element.color) + axis(2, cex.axis = cex.axis, col = element.color) + box(col = element.color) +} + + +#' Chart weights along an efficient frontier +#' +#' This function produces a stacked barplot of weights along an efficient frontier. +#' +#' @param object object of class \code{efficient.frontier} or \code{optimize.portfolio}. +#' @param \dots passthru parameters to \code{barplot}. +#' @param colorset color palette or vector of colors to use. +#' @param n.portfolios number of portfolios to extract along the efficient frontier. +#' @param by.groups TRUE/FALSE. If TRUE, the group weights are charted. +#' @param match.col string name of column to use for risk (horizontal axis). Must match the name of an objective. +#' @param main title used in the plot. +#' @param cex.lab the magnification to be used for x-axis and y-axis labels relative to the current setting of 'cex'. +#' @param cex.axis the magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}. +#' @param cex.legend the magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}. +#' @param legend.labels character vector to use for the legend labels. +#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. +#' @param legend.loc NULL, "topright", "right", or "bottomright". If legend.loc is NULL, the legend will not be plotted. +#' @author Ross Bennett +#' @rdname chart.Weights.EF +#' @export +chart.Weights.EF <- function(object, ...){ + UseMethod("chart.Weights.EF") +} + + +#' @rdname chart.Weights.EF +#' @method chart.Weights.EF efficient.frontier +#' @S3method chart.Weights.EF efficient.frontier +chart.Weights.EF.efficient.frontier <- function(object, ..., colorset=NULL, n.portfolios=25, by.groups=FALSE, match.col="ES", main="", cex.lab=0.8, cex.axis=0.8, cex.legend=0.8, legend.labels=NULL, element.color="darkgray", legend.loc="topright"){ + # using ideas from weightsPlot.R in fPortfolio package + + if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'") + + if(is.list(object)){ + # Objects created with create.EfficientFrontier will be a list of 2 elements + frontier <- object$frontier + } else { + # Objects created with extractEfficientFrontier will only be an efficient.frontier object + frontier <- object + } + + + # get the columns with weights + cnames <- colnames(frontier) + wts_idx <- grep(pattern="^w\\.", cnames) + wts <- frontier[, wts_idx] + + if(by.groups){ + constraints <- get_constraints(object$portfolio) + groups <- constraints$groups + if(is.null(groups)) stop("group constraints not in portfolio object") + if(!is.null(groups)){ + groupfun <- function(weights, groups){ + # This function is to calculate weights by group given the group list + # and a matrix of weights along the efficient frontier + ngroups <- length(groups) + group_weights <- rep(0, ngroups) + for(i in 1:ngroups){ + group_weights[i] <- sum(weights[groups[[i]]]) + } + group_weights + } + wts <- t(apply(wts, 1, groupfun, groups=groups)) + } + } + + # return along the efficient frontier + # get the "mean" column + mean.mtc <- pmatch("mean", cnames) + if(is.na(mean.mtc)) { + mean.mtc <- pmatch("mean.mean", cnames) + } + if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output") + + # risk along the efficient frontier + # get the match.col column + mtc <- pmatch(match.col, cnames) + if(is.na(mtc)) { + mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames) + } + if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") + + # compute the weights for the barplot + pos.weights <- +0.5 * (abs(wts) + wts) + neg.weights <- -0.5 * (abs(wts) - wts) + + # Define Plot Range: + ymax <- max(rowSums(pos.weights)) + ymin <- min(rowSums(neg.weights)) + range <- ymax - ymin + ymax <- ymax + 0.005 * range + ymin <- ymin - 0.005 * range + dim <- dim(wts) + range <- dim[1] + xmin <- 0 + if(is.null(legend.loc)){ + xmax <- range + } else { + xmax <- range + 0.3 * range + } + + # set the colorset if no colorset is passed in + if(is.null(colorset)) + colorset <- 1:dim[2] + + # plot the positive weights + barplot(t(pos.weights), col = colorset, space = 0, ylab = "", + xlim = c(xmin, xmax), ylim = c(ymin, ymax), + border = element.color, cex.axis=cex.axis, + axisnames=FALSE, ...) + + if(!is.null(legend.loc)){ + if(legend.loc %in% c("topright", "right", "bottomright")){ + # set the legend information + if(is.null(legend.labels)){ + if(by.groups){ + legend.labels <- names(groups) + if(is.null(legend.labels)) legend.labels <- constraints$group_labels + } else { + legend.labels <- gsub(pattern="^w\\.", replacement="", cnames[wts_idx]) + } + } + legend(legend.loc, legend = legend.labels, bty = "n", cex = cex.legend, fill = colorset) + } + } + # plot the negative weights + barplot(t(neg.weights), col = colorset, space = 0, add = TRUE, border = element.color, + cex.axis=cex.axis, axes=FALSE, axisnames=FALSE, ...) + + + # Add labels + ef.return <- frontier[, mean.mtc] + ef.risk <- frontier[, mtc] + n.risk <- length(ef.risk) + n.labels <- 6 + M <- c(0, ( 1:(n.risk %/% n.labels) ) ) * n.labels + 1 + # use 3 significant digits + axis(3, at = M, labels = signif(ef.risk[M], 3), cex.axis=cex.axis) + axis(1, at = M, labels = signif(ef.return[M], 3), cex.axis=cex.axis) + + # axis labels and titles + mtext(match.col, side = 3, line = 2, adj = 0.5, cex = cex.lab) + mtext("Mean", side = 1, line = 2, adj = 0.5, cex = cex.lab) + mtext("Weight", side = 2, line = 2, adj = 0.5, cex = cex.lab) + # add title + title(main=main, line=3) + # mtext(main, adj = 0, line = 2.5, font = 2, cex = 0.8) + box(col=element.color) +} + +#' @rdname chart.Weights.EF +#' @method chart.Weights.EF optimize.portfolio +#' @S3method chart.Weights.EF optimize.portfolio +chart.Weights.EF.optimize.portfolio <- function(object, ..., colorset=NULL, n.portfolios=25, by.groups=FALSE, match.col="ES", main="", cex.lab=0.8, cex.axis=0.8, cex.legend=0.8, legend.labels=NULL, element.color="darkgray", legend.loc="topright"){ + # chart the weights along the efficient frontier of an objected created by optimize.portfolio + + if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio") + + frontier <- extractEfficientFrontier(object=object, match.col=match.col, n.portfolios=n.portfolios) + PortfolioAnalytics:::chart.Weights.EF(object=frontier, colorset=colorset, ..., + match.col=match.col, by.groups=by.groups, main=main, cex.lab=cex.lab, + cex.axis=cex.axis, cex.legend=cex.legend, + legend.labels=legend.labels, element.color=element.color, + legend.loc=legend.loc) +} + +#' @rdname chart.EfficientFrontier +#' @method chart.EfficientFrontier efficient.frontier +#' @S3method chart.EfficientFrontier efficient.frontier +chart.EfficientFrontier.efficient.frontier <- function(object, ..., match.col="ES", n.portfolios=NULL, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ + if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'") + + # get the returns and efficient frontier object + R <- object$R + frontier <- object$frontier + + # get the column names from the frontier object + cnames <- colnames(frontier) + + # get the "mean" column + mean.mtc <- pmatch("mean", cnames) + if(is.na(mean.mtc)) { + mean.mtc <- pmatch("mean.mean", cnames) + } + if(is.na(mean.mtc)) stop("could not match 'mean' with column name of efficient frontier") + + # get the match.col column + mtc <- pmatch(match.col, cnames) + if(is.na(mtc)) { + mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames) + } + if(is.na(mtc)) stop("could not match match.col with column name of efficient frontier") + + # get the data to plot scatter of asset returns + asset_ret <- scatterFUN(R=R, FUN="mean") + asset_risk <- scatterFUN(R=R, FUN=match.col) + rnames <- colnames(R) + + # set the x and y limits + if(is.null(xlim)){ + xlim <- range(c(frontier[, mtc], asset_risk)) + # xlim[1] <- xlim[1] * 0.8 + xlim[1] <- 0 + xlim[2] <- xlim[2] * 1.15 + } + if(is.null(ylim)){ + ylim <- range(c(frontier[, mean.mtc], asset_ret)) + # ylim[1] <- ylim[1] * 0.9 + ylim[1] <- 0 + ylim[2] <- ylim[2] * 1.1 + } + + if(!is.null(rf)){ + sr <- (frontier[, mean.mtc] - rf) / (frontier[, mtc]) + idx.maxsr <- which.max(sr) + srmax <- sr[idx.maxsr] + } + + # plot the efficient frontier line + plot(x=frontier[, mtc], y=frontier[, mean.mtc], ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) + + # Add the global minimum variance or global minimum ETL portfolio + points(x=frontier[1, mtc], y=frontier[1, mean.mtc], pch=16) + + if(chart.assets){ + # risk-return scatter of the assets + points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) + if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) + } + + if(!is.null(rf)){ + # Plot tangency line and points at risk-free rate and tangency portfolio + if(tangent.line) abline(rf, srmax, lty=2) + points(0, rf, pch=16) + points(frontier[idx.maxsr, mtc], frontier[idx.maxsr, mean.mtc], pch=16) + # text(x=frontier[idx.maxsr], y=frontier[idx.maxsr], labels="T", pos=4, cex=0.8) + # Add legend with max Risk adjusted Return ratio and risk-free rate + legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) + legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) + } + axis(1, cex.axis = cex.axis, col = element.color) + axis(2, cex.axis = cex.axis, col = element.color) + box(col = element.color) +} + +#' Plot multiple efficient frontiers +#' +#' Overlay the efficient frontiers of multiple portfolio objects on a single plot. +#' +#' @param R an xts object of asset returns +#' @param portfolio_list list of portfolio objects created by \code{\link{portfolio.spec}} +#' @param type type of efficient frontier, see \code{\link{create.EfficientFrontier}} +#' @param n.portfolios number of portfolios to extract along the efficient frontier. +#' This is only used for objects of class \code{optimize.portfolio} +#' @param match.col string name of column to use for risk (horizontal axis). +#' Must match the name of an objective. +#' @param search_size passed to optimize.portfolio for type="DEoptim" or type="random". +#' @param main title used in the plot. +#' @param cex.axis the magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}. +#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. +#' @param legend.loc location of the legend; NULL, "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". +#' @param legend.labels character vector to use for the legend labels. +#' @param cex.legend The magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}. +#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. +#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. +#' @param \dots passthrough parameters to \code{\link{plot}}. +#' @param chart.assets TRUE/FALSE to include the assets. +#' @param labels.assets TRUE/FALSE to include the asset names in the plot. +#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}}. +#' @param cex.assets A numerical value giving the amount by which the asset points and labels should be magnified relative to the default. +#' @param col vector of colors with length equal to the number of portfolios in \code{portfolio_list}. +#' @param lty vector of line types with length equal to the number of portfolios in \code{portfolio_list}. +#' @param lwd vector of line widths with length equal to the number of portfolios in \code{portfolio_list}. +#' @author Ross Bennett +#' @export +chart.EfficientFrontierOverlay <- function(R, portfolio_list, type, n.portfolios=25, match.col="ES", search_size=2000, main="Efficient Frontiers", cex.axis=0.8, element.color="darkgray", legend.loc=NULL, legend.labels=NULL, cex.legend=0.8, xlim=NULL, ylim=NULL, ..., chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8, col=NULL, lty=NULL, lwd=NULL){ + # create multiple efficient frontier objects (one per portfolio in portfolio_list) + if(!is.list(portfolio_list)) stop("portfolio_list must be passed in as a list") + if(length(portfolio_list) == 1) warning("Only one portfolio object in portfolio_list") + # store in out + out <- list() + for(i in 1:length(portfolio_list)){ + if(!is.portfolio(portfolio_list[[i]])) stop("portfolio in portfolio_list must be of class 'portfolio'") + out[[i]] <- create.EfficientFrontier(R=R, portfolio=portfolio_list[[i]], type=type, n.portfolios=n.portfolios, match.col=match.col, search_size=search_size) + } + # get the data to plot scatter of asset returns + asset_ret <- scatterFUN(R=R, FUN="mean") + asset_risk <- scatterFUN(R=R, FUN=match.col) + rnames <- colnames(R) + + # set the x and y limits + if(is.null(xlim)){ + xlim <- range(asset_risk) + # xlim[1] <- xlim[1] * 0.8 + xlim[1] <- 0 + xlim[2] <- xlim[2] * 1.15 + } + if(is.null(ylim)){ + ylim <- range(asset_ret) + # ylim[1] <- ylim[1] * 0.9 + ylim[1] <- 0 + ylim[2] <- ylim[2] * 1.1 + } + + # plot the assets + plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="Mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, type="n", ...) + axis(1, cex.axis = cex.axis, col = element.color) + axis(2, cex.axis = cex.axis, col = element.color) + box(col = element.color) + + if(chart.assets){ + # risk-return scatter of the assets + points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) + if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) + } + + # set some basic plot parameters + if(is.null(col)) col <- 1:length(out) + if(is.null(lty)) lty <- 1:length(out) + if(is.null(lwd)) lwd <- rep(1, length(out)) + + for(i in 1:length(out)){ + tmp <- out[[i]] + tmpfrontier <- tmp$frontier + cnames <- colnames(tmpfrontier) + + # get the "mean" column + mean.mtc <- pmatch("mean", cnames) + if(is.na(mean.mtc)) { + mean.mtc <- pmatch("mean.mean", cnames) + } + if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output") + + # get the match.col column + mtc <- pmatch(match.col, cnames) + if(is.na(mtc)) { + mtc <- pmatch(paste(match.col, match.col, sep='.'),cnames) + } + if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") + # Add the efficient frontier lines to the plot + lines(x=tmpfrontier[, mtc], y=tmpfrontier[, mean.mtc], col=col[i], lty=lty[i], lwd=lwd[i]) + } + if(!is.null(legend.loc)){ + if(is.null(legend.labels)){ + legend.labels <- paste("Portfolio", 1:length(out), sep=".") + } + legend(legend.loc, legend=legend.labels, col=col, lty=lty, lwd=lwd, cex=cex.legend, bty="n") + } + return(invisible(out)) +} + + +chart.Weights.GenSA <- function(object, ..., neighbors = NULL, main="Weights", las = 3, xlab=NULL, cex.lab = 1, element.color = "darkgray", cex.axis=0.8, colorset=NULL, legend.loc="topright", cex.legend=0.8, plot.type="line"){ + + if(!inherits(object, "optimize.portfolio.GenSA")) stop("object must be of class 'optimize.portfolio.GenSA'") + + if(plot.type %in% c("bar", "barplot")){ + barplotWeights(object=object, ..., main=main, las=las, xlab=xlab, cex.lab=cex.lab, element.color=element.color, cex.axis=cex.axis, legend.loc=legend.loc, cex.legend=cex.legend, colorset=colorset) + } else if(plot.type == "line"){ + + columnnames = names(object$weights) + numassets = length(columnnames) + + constraints <- get_constraints(object$portfolio) + + if(is.null(xlab)) + minmargin = 3 + else + minmargin = 5 + if(main=="") topmargin=1 else topmargin=4 + if(las > 1) {# set the bottom border to accommodate labels + bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab + if(bottommargin > 10 ) { + bottommargin<-10 + columnnames<-substr(columnnames,1,19) + # par(srt=45) #TODO figure out how to use text() and srt to rotate long labels + } + } + else { + bottommargin = minmargin + } + par(mar = c(bottommargin, 4, topmargin, 2) +.1) + if(any(is.infinite(constraints$max)) | any(is.infinite(constraints$min))){ + # set ylim based on weights if box constraints contain Inf or -Inf + ylim <- range(object$weights) + } else { + # set ylim based on the range of box constraints min and max + ylim <- range(c(constraints$min, constraints$max)) + } + plot(object$weights, type="b", col="blue", axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, pch=16, ...) + if(!any(is.infinite(constraints$min))){ + points(constraints$min, type="b", col="darkgray", lty="solid", lwd=2, pch=24) + } + if(!any(is.infinite(constraints$max))){ + points(constraints$max, type="b", col="darkgray", lty="solid", lwd=2, pch=25) + } + # if(!is.null(neighbors)){ + # if(is.vector(neighbors)){ + # xtract=extractStats(ROI) + # weightcols<-grep('w\\.',colnames(xtract)) #need \\. to get the dot + # if(length(neighbors)==1){ + # # overplot nearby portfolios defined by 'out' + # orderx = order(xtract[,"out"]) + # subsetx = head(xtract[orderx,], n=neighbors) [TRUNCATED] To get the complete diff run: svnlook diff /svnroot/returnanalytics -r 3302 From noreply at r-forge.r-project.org Sun Jan 19 20:59:06 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sun, 19 Jan 2014 20:59:06 +0100 (CET) Subject: [Returnanalytics-commits] r3303 - pkg/PortfolioAnalytics/R Message-ID: <20140119195906.392C9186C10@r-forge.r-project.org> Author: braverock Date: 2014-01-19 20:59:05 +0100 (Sun, 19 Jan 2014) New Revision: 3303 Modified: pkg/PortfolioAnalytics/R/applyFUN.R Log: - roll back an errant `cat >>` Modified: pkg/PortfolioAnalytics/R/applyFUN.R =================================================================== --- pkg/PortfolioAnalytics/R/applyFUN.R 2014-01-19 19:52:42 UTC (rev 3302) +++ pkg/PortfolioAnalytics/R/applyFUN.R 2014-01-19 19:59:05 UTC (rev 3303) @@ -155,4107 +155,6 @@ return(out) } -#' classic risk reward scatter -#' -#' This function charts the \code{optimize.portfolio} object in risk-return space. -#' -#' @details -#' \code{neighbors} may be specified in three ways. -#' The first is as a single number of neighbors. This will extract the \code{neighbors} closest -#' portfolios in terms of the \code{out} numerical statistic. -#' The second method consists of a numeric vector for \code{neighbors}. -#' This will extract the \code{neighbors} with portfolio index numbers that correspond to the vector contents. -#' The third method for specifying \code{neighbors} is to pass in a matrix. -#' This matrix should look like the output of \code{\link{extractStats}}, and should contain -#' \code{risk.col},\code{return.col}, and weights columns all properly named. -#' -#' @param object optimal portfolio created by \code{\link{optimize.portfolio}}. -#' @param neighbors set of 'neighbor' portfolios to overplot, see Details. -#' @param \dots any other passthru parameters. -#' @param return.col string matching the objective of a 'return' objective, on vertical axis. -#' @param risk.col string matching the objective of a 'risk' objective, on horizontal axis. -#' @param chart.assets TRUE/FALSE. Includes a risk reward scatter of the assets in the chart. -#' @param element.color color for the default plot scatter points. -#' @param cex.axis The magnification to be used for axis annotation relative to the current setting of \code{cex}. -#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. -#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. -#' @param rp TRUE/FALSE to generate random portfolios to plot the feasible space -#' @param main a main title for the plot. -#' @param labels.assets TRUE/FALSE to include the names in the plot. -#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}} -#' @param cex.assets numerical value giving the amount by which the asset points should be magnified relative to the default. -#' @param cex.lab numerical value giving the amount by which the labels should be magnified relative to the default. -#' @param colorset color palette or vector of colors to use. -#' @seealso \code{\link{optimize.portfolio}} -#' @export -chart.RiskReward <- function(object, ...){ - UseMethod("chart.RiskReward") -} - - -#' Chart the efficient frontier and risk-return scatter -#' -#' Chart the efficient frontier and risk-return scatter of the assets for -#' \code{optimize.portfolio} or \code{efficient.frontier} objects -#' -#' @details -#' For objects created by optimize.portfolio with 'DEoptim', 'random', or 'pso' -#' specified as the optimize_method: -#' \itemize{ -#' \item The efficient frontier plotted is based on the the trace information (sets of -#' portfolios tested by the solver at each iteration) in objects created by -#' \code{optimize.portfolio}. -#' } -#' -#' For objects created by optimize.portfolio with 'ROI' specified as the -#' optimize_method: -#' \itemize{ -#' \item The mean-StdDev or mean-ETL efficient frontier can be plotted for optimal -#' portfolio objects created by \code{optimize.portfolio}. -#' -#' \item If \code{match.col="StdDev"}, the mean-StdDev efficient frontier is plotted. -#' -#' \item If \code{match.col="ETL"} (also "ES" or "CVaR"), the mean-ETL efficient frontier is plotted. -#' } -#' -#' Note that \code{trace=TRUE} must be specified in \code{\link{optimize.portfolio}} -#' -#' GenSA does not return any useable trace information for portfolios tested at -#' each iteration, therfore we cannot extract and chart an efficient frontier. -#' -#' By default, the tangency portfolio (maximum Sharpe Ratio or modified Sharpe Ratio) -#' will be plotted using a risk free rate of 0. Set \code{rf=NULL} to omit -#' this from the plot. -#' -#' @param object object to chart. -#' @param \dots passthru parameters to \code{\link{plot}} -#' @param match.col string name of column to use for risk (horizontal axis). -#' \code{match.col} must match the name of an objective measure in the -#' \code{objective_measures} or \code{opt_values} slot in the object created -#' by \code{\link{optimize.portfolio}}. -#' @param n.portfolios number of portfolios to use to plot the efficient frontier. -#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. -#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. -#' @param cex.axis numerical value giving the amount by which the axis should be magnified relative to the default. -#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. -#' @param main a main title for the plot. -#' @param RAR.text string name for risk adjusted return text to plot in the legend. -#' @param rf risk free rate. If \code{rf} is not null, the maximum Sharpe Ratio or modified Sharpe Ratio tangency portfolio will be plotted. -#' @param tangent.line TRUE/FALSE to plot the tangent line. -#' @param cex.legend numerical value giving the amount by which the legend should be magnified relative to the default. -#' @param chart.assets TRUE/FALSE to include the assets. -#' @param labels.assets TRUE/FALSE to include the asset names in the plot. -#' \code{chart.assets} must be \code{TRUE} to plot asset names. -#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}}. -#' @param cex.assets numerical value giving the amount by which the asset points and labels should be magnified relative to the default. -#' @author Ross Bennett -#' @rdname chart.EfficientFrontier -#' @export -chart.EfficientFrontier <- function(object, ...){ - UseMethod("chart.EfficientFrontier") -} - -#' @rdname chart.EfficientFrontier -#' @method chart.EfficientFrontier optimize.portfolio.ROI -#' @S3method chart.EfficientFrontier optimize.portfolio.ROI -chart.EfficientFrontier.optimize.portfolio.ROI <- function(object, ..., match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ - if(!inherits(object, "optimize.portfolio.ROI")) stop("object must be of class optimize.portfolio.ROI") - - portf <- object$portfolio - R <- object$R - if(is.null(R)) stop(paste("Not able to get asset returns from", object)) - wts <- object$weights - objectclass <- class(object)[1] - - # objnames <- unlist(lapply(portf$objectives, function(x) x$name)) - # if(!(match.col %in% objnames)){ - # stop("match.col must match an objective name") - # } - - # get the optimal return and risk metrics - xtract <- extractStats(object=object) - columnames <- names(xtract) - if(!(("mean") %in% columnames)){ - # we need to calculate the mean given the optimal weights - opt_ret <- applyFUN(R=R, weights=wts, FUN="mean") - } else { - opt_ret <- xtract["mean"] - } - # get the match.col column - mtc <- pmatch(match.col, columnames) - if(is.na(mtc)) { - mtc <- pmatch(paste(match.col,match.col,sep='.'), columnames) - } - if(is.na(mtc)){ - # if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") - opt_risk <- applyFUN(R=R, weights=wts, FUN=match.col) - } else { - opt_risk <- xtract[mtc] - } - - # get the data to plot scatter of asset returns - asset_ret <- scatterFUN(R=R, FUN="mean") - asset_risk <- scatterFUN(R=R, FUN=match.col) - rnames <- colnames(R) - - if(match.col %in% c("ETL", "ES", "CVaR")){ - frontier <- meanetl.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios) - rar <- "STARR" - } - if(match.col == "StdDev"){ - frontier <- meanvar.efficient.frontier(portfolio=portf, R=R, n.portfolios=n.portfolios) - rar <- "SR" - } - # data points to plot the frontier - x.f <- frontier[, match.col] - y.f <- frontier[, "mean"] - - # Points for the Sharpe Ratio ((mu - rf) / StdDev) or STARR ((mu - rf) / ETL) - if(!is.null(rf)){ - sr <- (y.f - rf) / (x.f) - idx.maxsr <- which.max(sr) - srmax <- sr[idx.maxsr] - } - - # set the x and y limits - if(is.null(xlim)){ - xlim <- range(c(x.f, asset_risk)) - # xlim[1] <- xlim[1] * 0.8 - xlim[1] <- 0 - xlim[2] <- xlim[2] * 1.15 - } - if(is.null(ylim)){ - ylim <- range(c(y.f, asset_ret)) - # ylim[1] <- ylim[1] * 0.9 - ylim[1] <- 0 - ylim[2] <- ylim[2] * 1.1 - } - - # plot the efficient frontier line - plot(x=x.f, y=y.f, ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) - - # Add the global minimum variance or global minimum ETL portfolio - points(x=x.f[1], y=y.f[1], pch=16) - - if(chart.assets){ - # risk-return scatter of the assets - points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) - if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) - } - - # plot the optimal portfolio - points(opt_risk, opt_ret, col="blue", pch=16) # optimal - text(x=opt_risk, y=opt_ret, labels="Optimal",col="blue", pos=4, cex=0.8) - if(!is.null(rf)){ - # Plot tangency line and points at risk-free rate and tangency portfolio - if(tangent.line) abline(rf, srmax, lty=2) - points(0, rf, pch=16) - points(x.f[idx.maxsr], y.f[idx.maxsr], pch=16) - # text(x=x.f[idx.maxsr], y=y.f[idx.maxsr], labels="T", pos=4, cex=0.8) - # Add lengend with max Sharpe Ratio and risk-free rate - legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) - legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) - } - axis(1, cex.axis = cex.axis, col = element.color) - axis(2, cex.axis = cex.axis, col = element.color) - box(col = element.color) -} - -#' @rdname chart.EfficientFrontier -#' @method chart.EfficientFrontier optimize.portfolio -#' @S3method chart.EfficientFrontier optimize.portfolio -chart.EfficientFrontier.optimize.portfolio <- function(object, ..., match.col="ES", n.portfolios=25, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ - # This function will work with objects of class optimize.portfolio.DEoptim, - # optimize.portfolio.random, and optimize.portfolio.pso - - if(inherits(object, "optimize.portfolio.GenSA")){ - stop("GenSA does not return any useable trace information for portfolios tested, thus we cannot extract an efficient frontier.") - } - - if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio") - - portf <- object$portfolio - R <- object$R - if(is.null(R)) stop(paste("Not able to get asset returns from", object)) - wts <- object$weights - - # get the stats from the object - xtract <- extractStats(object=object) - columnames <- colnames(xtract) - - # Check if match.col is in extractStats output - if(!(match.col %in% columnames)){ - stop(paste(match.col, "is not a column in extractStats output")) - } - - # check if 'mean' is in extractStats output - if(!("mean" %in% columnames)){ - stop("mean is not a column in extractStats output") - } - - # get the stats of the optimal portfolio - optstats <- xtract[which.min(xtract[, "out"]), ] - opt_ret <- optstats["mean"] - opt_risk <- optstats[match.col] - - # get the data to plot scatter of asset returns - asset_ret <- scatterFUN(R=R, FUN="mean") - asset_risk <- scatterFUN(R=R, FUN=match.col) - rnames <- colnames(R) - - # get the data of the efficient frontier - frontier <- extract.efficient.frontier(object=object, match.col=match.col, n.portfolios=n.portfolios) - - # data points to plot the frontier - x.f <- frontier[, match.col] - y.f <- frontier[, "mean"] - - # Points for the Sharpe or Modified Sharpe Ratio - if(!is.null(rf)){ - sr <- (y.f - rf) / (x.f) - idx.maxsr <- which.max(sr) - srmax <- sr[idx.maxsr] - } - - # set the x and y limits - if(is.null(xlim)){ - xlim <- range(c(x.f, asset_risk)) - # xlim[1] <- xlim[1] * 0.8 - xlim[1] <- 0 - xlim[2] <- xlim[2] * 1.15 - } - if(is.null(ylim)){ - ylim <- range(c(y.f, asset_ret)) - # ylim[1] <- ylim[1] * 0.9 - ylim[1] <- 0 - ylim[2] <- ylim[2] * 1.1 - } - - # plot the efficient frontier line - plot(x=x.f, y=y.f, ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) - - # Add the global minimum variance or global minimum ETL portfolio - points(x=x.f[1], y=y.f[1], pch=16) - - if(chart.assets){ - # risk-return scatter of the assets - points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) - if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) - } - - # plot the optimal portfolio - points(opt_risk, opt_ret, col="blue", pch=16) # optimal - text(x=opt_risk, y=opt_ret, labels="Optimal",col="blue", pos=4, cex=0.8) - if(!is.null(rf)){ - # Plot tangency line and points at risk-free rate and tangency portfolio - if(tangent.line) abline(rf, srmax, lty=2) - points(0, rf, pch=16) - points(x.f[idx.maxsr], y.f[idx.maxsr], pch=16) - # text(x=x.f[idx.maxsr], y=y.f[idx.maxsr], labels="T", pos=4, cex=0.8) - # Add lengend with max Sharpe Ratio and risk-free rate - legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) - legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) - } - axis(1, cex.axis = cex.axis, col = element.color) - axis(2, cex.axis = cex.axis, col = element.color) - box(col = element.color) -} - - -#' Chart weights along an efficient frontier -#' -#' This function produces a stacked barplot of weights along an efficient frontier. -#' -#' @param object object of class \code{efficient.frontier} or \code{optimize.portfolio}. -#' @param \dots passthru parameters to \code{barplot}. -#' @param colorset color palette or vector of colors to use. -#' @param n.portfolios number of portfolios to extract along the efficient frontier. -#' @param by.groups TRUE/FALSE. If TRUE, the group weights are charted. -#' @param match.col string name of column to use for risk (horizontal axis). Must match the name of an objective. -#' @param main title used in the plot. -#' @param cex.lab the magnification to be used for x-axis and y-axis labels relative to the current setting of 'cex'. -#' @param cex.axis the magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}. -#' @param cex.legend the magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}. -#' @param legend.labels character vector to use for the legend labels. -#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. -#' @param legend.loc NULL, "topright", "right", or "bottomright". If legend.loc is NULL, the legend will not be plotted. -#' @author Ross Bennett -#' @rdname chart.Weights.EF -#' @export -chart.Weights.EF <- function(object, ...){ - UseMethod("chart.Weights.EF") -} - - -#' @rdname chart.Weights.EF -#' @method chart.Weights.EF efficient.frontier -#' @S3method chart.Weights.EF efficient.frontier -chart.Weights.EF.efficient.frontier <- function(object, ..., colorset=NULL, n.portfolios=25, by.groups=FALSE, match.col="ES", main="", cex.lab=0.8, cex.axis=0.8, cex.legend=0.8, legend.labels=NULL, element.color="darkgray", legend.loc="topright"){ - # using ideas from weightsPlot.R in fPortfolio package - - if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'") - - if(is.list(object)){ - # Objects created with create.EfficientFrontier will be a list of 2 elements - frontier <- object$frontier - } else { - # Objects created with extractEfficientFrontier will only be an efficient.frontier object - frontier <- object - } - - - # get the columns with weights - cnames <- colnames(frontier) - wts_idx <- grep(pattern="^w\\.", cnames) - wts <- frontier[, wts_idx] - - if(by.groups){ - constraints <- get_constraints(object$portfolio) - groups <- constraints$groups - if(is.null(groups)) stop("group constraints not in portfolio object") - if(!is.null(groups)){ - groupfun <- function(weights, groups){ - # This function is to calculate weights by group given the group list - # and a matrix of weights along the efficient frontier - ngroups <- length(groups) - group_weights <- rep(0, ngroups) - for(i in 1:ngroups){ - group_weights[i] <- sum(weights[groups[[i]]]) - } - group_weights - } - wts <- t(apply(wts, 1, groupfun, groups=groups)) - } - } - - # return along the efficient frontier - # get the "mean" column - mean.mtc <- pmatch("mean", cnames) - if(is.na(mean.mtc)) { - mean.mtc <- pmatch("mean.mean", cnames) - } - if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output") - - # risk along the efficient frontier - # get the match.col column - mtc <- pmatch(match.col, cnames) - if(is.na(mtc)) { - mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames) - } - if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") - - # compute the weights for the barplot - pos.weights <- +0.5 * (abs(wts) + wts) - neg.weights <- -0.5 * (abs(wts) - wts) - - # Define Plot Range: - ymax <- max(rowSums(pos.weights)) - ymin <- min(rowSums(neg.weights)) - range <- ymax - ymin - ymax <- ymax + 0.005 * range - ymin <- ymin - 0.005 * range - dim <- dim(wts) - range <- dim[1] - xmin <- 0 - if(is.null(legend.loc)){ - xmax <- range - } else { - xmax <- range + 0.3 * range - } - - # set the colorset if no colorset is passed in - if(is.null(colorset)) - colorset <- 1:dim[2] - - # plot the positive weights - barplot(t(pos.weights), col = colorset, space = 0, ylab = "", - xlim = c(xmin, xmax), ylim = c(ymin, ymax), - border = element.color, cex.axis=cex.axis, - axisnames=FALSE, ...) - - if(!is.null(legend.loc)){ - if(legend.loc %in% c("topright", "right", "bottomright")){ - # set the legend information - if(is.null(legend.labels)){ - if(by.groups){ - legend.labels <- names(groups) - if(is.null(legend.labels)) legend.labels <- constraints$group_labels - } else { - legend.labels <- gsub(pattern="^w\\.", replacement="", cnames[wts_idx]) - } - } - legend(legend.loc, legend = legend.labels, bty = "n", cex = cex.legend, fill = colorset) - } - } - # plot the negative weights - barplot(t(neg.weights), col = colorset, space = 0, add = TRUE, border = element.color, - cex.axis=cex.axis, axes=FALSE, axisnames=FALSE, ...) - - - # Add labels - ef.return <- frontier[, mean.mtc] - ef.risk <- frontier[, mtc] - n.risk <- length(ef.risk) - n.labels <- 6 - M <- c(0, ( 1:(n.risk %/% n.labels) ) ) * n.labels + 1 - # use 3 significant digits - axis(3, at = M, labels = signif(ef.risk[M], 3), cex.axis=cex.axis) - axis(1, at = M, labels = signif(ef.return[M], 3), cex.axis=cex.axis) - - # axis labels and titles - mtext(match.col, side = 3, line = 2, adj = 0.5, cex = cex.lab) - mtext("Mean", side = 1, line = 2, adj = 0.5, cex = cex.lab) - mtext("Weight", side = 2, line = 2, adj = 0.5, cex = cex.lab) - # add title - title(main=main, line=3) - # mtext(main, adj = 0, line = 2.5, font = 2, cex = 0.8) - box(col=element.color) -} - -#' @rdname chart.Weights.EF -#' @method chart.Weights.EF optimize.portfolio -#' @S3method chart.Weights.EF optimize.portfolio -chart.Weights.EF.optimize.portfolio <- function(object, ..., colorset=NULL, n.portfolios=25, by.groups=FALSE, match.col="ES", main="", cex.lab=0.8, cex.axis=0.8, cex.legend=0.8, legend.labels=NULL, element.color="darkgray", legend.loc="topright"){ - # chart the weights along the efficient frontier of an objected created by optimize.portfolio - - if(!inherits(object, "optimize.portfolio")) stop("object must be of class optimize.portfolio") - - frontier <- extractEfficientFrontier(object=object, match.col=match.col, n.portfolios=n.portfolios) - PortfolioAnalytics:::chart.Weights.EF(object=frontier, colorset=colorset, ..., - match.col=match.col, by.groups=by.groups, main=main, cex.lab=cex.lab, - cex.axis=cex.axis, cex.legend=cex.legend, - legend.labels=legend.labels, element.color=element.color, - legend.loc=legend.loc) -} - -#' @rdname chart.EfficientFrontier -#' @method chart.EfficientFrontier efficient.frontier -#' @S3method chart.EfficientFrontier efficient.frontier -chart.EfficientFrontier.efficient.frontier <- function(object, ..., match.col="ES", n.portfolios=NULL, xlim=NULL, ylim=NULL, cex.axis=0.8, element.color="darkgray", main="Efficient Frontier", RAR.text="SR", rf=0, tangent.line=TRUE, cex.legend=0.8, chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8){ - if(!inherits(object, "efficient.frontier")) stop("object must be of class 'efficient.frontier'") - - # get the returns and efficient frontier object - R <- object$R - frontier <- object$frontier - - # get the column names from the frontier object - cnames <- colnames(frontier) - - # get the "mean" column - mean.mtc <- pmatch("mean", cnames) - if(is.na(mean.mtc)) { - mean.mtc <- pmatch("mean.mean", cnames) - } - if(is.na(mean.mtc)) stop("could not match 'mean' with column name of efficient frontier") - - # get the match.col column - mtc <- pmatch(match.col, cnames) - if(is.na(mtc)) { - mtc <- pmatch(paste(match.col,match.col,sep='.'),cnames) - } - if(is.na(mtc)) stop("could not match match.col with column name of efficient frontier") - - # get the data to plot scatter of asset returns - asset_ret <- scatterFUN(R=R, FUN="mean") - asset_risk <- scatterFUN(R=R, FUN=match.col) - rnames <- colnames(R) - - # set the x and y limits - if(is.null(xlim)){ - xlim <- range(c(frontier[, mtc], asset_risk)) - # xlim[1] <- xlim[1] * 0.8 - xlim[1] <- 0 - xlim[2] <- xlim[2] * 1.15 - } - if(is.null(ylim)){ - ylim <- range(c(frontier[, mean.mtc], asset_ret)) - # ylim[1] <- ylim[1] * 0.9 - ylim[1] <- 0 - ylim[2] <- ylim[2] * 1.1 - } - - if(!is.null(rf)){ - sr <- (frontier[, mean.mtc] - rf) / (frontier[, mtc]) - idx.maxsr <- which.max(sr) - srmax <- sr[idx.maxsr] - } - - # plot the efficient frontier line - plot(x=frontier[, mtc], y=frontier[, mean.mtc], ylab="Mean", xlab=match.col, main=main, xlim=xlim, ylim=ylim, axes=FALSE, ...) - - # Add the global minimum variance or global minimum ETL portfolio - points(x=frontier[1, mtc], y=frontier[1, mean.mtc], pch=16) - - if(chart.assets){ - # risk-return scatter of the assets - points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) - if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) - } - - if(!is.null(rf)){ - # Plot tangency line and points at risk-free rate and tangency portfolio - if(tangent.line) abline(rf, srmax, lty=2) - points(0, rf, pch=16) - points(frontier[idx.maxsr, mtc], frontier[idx.maxsr, mean.mtc], pch=16) - # text(x=frontier[idx.maxsr], y=frontier[idx.maxsr], labels="T", pos=4, cex=0.8) - # Add legend with max Risk adjusted Return ratio and risk-free rate - legend("topleft", paste(RAR.text, " = ", signif(srmax,3), sep = ""), bty = "n", cex=cex.legend) - legend("topleft", inset = c(0,0.05), paste("rf = ", signif(rf,3), sep = ""), bty = "n", cex=cex.legend) - } - axis(1, cex.axis = cex.axis, col = element.color) - axis(2, cex.axis = cex.axis, col = element.color) - box(col = element.color) -} - -#' Plot multiple efficient frontiers -#' -#' Overlay the efficient frontiers of multiple portfolio objects on a single plot. -#' -#' @param R an xts object of asset returns -#' @param portfolio_list list of portfolio objects created by \code{\link{portfolio.spec}} -#' @param type type of efficient frontier, see \code{\link{create.EfficientFrontier}} -#' @param n.portfolios number of portfolios to extract along the efficient frontier. -#' This is only used for objects of class \code{optimize.portfolio} -#' @param match.col string name of column to use for risk (horizontal axis). -#' Must match the name of an objective. -#' @param search_size passed to optimize.portfolio for type="DEoptim" or type="random". -#' @param main title used in the plot. -#' @param cex.axis the magnification to be used for sizing the axis text relative to the current setting of 'cex', similar to \code{\link{plot}}. -#' @param element.color provides the color for drawing less-important chart elements, such as the box lines, axis lines, etc. -#' @param legend.loc location of the legend; NULL, "bottomright", "bottom", "bottomleft", "left", "topleft", "top", "topright", "right" and "center". -#' @param legend.labels character vector to use for the legend labels. -#' @param cex.legend The magnification to be used for sizing the legend relative to the current setting of 'cex', similar to \code{\link{plot}}. -#' @param xlim set the x-axis limit, same as in \code{\link{plot}}. -#' @param ylim set the y-axis limit, same as in \code{\link{plot}}. -#' @param \dots passthrough parameters to \code{\link{plot}}. -#' @param chart.assets TRUE/FALSE to include the assets. -#' @param labels.assets TRUE/FALSE to include the asset names in the plot. -#' @param pch.assets plotting character of the assets, same as in \code{\link{plot}}. -#' @param cex.assets A numerical value giving the amount by which the asset points and labels should be magnified relative to the default. -#' @param col vector of colors with length equal to the number of portfolios in \code{portfolio_list}. -#' @param lty vector of line types with length equal to the number of portfolios in \code{portfolio_list}. -#' @param lwd vector of line widths with length equal to the number of portfolios in \code{portfolio_list}. -#' @author Ross Bennett -#' @export -chart.EfficientFrontierOverlay <- function(R, portfolio_list, type, n.portfolios=25, match.col="ES", search_size=2000, main="Efficient Frontiers", cex.axis=0.8, element.color="darkgray", legend.loc=NULL, legend.labels=NULL, cex.legend=0.8, xlim=NULL, ylim=NULL, ..., chart.assets=TRUE, labels.assets=TRUE, pch.assets=21, cex.assets=0.8, col=NULL, lty=NULL, lwd=NULL){ - # create multiple efficient frontier objects (one per portfolio in portfolio_list) - if(!is.list(portfolio_list)) stop("portfolio_list must be passed in as a list") - if(length(portfolio_list) == 1) warning("Only one portfolio object in portfolio_list") - # store in out - out <- list() - for(i in 1:length(portfolio_list)){ - if(!is.portfolio(portfolio_list[[i]])) stop("portfolio in portfolio_list must be of class 'portfolio'") - out[[i]] <- create.EfficientFrontier(R=R, portfolio=portfolio_list[[i]], type=type, n.portfolios=n.portfolios, match.col=match.col, search_size=search_size) - } - # get the data to plot scatter of asset returns - asset_ret <- scatterFUN(R=R, FUN="mean") - asset_risk <- scatterFUN(R=R, FUN=match.col) - rnames <- colnames(R) - - # set the x and y limits - if(is.null(xlim)){ - xlim <- range(asset_risk) - # xlim[1] <- xlim[1] * 0.8 - xlim[1] <- 0 - xlim[2] <- xlim[2] * 1.15 - } - if(is.null(ylim)){ - ylim <- range(asset_ret) - # ylim[1] <- ylim[1] * 0.9 - ylim[1] <- 0 - ylim[2] <- ylim[2] * 1.1 - } - - # plot the assets - plot(x=asset_risk, y=asset_ret, xlab=match.col, ylab="Mean", main=main, xlim=xlim, ylim=ylim, axes=FALSE, type="n", ...) - axis(1, cex.axis = cex.axis, col = element.color) - axis(2, cex.axis = cex.axis, col = element.color) - box(col = element.color) - - if(chart.assets){ - # risk-return scatter of the assets - points(x=asset_risk, y=asset_ret, pch=pch.assets, cex=cex.assets) - if(labels.assets) text(x=asset_risk, y=asset_ret, labels=rnames, pos=4, cex=cex.assets) - } - - # set some basic plot parameters - if(is.null(col)) col <- 1:length(out) - if(is.null(lty)) lty <- 1:length(out) - if(is.null(lwd)) lwd <- rep(1, length(out)) - - for(i in 1:length(out)){ - tmp <- out[[i]] - tmpfrontier <- tmp$frontier - cnames <- colnames(tmpfrontier) - - # get the "mean" column - mean.mtc <- pmatch("mean", cnames) - if(is.na(mean.mtc)) { - mean.mtc <- pmatch("mean.mean", cnames) - } - if(is.na(mean.mtc)) stop("could not match 'mean' with column name of extractStats output") - - # get the match.col column - mtc <- pmatch(match.col, cnames) - if(is.na(mtc)) { - mtc <- pmatch(paste(match.col, match.col, sep='.'),cnames) - } - if(is.na(mtc)) stop("could not match match.col with column name of extractStats output") - # Add the efficient frontier lines to the plot - lines(x=tmpfrontier[, mtc], y=tmpfrontier[, mean.mtc], col=col[i], lty=lty[i], lwd=lwd[i]) - } - if(!is.null(legend.loc)){ - if(is.null(legend.labels)){ - legend.labels <- paste("Portfolio", 1:length(out), sep=".") - } - legend(legend.loc, legend=legend.labels, col=col, lty=lty, lwd=lwd, cex=cex.legend, bty="n") - } - return(invisible(out)) -} - - -chart.Weights.GenSA <- function(object, ..., neighbors = NULL, main="Weights", las = 3, xlab=NULL, cex.lab = 1, element.color = "darkgray", cex.axis=0.8, colorset=NULL, legend.loc="topright", cex.legend=0.8, plot.type="line"){ - - if(!inherits(object, "optimize.portfolio.GenSA")) stop("object must be of class 'optimize.portfolio.GenSA'") - - if(plot.type %in% c("bar", "barplot")){ - barplotWeights(object=object, ..., main=main, las=las, xlab=xlab, cex.lab=cex.lab, element.color=element.color, cex.axis=cex.axis, legend.loc=legend.loc, cex.legend=cex.legend, colorset=colorset) - } else if(plot.type == "line"){ - - columnnames = names(object$weights) - numassets = length(columnnames) - - constraints <- get_constraints(object$portfolio) - - if(is.null(xlab)) - minmargin = 3 - else - minmargin = 5 - if(main=="") topmargin=1 else topmargin=4 - if(las > 1) {# set the bottom border to accommodate labels - bottommargin = max(c(minmargin, (strwidth(columnnames,units="in"))/par("cin")[1])) * cex.lab - if(bottommargin > 10 ) { - bottommargin<-10 - columnnames<-substr(columnnames,1,19) - # par(srt=45) #TODO figure out how to use text() and srt to rotate long labels - } - } - else { - bottommargin = minmargin - } - par(mar = c(bottommargin, 4, topmargin, 2) +.1) - if(any(is.infinite(constraints$max)) | any(is.infinite(constraints$min))){ - # set ylim based on weights if box constraints contain Inf or -Inf - ylim <- range(object$weights) - } else { - # set ylim based on the range of box constraints min and max - ylim <- range(c(constraints$min, constraints$max)) - } - plot(object$weights, type="b", col="blue", axes=FALSE, xlab='', ylim=ylim, ylab="Weights", main=main, pch=16, ...) - if(!any(is.infinite(constraints$min))){ - points(constraints$min, type="b", col="darkgray", lty="solid", lwd=2, pch=24) - } - if(!any(is.infinite(constraints$max))){ - points(constraints$max, type="b", col="darkgray", lty="solid", lwd=2, pch=25) - } - # if(!is.null(neighbors)){ - # if(is.vector(neighbors)){ - # xtract=extractStats(ROI) - # weightcols<-grep('w\\.',colnames(xtract)) #need \\. to get the dot - # if(length(neighbors)==1){ - # # overplot nearby portfolios defined by 'out' - # orderx = order(xtract[,"out"]) - # subsetx = head(xtract[orderx,], n=neighbors) - # for(i in 1:neighbors) points(subsetx[i,weightcols], type="b", col="lightblue") - # } else{ - # # assume we have a vector of portfolio numbers - # subsetx = xtract[neighbors,weightcols] - # for(i in 1:length(neighbors)) points(subsetx[i,], type="b", col="lightblue") - # } - # } - # if(is.matrix(neighbors) | is.data.frame(neighbors)){ - # # the user has likely passed in a matrix containing calculated values for risk.col and return.col - # nbweights<-grep('w\\.',colnames(neighbors)) #need \\. to get the dot - # for(i in 1:nrow(neighbors)) points(as.numeric(neighbors[i,nbweights]), type="b", col="lightblue") - # # note that here we need to get weight cols separately from the matrix, not from xtract [TRUNCATED] To get the complete diff run: svnlook diff /svnroot/returnanalytics -r 3303 From noreply at r-forge.r-project.org Sun Jan 19 21:09:15 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Sun, 19 Jan 2014 21:09:15 +0100 (CET) Subject: [Returnanalytics-commits] r3304 - pkg/PortfolioAnalytics Message-ID: <20140119200915.2A2E41810EC@r-forge.r-project.org> Author: braverock Date: 2014-01-19 21:09:14 +0100 (Sun, 19 Jan 2014) New Revision: 3304 Added: pkg/PortfolioAnalytics/codeblock.txt Modified: pkg/PortfolioAnalytics/.Rbuildignore Log: - add Copyright template - update .Rbuildignore Modified: pkg/PortfolioAnalytics/.Rbuildignore =================================================================== --- pkg/PortfolioAnalytics/.Rbuildignore 2014-01-19 19:59:05 UTC (rev 3303) +++ pkg/PortfolioAnalytics/.Rbuildignore 2014-01-19 20:09:14 UTC (rev 3304) @@ -3,3 +3,4 @@ .svn ^\.Rproj\.user$ ^.*\.Rproj$ +codeblock.txt Added: pkg/PortfolioAnalytics/codeblock.txt =================================================================== --- pkg/PortfolioAnalytics/codeblock.txt (rev 0) +++ pkg/PortfolioAnalytics/codeblock.txt 2014-01-19 20:09:14 UTC (rev 3304) @@ -0,0 +1,12 @@ + +############################################################################### +# R (http://r-project.org/) Numeric Methods for Optimization of Portfolios +# +# Copyright (c) 2004-2014 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt +# +# This library is distributed under the terms of the GNU Public License (GPL) +# for full details see the file COPYING +# +# $Id: applyFUN.R 3302 2014-01-19 19:52:42Z braverock $ +# +############################################################################### From noreply at r-forge.r-project.org Wed Jan 29 00:30:51 2014 From: noreply at r-forge.r-project.org (noreply at r-forge.r-project.org) Date: Wed, 29 Jan 2014 00:30:51 +0100 (CET) Subject: [Returnanalytics-commits] r3305 - pkg/PerformanceAnalytics/R Message-ID: <20140128233051.518081846C6@r-forge.r-project.org> Author: peter_carl Date: 2014-01-29 00:30:50 +0100 (Wed, 29 Jan 2014) New Revision: 3305 Modified: pkg/PerformanceAnalytics/R/Omega.R Log: - fixed Hmisc reference for CRAN note Modified: pkg/PerformanceAnalytics/R/Omega.R =================================================================== --- pkg/PerformanceAnalytics/R/Omega.R 2014-01-19 20:09:14 UTC (rev 3304) +++ pkg/PerformanceAnalytics/R/Omega.R 2014-01-28 23:30:50 UTC (rev 3305) @@ -143,7 +143,7 @@ a = min(x) b = max(x) - xcdf = Hmisc:::Ecdf.default(x, pl=FALSE) + xcdf = Hmisc::Ecdf.default(x, pl=FALSE) f <- approxfun(xcdf$x,xcdf$y,method="linear",ties="ordered") if(output == "full") {