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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 25 16:11:34 CEST 2012


Author: matthieu_lestel
Date: 2012-06-25 16:11:34 +0200 (Mon, 25 Jun 2012)
New Revision: 2071

Added:
   pkg/PerformanceAnalytics/R/CAPM.epsilon.R
   pkg/PerformanceAnalytics/man/CAPM.epsilon.Rd
Modified:
   pkg/PerformanceAnalytics/R/CAPM.alpha.R
   pkg/PerformanceAnalytics/R/UpsideRisk.R
   pkg/PerformanceAnalytics/data/portfolio_bacon.csv
Log:
epsilon regression with documentation

Modified: pkg/PerformanceAnalytics/R/CAPM.alpha.R
===================================================================
--- pkg/PerformanceAnalytics/R/CAPM.alpha.R	2012-06-25 01:33:27 UTC (rev 2070)
+++ pkg/PerformanceAnalytics/R/CAPM.alpha.R	2012-06-25 14:11:34 UTC (rev 2071)
@@ -64,7 +64,7 @@
     if(!is.null(dim(Rf)))
         Rf = checkData(Rf)
 
-    Ra.ncols = NCOL(Ra) 
+    Ra.ncols = NCOL(Ra)	
     Rb.ncols = NCOL(Rb)
 
     xRa = Return.excess(Ra, Rf)

Added: pkg/PerformanceAnalytics/R/CAPM.epsilon.R
===================================================================
--- pkg/PerformanceAnalytics/R/CAPM.epsilon.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/CAPM.epsilon.R	2012-06-25 14:11:34 UTC (rev 2071)
@@ -0,0 +1,67 @@
+#' Regression epsilon of the return distribution
+#'
+#' The regression epsilon is an error term measuring the vertical distance between
+#' the return predicted by the equation and the real result.
+#'
+#' \deqn{\epsilon_r = r_p - \alpha_r - \beta_r * b} 
+#' {epsilon_r = r_p - alpha_r - beta_r * b}
+#'
+#' where \eqn{\alpha_r} is the regression alpha, \eqn{\beta_r} is the regression beta,
+#' \eqn{r} is the portfolio return and b is the benchmark return
+#'
+#' @aliases Regression epsilon
+#' @param Ra an xts, vector, matrix, data frame, timeSeries or zoo object of
+#' asset returns
+#' @param Rb return vector of the benchmark asset
+#' @param Rf risk free rate, in same period 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.71
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' data(portfolio_bacon)
+#' print(CAPM.epsilon(portfolio_bacon[,1], portfolio_bacon[,2])) #expected -3.046
+#'
+#' data(managers)
+#' print(CAPM.epsilon(managers['1996',1], managers['1996',8]))
+#' print(CAPM.epsilon(managers['1996',1:5], managers['1996',8]))
+#'
+#' @export 
+
+CAPM.epsilon <-
+function (Ra, Rb, Rf = 0, ...)
+{
+    calcul = FALSE
+    Ra = checkData(Ra, method="matrix")
+    Rb = checkData(Rb, method="matrix")
+
+    if (ncol(Ra)==1 || is.null(Ra) || is.vector(Ra)) {
+    
+     Rp = (prod(0.01*Ra+1)-1)*100 #portfolio total return
+     Rpb = (prod(0.01*Rb+1)-1)*100 #benchmark total return
+     for (i in (1:length(Ra))) {
+     if (!is.na(Ra[i])) {
+     calcul = TRUE
+}
+}
+
+     if (calcul) {
+     result = Rf + Rp - CAPM.alpha(Ra,Rb,Rf) - (Rpb - Rf) * CAPM.beta(Ra,Rb,Rf) 
+}   
+    else {
+    result = NA
+}
+     return(result)
+    }
+    else {
+        Ra = checkData(Ra)
+        result = apply(Ra, MARGIN = 2, CAPM.epsilon, Rb = Rb, Rf = Rf, ...)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("Regression epsilon (Risk free = ",Rf,")", sep="")
+        return(result)
+    }
+}

Modified: pkg/PerformanceAnalytics/R/UpsideRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/UpsideRisk.R	2012-06-25 01:33:27 UTC (rev 2070)
+++ pkg/PerformanceAnalytics/R/UpsideRisk.R	2012-06-25 14:11:34 UTC (rev 2071)
@@ -53,8 +53,6 @@
 #'
 #' @export 
 
-#TODO tests for multi columns data
-
 UpsideRisk <-
 function (R, MAR = 0, method=c("full","subset"), stat=c("risk","variance","potential"), ...)
 {

Modified: pkg/PerformanceAnalytics/data/portfolio_bacon.csv
===================================================================
--- pkg/PerformanceAnalytics/data/portfolio_bacon.csv	2012-06-25 01:33:27 UTC (rev 2070)
+++ pkg/PerformanceAnalytics/data/portfolio_bacon.csv	2012-06-25 14:11:34 UTC (rev 2071)
@@ -1,25 +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
+,portfolio monthly return (%), benchmark return (%)
+2000-01-31,0.3,0.2
+2000-02-28,2.6,2.5
+2000-03-31,1.1,1.8
+2000-04-30,-1.0,-1.1
+2000-05-31,1.5,1.4
+2000-06-30,2.5,1.8
+2000-07-31,1.6,1.4
+2000-08-31,6.7,6.5
+2000-09-30,-1.4,-1.5
+2000-10-31,4.0,4.2
+2000-11-30,-0.5,-0.6
+2000-12-31,8.1,8.3
+2001-01-31,4.0,3.9
+2001-02-28,-3.7,-3.8
+2001-03-31,-6.1,-6.2
+2001-04-30,1.7,1.5
+2001-05-31,-4.9,-4.8
+2001-06-30,-2.2,2.1
+2001-07-31,7.0,6.0
+2001-08-31,5.8,5.6
+2001-09-30,-6.5,-6.7
+2001-10-31,2.4,1.9
+2001-11-30,-0.5,-0.3
+2001-12-31,-0.9,0
\ No newline at end of file

Added: pkg/PerformanceAnalytics/man/CAPM.epsilon.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/CAPM.epsilon.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/CAPM.epsilon.Rd	2012-06-25 14:11:34 UTC (rev 2071)
@@ -0,0 +1,51 @@
+\name{CAPM.epsilon}
+\alias{CAPM.epsilon}
+\alias{epsilon}
+\alias{Regression}
+\title{Regression epsilon of the return distribution}
+\usage{
+  CAPM.epsilon(Ra, Rb, Rf = 0, ...)
+}
+\arguments{
+  \item{Ra}{an xts, vector, matrix, data frame, timeSeries
+  or zoo object of asset returns}
+
+  \item{Rb}{return vector of the benchmark asset}
+
+  \item{Rf}{risk free rate, in same period as your returns}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  The regression epsilon is an error term measuring the
+  vertical distance between the return predicted by the
+  equation and the real result.
+}
+\details{
+  \deqn{\epsilon_r = r_p - \alpha_r - \beta_r * b}
+  {epsilon_r = r_p - alpha_r - beta_r * b}
+
+  where \eqn{\alpha_r} is the regression alpha,
+  \eqn{\beta_r} is the regression beta, \eqn{r} is the
+  portfolio return and b is the benchmark return
+}
+\examples{
+data(portfolio_bacon)
+print(CAPM.epsilon(portfolio_bacon[,1], portfolio_bacon[,2])) #expected -3.046
+
+data(managers)
+print(CAPM.epsilon(managers['1996',1], managers['1996',8]))
+print(CAPM.epsilon(managers['1996',1:5], managers['1996',8]))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.71
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list