[Returnanalytics-commits] r2619 - in pkg/PerformanceAnalytics/sandbox/pulkit: week2/code week2/tests week3_4/code

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jul 22 15:44:16 CEST 2013


Author: pulkit
Date: 2013-07-22 15:44:16 +0200 (Mon, 22 Jul 2013)
New Revision: 2619

Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkPlots.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week2/tests/test_SharpeIndifference.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/chart.Penance.R
Log:
changes in Triple Penance and Becnhmark plots

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkPlots.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkPlots.R	2013-07-22 10:38:54 UTC (rev 2618)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkPlots.R	2013-07-22 13:44:16 UTC (rev 2619)
@@ -3,8 +3,17 @@
 #'@description
 #'Benchmark Sharpe Ratio Plots are used to give the relation ship between the
 #'Benchmark Sharpe Ratio and average correlation,average sharpe ratio or the number of #'strategies keeping other parameters constant. Here average Sharpe ratio , average #'correlation stand for the average of all the strategies in the portfolio. The original 
-#'point of the return series is also shown on the plots. 
+#'point of the return series is also shown on the plots.
 #'
+#'The equation for the Benchamark Sharpe Ratio is.
+#'
+#'\deqn{SR_B = \overline{SR}\sqrt{\frac{S}{1+(S-1)\overline{\rho}}}}
+#'
+#'Here \eqn{S} is the number of strategies and \eqn{\overline{\rho}} is the average 
+#'correlation across off diagonal elements and is given by
+#'
+#'\deqn{\overline{\rho} = \frac{2\sum_{s=1}^{S} \sum_{t=s+1}^{S} \rho_{S,t}}{S(S-1)}}
+#'
 #'@param R an xts, vector, matrix, data frame, timeSeries or zoo object of
 #' asset returns
 #'@param ylab set the y-axis label, as in \code{\link{plot}}
@@ -133,4 +142,4 @@
 #
 # $Id: BenchmarkSRPlots.R $
 #
-###############################################################################
\ No newline at end of file
+###############################################################################

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week2/tests/test_SharpeIndifference.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week2/tests/test_SharpeIndifference.R	2013-07-22 10:38:54 UTC (rev 2618)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week2/tests/test_SharpeIndifference.R	2013-07-22 13:44:16 UTC (rev 2619)
@@ -4,6 +4,6 @@
 
 test_BenchmarkSR<-function(){
   
-  checkEqualsNumeric(BenchmanrkSR(edhec),0.170288,tolerance = 1.0e-6)
+  checkEqualsNumeric(BenchmarkSR(edhec),0.393797,tolerance = 1.0e-6)
   
 }
\ No newline at end of file

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R	2013-07-22 10:38:54 UTC (rev 2618)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R	2013-07-22 13:44:16 UTC (rev 2619)
@@ -1,17 +1,61 @@
-
 #' @title
 #' Triple Penance Rule
 #'
 #' @description
 #' \code{MaxDD} calculates the Maximum drawdown for a particular confidence interval.
+#' Maximum Drawdown tells us Up to how much could a particular strategy lose with 
+#' a given confidence level ?. This function calculated Maximum Drawdown for two
+#' underlying processes normal and autoregressive. For a normal process 
+#' Maximum Drawdown is given by the formula
+#' When the distibution is normal
+#' 
+#' \deqn{MaxDD_{\alpha}=max\left\{0,\frac{(z_{\alpha}\sigma)^2}{4\mu}\right\}}
+#' 
+#' The time at which the Maximum Drawdown occurs is given by
+#' \deqn{t^\ast=\biggl(\frac{Z_{\alpha}\sigma}{2\mu}\biggr)^2}
+#' Here $Z_{\alpha}$ is the critical value of the Standard Normal Distribution 
+#' associated with a probability $\alpha$.$\sigma$ and $\mu$ are the Standard 
+#' Distribution and the mean respectively.
+#' When the distribution is non-normal and time dependent, Autoregressive process.
+#' 
+#' \deqn{Q_{\alpha,t}=\frac{\phi^{(t+1)}-\phi}{\phi-1}(\triangle\pi_0-\mu)+{\mu}t+Z_{\alpha}\frac{\sigma}{|\phi-1|}\biggl(\frac{\phi^{2(t+1)}-1}{\phi^2-1}-2\frac{\phi^(t+1)-1}{\phi-1}+t+1\biggr)^{1/2}}
+#' 
+#' $\phi$ is estimated as
+#' 
+#' \deqn{\hat{\phi} = Cov_0[\triangle\pi_\tau,\triangle\pi_{\tau-1}](Cov_0[\triangle\pi_{\tau-1},\triangle\pi_{\tau-1}])^{-1}}
+#' 
+#' and the Maximum Drawdown is given by.
+#' 
+#' \deqn{MaxDD_{\alpha}=max\left\{0,-MinQ_\alpha\right\}}
+#' 
+#' Golden Section Algorithm is used to calculate the Minimum of the function Q.
 #'  
 #' @param R Returns
 #' @param confidence the confidence interval
+#' @param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.
 #' 
 #' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
