Path: blob/master/R/inverse.volatility.weight.R
1433 views
12#' Create an inverse volatility weighted portfolio3#'4#' This function calculates objective measures for an equal weight portfolio.5#'6#' @details7#' This function is simply a wrapper around \code{\link{constrained_objective}}8#' to calculate the objective measures in the given \code{portfolio} object of9#' an inverse volatility weight portfolio. The portfolio object should include all objectives10#' to be calculated.11#'12#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset returns13#' @param portfolio an object of type "portfolio" specifying the constraints and objectives for the optimization14#' @param \dots any other passthru parameters to \code{constrained_objective}15#' @return a list containing the returns, weights, objective measures, call, and portfolio object16#' @author Peter Carl17#' @export18inverse.volatility.weight <- function(R, portfolio, ...){19# Check for portfolio object passed in20if(!is.portfolio(portfolio)) stop("portfolio object passed in must be of class 'portfolio'")2122# get asset information for equal weight portfolio23assets <- portfolio$assets24nassets <- length(assets)2526# make sure the number of columns in R matches the number of assets27if(ncol(R) != nassets){28if(ncol(R) > nassets){29R <- R[, 1:nassets]30warning("number of assets is less than number of columns in returns object, subsetting returns object.")31} else {32stop("number of assets is greater than number of columns in returns object")33}34}3536# Here, max_sum will be 1.0 if not explicitly set by caller37max_sum <- get_constraints(portfolio)$max_sum3839invVol <- 1 / StdDev(R)40weights <- max_sum * as.vector(invVol / sum(invVol))41names(weights) <- names(assets)4243tmpout <- constrained_objective(w=weights, R=R, portfolio=portfolio, trace=TRUE, ...)44return(structure(list(45R=R,46weights=weights,47out=tmpout$out,48objective_measures=tmpout$objective_measures,49call=match.call(),50portfolio=portfolio),51class=c("optimize.portfolio.invol", "optimize.portfolio"))52)53}545556###############################################################################57# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios58#59# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt60#61# This library is distributed under the terms of the GNU Public License (GPL)62# for full details see the file COPYING63#64# $Id$65#66###############################################################################676869