[Returnanalytics-commits] r2534 - pkg/PerformanceAnalytics/sandbox/pulkit/week3/code

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Jul 10 12:48:31 CEST 2013


Author: pulkit
Date: 2013-07-10 12:48:30 +0200 (Wed, 10 Jul 2013)
New Revision: 2534

Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R
Log:
changes in table.Penance

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R	2013-07-10 10:29:02 UTC (rev 2533)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R	2013-07-10 10:48:30 UTC (rev 2534)
@@ -12,20 +12,29 @@
 
 MaxDD<-function(R,confidence,...)
 {
-    x = checkData(R)
-    columns = ncol(x)
-    i = 0 
-    tp = matrix(nrow=columns,ncol=2)
-
-
-     for(i in 1:columns){
-        column_MinQ <- get_minq(x[,i],confidence)
-        tp[i,] <- column_MinQ
-    } 
- row.names(tp)<-colnames(R)
-  colnames(tp) = c("MaxDD(in %)","t*")
-  return(tp)
+  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
+      }
+    }
+    x = na.omit(x)
+    if(!calcul){
+      result = NA
+    }
+    else{
+      result = get_minq(x,confidence)
+    }
+    return(result)
+  }
+    
+    result = apply(x,MARGIN = 2,get_minq,confidence)
+  rownames(result) = c("MaxDD(in %)","t*")
+  return(result)  
 }
 
 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R	2013-07-10 10:29:02 UTC (rev 2533)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R	2013-07-10 10:48:30 UTC (rev 2534)
@@ -11,19 +11,33 @@
 #' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
 
 TuW<-function(R,confidence,...){
-    x = checkData(R)
-    columns = ncol(R)
-    i = 0 
-    tp = matrix(nrow=columns)
-    for(i in 1:columns){
-        column_TuW = get_TuW(x[,i],confidence)
-        tp[i] <- column_TuW
+  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
+      }
     }
-
-
-rownames(tp)<-colnames(R)
-colnames(tp)<-"Max Time Under Water"
-return(tp)
-}
+    x = na.omit(x)
+    if(!calcul){
+      result = NA
+    }
+    else{
+      result = get_TuW(x,confidence)
+    }
+    return(result)
+  }
+    else{
+      result=apply(x,MARGIN = 2, get_TuW,confidence)
+      result<-as.data.frame(result)
+      result<-t(result)
+      rownames(result)=paste("Max Time Under Water")
+      return(result)
+    }
+    
+  }
       
 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R	2013-07-10 10:29:02 UTC (rev 2533)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R	2013-07-10 10:48:30 UTC (rev 2534)
@@ -20,20 +20,26 @@
   # Creates a Table showing mean stdDev phi sigma MaxDD t* MaxTuW and Penance
   #
   # Function:
-    x = checkData(R)
-    columns = ncol(x) 
-    tp = data.frame()
-    for(i in 1:columns){
-	phi = cov(x[,i][-1],x[,i][-length(x[,i])])/(cov(x[,i][-length(x[,i])]))
-	sigma_infinity = StdDev(x[,i])
-	sigma = sigma_infinity*((1-phi^2)^0.5)
-	column_MinQ<-c(mean(x[,i]),sigma_infinity,phi,sigma)
-        column_MinQ <- c(column_MinQ,get_minq(x[,i],confidence))
-        column_TuW = get_TuW(x[,i],confidence)
-        tp <- rbind(tp,c(column_MinQ,column_TuW,column_MinQ[5]/column_TuW))
+  x = checkData(R)
+  columns = ncol(x) 
+  columnnames = colnames(x)
+  rownames = c("mean","stdDev","phi","sigma","MaxDD(in %)","t*","MaxTuW","Penance")
+  for(column in 1:columns){
+    phi = cov(x[,column][-1],x[,column][-length(x[,column])])/(cov(x[,column][-length(x[,column])]))
+    sigma_infinity = StdDev(x[,column])
+    sigma = sigma_infinity*((1-phi^2)^0.5)
+    column_MinQ<-c(mean(x[,column]),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_MinQ[5]/column_TuW)
+    if(column == 1){
+      result = data.frame(Value = v, row.names = rownames)
     }
-  row.names(tp)<-colnames(R)
-  colnames(tp) = c("mean","stdDev","phi","sigma","MaxDD(in %)","t*","MaxTuW","Penance")
-  print(tp)
-  
+    else{
+      nextcolumn = data.frame(Value = v,row.names = rownames)
+      result = cbind(result,nextcolumn)
+    }
+  }
+  colnames(result) = columnnames
+  result
 }



More information about the Returnanalytics-commits mailing list