[Returnanalytics-commits] r2565 - pkg/PerformanceAnalytics/sandbox/pulkit/week1/code

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 13 20:08:15 CEST 2013


Author: pulkit
Date: 2013-07-13 20:08:15 +0200 (Sat, 13 Jul 2013)
New Revision: 2565

Added:
   pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSR.py
Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R
Log:
PSR documentation and error handling

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R	2013-07-13 18:02:30 UTC (rev 2564)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R	2013-07-13 18:08:15 UTC (rev 2565)
@@ -1,19 +1,31 @@
 #'@title Minimum Track Record Length
 #'
 #'@description
-#'“How long should a track record be in order to have statistical confidence 
-#'that its Sharpe ratio is above a given threshold? . if a track record is shorter#' than MinTRL, we do not have enough confidence that the observed ̂ is above the designated threshold
+#'Minimum Track Record Length will tell us “How long should a track record be in 
+#'order to have statistical confidence that its Sharpe ratio is above a given 
+#'threshold? ". If a track record is shorter than MinTRL, we do not have enough
+#'confidence that the observed Sharpe Ratio is above the designated threshold.
+#'The reference Sharpe Ratio should be less than the observed Sharpe Ratio and 
+#'the Values should be given in non-annualized terms, in the same periodicity as
+#'the return series. The Minimum Track Record Length is also given in the same 
+#'Periodicity as the Return Series.
 #'
+#'\deqn{MinTRL = n^\ast = 1+\biggl[1-\hat{\gamma_3}\hat{SR}+\frac{\hat{\gamma_4}}{4}\hat{SR^2}\biggr]\biggl(\frac{Z_\alpha}{\hat{SR}-SR^\ast}\biggr)^2}
+#'
+#'$\gamma{_3}$ and $\gamma{_4}$ are the skewness and kurtosis respectively. 
+#'It is important to note that MinTRL is expressed in terms of number of observations,
+#'not annual or calendar terms.
+#'
 #'@aliases MinTrackRecord
 #'
-#'@param R the return series
+#'@param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset return 
 #'@param Rf the risk free rate of return
-#'@param refSR the reference Sharpe Ratio
+#'@param refSR the reference Sharpe Ratio,in the same periodicity as the returns(non-annualized)
 #'@param p the confidence level
 #'@param weights the weights for the portfolio
-#'@param sr Sharpe Ratio
-#'@param sk Skewness
-#'@param kr Kurtosis
+#'@param sr Sharpe Ratio,in the same periodicity as the returns(non-annualized)
+#'@param sk Skewness, in the same periodicity as the returns(non-annualized)
+#'@param kr Kurtosis, in the same periodicity as the returns(non-annualized)
 #'
 #'@reference Bailey, David H. and Lopez de Prado, Marcos, \emph{The Sharpe Ratio 
 #'Efficient Frontier} (July 1, 2012). Journal of Risk, Vol. 15, No. 2, Winter
@@ -22,7 +34,7 @@
 #'@examples
 #'
 #'data(edhec)
