[Returnanalytics-commits] r2594 - in pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4: . Code Vignette

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 18 15:53:49 CEST 2013


Author: shubhanm
Date: 2013-07-18 15:53:49 +0200 (Thu, 18 Jul 2013)
New Revision: 2594

Added:
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/AcarSim.R.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarJames.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarNumberofObservations.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.Rnw
   pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.tex
Log:
Week4 : Added : Acar & Shane Maximum LossSimulation ( Stage : Development)

Added: Vignette (Acar & Shane) : (Stage : Development)



Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/AcarSim.R.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/AcarSim.R.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/AcarSim.R.R	2013-07-18 13:53:49 UTC (rev 2594)
@@ -0,0 +1,40 @@
+#To get some insight on the relationships between maximum drawdown per unit of volatility 
+#and mean return divided by volatility, we have proceeded to Monte-Carlo simulations.
+# We have simulated cash flows over a period of 36 monthly returns and measured maximum 
+#drawdown for varied levels of annualised return divided by volatility varying from minus
+# two to two by step of 0.1. The process has been repeated six thousand times.
+AcarSim <-
+  function ()
+{
+mu=mean(Return.annualized(edhec))
+monthly=(1+mu)^(1/12)-1
+sig=StdDev.annualized(edhec[,1])[1];
+T= 36
+j=1
+dt=1/T
+nsim=10;
+r=matrix(0,nsim,T+1)
+
+r[,1]=monthly;
+# Sigma 'monthly volatiltiy' will be the varying term
+ratio= seq(-2, 2, by=.1);
+len = length(ratio)
+ddown=matrix(0,nsim,len)
+Z <- array(0, c(len))
+for(i in 1:len)
+{
+  monthly = sig*ratio[i];
+
+  for(j in 1:nsim)
+{
+    dz=rnorm(T)
+    
+    
+      r[j,2:37]=monthly+sig*dz
+    
+    ddown[j,i]= ES((r[j,]))/monthly
+}
+}
+plot(ddown[1,])
+
+}
\ No newline at end of file

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarJames.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarJames.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarJames.R	2013-07-18 13:53:49 UTC (rev 2594)
@@ -0,0 +1,90 @@
+#' Expected Drawdown using Brownian Motion Assumptions
+#' 
+#' Works on the model specified by Maddon-Ismail
+#' 
+#' 
+#' 
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+
+#' @author R
+#' @keywords Expected Drawdown Using Brownian Motion Assumptions
+#'
+#' @export 
+chart.ArcarJames <-
+  function (R,digits =4)
+  {# @author 
+    
+    # DESCRIPTION:
+    # Downside Risk Summary: Statistics and Stylized Facts
+    
+    # Inputs:
+    # R: a regular timeseries of returns (rather than prices)
+    # Output: Table of Estimated Drawdowns 
+    
+    y = checkData(R, method = "xts")
+    columns = ncol(y)
+    rows = nrow(y)
+    columnnames = colnames(y)
+    rownames = rownames(y)
+    T= 36;
+    n <- 1000
+    #tlength <- 36
+    musig.ratio=seq(-2,2,by=.1)
+    dt <- 1/T;
+    s0 <- 100;
+    # for each column, do the following:
+    for(ratio in 1:length(musig.ratio))
+    {
+    for(column in 1:columns) {
+      x = y[,column]
+      mu = musig.ratio[ratio] *.01
+      sig= .01
+      r <- matrix(0,T+1,n)  # matrix to hold Asset Path
+      r[1,] <- s0  
+      drawdown <- matrix(0,length(musig.ratio),n)
+  
+      for(j in 1:n){
+        for(i in 2:(T+1)){
+          
+          dr <- mu*dt + sig*sqrt(dt)*rnorm(1,0,1)
+          r[i,j] <- r[i-1,j] + dr
+        }
+        drawdown[ratio,j] = maxDrawdown(r[ratio,j])
+      }
+      z = c((mu*100),
+            (sig*100),
+            ((mean(drawdown)*100)))
+      znames = c(
+        "Annual Returns in %",
+        "Std Devetions in %",
+        "Normalized Drawdown Drawdown in %"
+      )
+      if(column == 1) {
+        resultingtable = data.frame(Value = z, row.names = znames)
+      }
+      else {
+        nextcolumn = data.frame(Value = z, row.names = znames)
+        resultingtable = cbind(resultingtable, nextcolumn)
+      }
+    }
+  }
+    colnames(resultingtable) = columnnames
+    ans = base::round(resultingtable, digits)
+    ans
+    t <- seq(0, T, dt)
+    matplot(t, r[1,1:T], type="l", lty=1, main="Short Rate Paths", ylab="rt")
+    
+  }
+
+###############################################################################
+# R (http://r-project.org/) 
+#
+# Copyright (c) 2004-2013 
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: chart.ArcarJames
+#
+###############################################################################

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarNumberofObservations.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarNumberofObservations.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Code/chart.ArcarNumberofObservations.R	2013-07-18 13:53:49 UTC (rev 2594)
@@ -0,0 +1,114 @@
+#' Expected Drawdown using Brownian Motion Assumptions
+#' 
+#' Works on the model specified by Maddon-Ismail
+#' 
+#' 
+#' 
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+
+#' @author R
+#' @keywords Expected Drawdown Using Brownian Motion Assumptions
+#'
+#' @export 
+chart.ArcarJames <-
+  function (R,digits =4)
+  {# @author 
+    
+    # DESCRIPTION:
+    # Downside Risk Summary: Statistics and Stylized Facts
+    
+    # Inputs:
+    # R: a regular timeseries of returns (rather than prices)
+    # Output: Table of Estimated Drawdowns 
+    
+    y = checkData(R, method = "xts")
+    columns = ncol(y)
+    rows = nrow(y)
+    columnnames = colnames(y)
+    rownames = rownames(y)
+    T= 36;
+    n <- 1000
+    #tlength <- 36
+    musig.ratio=seq(-2,2,by=.1)
+    dt <- 1/T;
+    s0 <- 100;
+      for(column in 1:columns) {
+        x = y[,column]
+        mu = musig.ratio[ratio] *.01
+        sig= .01
+        r <- matrix(0,T+1,n)  # matrix to hold Asset Path
+        r[1,] <- s0  
+        drawdown <- matrix(0,length(musig.ratio),n)
+        # Generate k random walks across time {0, 1, ... , T}
+        T <- 100
+        k <- 250
+        initial.value <- 100
+        GetRandomWalk <- function() {
+          # Add a standard normal at each step
+          initial.value + c(0, cumsum(rnorm(T)))
+        
+        }
+        # Matrix of random walks
+        values <- replicate(k, GetRandomWalk())
+        # Create an empty plot
+        dev.new(height=8, width=12)
+        plot(0:T, rep(NA, T + 1), main=sprintf("%s Random Walks", k),
+             xlab="time", ylab="value",
+             ylim=100 + 4.5 * c(-1, 1) * sqrt(T))
+        mtext(sprintf("%s%s} with initial value of %s",
+                      "Across time {0, 1, ... , ", T, initial.value))
+        for (i in 1:k) {
+          lines(0:T, values[ , i], lwd=0.25)
+        }
+        for (sign in c(-1, 1)) {
+          curve(initial.value + sign * 1.96 * sqrt(x), from=0, to=T,
+                n=2*T, col="darkred", lty=2, lwd=1.5, add=TRUE)
+        }
+        legend("topright", "1.96 * sqrt(t)",
+               bty="n", lwd=1.5, lty=2, col="darkred")
+        
+        for(j in 1:n){
+          for(i in 2:(T+1)){
+            
+            dr <- mu*dt + sig*sqrt(dt)*rnorm(1,0,1)
+            r[i,j] <- r[i-1,j] + dr
+          }
+          drawdown[ratio,j] = maxDrawdown(r[ratio,j])
+        }
+        z = c((mu*100),
+              (sig*100),
+              ((mean(drawdown)*100)))
+        znames = c(
+          "Annual Returns in %",
+          "Std Devetions in %",
+          "Normalized Drawdown Drawdown in %"
+        )
+        if(column == 1) {
+          resultingtable = data.frame(Value = z, row.names = znames)
+        }
+        else {
+          nextcolumn = data.frame(Value = z, row.names = znames)
+          resultingtable = cbind(resultingtable, nextcolumn)
+        }
+      }
+    
+    colnames(resultingtable) = columnnames
+    ans = base::round(resultingtable, digits)
+    ans
+    t <- seq(0, T, dt)
+    matplot(t, r[1,1:T], type="l", lty=1, main="Short Rate Paths", ylab="rt")
+    
+  }
+
+###############################################################################
+# R (http://r-project.org/) 
+#
+# Copyright (c) 2004-2013 
+#
+# This R package is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: chart.ArcarNumberofObservations
+#
+###############################################################################

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.Rnw
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.Rnw	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.Rnw	2013-07-18 13:53:49 UTC (rev 2594)
@@ -0,0 +1,56 @@
+\documentclass[12pt,letterpaper,english]{article}
+\usepackage{times}
+\usepackage[T1]{fontenc}
+\IfFileExists{url.sty}{\usepackage{url}}
+                      {\newcommand{\url}{\texttt}}
+
+\usepackage{babel}
+\usepackage{Rd}
+
+\usepackage{Sweave}
+\SweaveOpts{engine=R,eps = FALSE}
+%\VignetteIndexEntry{Triple Penance Rule}
+%\VignetteDepends{PerformanceAnalytics}
+%\VignetteKeywords{Triple Penance Rule,Maximum Drawdown,Time under water,risk,portfolio}
+%\VignettePackage{PerformanceAnalytics}
+
+\begin{document}
+\SweaveOpts{concordance=TRUE}
+
+\title{ Maximum Loss and Maximum Drawdown in Financial
+Markets}
+
+% \keywords{Triple Penance Rule,Maximum Drawdown,Time Under Water,risk,portfolio}
+
+\makeatletter
+\makeatother
+\maketitle
+
+\begin{abstract}
+
+The main concern of this paper is the study of alternative risk measures: namely maximum loss and maximum drawdown. Both statistics have received little attention from academics despite their extensive use by proprietary traders and derivative fund managers.
+
+Firstly, this paper recalls from previously published research the expected maximum loss under the normal random walk with drift assumption. In that case, we see that exact analytical formulae can be established. The expected maximum loss can be derived as a function of the mean and standard deviation of the asset. For the maximum drawdown, exact formulae seems more difficult to establish.
+
+Therefore Monte-Carlo simulations have to be used.
+
+\end{abstract}
+
+<<echo = FALSE >>=
+library(PerformanceAnalytics)
+data(edhec)
+
+\section{ Maximum Drawdown }
+
+Unfortunately, there is no analytical formulae to establish the maximum drawdown properties under the random walk assumption. We should note first that due to its definition, the maximum drawdown divided by volatility is an only function of the ratio mean divided by volatility.
+
+When the distibution is normal
+
+\deqn{MD_{\sigma}=min\left\{0,\frac{({\X}\sigma)}}
+
+
+
+
+
+
+\end{document}

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.tex
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.tex	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/Week4/Vignette/MaximumLoss.tex	2013-07-18 13:53:49 UTC (rev 2594)
@@ -0,0 +1,35 @@
+\documentclass[12pt,letterpaper,english]{article}
+\usepackage{times}
+\usepackage[T1]{fontenc}
+\IfFileExists{url.sty}{\usepackage{url}}
+                      {\newcommand{\url}{\texttt}}
+
+\usepackage{babel}
+\usepackage{Rd}
+
+\usepackage{Sweave}
+
+%\VignetteIndexEntry{Triple Penance Rule}
+%\VignetteDepends{PerformanceAnalytics}
+%\VignetteKeywords{Triple Penance Rule,Maximum Drawdown,Time under water,risk,portfolio}
+%\VignettePackage{PerformanceAnalytics}
+
+\begin{document}
+\input{MaximumLoss-concordance}
+
+\title{ Maximum Loss and Maximum Drawdown in Financial
+Markets}
+
+% \keywords{Triple Penance Rule,Maximum Drawdown,Time Under Water,risk,portfolio}
+
+\makeatletter
+\makeatother
+\maketitle
+
+\begin{abstract}
+
+Drawdown based stopouts is a framework for informing the decision of stopping a portfolio manager or investment strategy once it has reached the drawdown or time under water limit associated with a certain confidence limit. 
+
+\end{abstract}
+
+



More information about the Returnanalytics-commits mailing list