SCM

[#6060] pfolioReturn calls [series<-] with incorrect signature

Date:
2015-03-20 15:07
Priority:
3
State:
Open
Submitted by:
Victor Wood (woodvi)
Assigned to:
Nobody (None)
Hardware:
None
Product:
None
Operating System:
None
Component:
None
Version:
None
Severity:
None
Resolution:
None
URL:
Summary:
pfolioReturn calls [series<-] with incorrect signature

Detailed description
in file
$RMETRICS_HOME\pkg\fPortfolio\R\portfolio-riskPfolio.R
at line 218

I found that the current version of the pfolioReturn function is missing a cast to timeSeries. I've added it in both the geometric and arithmetic branches of the conditional. I've tested it in the arithmetic branch, but it looks like we have further capitalization issues (x vs. X) in the geometric branch.

I'm happy (eager!) to submit my changes via svn except that 1) checkBeforeCommit("fPortfolio") fails on my source, citing LaTeX & PDF errors in the log; and 2) I just applied to the group and haven't been accepted yet. My changes are clearly marked below:



# ------------------------------------------------------------------------------


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:
weights <- as.vector(weights)
if(geometric) {
X <- t ( colCumprods(1+x) - 1 )
X <- rbind(diff( t ( X * weights ) ))
Return <- as.timeSeries(X[, 1]) # 20150319 VIW this forces [Return] to fit [timeSeries] in the [series<-] signature
series(Return[+1, ]) <- x[1, ] %*% weights
series(Return[-1, ]) <- rowSums(X)
} else {
Return <- as.timeSeries(x[, 1]) # 20150319 VIW this forces [Return] to fit [timeSeries] in the [series<-] signature
series(Return) <- x %*% weights
}
colnames(Return) <- "pfolioRet"

# Return Value:
Return
}


# -----------------------------------------------------------------------------



I got help on this from BondedDust on StackOverflow:
http://stackoverflow.com/questions/29154112/how-to-set-up-libraries-for-fportfolio-and-call-pfoliotargetreturn

Comments:

Message  ↓
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
}


# -----------------------------------------------------------------------------


Attached Files:

Changes

No Changes Have Been Made to This Item

Thanks to:
Vienna University of Economics and Business Powered By FusionForge