[Returnanalytics-commits] r3052 - in pkg/PerformanceAnalytics/sandbox/pulkit: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Sep 11 03:11:07 CEST 2013


Author: pulkit
Date: 2013-09-11 03:11:06 +0200 (Wed, 11 Sep 2013)
New Revision: 3052

Added:
   pkg/PerformanceAnalytics/sandbox/pulkit/R/Penance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/man/Penance.Rd
Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/DESCRIPTION
   pkg/PerformanceAnalytics/sandbox/pulkit/NAMESPACE
   pkg/PerformanceAnalytics/sandbox/pulkit/R/MaxDD.R
   pkg/PerformanceAnalytics/sandbox/pulkit/R/TriplePenance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/R/chart.Penance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/R/table.Penance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/man/BenchmarkSR.Rd
   pkg/PerformanceAnalytics/sandbox/pulkit/man/chart.Penance.Rd
   pkg/PerformanceAnalytics/sandbox/pulkit/man/table.Penance.Rd
Log:
Added Penance.R plus some correction and examples

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/DESCRIPTION
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/DESCRIPTION	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/DESCRIPTION	2013-09-11 01:11:06 UTC (rev 3052)
@@ -48,3 +48,4 @@
     'capm_aorda.R'
     'psr_python.R'
     'ret.R'
+    'Penance.R'

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/NAMESPACE
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/NAMESPACE	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/NAMESPACE	2013-09-11 01:11:06 UTC (rev 3052)
@@ -14,6 +14,7 @@
 export(MinTrackRecord)
 export(MonteSimulTriplePenance)
 export(MultiBetaDrawdown)
+export(Penance)
 export(ProbSharpeRatio)
 export(PsrPortfolio)
 export(REDDCOPS)

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/R/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/R/MaxDD.R	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/R/MaxDD.R	2013-09-11 01:11:06 UTC (rev 3052)
@@ -91,6 +91,7 @@
     }
     if(type[1]=="normal"){
         result = apply(x,MARGIN = 2,dd_norm,confidence)
+        print(result)
     }
     result = round(result,3)
   rownames(result) = c("MaxDD(in %)","t*")

Added: pkg/PerformanceAnalytics/sandbox/pulkit/R/Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/R/Penance.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/R/Penance.R	2013-09-11 01:11:06 UTC (rev 3052)
@@ -0,0 +1,77 @@
+#' @title
+#' Penance
+#'
+#'@description
+#'A plot for Penance vs phi for the given portfolio
+#'The relationship between penance and phi is given by
+#'
+#'\deqn{penance = \frac{Maximum Time under water}{t_\alpha^{*}-1}}
+#'
+#'Penance Measures how long it takes to recover from the maximum drawdown
+#'as a multiple of the time it took to reach the bottom. Penance is smaller,
+#'the higher the value of \eqn{\phi(Phi)} and the higher the ratio \eqn{\frac{\mu}{\sigma}}.
+#'Positive serial autocorrelation leads to smaller Penance due to greater periods under 
+#'water.
+#' @param R Returns
+#' @param confidence the confidence interval
+#' @param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.
+#' @param \dots any other passthru variable
+#' @author Pulkit Mehrotra
+#' @seealso  \code{\link{chart.Penance}} \code{\link{table.Penance}} \code{\link{TuW}}
+#' @references Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the "Triple Penance" Rule(January 1, 2013).
+#' 
+#' @examples
+#' data(edhec)
+#' Penance(edhec,0.95,"ar")
+#' Penance(edhec[,1],0.95,"normal") 
+#'@export
+Penance<-function(R,confidence=0.95,type=c("ar","normal"),...)
+{
+  
+  # DESCRIPTION:
+  # Calculates the maximum drawdown for the return series based on the given 
+  # distribution normal or autoregressive.
+  
+  # INPUT:
+  # The Return Series of the portfolio is taken as the input. The Return 
+  # Series can be an xts, vector, matrix, data frame, timeSeries or zoo object of
+  # asset returns. The type of distribution , "normal" or non-normal "ar", The confidence 
+  # level
+  
+  # FUNCTION:
+  x = checkData(R)
+  if(ncol(x)==1 || is.null(R) || is.vector(R)){
+    calcul = FALSE
+    for(i in (1:length(x))){
+      if(!is.na(x[i])){
+        calcul = TRUE
+      }
+    }
+    if(!calcul){
+      result = NA
+    }
+    else{
+      if(type[1]=="ar"){  
+        result = get_penance(x,confidence)
+        }
+      if(type[1]=="normal"){
+          result = penance_norm(x,confidence)
+      }
+    }
+
+    return(result)
+  }
+    if(type[1]=="ar"){
+        result = apply(x,MARGIN = 2,get_penance,confidence)
+    }
+    if(type[1]=="normal"){
+        result = apply(x,MARGIN = 2,penance_norm,confidence)
+    }
+    result = round(result,3)
+    result = as.data.frame(result)
+    result = t(result)
+  rownames(result) = paste("Penance")
+  return(result)  
+}
+
+

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/R/TriplePenance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/R/TriplePenance.R	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/R/TriplePenance.R	2013-09-11 01:11:06 UTC (rev 3052)
@@ -14,6 +14,19 @@
 ## Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs
 ## and the ‘Triple Penance’ Rule(January 1, 2013).
 