-#'MinTrackRecord(edhec[,1],0.20)
+#'MinTrackRecord(edhec[,1],refSR=0.20)
 
 
 MinTrackRecord<-function(R = NULL, refSR,Rf=0,p = 0.95, weights = NULL,sr = NULL,sk = NULL, kr = NULL, ...){
@@ -66,8 +78,14 @@
      #   message("no weights passed,will calculate Minimum Track Record Length for each column")
     #}
    
-    if(!is.null(dim(Rf)))
+    if(!is.null(dim(Rf))){
         Rf = checkData(Rf)
+    }
+    #If the refSR is greater than SR an error is displayed
+    if(refSR>sr){
+        stop("The Reference Sharpe Ratio should be less than the Observed Sharpe Ratio")
+    }
+
     result = 1 + (1 - sk*sr + ((kr-1)/4)*sr^2)*(qnorm(p)/(sr-refSR))^2
     if(!is.null(dim(result))){ 
         colnames(result) = columnnames

Added: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSR.py
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSR.py	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/PSR.py	2013-07-13 18:08:15 UTC (rev 2565)
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+# PSR class for computing the Probabilistic Sharpe Ratio
+# On 20120502 by MLdP <lopezdeprado at lbl.gov>
+
+from scipy.stats import norm
+#-------------------------------------------
+# PSR class
+class PSR:
+    def __init__(self,stats,sr_ref,obs,prob):
+        self.PSR=0
+        self.minTRL=0
+        self.stats=stats
+        self.sr_ref=sr_ref
+        self.obs=obs
+        self.prob=prob
+#-------------------------------------------
+    def set_PSR(self,moments):
+        stats=[0,0,0,3]
+        stats[:moments]=self.stats[:moments]
+        sr=self.stats[0]/self.stats[1]
+        self.PSR=norm.cdf((sr-self.sr_ref)*(self.obs-1)**0.5/(1-sr*stats[2]+sr**2*(stats[3]-1)/4.)**0.5)
+#-------------------------------------------
+    def set_TRL(self,moments):
+        stats=[0,0,0,3]
+        stats[:moments]=self.stats[:moments]
+        sr=self.stats[0]/self.stats[1]
+        self.minTRL=1+(1-stats[2]*sr+(stats[3]-1)/4.*sr**2)*(norm.ppf(self.prob)/(sr-self.sr_ref))**2
+#-------------------------------------------
+    def get_PSR(self,moments):
+        self.set_PSR(moments)
+        return self.PSR
+#-------------------------------------------
+    def get_TRL(self,moments):
+        self.set_TRL(moments)
+        return self.minTRL
+#-------------------------------------------
+#-------------------------------------------
+# Main function
+def main():
+    #1) Inputs (stats on excess returns)
+    stats=[1.5,12**0.5,-0.72,5.78]
+    #non-annualized stats
+    sr_ref=1/(12**0.5)
+    #reference Sharpe ratio (non-annualized)
+    obs=59.895
+    prob=0.95
+    #2) Create class
+    psr=PSR(stats,sr_ref,obs,prob)
+    #3) Compute and report values
+    print 'PSR(2m,3m,4m):',[psr.get_PSR(i) for i in range(2,5,1)]
+    print 'minTRL(2m,3m,4m):',[psr.get_TRL(i) for i in range(2,5,1)]
+#-------------------------------------------
+# Boilerplate
+if __name__=='__main__': main()
+

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R	2013-07-13 18:02:30 UTC (rev 2564)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R	2013-07-13 18:08:15 UTC (rev 2565)
@@ -1,33 +1,41 @@
-#'@title Probabilistic Sharpe Ratio
+#' @title Probabilistic Sharpe Ratio
 #'
-#'@description
-#'Given a predefined benchmark Sharpe ratio ,the observed Sharpe Ratio 
-#'can be expressed in probabilistic terms known as the Probabilistic Sharpe Ratio
-#'PSR takes higher moments  into account and delivers a corrected, atemporal 
-#'measure of performance expressed in terms of probability of skill.
+#' @description
+#' Given a predefined benchmark Sharpe ratio ,the observed Sharpe Ratio 
+#' can be expressed in probabilistic terms known as the Probabilistic 
+#' Sharpe Ratio PSR takes higher moments  into account and delivers a
+#' corrected, atemporal measure of performance expressed in terms of 
+#' probability of skill. The reference Sharpe Ratio should be less than 
+#' the Observed Sharpe Ratio.
+#' \deqn{\hat{PSR}(SR^\ast) = Z\biggl[\frac{(\hat{SR}-SR^\ast)\sqrt{n-1}}{\sqrt{1-\hat{\gamma_3}SR^\ast + \frac{\hat{\gamma_4}-1}{4}\hat{SR^2}}}\biggr]}
+
+#' Here $n$ is the track record length or the number of data points. It can be daily,weekly or yearly depending on the input given
+
+#' $\hat{\gamma{_3}}$ and $\hat{\gamma{_4}}$ are the skewness and kurtosis respectively.
+
 #'
-#'@aliases ProbSharpeRatio
+#' @aliases ProbSharpeRatio
 #'
-#'@param R the return series
-#'@param Rf the risk free rate of return
-#'@param refSR the reference Sharpe Ratio
-#'@param the confidence level
-#'@param weights the weights for the portfolio
-#'@param sr Sharpe Ratio
-#'@param sk Skewness
-#'@param kr Kurtosis
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of asset return 
+#' @param Rf the risk free rate of return
+#' @param refSR the reference Sharpe Ratio, in the same periodicity as the returns(non-annualized)
+#' @param the confidence level
+#' @param weights the weights for the portfolio
+#' @param sr Sharpe Ratio, in the same periodicity as the returns(non-annualized)
+#' @param sk Skewness, in the same periodicity as the returns(non-annualized)
+#' @param kr Kurtosis, in the same periodicity as the returns(non-annualized)
 #'
-#'@references Bailey, David H. and Lopez de Prado, Marcos, \emph{The Sharpe Ratio 
-#'Efficient Frontier} (July 1, 2012). Journal of Risk, Vol. 15, No. 2, Winter
+#' @references Bailey, David H. and Lopez de Prado, Marcos, \emph{The Sharpe Ratio 
+#' Efficient Frontier} (July 1, 2012). Journal of Risk, Vol. 15, No. 2, Winter
 #' 2012/13
 #'
-#'@keywords ts multivariate distribution models
+#' @keywords ts multivariate distribution models
 #'
-#'@examples
+#' @examples
 #'
-#'data(edhec)
-#'ProbSharpeRatio(edhec[,1],refSR = 0.28) 
-#'ProbSharpeRatio(edhec,reSR = 0.28,Rf = 0.06)
+#' data(edhec)
+#' ProbSharpeRatio(edhec[,1],refSR = 0.28) 
+#' ProbSharpeRatio(edhec,reSR = 0.28,Rf = 0.06)
 
 
 ProbSharpeRatio<-
@@ -73,8 +81,13 @@
  #       message("no weights passed,will calculate Probability Sharpe Ratio for each column")
   #  }
    
-    if(!is.null(dim(Rf)))
+    if(!is.null(dim(Rf))){
         Rf = checkData(Rf)
+    }
+    #If the Reference Sharpe Ratio is greater than the Observred Sharpe Ratio an error is displayed
+    if(refSR>sr){
+        stop("The Reference Sharpe Ratio should be less than the Observed Sharpe Ratio")
+    }
     result = pnorm(((sr - refSR)*(n-1)^(0.5))/(1-sr*sk+sr^2*(kr-1)/4)^(0.5))
     if(!is.null(dim(result))){ 
         colnames(result) = columnnames



More information about the Returnanalytics-commits mailing list