[Returnanalytics-commits] r2342 - pkg/PerformanceAnalytics/sandbox/pulkit

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 17 15:13:40 CEST 2013


Author: pulkit
Date: 2013-06-17 15:13:39 +0200 (Mon, 17 Jun 2013)
New Revision: 2342

Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/MinTRL.R
   pkg/PerformanceAnalytics/sandbox/pulkit/ProbSharpeRatio.R
Log:
-Added feature for weights and non return series based input in MinTRL

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/MinTRL.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/MinTRL.R	2013-06-17 10:34:12 UTC (rev 2341)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/MinTRL.R	2013-06-17 13:13:39 UTC (rev 2342)
@@ -10,25 +10,54 @@
 #'@param the confidence level
 #'@param weights the weights for the portfolio
 
-MinTrackRecord<-function(x,Rf,refSR,p=0.95,...){
+MinTrackRecord<-function(R = NULL, refSR,Rf=0,p = 0.95, weights = NULL,sr = NULL,sk = NULL, kr = NULL, ...){
+    columns = 1
+    columnnames = NULL
+    #Error handling if R is not NULL
+    if(!is.null(R)){
+        x = checkData(R)
+        columns = ncol(x)
+        n = nrow(x)
+        #Checking if the weights are provided or not
+        if(!is.null(weights)){
+            if(length(weights)!=columns){
+                stop("number of items in weights is not equal to the number of columns in R")
+            }
+            else{
+                # A potfolio is constructed by applying the weights
+                x = Return.portfolio(R,weights)
+                sr = SharpeRatio(x, Rf, p, "StdDev")
+                sk = skewness(x)
+                kr = kurtosis(x)
+            }
+        }
+        else{
+            sr = SharpeRatio(x, Rf, p, "StdDev")
+            sk = skewness(x)
+            kr = kurtosis(x)
+        }
 
-mintrl <- function(x,Rf,p,refSR,...){
-    sk = skewness(x)
-    kr =kurtosis(x)
-    sr = SharpeRatio(x, Rf, p, "StdDev")
-    MinTRL = 1 + (1 - sk*sr + ((kr-1)/4)*sr^2)*(qnorm(p)/(sr-refSR))^2
-    return(MinTRL)
-
-}
-    for(column in 1:columns){
-      column.mintrack <- mintrl(x[,column],Rf,p,refSR)
-      if(column == 1){
-        mintrack = column.mintrack
-      }
-      else {
-        mintrack = merge(mintrack, column.mintrack)
+    columnnames = colnames(x)
+ 
     }
-      
+    # If R is passed as null checking for sharpe ratio , skewness and kurtosis 
+    else{
+        if(is.null(sr) | is.null(sk) | is.null(kr)){
+             stop("You must either pass R or the Sharpe ratio, Skewness, Kurtosis,n etc")
+        }
     }
+    #If weights are not taken into account a message is displayed
+    if(is.null(weights)){
+        message("no weights passed,will calculate Probability Sharpe Ratio for each column")
+    }
+   
+    if(!is.null(dim(Rf)))
+        Rf = checkData(Rf)
+    result = 1 + (1 - sk*sr + ((kr-1)/4)*sr^2)*(qnorm(p)/(sr-refSR))^2
+    if(!is.null(dim(result))){ 
+        colnames(result) = columnnames
+        rownames(result) = paste("Minimum Track Record Length(p=",round(p*100,1),"%):")
+    }
+    return(result)
 }
 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/ProbSharpeRatio.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/ProbSharpeRatio.R	2013-06-17 10:34:12 UTC (rev 2341)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/ProbSharpeRatio.R	2013-06-17 13:13:39 UTC (rev 2342)
@@ -44,14 +44,13 @@
     # If R is passed as null checking for sharpe ratio , skewness and kurtosis 
     else{
 
-        if(is.null(sr)) stop("You must either pass R or the Sharpe ratio, Skewness, Kurtosis,n etc")
-        if(is.null(sk)) stop("You must either pass R or the Sharpe ratio, Skewness, Kurtosis,n etc")
-        if(is.null(kr)) stop("You must either pass R or the Sharpe Ratio, Skewness, Kurtosis,n etc")
-        if(is.null(n)) stop("You must either pass R or the Sharpe Ratio, Skewness, Kurtosis, n etc")
+        if(is.null(sr) | is.null(sk) | is.null(kr) | is.null(n)){
+             stop("You must either pass R or the Sharpe ratio, Skewness, Kurtosis,n etc")
+       }
     }
     #If weights are not taken into account a message is displayed
     if(is.null(weights)){
-        message("no weights passed will calculate Probability Sharpe Ratio for each column")
+        message("no weights passed,will calculate Probability Sharpe Ratio for each column")
     }
    
     if(!is.null(dim(Rf)))



More information about the Returnanalytics-commits mailing list