+#' 
+#' @examples
+#' 
+#' data(edhec)
+#' MaxDD(edhec,0.95,"ar")
+#' MaxDD(edhec[,1],0.95,"normal") #expected values 4.241799 6.618966
 
 MaxDD<-function(R,confidence,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)){

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R	2013-07-22 10:38:54 UTC (rev 2618)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R	2013-07-22 13:44:16 UTC (rev 2619)
@@ -1,14 +1,27 @@
 #' @title
-#' Time Under Water
+#' Maximum Time Under Water
 #'
 #' @description
 #' \code{TriplePenance} calculates the maximum 
-#' Time under water for a particular confidence interval. 
+#' Maximum Time under water for a particular confidence interval is given by
+#' 
+#' For a particular sequence $\left\{\pi_t\right\}$, the time under water $(TuW)$ 
+#' is the minimum number of observations, $t>0$, such that $\pi_{t-1}<0$ and $\pi_t>0$. 
+#' 
+#' For a normal distribution Maximum Time Under Water is given by the following expression.
+#' \deqn{MaxTuW_\alpha=\biggl(\frac{Z_\alpha{\sigma}}{\mu}\biggr)^2}
+#' 
+#' For a Autoregressive process the Time under water is found using the golden section algorithm.
 #'
 #' @param R return series
 #' @param confidence the confidence interval
+#' @param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.
 #' 
 #' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
+#' 
+#' @examples
+#' TuW(edhec,0.95,"ar")
+#' uW(edhec[,1],0.95,"normal") # expected value 103.2573 
 
 TuW<-function(R,confidence,type=c("ar","normal"),...){
   x = checkData(R)

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/chart.Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/chart.Penance.R	2013-07-22 10:38:54 UTC (rev 2618)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/chart.Penance.R	2013-07-22 13:44:16 UTC (rev 2619)
@@ -1,15 +1,59 @@
 #'@title
 #'Penance vs phi plot
-#'
 #'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}}
+#'
+#'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 an xts, vector, matrix, data frame,
 #'timeSeries or zoo object of asset returns.
 #'@param confidence the confidence level
+#'@param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.
+#'@param reference.grid if true, draws a grid aligned with the points on the x
+#'and y axes
+#'@param ylab set the y-axis label, as in \code{\link{plot}}
+#'@param xlab set the x-axis label, as in \code{\link{plot}}
+#'@param main set the chart title, as in \code{\link{plot}}
+#'@param element.color set the element.color value as in \code{\link{plot}}
+#'@param lwd set the width of the line, as in \code{\link{plot}}
+#'@param pch set the pch value, as in \code{\link{plot}}
+#'@param cex set the cex value, as in \code{\link{plot}}
+#'@param cex.axis set the cex.axis value, as in \code{\link{plot}}
+#'@param cex.main set the cex.main value, as in \code{\link{plot}}
+#'@param ylim set the ylim value, as in \code{\link{plot}}
+#'@param xlim set the xlim value, as in \code{\link{plot}}
 #'
+#'@seealso \code{\link{plot}}
+#'@keywords ts multivariate distribution models hplot
+#'@examples
+#'
+#'
 #'@reference Bailey, David H. and Lopez de Prado, Marcos,Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
 
-chart.Penance<-function(R,confidence,...){
+chart.Penance<-function(R,confidence,type=c("ar","normal"),reference.grid = TRUE,main=NULL,ylab = NULL,xlab = NULL,element.color="darkgrey",lwd = 2,pch = 1,cex = 1,cex.axis=0.8,cex.lab = 1,cex.main = 1,xlim = NULL,ylim = NULL,...){
+
+  # DESCRIPTION:
+  # Draws the scatter plot of Phi vs Penance.
+  
+  # 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
+  
+  # All other inputs are the same as "plot" and are principally included
+  # so that some sensible defaults could be set.
+  
+  # Output:
+  # Draws the scatter plot of Phi vs Penance with some sensible defaults.
+  
+  # FUNCTION:
+  
     x = checkData(R)
     columns = ncol(x)
     columnnames = colnames(x)
@@ -17,10 +61,25 @@
     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]<-get_minq(x[,column],confidence)[1]/get_TuW(x[,column],confidence)
+        penance[column]<-MaxDD(x[,column],confidence,type = type)[1]/TuW(x[,column],confidence,type = type)
     }
-    plot(x=phi,y=penance,xlab="Phi",ylab = "Penance",main="Penance vs Phi",pch=2)
-    text(phi,penance,columnnames,pos = 4,col=c(1:columns))
+    if(is.null(ylab)){
+      ylab = "Penance"
+    }
+    if(is.null(xlab)){
+      xlab = "Phi"
+    }
+    if(is.null(main)){
+      main = "Penance vs Phi"
+    }
+    
+    plot(x=phi,y=penance,xlab=xlab,ylab=ylab,main=main,lwd = lwd,pch=pch,cex = cex,cex.lab = cex.lab)
+    text(phi,penance,columnnames,pos = 4,col=c(1:columns),cex = 0.8)
+    if(reference.grid) {
+      grid(col = element.color)
+      abline(h = 0, col = element.color)
+      abline(v = 0, col = element.color)
+    }
 }
 
 



More information about the Returnanalytics-commits mailing list