+
+penance_norm<-function(x,confidence){
+    # DESCRIPTION:
+    # A function to return the Penance for a normal distribution
+
+    # Inputs:
+    # R: The Return Series
+    #
+    # confidence: The confidence Level
+    penance = (tuw_norm(x,confidence)/dd_norm(x,confidence)[2])-1
+    return(penance)
+}
+ 
 dd_norm<-function(x,confidence){
     # DESCRIPTION:
     # A function to return the maximum drawdown for a normal distribution
@@ -22,6 +35,7 @@
     # R: The Return Series
     #
     # confidence: The confidence Level
+    x = na.omit(x)
     sd = StdDev(x)
     mu = mean(x, na.rm = TRUE)
     dd = max(0,((qnorm(1-confidence)*sd)^2)/(4*mu))
@@ -37,6 +51,7 @@
     # Inputs:
     # R: Return series
     # confidence: The confidence level
+    x = na.omit(x)
     sd = StdDev(x)
     mu = mean(x,na.rm = TRUE)
     tuw = ((qnorm(1-confidence)*sd)/mu)^2
@@ -45,7 +60,17 @@
 }
 
 
+get_penance<-function(x,confidence){
+    # DESCRIPTION:
+    # A function to return the Penance for a first order serially autocorrelated distribution
 
+    # Inputs:
+    # R: The Return Series
+    #
+    # confidence: The confidence Level
+    penance = (get_TuW(x,confidence)/get_minq(x,confidence)[2])-1
+    return(penance)
+}
 get_minq<-function(R,confidence){
   
     # DESCRIPTION:

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/R/chart.Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/R/chart.Penance.R	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/R/chart.Penance.R	2013-09-11 01:11:06 UTC (rev 3052)
@@ -2,11 +2,10 @@
 #'Penance vs phi plot
 #'
 #'@description
-#'
 #'A plot for Penance vs phi for the given portfolio
 #'The relationship between penance and phi is given by
 #'
-#'\deqn{penance = \frac{Maximum Drawdown}{Maximum Time Under Water}}
+#'\deqn{penance = \frac{Maximum Time under water}{t_\alpha^{*}-1}}
 #'
 #'Penance Measures how long it takes to recover from the maximum drawdown
 #'as a multiple of the time it took to reach the bottom. Penance is smaller,
@@ -68,8 +67,9 @@
     phi = 1:columns
     penance = 1:columns
     for(column in 1:columns){
-        phi[column] = cov(x[,column][-1],x[,column][-length(x[,column])])/(cov(x[,column][-length(x[,column])]))
-        penance[column]<-MaxDD(x[,column],confidence,type = type)[1]/TuW(x[,column],confidence,type = type)
+        col_val = na.omit(x[,column])
+        phi[column] = cov(col_val[-1],col_val[-length(col_val)])/(cov(col_val[-length(col_val)]))
+        penance[column]<-Penance(x[,column],confidence,type=type[1])
     }
     if(is.null(ylab)){
       ylab = "Penance"

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/R/table.Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/R/table.Penance.R	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/R/table.Penance.R	2013-09-11 01:11:06 UTC (rev 3052)
@@ -8,13 +8,13 @@
 #'  
 #' @param R Returns
 #' @param confidence the confidence interval
-#' 
+#' @param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.
 #' @author Pulkit Mehrotra
 #' @seealso \code{\link{chart.Penance}} \code{\link{MaxDD}} \code{\link{TuW}}
 #' @references Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the "Triple Penance" Rule(January 1, 2013).
 #' @export
 
-table.Penance<-function(R,confidence=0.95){
+table.Penance<-function(R,confidence=0.95,type=c("ar","norm")){
   # DESCRIPTION:
   # Maximum Drawdown and Time under Water considering first-order serial correlation
   # 
@@ -35,9 +35,10 @@
     sigma_infinity = StdDev(col_val)
     sigma = sigma_infinity*((1-phi^2)^0.5)
     column_MinQ<-c(mean(col_val),sigma_infinity,phi,sigma)
-    column_MinQ <- c(column_MinQ,get_minq(x[,column],confidence))
-    column_TuW = get_TuW(x[,column],confidence)
-    v = c(column_MinQ,column_TuW,(column_TuW/column_MinQ[5])-1)
+    column_MinQ <- c(column_MinQ,MaxDD(x[,column],confidence,type=type[1]))
+    column_TuW = TuW(x[,column],confidence,type=type[1])
+    v = c(column_MinQ,column_TuW,Penance(x[,column],confidence,type=type[1]))
+    v = round(v,4)
     if(column == 1){
       result = data.frame(Value = v, row.names = rownames)
     }

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/man/BenchmarkSR.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/man/BenchmarkSR.Rd	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/man/BenchmarkSR.Rd	2013-09-11 01:11:06 UTC (rev 3052)
@@ -9,17 +9,18 @@
   object of asset returns}
 }
 \description{
-  The benchmark SR is a linear function of the average SR
-  of the individual strategies, and a decreasing convex
-  function of the number of strategies and the average
-  pairwise correlation. The Returns are given as the input
-  with the benchmark Sharpe Ratio as the output.
+  The benchmark SR is a linear function of the average
+  annualized SR of the individual strategies, and a
+  decreasing convex function of the number of strategies
+  and the average pairwise correlation. The Returns are
+  given as the input with the benchmark Sharpe Ratio as the
+  output.
 
   \deqn{SR_B = \bar{SR}\sqrt{\frac{S}{1+(S-1)\bar{\rho}}}}
 
-  Here \eqn{\bar{SR}} is the average SR of the portfolio
-  and \eqn{\bar{\rho}} is the average correlation across
-  off-diagonal elements
+  Here \eqn{\bar{SR}} is the average annualized Sharpe
+  Ratio of the portfolio and \eqn{\bar{\rho}} is the
+  average correlation across off-diagonal elements.
 }
 \examples{
 data(edhec)

Added: pkg/PerformanceAnalytics/sandbox/pulkit/man/Penance.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/man/Penance.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/man/Penance.Rd	2013-09-11 01:11:06 UTC (rev 3052)
@@ -0,0 +1,50 @@
+\name{Penance}
+\alias{Penance}
+\title{Penance}
+\usage{
+  Penance(R, confidence = 0.95, type = c("ar", "normal"),
+    ...)
+}
+\arguments{
+  \item{R}{Returns}
+
+  \item{confidence}{the confidence interval}
+
+  \item{type}{The type of distribution "normal" or
+  "ar"."ar" stands for Autoregressive.}
+
+  \item{\dots}{any other passthru variable}
+}
+\description{
+  A plot for Penance vs phi for the given portfolio The
+  relationship between penance and phi is given by
+
+  \deqn{penance = \frac{Maximum Time under
+  water}{t_\alpha^{*}-1}}
+
+  Penance Measures how long it takes to recover from the
+  maximum drawdown as a multiple of the time it took to
+  reach the bottom. Penance is smaller, the higher the
+  value of \eqn{\phi(Phi)} and the higher the ratio
+  \eqn{\frac{\mu}{\sigma}}. Positive serial autocorrelation
+  leads to smaller Penance due to greater periods under
+  water.
+}
+\examples{
+data(edhec)
+Penance(edhec,0.95,"ar")
+Penance(edhec[,1],0.95,"normal")
+}
+\author{
+  Pulkit Mehrotra
+}
+\references{
+  Bailey, David H. and Lopez de Prado, Marcos,
+  Drawdown-Based Stop-Outs and the "Triple Penance"
+  Rule(January 1, 2013).
+}
+\seealso{
+  \code{\link{chart.Penance}} \code{\link{table.Penance}}
+  \code{\link{TuW}}
+}
+

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/man/chart.Penance.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/man/chart.Penance.Rd	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/man/chart.Penance.Rd	2013-09-11 01:11:06 UTC (rev 3052)
@@ -58,8 +58,8 @@
   A plot for Penance vs phi for the given portfolio The
   relationship between penance and phi is given by
 
-  \deqn{penance = \frac{Maximum Drawdown}{Maximum Time
-  Under Water}}
+  \deqn{penance = \frac{Maximum Time under
+  water}{t_\alpha^{*}-1}}
 
   Penance Measures how long it takes to recover from the
   maximum drawdown as a multiple of the time it took to

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/man/table.Penance.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/man/table.Penance.Rd	2013-09-10 23:57:44 UTC (rev 3051)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/man/table.Penance.Rd	2013-09-11 01:11:06 UTC (rev 3052)
@@ -2,12 +2,16 @@
 \alias{table.Penance}
 \title{Table for displaying the Mximum Drawdown and the Time under Water}
 \usage{
-  table.Penance(R, confidence)
+  table.Penance(R, confidence = 0.95,
+    type = c("ar", "norm"))
 }
 \arguments{
   \item{R}{Returns}
 
   \item{confidence}{the confidence interval}
+
+  \item{type}{The type of distribution "normal" or
+  "ar"."ar" stands for Autoregressive.}
 }
 \description{
   \code{table.Penance} Displays the table showing



More information about the Returnanalytics-commits mailing list