[Returnanalytics-commits] r2634 - in pkg/PerformanceAnalytics/sandbox/pulkit: week2/code week3_4/code week5

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jul 23 21:23:01 CEST 2013


Author: pulkit
Date: 2013-07-23 21:23:01 +0200 (Tue, 23 Jul 2013)
New Revision: 2634

Modified:
   pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkSR.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MonteSimulTriplePenance.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R
   pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R
Log:
fixed monte simulation for triple penance

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkSR.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkSR.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week2/code/BenchmarkSR.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -50,11 +50,11 @@
   corr_avg = 0
   for(i in 1:(columns-1)){
     for(j in (i+1):columns){
-      corr_avg = corr_avg + corr[(i-1)*columns+j,]
+      corr_avg = corr_avg + corr[(i-1)*columns+j,1]
     }
   }
   corr_avg = corr_avg*2/(columns*(columns-1))
-  SR_Benchmark = sr_avg*sqrt(columns/(1+(columns-1)*corr_avg[1,1]))
+  SR_Benchmark = sr_avg*sqrt(columns/(1+(columns-1)*corr_avg))
   return(SR_Benchmark)
 }
 ###############################################################################

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MaxDD.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -27,6 +27,15 @@
 #' and the Maximum Drawdown is given by.
 #' 
 #' \deqn{MaxDD_{\alpha}=max\left\{0,-MinQ_\alpha\right\}}
+#'
+#'The non normal time dependent process is defined by
+#'
+#'\deqn{\triangle{\pi_{\tau}}=(1-\phi)\mu + \phi{\delta_{\tau-1}} + \sigma{\epsilon_{\tau}}}
+#'
+#'The random shocks are iid distributed \eqn{\epsilon_{\tau}~N(0,1)}. These random shocks follow an independent and 
+#'identically distributed Gaussian Process, however \eqn{\triangle{\pi_\tau}} is neither an independent nor an 
+#'identically distributed Gaussian Process. This is due to the parameter \eqn{\phi}, which incorporates a first-order 
+#'serial-correlation effect of auto-regressive form.
 #' 
 #' Golden Section Algorithm is used to calculate the Minimum of the function Q.
 #'  

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MonteSimulTriplePenance.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MonteSimulTriplePenance.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/MonteSimulTriplePenance.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -1,33 +1,64 @@
 #' @title
 #' Monte Carlo Simulation for the Triple Penance Rule
 #'
-#' @param R Hedge Fund log Returns
+#'@description
+#'
+#'The following process is simulated using the monte carlo process and the maximum drawdown is calculated using it.
+#'\deqn{\triangle{\pi_{\tau}}=(1-\phi)\mu + \phi{\delta_{\tau-1}} + \sigma{\epsilon_{\tau}}}
+#'
+#'The random shocks are iid distributed \eqn{\epsilon_{\tau}~N(0,1)}. These random shocks follow an independent and 
+#'identically distributed Gaussian Process, however \eqn{\triangle{\pi_\tau}} is neither an independent nor an 
+#'identically distributed Gaussian Process. This is due to the parameter \eqn{\phi}, which incorporates a first-order 
+#'serial-correlation effect of auto-regressive form. 
+#'
+#'
+#' @param size size of the Monte Carlo experiment
+#' @param phi AR(1) coefficient
+#' @param mu unconditional mean
+#' @param sigma Standard deviation of the random shock
+#' @param dp0 Bet at origin (initialization of AR(1))
+#' @param bets Number of bets in the cumulative process
+#' @param confidence Confidence level for quantile
 #' 
-#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs and the ‘Triple Penance’ Rule(January 1, 2013).
+#' @reference Bailey, David H. and Lopez de Prado, Marcos, Drawdown-Based Stop-Outs
+#'  and the ‘Triple Penance’ Rule(January 1, 2013).
+#'  
+#'  @examples
+#'  MonteSimulTriplePenance(10^6,0.5,1,2,1,25,0.95) # Expected Value Quantile (Exact) = 6.781592
+#'  
 
 
  
