[Returnanalytics-commits] r2605 - in pkg/PerformanceAnalytics/sandbox/pulkit: week1/code week5

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jul 20 15:31:25 CEST 2013


Author: pulkit
Date: 2013-07-20 15:31:25 +0200 (Sat, 20 Jul 2013)
New Revision: 2605

Added:
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/REM.R
Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R
Log:
Added Rolling economic Max 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R	2013-07-20 10:26:32 UTC (rev 2604)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/MinTRL.R	2013-07-20 13:31:25 UTC (rev 2605)
@@ -49,6 +49,7 @@
 #'MinTrackRecord(refSR = 1/12^0.5,Rf = 0,p=0.95,sr = 2/12^0.5,sk=-0.72,kr=5.78)
 #'MinTrackRecord(edhec[,1:2],refSR = c(0.28,0.24))
 #'@export
+#'
 MinTrackRecord<-function(R = NULL, refSR,Rf=0,p = 0.95, weights = NULL,sr = NULL,sk = NULL, kr = NULL, ...){
     columns = 1
     columnnames = NULL
@@ -107,3 +108,14 @@
     return(result)
 }
 
+###############################################################################
+# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
+#
+# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: MinTRL.R $
+#
+##############################################################################
\ No newline at end of file

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R	2013-07-20 10:26:32 UTC (rev 2604)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week1/code/ProbSharpeRatio.R	2013-07-20 13:31:25 UTC (rev 2605)
@@ -105,3 +105,14 @@
     return(result)
     
 }
+###############################################################################
+# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
+#
+# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: ProbSharpeRatio.R $
+#
+##############################################################################

Added: pkg/PerformanceAnalytics/sandbox/pulkit/week5/REM.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/REM.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/REM.R	2013-07-20 13:31:25 UTC (rev 2605)
@@ -0,0 +1,83 @@
+#'@title 
+#'Rolling Economic Max
+#'
+#'@description
+#'Rolling Economic Max at time t, looking back at portfolio Wealth history
+#'for a rolling window of length H is given by:
+#'
+#'\deqn{REM(t,h)=\max_{t-H \leq s}\[(1+r_f)^{t-s}W_s\]}
+#'
+#'Here rf is the average realized risk free rate over a period of length t-s. If the risk free rate is changing. This is used to compound.
+#'
+#'\deqn{ \prod_{i=s}^{t}(1+r_{i}.{\triangle}t)}
+#'
+#'here  \eqn{r_i} denotes the risk free interest rate during \eqn{i^{th}} discrete 
+#'time interval \eqn{{\triangle}t}.
+
+#'
+#'@param R R an xts, vector, matrix, data frame, timeseries, or zoo object of asset return.
+#'@param Rf risk free rate can be vector such as government security rate of return.
+#'@param h Look back period
+#'@param geomtric geometric utilize geometric chaining (TRUE) or simple/arithmetic #'chaining(FALSE) to aggregate returns, default is TRUE.
+#'@param ... any other variable
+#'@examples
+#'rollEconomicMax(edhec,0.08,100)
+#'@export
+#'
+rollEconomicMax<-function(R,Rf,h,geometric = TRUE,...){
+  
+  
+  # DESCRIPTION:
+  # calculates the Rolling Economic Max(REDD) for a return series.
+  # The risk free return(rf) and the lookback period(h) is taken as the input.
+  
+  # FUNCTION:
+  x = checkData(R)
+  columns = ncol(x)
+  n = nrow(x)
+  columnnames = colnames(x)
+  rf = checkData(Rf)
+  nr = length(Rf)
+  if(nr != 1 && nr != n ){
+    stop("The number of rows of the returns and the risk free rate do not match")
+  }
+  
+  REM<-function(x,geometric){
+    if(geometric)
+      Return.cumulative = cumprod(1+x)
+    else Return.cumulative = 1 + cumsum(x)
+    l = length(Return.cumulative)
+    if(nr == 1){
+      REM = max(Return.cumulative*(1+rf)^(l-c(1:l)))
+    }
+    else{
+      prodRf = prod(1+rf)
+      REM = max(Return.cumulative*prodRf)
+    }
+    result = REM
+  }
+  
+  for(column in 1:columns){
+    column.drawdown <- apply.rolling(x[,column],width = h, FUN = REM, geometric = geometric)
+    if(column == 1)
+      rolldrawdown = column.drawdown
+    else rolldrawdown = merge(rolldrawdown, column.drawdown) 
+  }
+  colnames(rolldrawdown) = columnnames
+  rolldrawdown = reclass(rolldrawdown, x)
+  return(rolldrawdown)
+}
+
+###############################################################################
+# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
+#
+# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: REM.R $
+#
+##############################################################################
+
+  

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R	2013-07-20 10:26:32 UTC (rev 2604)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R	2013-07-20 13:31:25 UTC (rev 2605)
@@ -2,21 +2,31 @@
 #'
 #'@description
 #'\code{rollDrawdown} calculates the Rolling Economic Drawdown(REDD) for
