[Returnanalytics-commits] r2305 - pkg/PerformanceAnalytics/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Dec 20 21:09:19 CET 2012


Author: braverock
Date: 2012-12-20 21:09:18 +0100 (Thu, 20 Dec 2012)
New Revision: 2305

Modified:
   pkg/PerformanceAnalytics/R/StdDev.annualized.R
   pkg/PerformanceAnalytics/R/TreynorRatio.R
Log:
- rearrange handling of 'scale'

Modified: pkg/PerformanceAnalytics/R/StdDev.annualized.R
===================================================================
--- pkg/PerformanceAnalytics/R/StdDev.annualized.R	2012-12-15 20:19:56 UTC (rev 2304)
+++ pkg/PerformanceAnalytics/R/StdDev.annualized.R	2012-12-20 20:09:18 UTC (rev 2305)
@@ -49,6 +49,22 @@
 StdDev.annualized <- sd.annualized <- sd.multiperiod <-
 function (x, scale = NA, ...)
 {
+    if(is.na(scale) && !xtsible(x))
+        stop("'x' needs to be timeBased or xtsible, or scale must be specified." )
+    
+    if(is.na(scale)) {
+        freq = periodicity(x)
+        switch(freq$scale,
+                minute = {stop("Data periodicity too high")},
+                hourly = {stop("Data periodicity too high")},
+                daily = {scale = 252},
+                weekly = {scale = 52},
+                monthly = {scale = 12},
+                quarterly = {scale = 4},
+                yearly = {scale = 1}
+        )
+    }
+    
     if (is.vector(x)) {
         #scale standard deviation by multiplying by the square root of the number of periods to scale by
         sqrt(scale)*sd(x, na.rm=TRUE)
@@ -56,19 +72,7 @@
         if(!xtsible(x) & is.na(scale))
             stop("'x' needs to be timeBased or xtsible, or scale must be specified." )
         x = checkData (x)
-        if(is.na(scale)) {
-            freq = periodicity(x)
-            switch(freq$scale,
-                    minute = {stop("Data periodicity too high")},
-                    hourly = {stop("Data periodicity too high")},
-                    daily = {scale = 252},
-                    weekly = {scale = 52},
-                    monthly = {scale = 12},
-                    quarterly = {scale = 4},
-                    yearly = {scale = 1}
-            )
-        }        
-        
+                
         result = apply(x, 2, sd.multiperiod, scale=scale)
         
         dim(result) = c(1,NCOL(x))

Modified: pkg/PerformanceAnalytics/R/TreynorRatio.R
===================================================================
--- pkg/PerformanceAnalytics/R/TreynorRatio.R	2012-12-15 20:19:56 UTC (rev 2304)
+++ pkg/PerformanceAnalytics/R/TreynorRatio.R	2012-12-20 20:09:18 UTC (rev 2305)
@@ -57,6 +57,19 @@
   Rb = checkData(Rb)
   if(!is.null(dim(Rf)))
     Rf = checkData(Rf)
+
+  if(is.na(scale)) {
+    freq = periodicity(Ra)
+    switch(freq$scale,
+            minute = {stop("Data periodicity too high")},
+            hourly = {stop("Data periodicity too high")},
+            daily = {scale = 252},
+            weekly = {scale = 52},
+            monthly = {scale = 12},
+            quarterly = {scale = 4},
+            yearly = {scale = 1}
+    )
+  }
   
   Ra.ncols = NCOL(Ra) 
   Rb.ncols = NCOL(Rb)
@@ -65,20 +78,7 @@
   
   xRa = Return.excess(Ra, Rf)
   xRb = Return.excess(Rb, Rf)
-  
-  if(is.na(scale)) {
-    freq = periodicity(Ra)
-    switch(freq$scale,
-           minute = {stop("Data periodicity too high")},
-           hourly = {stop("Data periodicity too high")},
-           daily = {scale = 252},
-           weekly = {scale = 52},
-           monthly = {scale = 12},
-           quarterly = {scale = 4},
-           yearly = {scale = 1}
-    )
-  }
-  
+    
   tr <-function (xRa, xRb, scale)
   {
     beta = CAPM.beta(xRa, xRb)



More information about the Returnanalytics-commits mailing list