[Returnanalytics-commits] r2922 - in pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm: . R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Aug 29 04:27:05 CEST 2013


Author: shubhanm
Date: 2013-08-29 04:27:01 +0200 (Thu, 29 Aug 2013)
New Revision: 2922

Added:
   pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/EmaxDDGBM.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/GLMSmoothIndex.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/LoSharpe.R
   pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/inst/
Modified:
   pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION
Log:
noniid.sm package

Modified: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION	2013-08-29 01:29:57 UTC (rev 2921)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/DESCRIPTION	2013-08-29 02:27:01 UTC (rev 2922)
@@ -35,3 +35,5 @@
     'table.UnsmoothReturn.R'
     'UnsmoothReturn.R'
     'EmaxDDGBM.R'
+    'table.EMaxDDGBM.R'
+

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/EmaxDDGBM.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/EmaxDDGBM.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/EmaxDDGBM.R	2013-08-29 02:27:01 UTC (rev 2922)
@@ -0,0 +1,207 @@
+#' @title Expected Drawdown using Brownian Motion Assumptions
+#' 
+#' @description  Works on the model specified by Maddon-Ismail which investigates the behavior of this statistic for a Brownian motion 
+#' with drift.
+#' @details If X(t) is a random process on [0, T ], the maximum drawdown at time T , D(T), is defined by
+#' where \deqn{D(T) = sup [X(s) - X(t)]} where s belongs to [0,t] and s belongs to [0,T]
+#'Informally, this is the largest drop from a peak to a bottom. In this paper, we investigate the
+#'behavior of this statistic for a Brownian motion with drift. In particular, we give an infinite 
+#'series representation of its distribution, and consider its expected value. When the drift is zero,
+#'we give an analytic expression for the expected value, and for non-zero drift, we give an infinite
+#'series representation. For all cases, we compute the limiting \bold{(\eqn{T tends to \infty})} behavior, which can be
+#'logarithmic (\eqn{\mu} > 0), square root (\eqn{\mu} = 0), or linear (\eqn{\mu} < 0).
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of  asset returns
+#' @param digits significant number
+#' @author Shubhankit Mohan
+#' @keywords Expected Drawdown Using Brownian Motion Assumptions
+#' @references Magdon-Ismail, M., Atiya, A., Pratap, A., and Yaser S. Abu-Mostafa: On the Maximum Drawdown of a Browninan Motion, Journal of Applied Probability 41, pp. 147-161, 2004 \url{http://alumnus.caltech.edu/~amir/drawdown-jrnl.pdf}
+#' @keywords Drawdown models Brownian Motion Assumptions
+#' @examples
+#' 
+#'library(PerformanceAnalytics)
+#' data(edhec)
+#' table.EmaxDDGBM(edhec)
+#' @rdname table.EmaxDDGBM
+#' @export 
+table.EMaxDDGBM <-
+  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= nyears(y);
+    
+    # for each column, do the following:
+    for(column in 1:columns) {
+      x = y[,column]
+      mu = Return.annualized(x, scale = NA, geometric = TRUE)
+      sig=StdDev(x)
+      gamma<-sqrt(pi/8)
+      
+      if(mu==0){
+        
+        Ed<-2*gamma*sig*sqrt(T)
+        
+      }
+      
+      else{
+        
+        alpha<-mu*sqrt(T/(2*sig^2))
+        
+        x<-alpha^2
+        
+        if(mu>0){
+          
+          mQp<-matrix(c(
+            
+            0.0005, 0.0010, 0.0015, 0.0020, 0.0025, 0.0050, 0.0075, 0.0100, 0.0125,
+            
+            0.0150, 0.0175, 0.0200, 0.0225, 0.0250, 0.0275, 0.0300, 0.0325, 0.0350,
+            
+            0.0375, 0.0400, 0.0425, 0.0450, 0.0500, 0.0600, 0.0700, 0.0800, 0.0900,
+            
+            0.1000, 0.2000, 0.3000, 0.4000, 0.5000, 1.5000, 2.5000, 3.5000, 4.5000,
+            
+            10, 20, 30, 40, 50, 150, 250, 350, 450, 1000, 2000, 3000, 4000, 5000, 0.019690,
+            
+            0.027694, 0.033789, 0.038896, 0.043372, 0.060721, 0.073808, 0.084693, 0.094171,
+            
+            0.102651, 0.110375, 0.117503, 0.124142, 0.130374, 0.136259, 0.141842, 0.147162,
+            
+            0.152249, 0.157127, 0.161817, 0.166337, 0.170702, 0.179015, 0.194248, 0.207999,
+            
+            0.220581, 0.232212, 0.243050, 0.325071, 0.382016, 0.426452, 0.463159, 0.668992,
+            
+            0.775976, 0.849298, 0.905305, 1.088998, 1.253794, 1.351794, 1.421860, 1.476457,
+            
+            1.747485, 1.874323, 1.958037, 2.020630, 2.219765, 2.392826, 2.494109, 2.565985,
+            
+            2.621743),ncol=2)
+          
+          
+          
+          if(x<0.0005){
+            
+            Qp<-gamma*sqrt(2*x)
+            
+          }
+          
+          if(x>0.0005 & x<5000){
+            
+            Qp<-spline(log(mQp[,1]),mQp[,2],n=1,xmin=log(x),xmax=log(x))$y
+            
+          }
+          
+          if(x>5000){
+            
+            Qp<-0.25*log(x)+0.49088
+            
+          }
+          
+          Ed<-(2*sig^2/mu)*Qp
+          
+        }
+        
+        if(mu<0){
+          
+          mQn<-matrix(c(
+            
+            0.0005, 0.0010, 0.0015, 0.0020, 0.0025, 0.0050, 0.0075, 0.0100, 0.0125, 0.0150,
+            
+            0.0175, 0.0200, 0.0225, 0.0250, 0.0275, 0.0300, 0.0325, 0.0350, 0.0375, 0.0400,
+            
+            0.0425, 0.0450, 0.0475, 0.0500, 0.0550, 0.0600, 0.0650, 0.0700, 0.0750, 0.0800,
+            
+            0.0850, 0.0900, 0.0950, 0.1000, 0.1500, 0.2000, 0.2500, 0.3000, 0.3500, 0.4000,
+            
+            0.5000, 1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000, 5.0000,
+            
+            0.019965, 0.028394, 0.034874, 0.040369, 0.045256, 0.064633, 0.079746, 0.092708,
+            
+            0.104259, 0.114814, 0.124608, 0.133772, 0.142429, 0.150739, 0.158565, 0.166229,
+            
+            0.173756, 0.180793, 0.187739, 0.194489, 0.201094, 0.207572, 0.213877, 0.220056,
+            
+            0.231797, 0.243374, 0.254585, 0.265472, 0.276070, 0.286406, 0.296507, 0.306393,
+            
+            0.316066, 0.325586, 0.413136, 0.491599, 0.564333, 0.633007, 0.698849, 0.762455,
+            
+            0.884593, 1.445520, 1.970740, 2.483960, 2.990940, 3.492520, 3.995190, 4.492380,
+            
+            4.990430, 5.498820),ncol=2)
+          
+          
+          
+          
+          
+          if(x<0.0005){
+            
+            Qn<-gamma*sqrt(2*x)
+            
+          }
+          
+          if(x>0.0005 & x<5000){
+            
+            Qn<-spline(mQn[,1],mQn[,2],n=1,xmin=x,xmax=x)$y
+            
+          }
+          
+          if(x>5000){
+            
+            Qn<-x+0.50
+            
+          }
+          
+          Ed<-(2*sig^2/mu)*(-Qn)
+          
+        }
+        
+      }
+      
+    #  return(Ed)
+      
+      z = c((mu*100),
+            (sig*100),
+            (Ed*100))
+      znames = c(
+        "Annual Returns in %",
+        "Std Devetions in %",
+        "Expected 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
+    
+    
+  }
+
+###############################################################################
+################################################################################
+# 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: EmaxDDGBM.R 2271 2012-09-02 01:56:23Z braverock $
+#
+###############################################################################

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/GLMSmoothIndex.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/GLMSmoothIndex.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/GLMSmoothIndex.R	2013-08-29 02:27:01 UTC (rev 2922)
@@ -0,0 +1,74 @@
+#'@title  GLM Index 
+#'@description
+#'Getmansky Lo Markov Smoothing Index is a useful summary statistic for measuring the concentration of weights is
+#' a sum of square of Moving Average lag coefficient.
+#' This measure is well known in the industrial organization literature as the 
+#' \bold{ Herfindahl index}, a measure of the concentration of firms in a given industry. 
+#' The index is maximized when one coefficient is 1 and the rest are 0. In the context of
+#'smoothed returns, a lower value implies more smoothing, and the upper bound
+#'of 1 implies no smoothing,  hence \eqn{\xi} is reffered as a '\bold{smoothingindex}'.
+#'\deqn{ \xi =   \sum\theta(j)^2}
+#'Where j belongs to 0 to k,which is the number of lag factors input.
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @author Peter Carl, Brian Peterson, Shubhankit Mohan
+#' @aliases Return.Geltner
+#' @references \emph{Getmansky, Mila, Lo, Andrew W. and Makarov, Igor} An Econometric Model of Serial Correlation and Illiquidity in Hedge Fund Returns (March 1, 2003). MIT Sloan Working Paper No. 4288-03; MIT Laboratory for Financial Engineering Working Paper No. LFE-1041A-03; EFMA 2003 Helsinki Meetings. Available at SSRN: \url{http://ssrn.com/abstract=384700}
+#' 
+#' @keywords ts multivariate distribution models non-iid 
+#' @examples
+#' 
+#' data(edhec)
+#' head(GLMSmoothIndex(edhec))
+#' 
+#' @export
+GLMSmoothIndex<-
+  function(R = NULL)
+  {
+    columns = 1
+    columnnames = NULL
+    #Error handling if R is not NULL
+    if(!is.null(R)){
+      x = checkData(R)
+      columns = ncol(x)
+      n = nrow(x)
+      count = q
+        x=edhec
+        columns = ncol(x)
+        columnnames = colnames(x)
+        
+        # Calculate AutoCorrelation Coefficient
+        for(column in 1:columns) { # for each asset passed in as R
+          y = checkData(x[,column], method="vector", na.rm = TRUE)
+          sum = sum(abs(acf(y,plot=FALSE,lag.max=6)[[1]][2:7]));
+          acflag6 = acf(y,plot=FALSE,lag.max=6)[[1]][2:7]/sum;
+          values = sum(acflag6*acflag6)
+          
+          if(column == 1) {
+            result.df = data.frame(Value = values)
+            colnames(result.df) = columnnames[column]
+          }
+          else {
+            nextcol = data.frame(Value = values)
+            colnames(nextcol) = columnnames[column]
+            result.df = cbind(result.df, nextcol)
+          }
+        }
+      rownames(result.df)= paste("GLM Smooth Index")
+      
+        return(result.df)
+      
+    }  
+  }
+
+###############################################################################
+# 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: GLMSmoothIndex.R 2163 2012-07-16 00:30:19Z braverock $
+#
+###############################################################################

Added: pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/LoSharpe.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/LoSharpe.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/Shubhankit/noniid.sm/R/LoSharpe.R	2013-08-29 02:27:01 UTC (rev 2922)
@@ -0,0 +1,91 @@
+#'@title Andrew Lo Sharpe Ratio
+#'@description
+#' Although the Sharpe ratio has become part of the canon of modern financial 
+#' analysis, its applications typically do not account for the fact that it is an
+#' estimated quantity, subject to estimation errors that can be substantial in 
+#' some cases.
+#' 
+#' Many studies have documented various violations of the assumption of 
+#' IID returns for financial securities.
+#' 
+#' Under the assumption of stationarity,a version of the Central Limit Theorem can 
+#' still be  applied to the estimator .
+#' @details
+#' The relationship between SR and SR(q) is somewhat more involved for non-
+#'IID returns because the variance of Rt(q) is not just the sum of the variances of component returns but also includes all the covariances. Specifically, under
+#' the assumption that returns \eqn{R_t}  are stationary,
+#' \deqn{ Var[(R_t)] =   \sum \sum Cov(R(t-i),R(t-j)) = q{\sigma^2} + 2{\sigma^2} \sum (q-k)\rho(k) }
+#' Where  \eqn{ \rho(k) = Cov(R(t),R(t-k))/Var[(R_t)]} is the \eqn{k^{th}} order autocorrelation coefficient of the series of returns.This yields the following relationship between SR and SR(q):
+#' and i,j belongs to 0 to q-1
+#'\deqn{SR(q)  =  \eta(q) }
+#'Where :
+#' \deqn{ }{\eta(q) = [q]/[\sqrt(q\sigma^2) + 2\sigma^2 \sum(q-k)\rho(k)] }
+#' Where k belongs to 0 to q-1
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' daily asset returns
+#' @param Rf an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' annualized Risk Free Rate
+#' @param q Number of autocorrelated lag periods. Taken as 3 (Default)
+#' @param \dots any other pass thru parameters
+#' @author Brian G. Peterson, Peter Carl, Shubhankit Mohan
+#' @references Getmansky, Mila, Lo, Andrew W. and Makarov, Igor,\emph{ An Econometric Model of Serial Correlation and Illiquidity in Hedge Fund Returns} (March 1, 2003). MIT Sloan Working Paper No. 4288-03; MIT Laboratory for Financial Engineering Working Paper No. LFE-1041A-03; EFMA 2003 Helsinki Meetings.
+#' \url{http://ssrn.com/abstract=384700}
+#' @keywords ts multivariate distribution models non-iid 
+#' @examples
+#' 
+#' data(edhec)
+#' head(LoSharpe(edhec,0,3)
+#' @rdname LoSharpe
+#' @export
+LoSharpe <-
+  function (Ra,Rf = 0,q = 3, ...)
+  { # @author Brian G. Peterson, Peter Carl
+    
+    
+    # Function:
+    R = checkData(Ra, method="xts")
+    # Get dimensions and labels
+    columns.a = ncol(R)
+    columnnames.a = colnames(R)
+    # Time used for daily Return manipulations
+    Time= 252*nyears(edhec)
+    clean.lo <- function(column.R,q) {
+      # compute the lagged return series
+      gamma.k =matrix(0,q)
+      mu = sum(column.R)/(Time)
+      Rf= Rf/(Time)
+      for(i in 1:q){
+        lagR = lag(column.R, k=i)
+        # compute the Momentum Lagged Values
+        gamma.k[i]= (sum(((column.R-mu)*(lagR-mu)),na.rm=TRUE))
+      }
+      return(gamma.k)
+    }
+    neta.lo <- function(pho.k,q) {
+      # compute the lagged return series
+      sumq = 0
+      for(j in 1:q){
+        sumq = sumq+ (q-j)*pho.k[j]
+      }
+      return(q/(sqrt(q+2*sumq)))
+    }
+    for(column.a in 1:columns.a) { # for each asset passed in as R
+      # clean the data and get rid of NAs
+      mu = sum(R[,column.a])/(Time)
+      sig=sqrt(((R[,column.a]-mu)^2/(Time)))
+      pho.k = clean.lo(R[,column.a],q)/(as.numeric(sig[1]))
+      netaq=neta.lo(pho.k,q)
+      column.lo = (netaq*((mu-Rf)/as.numeric(sig[1])))
+      
+      if(column.a == 1)  { lo = column.lo }
+      else { lo = cbind (lo, column.lo) }
+      
+    }
+    colnames(lo) = columnnames.a
+    rownames(lo)= paste("Lo Sharpe Ratio")
+    return(lo)
+    
+    
+    # RESULTS:
+    
+  }



More information about the Returnanalytics-commits mailing list