[Returnanalytics-commits] r2157 - in pkg/PerformanceAnalytics: R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Jul 13 17:57:17 CEST 2012


Author: matthieu_lestel
Date: 2012-07-13 17:57:17 +0200 (Fri, 13 Jul 2012)
New Revision: 2157

Modified:
   pkg/PerformanceAnalytics/R/BurkeRatio.R
   pkg/PerformanceAnalytics/R/kurtosis.R
   pkg/PerformanceAnalytics/R/skewness.R
   pkg/PerformanceAnalytics/man/kurtosis.Rd
   pkg/PerformanceAnalytics/man/skewness.Rd
Log:
correction of calcul of skewness and kurtosis, update of documentation, addition of sample skewness, sample kurtosis and sample excess kurtosis

Modified: pkg/PerformanceAnalytics/R/BurkeRatio.R
===================================================================
--- pkg/PerformanceAnalytics/R/BurkeRatio.R	2012-07-13 12:33:44 UTC (rev 2156)
+++ pkg/PerformanceAnalytics/R/BurkeRatio.R	2012-07-13 15:57:17 UTC (rev 2157)
@@ -102,6 +102,10 @@
 		in_drawdown = FALSE
 	    }
 
+	    print(drawdown)
+	    D = Drawdowns(R)
+	    print(D)
+
        Rp = period/n*sum(R)
        result = (Rp - Rf)/sqrt(sum(drawdown^2))
        if(modified)

Modified: pkg/PerformanceAnalytics/R/kurtosis.R
===================================================================
--- pkg/PerformanceAnalytics/R/kurtosis.R	2012-07-13 12:33:44 UTC (rev 2156)
+++ pkg/PerformanceAnalytics/R/kurtosis.R	2012-07-13 15:57:17 UTC (rev 2157)
@@ -5,6 +5,22 @@
 #' This function was ported from the RMetrics package fUtilities to eliminate a
 #' dependency on fUtilties being loaded every time.  This function is identical
 #' except for the addition of \code{\link{checkData}} and additional labeling.
+#'
+#' \deqn{Kurtosis(moment) = \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_P})^4}
+#' {kurtosis(moment) = sum((x-mean(x))^4/var(x)^2)/length(x)}
+#' \deqn{Kurtosis(excess) = \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_P})^4 - 3}
+#' {kurtosis(excess) = sum((x-mean(x))^4/var(x)^2)/length(x) - 3}
+#' \deqn{Kurtosis(sample) =  \frac{n*(n+1)}{(n-1)*(n-2)*(n-3)}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_{S_P}})^4 }
+#' {kurtosis(sample) = sum(((x-mean(x))/var(x))^4)*n*(n+1)/((n-1)*(n-2)*(n-3))}
+#' \deqn{Kurtosis(fisher) = \frac{(n+1)*(n-1)}{(n-2)*(n-3)}*(\frac{\sum^{n}_{i=1}\frac{(r_i)^4}{n}}{(\sum^{n}_{i=1}(\frac{(r_i)^2}{n})^2} - \frac{3*(n-1)}{n+1})}
+#' {kurtosis (fisher) = ((n+1)*(n-1)*((sum(x^4)/n)/(sum(x^2)/n)^2 - (3*(n-1))/(n+1)))/((n-2)*(n-3))}
+#' \deqn{Kurtosis(sample excess) =  \frac{n*(n+1)}{(n-1)*(n-2)*(n-3)}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_{S_P}})^4  - \frac{3*(n-1)^2}{(n-2)*(n-3)}}
+#' {kurtosis(sample excess) = sum(((x-mean(x))/var(x))^4)*n*(n+1)/((n-1)*(n-2)*(n-3)) - 3*(n-1)^2/((n-2)*(n-3))}
+#'
+#'
+#' where \eqn{n} is the number of return, \eqn{\overline{r}} is the mean of the return
+#' distribution, \eqn{\sigma_P} is its standard deviation and \eqn{\sigma_{S_P}} is its
+#' sample standard deviation
 #' 
 #' @param na.rm a logical. Should missing values be removed?
 #' @param method a character string which specifies the method of computation.
@@ -15,11 +31,14 @@
 #' distributions; these forms should be used when resampling (bootstrap or
 #' jackknife). The \code{"fisher"} method correspond to the usual "unbiased"
 #' definition of sample variance, although in the case of kurtosis exact
-#' unbiasedness is not possible.
+#' unbiasedness is not possible. The \code{"sample"} method gives the sample
+#' kurtosis of the distribution.
 #' @param x a numeric vector or object.
 #' @param \dots arguments to be passed.