-#' a return series.To calculate the rolling economic drawdown cumulative 
-#' return and rolling economic max is calculated for each point. The risk 
-#' free return(rf) and the lookback period(h) is taken as the input. 
+#'a return series.To calculate the rolling economic drawdown cumulative 
+#'return and rolling economic max is calculated for each point. The risk 
+#'free return(rf) and the lookback period(h) is taken as the input.
 #'
+#'Rolling Economic Drawdown is given by the equation
+#'
+#'\deqn{REDD(t,h)=1-\frac{W_t}/{REM(t,H)}}
+#'
+#'Here REM stands for Rolling Economic Max and is the code \code{\link{rollEconomicMax}}
+#' 
+#'
 #'@param R an xts, vector, matrix, data frame, timeseries, or zoo object of asset return.
 #'@param weights portfolio weighting vector, default NULL
-#'@param geometric utilize geometric chaining (TRUE) or  simple/arithmetic chaining(FALSE)
+#'@param geometric utilize geometric chaining (TRUE) or simple/arithmetic chaining(FALSE)
 #'to aggregate returns, default is TRUE
 #'@param rf risk free rate can be vector such as government security rate of return
 #'@param h lookback period 
-#'@param \dots any other passthru variable
+#'@param \dots any other  variable
 #'@references Yang, Z. George and Zhong, Liang, Optimal Portfolio Strategy to 
 #'Control Maximum Drawdown - The Case of Risk Based Dynamic Asset Allocation (February 25, 2012)
+#'@examples
+#'rollDrawdown(edhec,0.08,100)
+#'
 #' @export
-rollDrawdown<-function(R,Rf,h, geometric = TRUE, weights = NULL,...)
+rollDrawdown<-function(R,Rf,h, geometric = TRUE,...)
 {
   
   # DESCRIPTION:
@@ -28,11 +38,11 @@
   # FUNCTION:
     x = checkData(R)
     columns = ncol(x)
-    rowx = nrow(x)
+    n = nrow(x)
     columnnames = colnames(x)
     rf = checkData(Rf)
-    rowr = length(Rf)
-    if(rowr != 1 && rowr != rowx ){
+    nr = length(Rf)
+    if(nr != 1 && nr != n ){
       stop("The number of rows of the returns and the risk free rate do not match")
     }
     
@@ -41,7 +51,7 @@
             Return.cumulative = cumprod(1+x)
         else Return.cumulative = 1 + cumsum(x)
         l = length(Return.cumulative)
-        if(rowr == 1){
+        if(nr == 1){
           REM = max(Return.cumulative*(1+rf)^(l-c(1:l)))
         }
         else{
@@ -62,14 +72,23 @@
     return(rolldrawdown)
 }
 
+###############################################################################
+# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
+#
+# Copyright (c) 2004-2012 Peter Carl and Brian G. Peterson
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: redd.R $
+#
+##############################################################################
 
 
 
 
 
 
-
-
   
 
 



More information about the Returnanalytics-commits mailing list