[Returnanalytics-commits] r3438 - in pkg/PerformanceAnalytics/sandbox/PAenhance: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jun 22 23:27:22 CEST 2014


Author: kecoli
Date: 2014-06-22 23:27:22 +0200 (Sun, 22 Jun 2014)
New Revision: 3438

Added:
   pkg/PerformanceAnalytics/sandbox/PAenhance/R/Uncertainty.R
   pkg/PerformanceAnalytics/sandbox/PAenhance/man/UncertaintyMeasure.Rd
Modified:
   pkg/PerformanceAnalytics/sandbox/PAenhance/NAMESPACE
   pkg/PerformanceAnalytics/sandbox/PAenhance/R/SharpeRatio.R
   pkg/PerformanceAnalytics/sandbox/PAenhance/man/SharpeRatio.Rd
Log:
change of man page, 
add uncertainty measure of Variance estimator, 
add bootstrap sd for SharpRatio, 
modify Sharpratio to adapt to table.Performance

Modified: pkg/PerformanceAnalytics/sandbox/PAenhance/NAMESPACE
===================================================================
--- pkg/PerformanceAnalytics/sandbox/PAenhance/NAMESPACE	2014-06-22 20:52:01 UTC (rev 3437)
+++ pkg/PerformanceAnalytics/sandbox/PAenhance/NAMESPACE	2014-06-22 21:27:22 UTC (rev 3438)
@@ -6,3 +6,4 @@
 export(table.Performance)
 export(table.Performance.pool)
 export(table.Performance.pool.cran)
+export(var.se)

Modified: pkg/PerformanceAnalytics/sandbox/PAenhance/R/SharpeRatio.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/PAenhance/R/SharpeRatio.R	2014-06-22 20:52:01 UTC (rev 3437)
+++ pkg/PerformanceAnalytics/sandbox/PAenhance/R/SharpeRatio.R	2014-06-22 21:27:22 UTC (rev 3438)
@@ -1,3 +1,6 @@
+#' calculate a traditional or modified Sharpe Ratio of Return over StdDev or
+#' VaR or ES, accompanied with bootstrap of standard error. 
+#' 
 #' A modified version of SharpeRatio that compatible with table.Peroformance
 #' 
 #' The Sharpe ratio is simply the return per unit of risk (represented by
@@ -72,11 +75,15 @@
 #' SharpeRatio(managers[,1:9], Rf = managers[,10,drop=FALSE])
 #' SharpeRatio(edhec,Rf = .04/12)
 #' 
+#' # bootstrap sd
+#' R =managers[,1:2,drop=FALSE]
+#' SharpeRatio(edhec[, 6, drop = FALSE], Rf = .04/12, FUN="VaR", bootsd=TRUE)
+#' 
 #' @export 
 #' @rdname SharpeRatio
 SharpeRatio <-
 function (R, Rf = 0, p = 0.95, method = c("StdDev", "VaR", "ES"), 
-		weights = NULL, annualize = FALSE, ...) 
+		weights = NULL, annualize = FALSE, bootsd = FALSE, ...) 
 {
 	R = checkData(R)
 	
@@ -121,23 +128,47 @@
 				invert = FALSE)
 		SRA
 	}
+	
+	boot.sd.fn <- function(X,idx,...,Rf, p, FUNC, FUN_ma) # FUN_ma: selecting srm.boot or sra
+	{
+		match.fun(FUN_ma)(X[idx],Rf=Rf,FUNC=FUNC,p=p)
+	}
+	
+	boot.sd <- function(X, ..., Rf, p, FUNC, FUN_ma) 
+	{
+		boot.res = boot(X, statistic=boot.sd.fn, FUN_ma=FUN_ma, Rf=Rf, p=p, R=10*length(X), FUNC=FUNC)
+		sd(as.vector(boot.res$t))
+	}
+	
+	
 	i = 1
 	if (is.null(weights)) {
 		result = matrix(nrow = length(method), ncol = ncol(R))
 		colnames(result) = colnames(R)
+		if(bootsd){
+			result.boot.sd = matrix(nrow=length(FUN), ncol=ncol(R))
+			}
 	}
 	else {
 		result = matrix(nrow = length(method))
 	}
 	tmprownames = vector()
 	
