PerformanceAnalytics icon indicating copy to clipboard operation
PerformanceAnalytics copied to clipboard

maxDrawdown fails for single-column ts object

Open kschmid opened this issue 9 years ago • 8 comments

I tried the PerformanceAnalytics package mainly for the maxDrawdowns method. (This was with the PerformanceAnalytics as downloaded from CRAN - I hope, this is the same..?)

As per its docu, it should allow a vector as an input, but it seems, this is not working:

maxDrawdown(c(1,3,2,5,1)) [1] 0 Warning: In merge.zoo(fx, .xts(, .index(x))) : Index vectors are of different classes: integer POSIXct

I then tried to create a zoo object or other time series objects (e.g. ts), but ran into the problem that they are neither a vector, nor do they seem to have a number of columns:

maxDrawdown(x) Error in if (is.vector(R) || ncol(R) == 1) { : missing value where TRUE/FALSE needed

kschmid avatar Jul 15 '16 19:07 kschmid

I can verify the warning if I use a vector, and an error if I use the long-deprecated 'ts' class (does anyone actually use this anymore?), but it works for vector, data.frame, matrix, zoo, and xts.

example:

data(edhec)
maxDrawdown(edhec[,1])
maxDrawdown(as.data.frame(edhec[,1]))
maxDrawdown(as.zoo(edhec[,1]))
maxDrawdown(as.matrix(edhec[,1]))
maxDrawdown(as.vector(edhec[,1])) # generates warning
maxDrawdown(as.ts(edhec[,1])) # fails

braverock avatar Jul 15 '16 19:07 braverock

The error with ts objects is because ts objects with only one column don't set a ncol attribute (see what I said above about deprecated... there are reasons no-one uses ts anymore). It works with a warning for a two or more column ts object, e.g.:

maxDrawdown(as.ts(edhec[,1:2]))

braverock avatar Jul 15 '16 19:07 braverock

Just for completeness, it also works for TimeSeries objects and irts objects.

braverock avatar Jul 15 '16 19:07 braverock

I'll also note that most functions in PerformanceAnalytics normally/mostly expect returns, not cash P&L. Your example, which contrived, has no drawdown, but implies returns of up to 500% per period, which seems implausible.

braverock avatar Jul 15 '16 19:07 braverock

sorry, I can confirm all of this. This was my misunderstanding / mistake. Indeed this is only a problem for ts (i.e., if considered deprecated not really a problem) - and the warning is somewhat annoying, but might not be a true problem in practice. So, perhaps it is best to close / delete this issue.

kschmid avatar Jul 15 '16 19:07 kschmid

It is a problem for single-column ts objects (though works for multi-column ts objects), so I wouldn't consider this a high priority issue, but I might want to sort out how to fix it.

This is a very old function, with initial code going back well over a decade, so internally, it uses a matrix. I could probably update the function to use xts internally, which would allow even the single-column ts case to work.

I've updated the title to reflect the actual problem, and I'm OK leaving it open.

braverock avatar Jul 15 '16 19:07 braverock

@braverock If you do use xts internally, consider using the try.xts/reclass paradigm. TTR has tons of examples.

joshuaulrich avatar Jul 15 '16 20:07 joshuaulrich

@joshuaulrich yes, we use that paradigm inside checkData in PerformanceAnalytics and in many PerfA functions already. This code is just old enough that it was originally written on matrix objects, pre-xts

braverock avatar Jul 15 '16 20:07 braverock