12#' Create an equal weight 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 equal 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 Ross Bennett17#' @export18equal.weight <- function(R, portfolio, ...){19# Check for portfolio object passed in20if(!is.portfolio(portfolio)) stop("portfolio object passed in must be of class 'portfolio'")2122max_sum <- get_constraints(portfolio)$max_sum2324# get asset information for equal weight portfolio25assets <- portfolio$assets26nassets <- length(assets)27weights <- rep(max_sum / nassets, nassets)28names(weights) <- names(assets)2930# make sure the number of columns in R matches the number of assets31if(ncol(R) != nassets){32if(ncol(R) > nassets){33R <- R[, 1:nassets]34warning("number of assets is less than number of columns in returns object, subsetting returns object.")35} else {36stop("number of assets is greater than number of columns in returns object")37}38}3940tmpout <- constrained_objective(w=weights, R=R, portfolio=portfolio, trace=TRUE, ...)41return(structure(list(42R=R,43weights=weights,44out=tmpout$out,45objective_measures=tmpout$objective_measures,46call=match.call(),47portfolio=portfolio),48class=c("optimize.portfolio.eqwt", "optimize.portfolio"))49)50}515253###############################################################################54# R (https://r-project.org/) Numeric Methods for Optimization of Portfolios55#56# Copyright (c) 2004-2021 Brian G. Peterson, Peter Carl, Ross Bennett, Kris Boudt57#58# This library is distributed under the terms of the GNU Public License (GPL)59# for full details see the file COPYING60#61# $Id$62#63###############################################################################646566