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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Aug 18 07:18:37 CEST 2013


Author: rossbennett34
Date: 2013-08-18 07:18:36 +0200 (Sun, 18 Aug 2013)
New Revision: 2813

Modified:
   pkg/PortfolioAnalytics/R/generics.R
Log:
modifying print method for portfolio objects to add description for box constraints and print asset names per Doug's comments

Modified: pkg/PortfolioAnalytics/R/generics.R
===================================================================
--- pkg/PortfolioAnalytics/R/generics.R	2013-08-17 22:18:51 UTC (rev 2812)
+++ pkg/PortfolioAnalytics/R/generics.R	2013-08-18 05:18:36 UTC (rev 2813)
@@ -60,7 +60,12 @@
   # Assets
   cat("\nAssets\n")
   nassets <- length(portfolio$assets)
-  cat("Number of assets:", nassets, "\n")
+  cat("Number of assets:", nassets, "\n\n")
+  cat("Asset Names\n")
+  print(head(names(portfolio$assets), 10))
+  if(nassets > 10){
+    cat("More than 10 assets, only printing the first 10\n")
+  }
   
   # Constraints
   cat("\nConstraints\n")
@@ -79,15 +84,55 @@
   cat("Number of enabled constraints:", n.enabled.constraints, "\n")
   if(length(enabled.constraints) > 0){
     cat("Enabled constraint types\n")
-    for(type in names.constraints[enabled.constraints]) {
-      cat("\t\t-", type, "\n")
+    constraints <- portfolio$constraints
+    nconstraints <- length(constraints)
+    for(i in 1:nconstraints){
+      if(constraints[[i]]$enabled){
+        type <- constraints[[i]]$type
+        if(type == "box"){
+          # long only
+          if(all(constraints[[i]]$min == 0) & all(constraints[[i]]$max == 1)){
+            cat("\t\t-", "box (long only)", "\n")
+          } else if(all(constraints[[i]]$min == -Inf) & all(constraints[[i]]$max == Inf)){
+            # unconstrained
+            cat("\t\t-", "box (unconstrained)", "\n")
+          } else if(any(constraints[[i]]$min < 0)){
+            # with shorting
+            cat("\t\t-", "box (with shorting)", "\n")
+          } else {
+            cat("\t\t-", type, "\n")
+          }
+        } else {
+          cat("\t\t-", type, "\n")
+        }
+      }
     }
   }
   cat("Number of disabled constraints:", nconstraints - n.enabled.constraints, "\n")
   if((nconstraints - n.enabled.constraints) > 0){
     cat("Disabled constraint types\n")
-    for(type in setdiff(names.constraints, names.constraints[enabled.constraints])) {
-      cat("\t\t-", type, "\n")
+    constraints <- portfolio$constraints
+    nconstraints <- length(constraints)
+    for(i in 1:nconstraints){
+      if(!constraints[[i]]$enabled){
+        type <- constraints[[i]]$type
+        if(type == "box"){
+          # long only
+          if(all(constraints[[i]]$min == 0) & all(constraints[[i]]$max == 1)){
+            cat("\t\t-", "box (long only)", "\n")
+          } else if(all(constraints[[i]]$min == -Inf) & all(constraints[[i]]$max == Inf)){
+            # unconstrained
+            cat("\t\t-", "box (unconstrained)", "\n")
+          } else if(any(constraints[[i]]$min < 0)){
+            # with shorting
+            cat("\t\t-", "box (with shorting)", "\n")
+          } else {
+            cat("\t\t-", type, "\n")
+          }
+        } else {
+          cat("\t\t-", type, "\n")
+        }
+      }
     }
   }
   



More information about the Returnanalytics-commits mailing list