[Returnanalytics-commits] r2580 - pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 16 06:48:06 CEST 2013
Author: pulkit
Date: 2013-07-16 06:48:05 +0200 (Tue, 16 Jul 2013)
New Revision: 2580
Modified:
pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R
pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
Log:
MaxDD and Time under water for normal distribution
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R 2013-07-15 06:02:36 UTC (rev 2579)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R 2013-07-16 04:48:05 UTC (rev 2580)
@@ -10,12 +10,12 @@
#'
#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
-MaxDD<-function(R,confidence,...)
+MaxDD<-function(R,confidence,type=c("ar","normal"),...)
{
x = checkData(R)
if(ncol(x)==1 || is.null(R) || is.vector(R)){
-
+ type = type[1]
calcul = FALSE
for(i in (1:length(x))){
if(!is.na(x[i])){
@@ -27,12 +27,22 @@
result = NA
}
else{
- result = get_minq(x,confidence)
+ if(type=="ar"){
+ result = get_minq(x,confidence)
+ }
+ if(type=="normal"){
+ result = dd_norm(x,confidence)
+ }
}
+
return(result)
}
-
- result = apply(x,MARGIN = 2,get_minq,confidence)
+ if(type=="ar"){
+ result = apply(x,MARGIN = 2,get_minq,confidence)
+ }
+ if(type=="normal"){
+ result = apply(x,MARGIN = 2,dd_norm,confidence)
+ }
rownames(result) = c("MaxDD(in %)","t*")
return(result)
}
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R 2013-07-15 06:02:36 UTC (rev 2579)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TriplePenance.R 2013-07-16 04:48:05 UTC (rev 2580)
@@ -1,3 +1,38 @@
+
+
+dd_norm<-function(x,confidence){
+ # DESCRIPTION:
+ # A function to return the maximum drawdown for a normal distribution
+
+ # Inputs:
+ # R: The Return Series
+ #
+ # confidence: The confidence Level
+ sd = StdDev(x)
+ mu = mean(x, na.rm = TRUE)
+ dd = max(0,((qnorm(1-confidence)*sd)^2)/(4*mu))
+ t = ((qnorm(1-confidence)*sd)/(2*mu))^2
+
+ return(c(dd*100,t))
+}
+
+tuw_norm<-function(x,confidence){
+ # DESCRIPTION:
+ # A function to return the Time under water
+
+ # Inputs:
+ # R: Return series
+ # confidence: The confidence level
+ sd = StdDev(x)
+ mu = mean(x,na.rm = TRUE)
+ tuw = ((qnorm(1-confidence)*sd)/mu)^2
+
+ return(tuw)
+}
+
+
+
+
get_minq<-function(R,confidence){
# DESCRIPTION:
@@ -8,7 +43,7 @@
# Inputs:
# R: The function takes Returns as the input
#
- # confidence: The confidence interval of the input.
+ # confidence: The confidence interval.
x = checkData(R)
mu = mean(x, na.rm = TRUE)
sigma_infinity = StdDev(x)
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R 2013-07-15 06:02:36 UTC (rev 2579)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R 2013-07-16 04:48:05 UTC (rev 2580)
@@ -10,11 +10,10 @@
#'
#' @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,...){
+TuW<-function(R,confidence,type=c("ar","normal"),...){
x = checkData(R)
-
- if(ncol(x)==1 || is.null(R) || is.vector(R)){
-
+ type = type[1]
+ if(ncol(x)==1 || is.null(R) || is.vector(R)){
calcul = FALSE
for(i in (1:length(x))){
if(!is.na(x[i])){
@@ -26,12 +25,23 @@
result = NA
}
else{
- result = get_TuW(x,confidence)
+ if(type=="ar"){
+ result = get_TuW(x,confidence)
+ }
+ if(type=="normal"){
+ result = tuw_norm(x,confidence)
+ }
}
return(result)
}
else{
- result=apply(x,MARGIN = 2, get_TuW,confidence)
+ if(type=="ar"){
+ result=apply(x,MARGIN = 2, get_TuW,confidence)
+ }
+ if(type=="normal"){
+ result=apply(x,MARGIN = 2, tuw_norm,confidence)
+ }
+
result<-as.data.frame(result)
result<-t(result)
rownames(result)=paste("Max Time Under Water")
More information about the Returnanalytics-commits
mailing list