Date: 2015-03-20 21:42 Sender: Victor Wood if (is.null(weights))
weights = rep(1/dim(x)[[2]], dim(x)[[2]])
should also be included in pfolioTargetReturn to implement the spec "By default NULL, then an equally weighted set of assets is assumed.", making it:
# ------------------------------------------------------------------------------
pfolioReturn <-
function(x, weights=NULL, geometric=FALSE)
{
# A function implemented by Diethelm Wuertz
# Description:
# Returns portfolio returns
# Arguments:
# x - a 'timeSeries' object
# Details:
# A fast(er) reimplementation
# FUNCTION:
# Compute Portfolio Returns:
if (is.null(weights)) # 20150319 VIW By default NULL, then an equally weighted set of assets is assumed.
weights = rep(1/dim(x)[[2]], dim(x)[[2]])
else
weights <- as.vector(weights)
if(geometric) {
X <- t ( colCumprods(1+x) - 1 )
X <- rbind(diff( t ( X * weights ) ))
Return <- x[, 1]
series(Return[+1, ]) <- x[1, ] %*% weights
series(Return[-1, ]) <- rowSums(X)
} else {
Return <- x[, 1]
Return <- as.timeSeries(Return) # 20150319 VIW looks like [Return] will never fit the timeSeries in the [series<-] signature; this forces [Return] to fit that signature
series(Return) <- x %*% weights
}
colnames(Return) <- "pfolioRet"
# Return Value:
Return
}
# -----------------------------------------------------------------------------
|