[Returnanalytics-commits] r3227 - pkg/PortfolioAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Oct 18 01:25:33 CEST 2013


Author: rossbennett34
Date: 2013-10-18 01:25:32 +0200 (Fri, 18 Oct 2013)
New Revision: 3227

Modified:
   pkg/PortfolioAnalytics/R/optFUN.R
Log:
Adding require(package) for all optFun functions.

Modified: pkg/PortfolioAnalytics/R/optFUN.R
===================================================================
--- pkg/PortfolioAnalytics/R/optFUN.R	2013-10-17 23:04:24 UTC (rev 3226)
+++ pkg/PortfolioAnalytics/R/optFUN.R	2013-10-17 23:25:32 UTC (rev 3227)
@@ -14,6 +14,8 @@
 #' @author Ross Bennett
 gmv_opt <- function(R, constraints, moments, lambda, target, lambda_hhi, conc_groups){
   stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
+  # stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE))
+  # stopifnot("package:ROI.plugin.quadprog" %in% search() || require("ROI.plugin.quadprog",quietly = TRUE))
   
   N <- ncol(R)
   # Applying box constraints, used for ROI
@@ -132,6 +134,8 @@
 #' @param target target return value
 #' @author Ross Bennett
 maxret_opt <- function(R, moments, constraints, target){
+  stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE))
+  stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE))
   
   N <- ncol(R)
   # Applying box constraints
@@ -212,6 +216,7 @@
 #' @param target target return value
 #' @author Ross Bennett
 maxret_milp_opt <- function(R, constraints, moments, target){
+  stopifnot("package:Rglpk" %in% search() || require("Rglpk",quietly = TRUE))
   
   N <- ncol(R)
   
@@ -280,7 +285,6 @@
   types <- c(rep("C", N), rep("B", N))
   
   # Solve directly with Rglpk... getting weird errors with ROI
-  stopifnot("package:Rglpk" %in% search() || require("Rglpk",quietly = TRUE))
   result <- Rglpk_solve_LP(obj=objL, mat=Amat, dir=dir, rhs=rhs, types=types, bounds=bnds, max=FALSE)
   
   # The Rglpk solvers status returns an an integer with status information
@@ -312,6 +316,8 @@
 #' @param alpha alpha value for ETL/ES/CVaR
 #' @author Ross Bennett
 etl_opt <- function(R, constraints, moments, target, alpha){
+  stopifnot("package:ROI" %in% search() || require("ROI",quietly = TRUE))
+  stopifnot("package:ROI.plugin.glpk" %in% search() || require("ROI.plugin.glpk",quietly = TRUE))
   
   # Check for cleaned returns in moments
   if(!is.null(moments$cleanR)) R <- moments$cleanR
@@ -377,6 +383,8 @@
 #' @author Ross Bennett
 etl_milp_opt <- function(R, constraints, moments, target, alpha){
   
+  stopifnot("package:Rglpk" %in% search() || require("Rglpk",quietly = TRUE))
+  
   # Check for cleaned returns in moments
   if(!is.null(moments$cleanR)) R <- moments$cleanR
   
@@ -473,7 +481,7 @@
   bounds <- list( lower = list( ind = 1L:(m + n + 2 + m), val = c(LB,  -1, rep(0, n), 1, rep(0, m)) ),
                   upper = list( ind = 1L:(m + n + 2 + m), val = c( UB, 1, rep(Inf, n), 1 , rep(1, m)) ) )
   
-  stopifnot("package:Rglpk" %in% search() || require("Rglpk",quietly = TRUE))
+  
   result <- Rglpk_solve_LP(obj=objL, mat=tmpAmat, dir=dir, rhs=rhs, types=types, bounds=bounds)
   # The Rglpk solvers status returns an an integer with status information
   # about the solution returned: 0 if the optimal solution was found, a 
@@ -506,6 +514,8 @@
 #' @author Ross Bennett
 gmv_opt_toc <- function(R, constraints, moments, lambda, target, init_weights){
   # function for minimum variance or max quadratic utility problems
+  stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE))
+  stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
   
   # Check for cleaned returns in moments
   if(!is.null(moments$cleanR)) R <- moments$cleanR
@@ -604,8 +614,7 @@
   
   d <- rep(-moments$mean, 3)
   # print(Amat)
-  stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE))
-  stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
+  
   qp.result <- try(solve.QP(Dmat=make.positive.definite(2*lambda*V), 
                             dvec=d, Amat=t(Amat), bvec=rhs, meq=meq), silent=TRUE)
   if(inherits(qp.result, "try-error")) stop("No solution found, consider adjusting constraints.")
@@ -639,6 +648,8 @@
 gmv_opt_ptc <- function(R, constraints, moments, lambda, target, init_weights){
   # function for minimum variance or max quadratic utility problems
   # modifying ProportionalCostOpt function from MPO package
+  stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE))
+  stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
   
   # Check for cleaned returns in moments
   if(!is.null(moments$cleanR)) R <- moments$cleanR
@@ -723,8 +734,6 @@
   
   d <- rep(-moments$mean, 3)
   
-  stopifnot("package:corpcor" %in% search() || require("corpcor",quietly = TRUE))
-  stopifnot("package:quadprog" %in% search() || require("quadprog",quietly = TRUE))
   qp.result <- try(solve.QP(Dmat=make.positive.definite(2*lambda*V), 
                             dvec=d, Amat=t(Amat), bvec=rhs, meq=meq), silent=TRUE)
   if(inherits(qp.result, "try-error")) stop("No solution found, consider adjusting constraints.")



More information about the Returnanalytics-commits mailing list