+	if(bootsd) require(boot)
+	
 	for (FUNCT in method) {
 		if (is.null(weights)) {
-			if (annualize) 
+			if (annualize) {
 				result[i, ] = sapply(R, FUN = sra, Rf = Rf, p = p, 
 						FUNC = FUNCT, ...)
-			else result[i, ] = sapply(R, FUN = srm, Rf = Rf, 
+			if(bootsd)
+				result.boot.sd[i,] = sapply(R,FUN=boot.sd, Rf=Rf, p=p, FUNC=FUNCT,FUN_ma="sra",...)
+		}
+			else {result[i, ] = sapply(R, FUN = srm, Rf = Rf, 
 						p = p, FUNC = FUNCT, ...)
+				if(bootsd)
+					result.boot.sd[i,] = sapply(R,FUN=boot.sd,Rf=Rf, p=p, FUNC=FUNCT,FUN_ma="srm",...)
+			}
 		}
 		else {
 			result[i, ] = mean(R %*% weights, na.rm = TRUE)/match.fun(FUNCT)(R, 
@@ -150,5 +181,9 @@
 		i = i + 1
 	}
 	rownames(result) = tmprownames
+	if(bootsd)
+		rownames(result.boot.sd)=tmprownames
+	if(bootsd) 
+		return(list(estimate = result, boot.sd = result.boot.sd))
 	return(result)
 }

Added: pkg/PerformanceAnalytics/sandbox/PAenhance/R/Uncertainty.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/PAenhance/R/Uncertainty.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/PAenhance/R/Uncertainty.R	2014-06-22 21:27:22 UTC (rev 3438)
@@ -0,0 +1,46 @@
+#' Uncertainty measure of Variance Estimator
+#' This function returns the standard error of the three estimator of Variance.
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param methods the estimation methods for Variance estimator, default is normal
+#' @author Douglass Martin, Kirk Li 
+#' @references TBA
+#' @keywords variance estimation, bootstrap
+#' @examples
+#' data(edhec)
+#' var.se(edhec[,1],methods="normal")
+#' @export 
+#' @rdname UncertaintyMeasure
+var.se <- function(R,methods=c("normal","non-normal","bootstrap")){
+	R <- checkData(R, method="xts")
+	columns=colnames(R)
+	
+	methods <- match.arg(methods,c("normal","non-normal","bootstrap"))
+	
+	switch(methods, 
+			"normal" = {		var.std.error = sapply(R, function(x) sqrt(2/(length(x)-1)* var(x,na.rm=TRUE)^2))
+			},
+			"non-normal" = {var.std.error = sapply(R, function(x) sqrt((length(x)/(length(x)-1))^2 * 
+											(
+												(mean((x-mean(x,na.rm=TRUE))^4,na.rm=TRUE)-var(x,na.rm=TRUE)^2)/length(x)
+												
+												-2*(mean((x-mean(x,na.rm=TRUE))^4)-2*var(x,na.rm=TRUE)^2)/length(x)^2
+												
+												+(mean((x-mean(x,na.rm=TRUE))^4,na.rm=TRUE)-3*var(x,na.rm=TRUE)^2)/length(x)^3
+												)
+							))},
+			"bootstrap" = {	require("boot")
+				
+				boot.sd.var <- function(X,idx) var(X[idx],na.rm=TRUE)
+				boot.sd <- function(X) 
+				{
+					boot.res = boot(X, statistic=boot.sd.var,  R=10*length(X))
+					sd(as.vector(boot.res$t))
+				}
+				
+				var.std.error = sapply(R,boot.sd)}
+	)
+	var.std.error
+} 
+
+


Property changes on: pkg/PerformanceAnalytics/sandbox/PAenhance/R/Uncertainty.R
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: pkg/PerformanceAnalytics/sandbox/PAenhance/man/SharpeRatio.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/PAenhance/man/SharpeRatio.Rd	2014-06-22 20:52:01 UTC (rev 3437)
+++ pkg/PerformanceAnalytics/sandbox/PAenhance/man/SharpeRatio.Rd	2014-06-22 21:27:22 UTC (rev 3438)
@@ -2,10 +2,11 @@
 \name{SharpeRatio}
 \alias{SharpeRatio}
 \alias{SharpeRatio.modified}
-\title{A modified version of SharpeRatio that compatible with table.Peroformance}
+\title{calculate a traditional or modified Sharpe Ratio of Return over StdDev or
+VaR or ES, accompanied with bootstrap of standard error.}
 \usage{
 SharpeRatio(R, Rf = 0, p = 0.95, method = c("StdDev", "VaR", "ES"),
-  weights = NULL, annualize = FALSE, ...)
+  weights = NULL, annualize = FALSE, bootsd = FALSE, ...)
 }
 \arguments{
   \item{R}{an xts, vector, matrix, data frame, timeSeries
@@ -28,11 +29,14 @@
   ES functions}
 }
 \description{
+A modified version of SharpeRatio that compatible with
+table.Peroformance
+}
+\details{
 The Sharpe ratio is simply the return per unit of risk
 (represented by variability).  In the classic case, the
 unit of risk is the standard deviation of the returns.
-}
-\details{
+
 \deqn{\frac{\overline{(R_{a}-R_{f})}}{\sqrt{\sigma_{(R_{a}-R_{f})}}}}
 
 William Sharpe now recommends
@@ -81,6 +85,10 @@
 # and all the methods
 SharpeRatio(managers[,1:9], Rf = managers[,10,drop=FALSE])
 SharpeRatio(edhec,Rf = .04/12)
+
+# bootstrap sd
+R =managers[,1:2,drop=FALSE]
+SharpeRatio(edhec[, 6, drop = FALSE], Rf = .04/12, FUN="VaR", bootsd=TRUE)
 }
 \author{
 Brian G. Peterson, Kirk Li

Added: pkg/PerformanceAnalytics/sandbox/PAenhance/man/UncertaintyMeasure.Rd
===================================================================
--- pkg/PerformanceAnalytics/sandbox/PAenhance/man/UncertaintyMeasure.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/sandbox/PAenhance/man/UncertaintyMeasure.Rd	2014-06-22 21:27:22 UTC (rev 3438)
@@ -0,0 +1,34 @@
+% Generated by roxygen2 (4.0.1.99): do not edit by hand
+\name{var.se}
+\alias{var.se}
+\title{Uncertainty measure of Variance Estimator
+This function returns the standard error of the three estimator of Variance.}
+\usage{
+var.se(R, methods = c("normal", "non-normal", "bootstrap"))
+}
+\arguments{
+  \item{R}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{methods}{the estimation methods for Variance
+  estimator, default is normal}
+}
+\description{
+Uncertainty measure of Variance Estimator This function
+returns the standard error of the three estimator of
+Variance.
+}
+\examples{
+data(edhec)
+var.se(edhec[,1],methods="normal")
+}
+\author{
+Douglass Martin, Kirk Li
+}
+\references{
+TBA
+}
+\keyword{bootstrap}
+\keyword{estimation,}
+\keyword{variance}
+


Property changes on: pkg/PerformanceAnalytics/sandbox/PAenhance/man/UncertaintyMeasure.Rd
___________________________________________________________________
Added: svn:mime-type
   + text/plain



More information about the Returnanalytics-commits mailing list