-#' @author Diethelm Wuertz
+#' @author Diethelm Wuertz, Matthieu Lestel
 #' @seealso \code{\link{skewness}}.
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.84-85
 #' @keywords univar
 #' @examples
 #' 
@@ -35,9 +54,15 @@
 #' 
 #' data(managers)
 #' kurtosis(managers[,1:8])
+#'
+#' data(portfolio_bacon)
+#' print(kurtosis(portfolio_bacon[,1], method="sample")) #expected 3.03
+#' print(kurtosis(portfolio_bacon[,1], method="sample_excess")) #expected -0.41
+#' print(kurtosis(managers['1996'], method="sample"))
+#' print(kurtosis(managers['1996',1], method="sample"))
 #' 
 kurtosis <-
-    function (x, na.rm = FALSE, method = c("excess", "moment", "fisher"), ...)
+    function (x, na.rm = FALSE, method = c("excess", "moment", "fisher", "sample", "sample_excess"), ...)
 {
     # @author Diethelm Wuertz
     # @author Brian Peterson   (modify for PerformanceAnalytics)
@@ -77,15 +102,21 @@
         n = length(x)
         if (is.integer(x)) x = as.numeric(x)
         if (method == "excess") {
-            kurtosis = sum((x-mean(x))^4/var(x)^2)/length(x) - 3
+            kurtosis = sum((x-mean(x))^4/(var(x)*(n-1)/n)^2)/length(x) - 3
         }
         if (method == "moment") {
-            kurtosis = sum((x-mean(x))^4/var(x)^2)/length(x)
+            kurtosis = sum((x-mean(x))^4/(var(x)*(n-1)/n)^2)/length(x)
         }
         if (method == "fisher") {
             kurtosis = ((n+1)*(n-1)*((sum(x^4)/n)/(sum(x^2)/n)^2 -
                 (3*(n-1))/(n+1)))/((n-2)*(n-3))
         }
+	if (method == "sample") {
+	   kurtosis = sum((x-mean(x))^4/var(x)^2)*n*(n+1)/((n-1)*(n-2)*(n-3))
+	}
+	if (method == "sample_excess") {
+	    kurtosis = sum((x-mean(x))^4/var(x)^2)*n*(n+1)/((n-1)*(n-2)*(n-3)) - 3*(n-1)^2/((n-2)*(n-3))
+	}
         kurtosis=array(kurtosis)
         if (column==1) {
             #create data.frame

Modified: pkg/PerformanceAnalytics/R/skewness.R
===================================================================
--- pkg/PerformanceAnalytics/R/skewness.R	2012-07-13 12:33:44 UTC (rev 2156)
+++ pkg/PerformanceAnalytics/R/skewness.R	2012-07-13 15:57:17 UTC (rev 2157)
@@ -2,18 +2,20 @@
 #' 
 #' compute skewness of a univariate distribution.
 #' 
-
-
-
-#' Skewness
-#' 
-#' compute skewness of a univariate distribution.
-#' 
-
-#' 
 #' This function was ported from the RMetrics package fUtilities to eliminate a
 #' dependency on fUtiltiies being loaded every time.  The function is identical
 #' except for the addition of \code{\link{checkData} and column support.}
+#'
+#' \deqn{Skewness(moment) = \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_P})^3}
+#' {Skewness(moment) = sum((x-mean(x))^3/var(x)^(3/2))/length(x)}
+#' \deqn{Skewness(sample) =  \frac{n}{(n-1)*(n-2)}*\sum^{n}_{i=1}(\frac{r_i - \overline{r}}{\sigma_{S_P}})^3 }
+#' {skewness(sample) = sum(((x-mean(x))/var(x))^3)*n/((n-1)*(n-2))}
+#' \deqn{Skewness(fisher) = \frac{\frac{\sqrt{n*(n-1)}}{n-2}*\sum^{n}_{i=1}\frac{x^3}{n}}{\sum^{n}_{i=1}(\frac{x^2}{n})^{3/2}}}
+#' {Skewness(fisher)((sqrt(n*(n-1))/(n-2))*(sum(x^3)/n))/((sum(x^2)/n)^(3/2))}
+#'
+#' where \eqn{n} is the number of return, \eqn{\overline{r}} is the mean of the return
+#' distribution, \eqn{\sigma_P} is its standard deviation and \eqn{\sigma_{S_P}} is its
+#' sample standard deviation
 #' 
 #' @param na.rm a logical. Should missing values be removed?
 #' @param method a character string which specifies the method of computation.
@@ -21,13 +23,16 @@
 #' method is based on the definitions of skewnessfor distributions; these forms
 #' should be used when resampling (bootstrap or jackknife). The \code{"fisher"}
 #' method correspond to the usual "unbiased" definition of sample variance,
-#' although in the case of skewness exact unbiasedness is not possible.
+#' although in the case of skewness exact unbiasedness is not possible. The 
+#' \code{"sample"} method gives the sample skewness of the distribution.
 #' @param x a numeric vector or object.
 #' @param \dots arguments to be passed.
-#' @author Diethelm Wuertz
+#' @author Diethelm Wuertz, Matthieu Lestel
 #' @seealso
 #' 
 #' \code{\link{kurtosis}}
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.83-84
 #' @keywords univar
 #' @examples
 #' 
@@ -44,7 +49,7 @@
 #' skewness(managers)
 #' 
 skewness <-
-    function (x, na.rm = FALSE, method = c("moment", "fisher"), ...)
+    function (x, na.rm = FALSE, method = c("moment", "fisher", "sample"), ...)
 {
     # @author Diethelm Wuertz
     # @author Brian Peterson   (modify for PerformanceAnalytics)
@@ -85,7 +90,7 @@
 
         # Selected Method:
         if (method == "moment") {
-            skewness = sum((x-mean(x))^3/sqrt(var(x))^3)/length(x)
+            skewness = sum((x-mean(x))^3/sqrt(var(x)*(n-1)/n)^3)/length(x)
         }
         if (method == "fisher") {
             if (n < 3)
@@ -93,6 +98,9 @@
             else
                 skewness = ((sqrt(n*(n-1))/(n-2))*(sum(x^3)/n))/((sum(x^2)/n)^(3/2))
         }
+	if (method == "sample") {
+            skewness = sum((x-mean(x))^3/sqrt(var(x)*(n-1)/n)^3)*n/((n-1)*(n-2))
+	}
 
         skewness=array(skewness)
         if (column==1) {

Modified: pkg/PerformanceAnalytics/man/kurtosis.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/kurtosis.Rd	2012-07-13 12:33:44 UTC (rev 2156)
+++ pkg/PerformanceAnalytics/man/kurtosis.Rd	2012-07-13 15:57:17 UTC (rev 2157)
@@ -3,7 +3,8 @@
 \title{Kurtosis}
 \usage{
   kurtosis(x, na.rm = FALSE,
-    method = c("excess", "moment", "fisher"), ...)
+    method = c("excess", "moment", "fisher", "sample", "sample_excess"),
+    ...)
 }
 \arguments{
   \item{na.rm}{a logical. Should missing values be
@@ -19,7 +20,9 @@
   should be used when resampling (bootstrap or jackknife).
   The \code{"fisher"} method correspond to the usual
   "unbiased" definition of sample variance, although in the
-  case of kurtosis exact unbiasedness is not possible.}
+  case of kurtosis exact unbiasedness is not possible. The
+  \code{"sample"} method gives the sample kurtosis of the
+  distribution.}
 
   \item{x}{a numeric vector or object.}
 
@@ -34,6 +37,35 @@
   loaded every time.  This function is identical except for
   the addition of \code{\link{checkData}} and additional
   labeling.
+
+  \deqn{Kurtosis(moment) =
+  \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i -
+  \overline{r}}{\sigma_P})^4} {kurtosis(moment) =
+  sum((x-mean(x))^4/var(x)^2)/length(x)}
+  \deqn{Kurtosis(excess) =
+  \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i -
+  \overline{r}}{\sigma_P})^4 - 3} {kurtosis(excess) =
+  sum((x-mean(x))^4/var(x)^2)/length(x) - 3}
+  \deqn{Kurtosis(sample) =
+  \frac{n*(n+1)}{(n-1)*(n-2)*(n-3)}*\sum^{n}_{i=1}(\frac{r_i
+  - \overline{r}}{\sigma_{S_P}})^4 } {kurtosis(sample) =
+  sum(((x-mean(x))/var(x))^4)*n*(n+1)/((n-1)*(n-2)*(n-3))}
+  \deqn{Kurtosis(fisher) =
+  \frac{(n+1)*(n-1)}{(n-2)*(n-3)}*(\frac{\sum^{n}_{i=1}\frac{(r_i)^4}{n}}{(\sum^{n}_{i=1}(\frac{(r_i)^2}{n})^2}
+  - \frac{3*(n-1)}{n+1})} {kurtosis (fisher) =
+  ((n+1)*(n-1)*((sum(x^4)/n)/(sum(x^2)/n)^2 -
+  (3*(n-1))/(n+1)))/((n-2)*(n-3))} \deqn{Kurtosis(sample
+  excess) =
+  \frac{n*(n+1)}{(n-1)*(n-2)*(n-3)}*\sum^{n}_{i=1}(\frac{r_i
+  - \overline{r}}{\sigma_{S_P}})^4 -
+  \frac{3*(n-1)^2}{(n-2)*(n-3)}} {kurtosis(sample excess) =
+  sum(((x-mean(x))/var(x))^4)*n*(n+1)/((n-1)*(n-2)*(n-3)) -
+  3*(n-1)^2/((n-2)*(n-3))}
+
+  where \eqn{n} is the number of return, \eqn{\overline{r}}
+  is the mean of the return distribution, \eqn{\sigma_P} is
+  its standard deviation and \eqn{\sigma_{S_P}} is its
+  sample standard deviation
 }
 \examples{
 ## mean -
@@ -48,10 +80,20 @@
 
 data(managers)
 kurtosis(managers[,1:8])
+
+data(portfolio_bacon)
+print(kurtosis(portfolio_bacon[,1], method="sample")) #expected 3.03
+print(kurtosis(portfolio_bacon[,1], method="sample_excess")) #expected -0.41
+print(kurtosis(managers['1996'], method="sample"))
+print(kurtosis(managers['1996',1], method="sample"))
 }
 \author{
-  Diethelm Wuertz
+  Diethelm Wuertz, Matthieu Lestel
 }
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.84-85
+}
 \seealso{
   \code{\link{skewness}}.
 }

