[R-SIG-Finance] Possible bug in findDrawdowns

Brian G. Peterson brian at braverock.com
Wed Mar 23 13:43:51 CET 2011


On 03/22/2011 04:55 PM, Worik wrote:
> Given a very short series with a draw down one period from "from" to
> "trough" findDrawdowns seems to find the "from" and "trough" in the same
> place, but the wrong place.
>
> For example:
>
>
>  > Z
> Y returns.Y.
> 1936-02-29 14.5500 0.016632400
> 1936-03-31 14.9600 0.027788979
> 1936-04-30 13.8069 -0.080211505
> 1936-05-31 14.4386 0.044736709
> 1936-06-30 15.0402 0.040821440
> 1936-07-31 16.0638 0.065841677
> 1936-08-31 16.2057 0.008794739
> 1936-09-30 16.4794 0.016748084
> 1936-10-31 17.7146 0.072278042
> 1936-11-30 17.7866 0.004056206
> 1936-12-31 17.9616 0.009790781
>
>  > findDrawdowns(Z[,2])
> $return
> [1] 0.0000000 -0.0802115 0.0000000
>
> $from
> [1] 1 3 5
>
> $trough
> [1] 1 3 5
>
> $to
> [1] 3 5 12
>
> $length
> [1] 3 3 8
>
> $peaktotrough
> [1] 1 1 1
>
> $recovery
> [1] 2 2 7
>
> There is a draw down from position 2 to trough at 3 and regains its
> level in 4. But that is missed.
>
> Either I missunderstand or this is a bug

There may be bug here, but it is not the bug you think it is.

The answer can be seen by looking at the Drawdowns time series:

 > x<-read.csv('worik.csv',stringsAsFactors=FALSE)
 > Z<-as.xts(x[,3,drop=FALSE],order.by=as.Date(x[,1]))

 > Drawdowns(Z)
             returns.Y.
1936-02-29  0.00000000
1936-03-31  0.00000000
1936-04-30 -0.08021150
1936-05-31 -0.03906319
1936-06-30  0.00000000
1936-07-31  0.00000000
1936-08-31  0.00000000
1936-09-30  0.00000000
1936-10-31  0.00000000
1936-11-30  0.00000000
1936-12-31  0.00000000

Note that the -0.08021150 return on period 3 (1936-04-30) creates a 
drawdown that is not resolved until row 5. This would also work without 
geometric returns, but the answer would be the same in terms of length 
of drawdown.

 > findDrawdowns(Z)
$return
[1]  0.0000000 -0.0802115  0.0000000

$from
[1] 1 3 5

$trough
[1] 1 3 5

$to
[1]  3  5 12

$length
[1] 3 3 8

$peaktotrough
[1] 1 1 1

$recovery
[1] 2 2 7

All the stats for the second drawdown reported (the only real one) are 
correct.

The drawdown starts on row 3 (from), has a trough at row 3, and is not 
fully recovered until row 5, for a length of 3.  The peak to trough is 1 
period, and the recovery is two periods.

Typically, the function more likely to be used by a user is the 
table.Drawdowns function, which gives results you're more likely to 
immediately agree with.

 > table.Drawdowns(Z)
         From     Trough         To   Depth Length To Trough Recovery
1 1936-04-30 1936-04-30 1936-06-30 -0.0802      3         1        2

findDrawdowns is really more of a utility function.  if you want to use 
it directly, you'd need to filter out the 0 drawdowns (which basically 
cover the periods of the time series not in drawdown).

For reproducibility, I attach the csv file.

Regards,

    - Brian

-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock
-------------- next part --------------
A non-text attachment was scrubbed...
Name: worik.csv
Type: text/csv
Size: 381 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20110323/6a442842/attachment.bin>


More information about the R-SIG-Finance mailing list