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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 28 17:16:44 CEST 2012


Author: matthieu_lestel
Date: 2012-06-28 17:16:44 +0200 (Thu, 28 Jun 2012)
New Revision: 2088

Added:
   pkg/PerformanceAnalytics/R/TotalRisk.R
   pkg/PerformanceAnalytics/man/TotalRisk.Rd
Log:
Total risk with examples and documentation

Added: pkg/PerformanceAnalytics/R/TotalRisk.R
===================================================================
--- pkg/PerformanceAnalytics/R/TotalRisk.R	                        (rev 0)
+++ pkg/PerformanceAnalytics/R/TotalRisk.R	2012-06-28 15:16:44 UTC (rev 2088)
@@ -0,0 +1,67 @@
+#' Total risk of the return distribution
+#'
+#' The square of total risk is the sum of the square of systematic risk and the square
+#' of specific risk. Specific risk is the standard deviation of the error term in the
+#' regression equation. Both terms are annualized to calculate total risk.
+#'
+#' \deqn{Total Risk = \sqrt{Systematic Risk^2 + Specific Risk^2}} 
+#' {Total Risk^2 = Systematic Risk^2 + Specific Risk^2}
+#'
+#' @aliases total risk
+#' @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 Period the number of return in a year in the asset return 
+#' (i.e. 12 for monthly returns, 4 for quarterly returns)
+#' @param \dots any other passthru parameters
+#' @author Matthieu Lestel
+#' @references Carl Bacon, \emph{Practical portfolio performance measurement 
+#' and attribution}, second edition 2008 p.75
+#' 
+#' @keywords ts multivariate distribution models
+#' @examples
+#'
+#' data(portfolio_bacon)
+#' print(TotalRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 13.4
+#'
+#' data(managers)
+#' print(TotalRisk(managers['1996',1], managers['1996',8]))
+#' print(TotalRisk(managers['1996',1:5], managers['1996',8]))
+#'
+#' @export 
+
+TotalRisk <-
+function (Ra, Rb, Rf = 0, Period = 12,  ...)
+{
+    calcul = FALSE
+    Ra = checkData(Ra, method="matrix")
+    Rb = checkData(Rb, method="matrix")
+
+    if (ncol(Ra)==1 || is.null(Ra) || is.vector(Ra)) {
+    
+     for (i in (1:length(Ra))) {
+     	 if (!is.na(Ra[i])) {
+     	    calcul = TRUE
+	 }
+      }
+
+     if (calcul) {
+     	epsilon = Ra - Rb * CAPM.beta(Ra,Rb,Rf) - CAPM.alpha(Ra,Rb,Rf)
+	specifikRisk = sqrt(sum((epsilon - mean(epsilon))^2)/length(epsilon))*sqrt(Period)
+        result = sqrt((SystematicRisk(Ra,Rb,Rf)*sqrt(Period))^2 + specifikRisk^2)
+     }    
+     else {
+        result = NA
+     }
+      return(result)
+    }
+    else {
+        Ra = checkData(Ra)
+        result = apply(Ra, MARGIN = 2, TotalRisk, Rb = Rb, Rf = Rf, ...)
+        result<-t(result)
+        colnames(result) = colnames(Ra)
+        rownames(result) = paste("Total Risk = ", sep="")
+        return(result)
+    }
+}

Added: pkg/PerformanceAnalytics/man/TotalRisk.Rd
===================================================================
--- pkg/PerformanceAnalytics/man/TotalRisk.Rd	                        (rev 0)
+++ pkg/PerformanceAnalytics/man/TotalRisk.Rd	2012-06-28 15:16:44 UTC (rev 2088)
@@ -0,0 +1,54 @@
+\name{TotalRisk}
+\alias{risk}
+\alias{total}
+\alias{TotalRisk}
+\title{Total risk of the return distribution}
+\usage{
+  TotalRisk(Ra, Rb, Rf = 0, Period = 12, ...)
+}
+\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{Period}{the number of return in a year in the asset
+  return (i.e. 12 for monthly returns, 4 for quarterly
+  returns)}
+
+  \item{\dots}{any other passthru parameters}
+}
+\description{
+  The square of total risk is the sum of the square of
+  systematic risk and the square of specific risk. Specific
+  risk is the standard deviation of the error term in the
+  regression equation. Both terms are annualized to
+  calculate total risk.
+}
+\details{
+  \deqn{Total Risk = \sqrt{Systematic Risk^2 + Specific
+  Risk^2}} {Total Risk^2 = Systematic Risk^2 + Specific
+  Risk^2}
+}
+\examples{
+data(portfolio_bacon)
+print(TotalRisk(portfolio_bacon[,1], portfolio_bacon[,2])) #expected 13.4
+
+data(managers)
+print(TotalRisk(managers['1996',1], managers['1996',8]))
+print(TotalRisk(managers['1996',1:5], managers['1996',8]))
+}
+\author{
+  Matthieu Lestel
+}
+\references{
+  Carl Bacon, \emph{Practical portfolio performance
+  measurement and attribution}, second edition 2008 p.75
+}
+\keyword{distribution}
+\keyword{models}
+\keyword{multivariate}
+\keyword{ts}
+



More information about the Returnanalytics-commits mailing list