[Returnanalytics-commits] r2617 - in pkg/PortfolioAnalytics: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Mon Jul 22 03:18:35 CEST 2013
Author: rossbennett34
Date: 2013-07-22 03:18:35 +0200 (Mon, 22 Jul 2013)
New Revision: 2617
Added:
pkg/PortfolioAnalytics/man/summary.optimize.portfolio.Rd
Modified:
pkg/PortfolioAnalytics/NAMESPACE
pkg/PortfolioAnalytics/R/generics.R
Log:
modifying print and summary methods for objective measures
Modified: pkg/PortfolioAnalytics/NAMESPACE
===================================================================
--- pkg/PortfolioAnalytics/NAMESPACE 2013-07-22 00:31:59 UTC (rev 2616)
+++ pkg/PortfolioAnalytics/NAMESPACE 2013-07-22 01:18:35 UTC (rev 2617)
@@ -62,6 +62,7 @@
export(set.portfolio.moments_v2)
export(set.portfolio.moments)
export(summary.optimize.portfolio.rebalancing)
+export(summary.optimize.portfolio)
export(summary.portfolio)
export(trailingFUN)
export(turnover_constraint)
Modified: pkg/PortfolioAnalytics/R/generics.R
===================================================================
--- pkg/PortfolioAnalytics/R/generics.R 2013-07-22 00:31:59 UTC (rev 2616)
+++ pkg/PortfolioAnalytics/R/generics.R 2013-07-22 01:18:35 UTC (rev 2617)
@@ -137,7 +137,7 @@
# get objective measure
cat("Objective Measure:\n")
- print.default(object$out, digits=digits)
+ print(as.numeric(object$out), digits=digits)
cat("\n")
}
@@ -163,9 +163,12 @@
cat("\n")
# get objective measure
+ objective_measures <- object$objective_measures
+ tmp_obj <- as.numeric(unlist(objective_measures))
+ names(tmp_obj) <- names(objective_measures)
cat("Objective Measures:\n")
- for(obj in object$objective_measures){
- print.default(obj, digits=digits)
+ for(i in 1:length(tmp_obj)){
+ print(tmp_obj[i], digits=digits)
cat("\n")
}
cat("\n")
@@ -193,9 +196,12 @@
cat("\n")
# get objective measure
+ objective_measures <- object$objective_measures
+ tmp_obj <- as.numeric(unlist(objective_measures))
+ names(tmp_obj) <- names(objective_measures)
cat("Objective Measures:\n")
- for(obj in object$objective_measures){
- print.default(obj, digits=digits)
+ for(i in 1:length(tmp_obj)){
+ print(tmp_obj[i], digits=digits)
cat("\n")
}
cat("\n")
@@ -223,9 +229,12 @@
cat("\n")
# get objective measure
+ objective_measures <- object$objective_measures
+ tmp_obj <- as.numeric(unlist(objective_measures))
+ names(tmp_obj) <- names(objective_measures)
cat("Objective Measures:\n")
- for(obj in object$objective_measures){
- print.default(obj, digits=digits)
+ for(i in 1:length(tmp_obj)){
+ print(tmp_obj[i], digits=digits)
cat("\n")
}
cat("\n")
@@ -253,11 +262,183 @@
cat("\n")
# get objective measure
+ # get objective measure
+ objective_measures <- object$objective_measures
+ tmp_obj <- as.numeric(unlist(objective_measures))
+ names(tmp_obj) <- names(objective_measures)
cat("Objective Measures:\n")
- for(obj in object$objective_measures){
- print.default(obj, digits=digits)
+ for(i in 1:length(tmp_obj)){
+ print(tmp_obj[i], digits=digits)
cat("\n")
}
cat("\n")
}
+#' Summarizing Output of optimize.portfolio
+#'
+#' summary method for class "optimize.portfolio"
+#'
+#' @param object an object of class "optimize.portfolio.pso" resulting from a call to optimize.portfolio
+#' @param ... any other passthru parameters. Currently not used.
+#' @export
+summary.optimize.portfolio <- function(object, ...){
+
+ cat(rep("*", 50) ,"\n", sep="")
+ cat("PortfolioAnalytics Optimization Summary", "\n")
+ cat(rep("*", 50) ,"\n", sep="")
+
+ # show the call to optimize.portfolio
+ cat("\nCall:\n", paste(deparse(object$call), sep = "\n", collapse = "\n"),
+ "\n\n", sep = "")
+
+ # get optimal weights
+ cat("Optimal Weights:\n")
+ print.default(object$weights)
+ cat("\n")
+
+ # objective measures
+ # The objective measure is object$out for ROI
+ cat("Objective Measures:\n")
+ if(!is.null(object$objective_measures)){
+ # get objective measure
+ objective_measures <- object$objective_measures
+ tmp_obj <- as.numeric(unlist(objective_measures))
+ names(tmp_obj) <- names(objective_measures)
+ for(i in 1:length(tmp_obj)){
+ print(tmp_obj[i])
+ cat("\n")
+ }
+ } else {
+ print(as.numeric(object$out))
+ }
+ cat("\n")
+
+ # get seed portfolio
+ cat("Portfolio Assets and Seed Weights:\n")
+ print.default(object$portfolio$assets)
+ cat("\n")
+
+ # summary of the portfolio object
+ summary(object$portfolio)
+
+ # Constraints
+ cat(rep("*", 40), "\n", sep="")
+ cat("Constraints\n")
+ cat(rep("*", 40), "\n", sep="")
+
+ # get the constraints
+ constraints <- get_constraints(object$portfolio)
+
+ # leverage constraints
+ cat("Leverage Constraint:\n")
+ if(!is.null(constraints$min_sum) & !is.null(constraints$max_sum)){
+ cat("min_sum = ", constraints$min_sum, "\n", sep="")
+ cat("max_sum = ", constraints$max_sum, "\n", sep="")
+ cat("\n")
+ }
+
+ # box constraints
+ cat("Box Constraints:\n")
+ if(!is.null(constraints$min) & !is.null(constraints$max)){
+ cat("min:\n")
+ print(constraints$min)
+ cat("max:\n")
+ print(constraints$max)
+ cat("\n")
+ }
+
+ # group constraints
+ cat("Group Constraints:\n")
+ if(!is.null(constraints$groups) & !is.null(constraints$cLO) & !is.null(constraints$cUP)){
+ cat("Groups:\n")
+ groups <- constraints$groups
+ group_labels <- constraints$group_labels
+ names(groups) <- group_labels
+ print(groups)
+ cat("\n")
+ cat("Lower bound on group weights, group_min:\n")
+ cLO <- constraints$cLO
+ names(cLO) <- group_labels
+ print(cLO)
+ cat("\n")
+ cat("Upper bound on group weights, group_max:\n")
+ cUP <- constraints$cUP
+ names(cUP) <- group_labels
+ print(cUP)
+ cat("\n")
+ cat("Group position limits, group_pos:\n")
+ group_pos <- constraints$group_pos
+ if(!is.null(group_pos)) names(group_pos) <- group_labels
+ print(group_pos)
+ cat("\n")
+
+ cat("Group Weights:\n")
+ n.groups <- length(groups)
+ group_weights <- rep(0, n.groups)
+ k <- 1
+ l <- 0
+ for(i in 1:n.groups){
+ j <- groups[i]
+ group_weights[i] <- sum(object$weights[k:(l+j)])
+ k <- k + j
+ l <- k - 1
+ }
+ names(group_weights) <- group_labels
+ print(group_weights)
+ cat("\n")
+ }
+ tolerance <- .Machine$double.eps^0.5
+
+ # position limit constraints
+ cat("Position Limit Constraints:\n")
+ cat("Maximum number of non-zero weights, max_pos:\n")
+ print(constraints$max_pos)
+ cat("Realized number of non-zero weights (i.e. positions):\n")
+ print(sum(abs(object$weights) > tolerance))
+ cat("\n")
+
+ cat("Maximum number of long positions, max_pos_long:\n")
+ print(constraints$max_pos_long)
+ cat("Realized number of long positions:\n")
+ print(sum(object$weights > tolerance))
+ cat("\n")
+
+ cat("Maximum number of short positions, max_pos_short:\n")
+ print(constraints$max_pos_short)
+ cat("Realized number of short positions:\n")
+ print(sum(object$weights < -tolerance))
+ cat("\n\n")
+
+ # diversification
+ cat("Diversification Target Constraint:\n")
+ print(constraints$div_target)
+ cat("\n")
+ cat("Realized diversification:\n")
+ print(diversification(object$weights))
+ cat("\n")
+
+ # turnover
+ cat("Turnover Target Constraint:\n")
+ print(constraints$turnover_target)
+ cat("\n")
+ cat("Realized turnover:\n")
+ print(turnover(object$weights, wts.init=object$portfolio$assets))
+ cat("\n")
+
+ # Objectives
+ cat(rep("*", 40), "\n", sep="")
+ cat("Objectives\n")
+ cat(rep("*", 40), "\n\n", sep="")
+
+ for(obj in object$portfolio$objectives){
+ cat("Objective:", class(obj)[1], "\n")
+ print(obj)
+ cat("\n", rep("*", 40), "\n", sep="")
+ }
+ cat("\n")
+
+ # show the elapsed time for the optimization
+ cat("Elapsed Time:\n")
+ print(object$elapsed_time)
+ cat("\n")
+}
Added: pkg/PortfolioAnalytics/man/summary.optimize.portfolio.Rd
===================================================================
--- pkg/PortfolioAnalytics/man/summary.optimize.portfolio.Rd (rev 0)
+++ pkg/PortfolioAnalytics/man/summary.optimize.portfolio.Rd 2013-07-22 01:18:35 UTC (rev 2617)
@@ -0,0 +1,17 @@
+\name{summary.optimize.portfolio}
+\alias{summary.optimize.portfolio}
+\title{Summarizing Output of optimize.portfolio}
+\usage{
+ summary.optimize.portfolio(object, ...)
+}
+\arguments{
+ \item{object}{an object of class "optimize.portfolio.pso"
+ resulting from a call to optimize.portfolio}
+
+ \item{...}{any other passthru parameters. Currently not
+ used.}
+}
+\description{
+ summary method for class "optimize.portfolio"
+}
+
More information about the Returnanalytics-commits
mailing list