Modified: pkg/PerformanceAnalytics/man/skewness.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/skewness.Rd	2012-07-13 12:33:44 UTC (rev 2156)
+++ pkg/PerformanceAnalytics/man/skewness.Rd	2012-07-13 15:57:17 UTC (rev 2157)
@@ -3,7 +3,7 @@
 \title{Skewness}
 \usage{
   skewness(x, na.rm = FALSE,
-    method = c("moment", "fisher"), ...)
+    method = c("moment", "fisher", "sample"), ...)
 }
 \arguments{
   \item{na.rm}{a logical. Should missing values be
@@ -16,7 +16,9 @@
   should be used when resampling (bootstrap or jackknife).
   The \code{"fisher"} method correspond to the usual
   "unbiased" definition of sample variance, although in the
-  case of skewness exact unbiasedness is not possible.}
+  case of skewness exact unbiasedness is not possible. The
+  \code{"sample"} method gives the sample skewness of the
+  distribution.}
 
   \item{x}{a numeric vector or object.}
 
@@ -26,15 +28,28 @@
   compute skewness of a univariate distribution.
 }
 \details{
-  Skewness
-
-  compute skewness of a univariate distribution.
-
   This function was ported from the RMetrics package
   fUtilities to eliminate a dependency on fUtiltiies being
   loaded every time.  The function is identical except for
   the addition of \code{\link{checkData} and column
   support.}
+
+  \deqn{Skewness(moment) =
+  \frac{1}{n}*\sum^{n}_{i=1}(\frac{r_i -
+  \overline{r}}{\sigma_P})^3} {Skewness(moment) =
+  sum((x-mean(x))^3/var(x)^(3/2))/length(x)}
+  \deqn{Skewness(sample) =
+  \frac{n}{(n-1)*(n-2)}*\sum^{n}_{i=1}(\frac{r_i -
+  \overline{r}}{\sigma_{S_P}})^3 } {skewness(sample) =
+  sum(((x-mean(x))/var(x))^3)*n/((n-1)*(n-2))}
+  \deqn{Skewness(fisher) =
+  \frac{\frac{\sqrt{n*(n-1)}}{n-2}*\sum^{n}_{i=1}\frac{x^3}{n}}{\sum^{n}_{i=1}(\frac{x^2}{n})^{3/2}}}
+  {Skewness(fisher)((sqrt(n*(n-1))/(n-2))*(sum(x^3)/n))/((sum(x^2)/n)^(3/2))}
+
+  where \eqn{n} is the number of return, \eqn{\overline{r}}
+  is the mean of the return distribution, \eqn{\sigma_P} is
+  its standard deviation and \eqn{\sigma_{S_P}} is its
+  sample standard deviation
 }
 \examples{
 ## mean -
@@ -50,8 +65,12 @@
 skewness(managers)
 }
 \author{
-  Diethelm Wuertz
+  Diethelm Wuertz, Matthieu Lestel
 }
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.83-84
+}
 \seealso{
   \code{\link{kurtosis}}
 }



More information about the Returnanalytics-commits mailing list