[Returnanalytics-commits] r2041 - in pkg/PerformanceAnalytics: R data man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jun 19 18:45:22 CEST 2012
Author: matthieu_lestel
Date: 2012-06-19 18:45:22 +0200 (Tue, 19 Jun 2012)
New Revision: 2041
Added:
pkg/PerformanceAnalytics/R/DownsideFrequency.R
pkg/PerformanceAnalytics/data/portfolio_bacon.csv
pkg/PerformanceAnalytics/data/portfolio_bacon.rda
pkg/PerformanceAnalytics/man/DownsideFrequency.Rd
Modified:
pkg/PerformanceAnalytics/man/UpsideRisk.Rd
Log:
DownsideFrequency with exemples and documentation and addition of Bacon data
Added: pkg/PerformanceAnalytics/R/DownsideFrequency.R
===================================================================
--- pkg/PerformanceAnalytics/R/DownsideFrequency.R (rev 0)
+++ pkg/PerformanceAnalytics/R/DownsideFrequency.R 2012-06-19 16:45:22 UTC (rev 2041)
@@ -0,0 +1,66 @@
+#' downside frequency of the return distribution
+#'
+#' To calculate Downside Frequency, we take the subset of returns that are
+#' less than the target (or Minimum Acceptable Returns (MAR)) returns and
+#' divide the length of this subset by the total number of returns.
+#'
+#' \deqn{ DownsideFrequency(R , MAR) = \sum^{n}_{t=1}\frac{min[(R_{t} - MAR),
+#' 0]}{R_{t}*n}} {DownsideFrequency(R, MAR) = length(subset of returns below MAR) /
+#' length(total returns)}
+#'
+#' where \eqn{n} is the number of observations of the entire series
+#'
+#' @aliases DownsideFrequency
+#' @param R an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param MAR Minimum Acceptable Return, in the same periodicity as your
+#' returns
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement
+#' and attribution}, second edition 2008 p.94
+#'
+#' @keywords ts multivariate distribution models
+#' @examples
+#' data(portfolio_bacon)
+#' MAR = 0.5
+#' print(DownsideFrequency(portfolio_return, MAR)) #expected 0.458
+#'
+#' data(managers)
+#' print(DownsideFrequency(managers['1996']))
+#' print(DownsideFrequency(managers['1996',1])) #expected 0.25
+#'
+#' @export
+
+DownsideFrequency <- function (R, MAR = 0, ...)
+{
+ R0 <- R
+ R = checkData(R, method="matrix")
+
+ if (ncol(R)==1 || is.null(R) || is.vector(R)) {
+ R = na.omit(R)
+ r = subset(R, R < MAR)
+
+ if(!is.null(dim(MAR))){
+ if(is.timeBased(index(MAR))){
+ MAR <-MAR[index(r)] #subset to the same dates as the R data
+ }
+ else{
+ MAR = mean(checkData(MAR, method = "vector"))
+ # we have to assume that Ra and a vector of Rf passed in for MAR both cover the same time period
+ }
+ }
+ result = length(r) / length(R)
+ reclass(result, R0)
+ return(result)
+ }
+ else {
+ R = checkData(R)
+ result = apply(R, MARGIN = 2, DownsideFrequency, MAR = MAR, ...)
+ result<-t(result)
+ colnames(result) = colnames(R)
+ print(MAR)
+ rownames(result) = paste("Downside Frequency (MAR = ",MAR,"%)", sep="")
+ return(result)
+ }
+}
\ No newline at end of file
Added: pkg/PerformanceAnalytics/data/portfolio_bacon.csv
===================================================================
--- pkg/PerformanceAnalytics/data/portfolio_bacon.csv (rev 0)
+++ pkg/PerformanceAnalytics/data/portfolio_bacon.csv 2012-06-19 16:45:22 UTC (rev 2041)
@@ -0,0 +1,25 @@
+portfolio monthly return (%)
+0.3
+2.6
+1.1
+-1.0
+1.5
+2.5
+1.6
+6.7
+-1.4
+4.0
+-0.5
+8.1
+4.0
+-3.7
+-6.1
+1.7
+-4.9
+-2.2
+7.0
+5.8
+-6.5
+2.4
+-0.5
+-0.9
\ No newline at end of file
Added: pkg/PerformanceAnalytics/data/portfolio_bacon.rda
===================================================================
(Binary files differ)
Property changes on: pkg/PerformanceAnalytics/data/portfolio_bacon.rda
___________________________________________________________________
Added: svn:mime-type
+ application/octet-stream
Added: pkg/PerformanceAnalytics/man/DownsideFrequency.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/DownsideFrequency.Rd (rev 0)
+++ pkg/PerformanceAnalytics/man/DownsideFrequency.Rd 2012-06-19 16:45:22 UTC (rev 2041)
@@ -0,0 +1,51 @@
+\name{DownsideFrequency}
+\alias{DownsideFrequency}
+\title{downside frequency of the return distribution}
+\usage{
+ DownsideFrequency(R, MAR = 0, ...)
+}
+\arguments{
+ \item{R}{an xts, vector, matrix, data frame, timeSeries
+ or zoo object of asset returns}
+
+ \item{MAR}{Minimum Acceptable Return, in the same
+ periodicity as your returns}
+
+ \item{\dots}{any other passthru parameters}
+}
+\description{
+ To calculate Downside Frequency, we take the subset of
+ returns that are less than the target (or Minimum
+ Acceptable Returns (MAR)) returns and divide the length
+ of this subset by the total number of returns.
+}
+\details{
+ \deqn{ DownsideFrequency(R , MAR) =
+ \sum^{n}_{t=1}\frac{min[(R_{t} - MAR), 0]}{R_{t}*n}}
+ {DownsideFrequency(R, MAR) = length(subset of returns
+ below MAR) / length(total returns)}
+
+ where \eqn{n} is the number of observations of the entire
+ series
+}
+\examples{
+data(portfolio_bacon)
+MAR = 0.5
+print(DownsideFrequency(portfolio_return, MAR)) #expected 0.458
+
+data(managers)
+print(DownsideFrequency(managers['1996']))
+print(DownsideFrequency(managers['1996',1])) #expected 0.25
+}
+\author{
+ Matthieu Lestel
+}
+\references{
+ Carl Bacon, \emph{Practical portfolio performance
+ measurement and attribution}, second edition 2008 p.94
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+
Modified: pkg/PerformanceAnalytics/man/UpsideRisk.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/UpsideRisk.Rd 2012-06-19 12:58:44 UTC (rev 2040)
+++ pkg/PerformanceAnalytics/man/UpsideRisk.Rd 2012-06-19 16:45:22 UTC (rev 2041)
@@ -17,6 +17,10 @@
length of the subset of the series below the MAR as the
denominator, defaults to "subset"}
+ \item{method}{one of "risk", "variance" or "potential"
+ indicating whether to return the Upside risk, variance or
+ potential}
+
\item{\dots}{any other passthru parameters}
}
\description{
More information about the Returnanalytics-commits
mailing list