[Returnanalytics-commits] r2528 - pkg/PerformanceAnalytics/sandbox/pulkit/week3/code
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 9 09:06:21 CEST 2013
Author: pulkit
Date: 2013-07-09 09:06:21 +0200 (Tue, 09 Jul 2013)
New Revision: 2528
Added:
pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TriplePenance.R
pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R
Modified:
pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/GoldenSection.R
pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R
pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R
Log:
table for Triple Penance Rule
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/GoldenSection.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/GoldenSection.R 2013-07-09 02:53:42 UTC (rev 2527)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/GoldenSection.R 2013-07-09 07:06:21 UTC (rev 2528)
@@ -3,8 +3,12 @@
#'
#' @description
#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
+#'
+#'@param a initial point
+#'@param b final point
+#'@param minimum TRUE to calculate the minimum and FALSE to calculate the Maximum
+#'@param function_name The name of the function
-
golden_section<-function(a,b,minimum = TRUE,function_name,...){
# DESCRIPTION
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R 2013-07-09 02:53:42 UTC (rev 2527)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/MaxDD.R 2013-07-09 07:06:21 UTC (rev 2528)
@@ -1,18 +1,12 @@
-library(PerformanceAnalytics)
-data(edhec)
+
#' @title
#' Triple Penance Rule
#'
#' @description
-#' \code{TriplePenance} calculates the Maximum drawdown and the maximum
-#' Time under water for a particular confidence interval. These concepts
-#' are intenately related through the "triple penance" rule which states
-#' that under standard portfolio theory assumptions, it takes three times
-#' longer to recover from the expected maximum drawdown than the time it
-#' takes to produce it, with the same confidence level. The framework is
-#' generalized to deal with the case of first-order auto-correlated cashflows
-#'
-#' @param R Hedge Fund log Returns
+#' \code{MaxDD} calculates the Maximum drawdown for a particular confidence interval.
+#'
+#' @param R Returns
+#' @param confidence the confidence interval
#'
#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
@@ -22,60 +16,8 @@
columns = ncol(x)
i = 0
tp = matrix(nrow=columns,ncol=2)
-get_minq<-function(R,confidence){
-
- # DESCRIPTION:
- # A function to get the maximum drawdown for first order serially autocorrelated
- # returns from the quantile function defined for accumulated returns for a
- # particular confidence interval
- # Inputs:
- # R: The function takes Returns as the input
- #
- # confidence: The confidence interval of the input.
- x = checkData(R)
- mu = mean(x, na.rm = TRUE)
- sigma_infinity = StdDev(x)
- phi = cov(x[-1],x[-length(x)])/(cov(x[-length(x)]))
- sigma = sigma_infinity*((1-phi^2)^0.5)
- dp0 = 0
- q_value = 0
- bets = 0
- while(q_value <= 0){
- bets = bets + 1
- q_value = getQ(bets, phi, mu, sigma, dp0, confidence)
- }
- minQ = golden_section(0,bets,TRUE,getQ,phi,mu,sigma,dp0,confidence)
- return(c(-minQ$value*100,minQ$x))
-}
-
-getQ<-function(bets,phi,mu,sigma,dp0,confidence){
-
- # DESCRIPTION:
- # A function to get the quantile function for cumulative returns
- # and a particular confidence interval.
-
- # Inputs:
- # bets: The number fo steps
- #
- # phi: The coefficient for AR[1]
- #
- # mu: The mean of the returns
- #
- # sigma: The standard deviation of the returns
- #
- # dp0: The r0 or the first return
- #
- # confidence: The confidence level of the quantile function
- mu_new = (phi^(bets+1)-phi)/(1-phi)*(dp0-mu)+mu*bets
- var = sigma^2/(phi-1)^2
- var = var*((phi^(2*(bets+1))-1)/(phi^2-1)-2*(phi^(bets+1)-1)/(phi-1)+bets +1)
- q_value = mu_new + qnorm(1-confidence)*(var^0.5)
- return(q_value)
-}
-
-
for(i in 1:columns){
column_MinQ <- get_minq(x[,i],confidence)
tp[i,] <- column_MinQ
Added: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TriplePenance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TriplePenance.R (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TriplePenance.R 2013-07-09 07:06:21 UTC (rev 2528)
@@ -0,0 +1,86 @@
+get_minq<-function(R,confidence){
+
+ # DESCRIPTION:
+ # A function to get the maximum drawdown for first order serially autocorrelated
+ # returns from the quantile function defined for accumulated returns for a
+ # particular confidence interval
+
+ # Inputs:
+ # R: The function takes Returns as the input
+ #
+ # confidence: The confidence interval of the input.
+ x = checkData(R)
+ mu = mean(x, na.rm = TRUE)
+ sigma_infinity = StdDev(x)
+ phi = cov(x[-1],x[-length(x)])/(cov(x[-length(x)]))
+ sigma = sigma_infinity*((1-phi^2)^0.5)
+ dp0 = 0
+ q_value = 0
+ bets = 0
+ while(q_value <= 0){
+ bets = bets + 1
+ q_value = getQ(bets, phi, mu, sigma, dp0, confidence)
+ }
+ minQ = golden_section(0,bets,TRUE,getQ,phi,mu,sigma,dp0,confidence)
+ return(c(-minQ$value*100,minQ$x))
+}
+
+
+getQ<-function(bets,phi,mu,sigma,dp0,confidence){
+
+ # DESCRIPTION:
+ # A function to get the quantile function for cumulative returns
+ # and a particular confidence interval.
+
+ # Inputs:
+ # bets: The number fo steps
+ #
+ # phi: The coefficient for AR[1]
+ #
+ # mu: The mean of the returns
+ #
+ # sigma: The standard deviation of the returns
+ #
+ # dp0: The r0 or the first return
+ #
+ # confidence: The confidence level of the quantile function
+ mu_new = (phi^(bets+1)-phi)/(1-phi)*(dp0-mu)+mu*bets
+ var = sigma^2/(phi-1)^2
+ var = var*((phi^(2*(bets+1))-1)/(phi^2-1)-2*(phi^(bets+1)-1)/(phi-1)+bets +1)
+ q_value = mu_new + qnorm(1-confidence)*(var^0.5)
+ return(q_value)
+}
+get_TuW<-function(R,confidence){
+
+ # DESCRIPTION:
+ # A function to generate the time under water
+ #
+ # Inputs:
+ # R: The function takes Returns as the input.
+ #
+ # confidence: Confidence level of the quantile function
+
+
+ x = checkData(R)
+ mu = mean(x, na.rm = TRUE)
+ sigma_infinity = StdDev(x)
+ phi = cov(x[-1],x[-length(x)])/(cov(x[-length(x)]))
+ sigma = sigma_infinity*((1-phi^2)^0.5)
+
+ dp0 = 0
+ q_value = 0
+ bets = 0
+ while(q_value <= 0){
+ bets = bets + 1
+ q_value = getQ(bets, phi, mu, sigma, dp0, confidence)
+ }
+ TuW = golden_section(bets-1,bets,TRUE,diff_Q,phi,mu,sigma,dp0,confidence)
+ return(TuW$x)
+}
+
+
+
+diff_Q<-function(bets,phi,mu,sigma,dp0,confidence){
+ return(abs(getQ(bets,phi,mu,sigma,dp0,confidence)))
+}
+
Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R 2013-07-09 02:53:42 UTC (rev 2527)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/TuW.R 2013-07-09 07:06:21 UTC (rev 2528)
@@ -5,8 +5,8 @@
#' \code{TriplePenance} calculates the maximum
#' Time under water for a particular confidence interval.
#'
-#'@param confidence The confidence level
-#' @param R Hedge Fund log Returns
+#' @param R return series
+#' @param confidence the confidence interval
#'
#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
@@ -15,70 +15,6 @@
columns = ncol(R)
i = 0
tp = matrix(nrow=columns)
-
-
-getQ<-function(bets,phi,mu,sigma,dp0,confidence){
-
- # DESCRIPTION:
- # A function to get the quantile function for cumulative returns
- # and a particular confidence interval.
-
- # Inputs:
- # bets: The number fo steps
- #
- # phi: The coefficient for AR[1]
- #
- # mu: The mean of the returns
- #
- # sigma: The standard deviation of the returns
- #
- # dp0: The r0 or the first return
- #
- # confidence: The confidence level of the quantile function
- mu_new = (phi^(bets+1)-phi)/(1-phi)*(dp0-mu)+mu*bets
- var = sigma^2/(phi-1)^2
- var = var*((phi^(2*(bets+1))-1)/(phi^2-1)-2*(phi^(bets+1)-1)/(phi-1)+bets +1)
- q_value = mu_new + qnorm(1-confidence)*(var^0.5)
- return(q_value)
-}
-
-
-get_TuW<-function(R,confidence){
-
- # DESCRIPTION:
- # A function to generate the time under water
- #
- # Inputs:
- # R: The function takes Returns as the input.
- #
- # confidence: Confidence level of the quantile function
-
-
- x = checkData(R)
- mu = mean(x, na.rm = TRUE)
- sigma_infinity = StdDev(x)
- phi = cov(x[-1],x[-length(x)])/(cov(x[-length(x)]))
- sigma = sigma_infinity*((1-phi^2)^0.5)
-
- dp0 = 0
- q_value = 0
- bets = 0
- while(q_value <= 0){
- bets = bets + 1
- q_value = getQ(bets, phi, mu, sigma, dp0, confidence)
- }
- TuW = golden_section(bets-1,bets,TRUE,diff,phi,mu,sigma,dp0,confidence)
- return(TuW$x)
-}
-
-
-
-diff<-function(bets,phi,mu,sigma,dp0,confidence){
- return(abs(getQ(bets,phi,mu,sigma,dp0,confidence)))
-}
-
-
-
for(i in 1:columns){
column_TuW = get_TuW(x[,i],confidence)
tp[i] <- column_TuW
Added: pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3/code/table.Penance.R 2013-07-09 07:06:21 UTC (rev 2528)
@@ -0,0 +1,39 @@
+#' @title
+#' Table for displaying the Mximum Drawdown and the Time under Water
+#'
+#' @description
+#' \code{table.Penance} Displays the table showing mean,Standard Deviation , phi, sigma , MaxDD,time at which MaxDD occurs, MaxTuW and the penance.
+#'
+#' @param R Returns
+#' @param confidence the confidence interval
+#'
+#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
+
+table.Penance<-function(R,confidence){
+ # DESCRIPTION:
+ # Maximum Drawdown and Time under Water considering first-order serial correlation
+ #
+ # Input:
+ # R log returns
+ #
+ # Output:
+ # 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))
+ }
+ row.names(tp)<-colnames(R)
+ colnames(tp) = c("mean","stdDev","phi","sigma","MaxDD(in %)","t*","MaxTuW","Penance")
+ print(tp)
+
+}
More information about the Returnanalytics-commits
mailing list