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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Oct 17 18:30:37 CEST 2013


Author: rossbennett34
Date: 2013-10-17 18:30:37 +0200 (Thu, 17 Oct 2013)
New Revision: 3222

Added:
   pkg/PortfolioAnalytics/R/inverse.volatility.weight.R
Log:
Adding constructor for inverse volatility weighted portfolio.

Added: pkg/PortfolioAnalytics/R/inverse.volatility.weight.R
===================================================================
--- pkg/PortfolioAnalytics/R/inverse.volatility.weight.R	                        (rev 0)
+++ pkg/PortfolioAnalytics/R/inverse.volatility.weight.R	2013-10-17 16:30:37 UTC (rev 3222)
@@ -0,0 +1,49 @@
+
+
+#' Create an inverse volatility weighted portfolio
+#' 
+#' This function calculates objective measures for an equal weight portfolio.
+#' 
+#' @details
+#' This function is simply a wrapper around \code{\link{constrained_objective}}
+#' to calculate the objective measures in the given \code{portfolio} object of
+#' an inverse volatility weight portfolio. The portfolio object should include all objectives
+#' to be calculated.
+#' 
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns
+#' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization
+#' @param \dots any other passthru parameters to \code{constrained_objective}
+#' @return a list containing the returns, weights, objective measures, call, and portfolio object
+#' @author Peter Carl
+#' @export
+inverse.volatility.weight <- function(R, portfolio, ...){
+  # Check for portfolio object passed in
+  if(!is.portfolio(portfolio)) stop("portfolio object passed in must be of class 'portfolio'")
+  
+  # get asset information for equal weight portfolio
+  assets <- portfolio$assets
+  nassets <- length(assets)
+  
+  # make sure the number of columns in R matches the number of assets
+  if(ncol(R) != nassets){
+    if(ncol(R) > nassets){
+      R <- R[, 1:nassets]
+      warning("number of assets is less than number of columns in returns object, subsetting returns object.")
+    } else {
+      stop("number of assets is greater than number of columns in returns object")
+    }
+  }
+  
+  weights <- as.vector((1/StdDev(R))/sum(1/StdDev(R)))
+  names(weights) <- names(assets)
+  
+  out <- constrained_objective(w=weights, R=R, portfolio=portfolio, trace=TRUE, ...)$objective_measures
+  return(structure(list(
+    R=R,
+    weights=weights,
+    objective_measures=out,
+    call=match.call(),
+    portfolio=portfolio),
+                   class=c("optimize.portfolio.invol", "optimize.portfolio"))
+  )
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list