-monte_simul<-function(size){
-    
-  phi = 0.5
-  mu = 1
-  sigma = 2 
-  dp0 = 1
-  bets = 25
-  confidence = 0.95
+MonteSimulTriplePenance<-function(size,phi,mu,sigma,dp0,bets,confidence){
   
+  # DESCRIPTION:
+  # The function gives Value of Maximum Drawdown generated by a monte carlo process. 
+  
+  # INPUTS:
+  # The size, AR(1) coefficient, unconditional mean,  Standard deviation of the random shock,
+  # Bet at origin (initialization of AR(1)), Number of bets in the cumulative process,Confidence level for quantile
+  # are taken as the input
+  
+  # FUNCTION:
   q_value = getQ(bets, phi, mu, sigma, dp0, confidence)
-  ms = NULL
-  
+  ms = numeric(size)
+  delta = 0
+
   for(i in 1:size){
-    ms[i] = sum((1-phi)*mu + rnorm(bets)*sigma + delta*phi)
+    pnl = 0
+    delta = 0
+    for(j in 1:bets){
+      delta = (1-phi)*mu + rnorm(1)*sigma + delta*phi
+      pnl = pnl +delta
+    }
+    ms[i] <- pnl
   }
-  q_ms = quantile(ms,(1-confidence)*100)
+  q_ms = quantile(ms,(1-confidence))
   diff = q_value - q_ms 
-
-  print(q_value)
-  print(q_ms)
-  print(q_value - q_ms)
+  result <- matrix(c(q_value,q_ms,diff),nrow = 3)
+  rownames(result)= c("Exact","Monte Carlo","Difference")
+  colnames(result) = "Quantile"
+  return(result)
 }
 
 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week3_4/code/TuW.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -12,7 +12,17 @@
 #' \deqn{MaxTuW_\alpha=\biggl(\frac{Z_\alpha{\sigma}}{\mu}\biggr)^2}
 #' 
 #' For a Autoregressive process the Time under water is found using the golden section algorithm.
+#' 
+#'The non normal time dependent process is defined by
 #'
+#'\deqn{\triangle{\pi_{\tau}}=(1-\phi)\mu + \phi{\delta_{\tau-1}} + \sigma{\epsilon_{\tau}}}
+#'
+#'The random shocks are iid distributed \eqn{\epsilon_{\tau}~N(0,1)}. These random shocks follow an independent and 
+#'identically distributed Gaussian Process, however \eqn{\triangle{\pi_\tau}} is neither an independent nor an 
+#'identically distributed Gaussian Process. This is due to the parameter \eqn{\phi}, which incorporates a first-order 
+#'serial-correlation effect of auto-regressive form.
+
+#'
 #' @param R return series
 #' @param confidence the confidence interval
 #' @param type The type of distribution "normal" or "ar"."ar" stands for Autoregressive.

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/REDDCOPS.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -63,12 +63,11 @@
     sd = StdDev(R)
     factor = (as.vector(sharpe)/as.vector(sd)+0.5)/(1-delta^2)
     redd = rollDrawdown(R,Rf,h,geometric)
-    redd = na.omit(redd)
     xt = max(0,(delta-redd)/(1-redd))
     return(xt)
   }
   for(column in 1:columns){
-    column.xt <- na.skip(x[,column],FUN = dynamicPort)
+    column.xt <- as.xts(apply((x[,column],MARGIN = 1,FUN = dynamicPort)))
     if(column == 1)
       xt = column.xt
     else xt = merge(xt, column.xt) 

Modified: pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R	2013-07-23 03:25:51 UTC (rev 2633)
+++ pkg/PerformanceAnalytics/sandbox/pulkit/week5/redd.R	2013-07-23 19:23:01 UTC (rev 2634)
@@ -61,7 +61,7 @@
     }
 
     for(column in 1:columns){
-        column.drawdown <- apply.rolling(x[,column],width = h, FUN = REDD, geometric = geometric)
+        column.drawdown <- rollapplyr(x[,column],width = h, FUN = REDD, geometric = geometric)
         if(column == 1)
             rolldrawdown = column.drawdown
         else rolldrawdown = merge(rolldrawdown, column.drawdown) 



More information about the Returnanalytics-commits mailing list