[Returnanalytics-commits] r2648 - in pkg/PerformanceAnalytics/sandbox/pulkit: week3_4/code week5

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 26 17:25:01 CEST 2013


Author: pulkit
Date: 2013-07-26 17:25:01 +0200 (Fri, 26 Jul 2013)
New Revision: 2648

Added:
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/edd.R
Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R
Log:
Economic Drawdown

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R	2013-07-25 22:12:37 UTC (rev 2647)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R	2013-07-26 15:25:01 UTC (rev 2648)
@@ -1,5 +1,19 @@
+## A set of functions for Triple Penance Rule
+##
+## These set of functions are used for calculating Maximum Drawdown and Maximum Time under water
+## for different distributions such as normal and non-normal.
+## 
+## FUNCTIONS:
+## dd_norm
+## tuw_norm
+## get_minq
+## getQ
+## get_TuW
+##
+## REFERENCE:
+## Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs
+## and the ‘Triple Penance’ Rule(January 1, 2013).
 
-
 dd_norm<-function(x,confidence){
     # DESCRIPTION:
     # A function to return the maximum drawdown for a normal distribution
@@ -79,6 +93,7 @@
     # dp0: The r0 or the first return
     #
     # confidence: The confidence level of the quantile function
+  
     mu_new = (phi^(bets+1)-phi)/(1-phi)*(dp0-mu)+mu*bets
     var = sigma^2/(phi-1)^2
     var = var*((phi^(2*(bets+1))-1)/(phi^2-1)-2*(phi^(bets+1)-1)/(phi-1)+bets +1)
@@ -116,6 +131,24 @@
 
 
 diff_Q<-function(bets,phi,mu,sigma,dp0,confidence){
+  
+  # DESCRIPTION:
+  # The functions to be minimized to calculate the maximum Time Under water using
+  # Golden section algorithm
+  #
+  # Inputs:
+  # bets: The number fo steps
+  #
+  # phi: The coefficient for AR[1]
+  #
+  # mu: The mean of the returns
+  #
+  # sigma: The standard deviation of the returns
+  #
+  # dp0: The r0 or the first return
+  #
+  # confidence: The confidence level of the quantile function
+  
     return(abs(getQ(bets,phi,mu,sigma,dp0,confidence)))
 }
 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R	2013-07-25 22:12:37 UTC (rev 2647)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R	2013-07-26 15:25:01 UTC (rev 2648)
@@ -75,10 +75,9 @@
   dynamicPort<-function(x,column){
     if(type == "calibrated"){
       if(asset == "one"){
+        mu = mean(x[,column])
         factor = (sharpe[,column]/sd[,column]+0.5)/(1-delta^2)
-        print(factor)
         xt = ifelse(factor*(delta-x)/(1-x)>0,factor*(delta-x)/(1-x),0)
-        print(sd[,column])
       }
       if(asset == "two"){
         if(column == 1){

Added: pkg/PerformanceAnalytics/sandbox/pulkit/week5/edd.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/edd.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/edd.R	2013-07-26 15:25:01 UTC (rev 2648)
@@ -0,0 +1,84 @@
+#'@title Calculate the Economic Drawdown
+#'
+#'@description
+#'\code{EconomicDrawdown} calculates the  Economic Drawdown(EDD) for
+#'a return series.To calculate the economic drawdown cumulative 
+#'return and economic max is calculated for each point. The risk 
+#'free return(rf)  is taken as the input.
+#'
+#'Economic Drawdown is given by the equation
+#'
+#'\deqn{EDD(t)=1-\frac{W_t}/{EM(t)}}
+#'
+#'Here EM stands for Economic Max and is the code \code{\link{EconomicMax}}
+#' 
+#'
+#'@param R an xts, vector, matrix, data frame, timeseries, or zoo object of asset return.
+#'@param Rf risk free rate can be vector such as government security rate of return 
+#'@param geometric utilize geometric chaining (TRUE) or simple/arithmetic chaining(FALSE)
+#'to aggregate returns, default is TRUE
+#'@param \dots any other  variable
+#'@references Yang, Z. George and Zhong, Liang, Optimal Portfolio Strategy to 
+#'Control Maximum Drawdown - The Case of Risk Based Dynamic Asset Allocation (February 25, 2012)
+#'@examples
+#'EconomicDrawdown(edhec,0.08,100)
+#'
+#' @export
+EconomicDrawdown<-function(R,Rf, geometric = TRUE,...)
+{
+  
+  # DESCRIPTION:
+  # calculates the Economic Drawdown(EDD) for
+  # a return series.To calculate the economic drawdown cumulative 
+  # return and rolling economic max is calculated for each point. The risk 
+  # free return(rf) is taken as the input.
+  
+  # FUNCTION:
+  x = checkData(R)
+  columns = ncol(x)
+  n = nrow(x)
+  columnnames = colnames(x)
+  rf = checkData(Rf)
+  nr = length(Rf)
+  if(nr != 1 && nr != n ){
+    stop("The number of rows of the returns and the risk free rate do not match")
+  }
+  
+  EDD<-function(xh,geometric){
+    if(geometric)
+      Return.cumulative = cumprod(1+xh)
+    else Return.cumulative = 1 + cumsum(xh)
+    l = length(Return.cumulative)
+    if(nr == 1){
+      EM = max(Return.cumulative*(1+rf)^(l-c(1:l)))
+    }
+    else{
+      rf = rf[index(xh)]
+      prodRf = cumprod(1+rf)
+      EM = max(Return.cumulative*as.numeric(last(prodRf)/prodRf))
+    }
+    result = 1 - last(Return.cumulative)/EM
+  }
+  
+  for(column in 1:columns){
+    column.drawdown <- as.xts(apply.fromstart(x[,column], FUN = EDD, geometric = geometric,gap = 1))
+    if(column == 1)
+      Economicdrawdown = column.drawdown
+    else Economicdrawdown = merge(Economicdrawdown, column.drawdown) 
+  }
+  colnames(Economicdrawdown) = columnnames
+  Economicdrawdown = reclass(Economicdrawdown, x)
+  return(Economicdrawdown)
+}
+
+###############################################################################
+# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
+#
+# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: edd.R $
+#
+##############################################################################



More information about the Returnanalytics-commits mailing list