[Returnanalytics-commits] r4004 - pkg/PerformanceAnalytics/sandbox
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Nov 25 23:24:02 CET 2015
Author: peter_carl
Date: 2015-11-25 23:24:01 +0100 (Wed, 25 Nov 2015)
New Revision: 4004
Modified:
pkg/PerformanceAnalytics/sandbox/Normalize.R
Log:
- added normalization in units of std dev
Modified: pkg/PerformanceAnalytics/sandbox/Normalize.R
===================================================================
--- pkg/PerformanceAnalytics/sandbox/Normalize.R 2015-11-18 09:47:25 UTC (rev 4003)
+++ pkg/PerformanceAnalytics/sandbox/Normalize.R 2015-11-25 22:24:01 UTC (rev 4004)
@@ -6,8 +6,11 @@
#'
#'
#' @param R
-#' @param targetMean
-#' @param targetVol
+#' @param targetMean specifies a return level to normalize each column to, in the same time units as the timeseries; default is zero
+#' @param targetVol specifies a volatility level to normalize each column to, in the same time units as the timeseries
+#' @param show.units.sd Normalize the plot's y-axis to units of annualized standard deviation
+#' @param scale number of periods in a year (daily scale = 252, monthly scale =
+#' 12, quarterly scale = 4)
#' @param ... passes arguments to par
#' @return xts or other time series
#' @author Peter Carl
@@ -15,7 +18,7 @@
#' @rdname Return.normalize
#' @export Return.normalize
#' @export chart.NDD
-Return.normalize <- (R, targetMean=0, targetVol=0, ...){
+Return.normalize <- function(R, targetMean=0, targetVol, ...){
# Peter Carl
x=checkData(R)
x.Mean=apply(x, MARGIN=2, FUN="mean", na.rm = TRUE)
@@ -23,19 +26,36 @@
# @TODO wil this work for vector? checkData as matrix?
# Apply z-score
x.Z = apply(x, MARGIN=2, FUN=function(x){ (x - mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE) }) # x.Z has mean=0, sd=1
- x.N= targetMean + x.Z * (rep(1, nrow(x.R)) %o% rep(targetVol,NCOL(x.R)))
+ x.N= targetMean + x.Z * (rep(1, nrow(x)) %o% rep(targetVol,NCOL(x)))
x.N = as.xts(x.N, by=index(x.R))
x.N = reclass(x.N, R)
return(x.N)
}
-chart.NDD <- function(R, targetMean=0, targetVol=0){
+chart.NDD <- function(R, targetMean=0, targetVol, show.units.sd=FALSE, ylab="Drawdown", scale=NA, ...){
# Peter Carl
- x.N = Return.normalize(x)
+ # @TODO: determine periodicity of R, specify multiplier for x.NDD below
+ if(is.na(scale)) {
+ freq = periodicity(R)
+ 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}
+ )
+ }
+ x.N = Return.normalize(R, targetMean=targetMean, targetVol=targetVol)
x.NDD = PerformanceAnalytics:::Drawdowns(x.N)
- par(mar = c(3, 5, 2, 3)+0.1) #c(bottom, left, top, right)
- chart.TimeSeries(x.NDD[start.date,c(manager.col, index.cols, peer.cols)], colorset=colorset, lwd=lwdset, legend.loc=NULL, lty=lineset, main="", cex.axis=1.2, cex.lab=1.5)
- par(op)
+ if(show.units.sd){
+ x.NDD = x.NDD/(targetVol*sqrt(scale)) # targetVol must be annualized
+ ylab = "Annualized Std Dev"
+ }
+ # par(mar = c(3, 5, 2, 3)+0.1) #c(bottom, left, top, right)
+ chart.TimeSeries(x.NDD, ylab=ylab, ...)
+ # par(op)
}
###############################################################################
More information about the Returnanalytics-commits
mailing list