[Returnanalytics-commits] r3523 - in pkg/PerformanceAnalytics: . R man sandbox tests/Examples
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Sep 4 12:06:13 CEST 2014
Author: braverock
Date: 2014-09-04 12:06:12 +0200 (Thu, 04 Sep 2014)
New Revision: 3523
Added:
pkg/PerformanceAnalytics/sandbox/decomposeMVaR.R
pkg/PerformanceAnalytics/sandbox/expectedShortFallFunctions.r
pkg/PerformanceAnalytics/sandbox/rCornishFisher.r
pkg/PerformanceAnalytics/sandbox/valueAtRiskFunctions.r
Removed:
pkg/PerformanceAnalytics/R/decomposeMVaR.R
pkg/PerformanceAnalytics/R/expectedShortFallFunctions.r
pkg/PerformanceAnalytics/R/rCornishFisher.r
pkg/PerformanceAnalytics/R/valueAtRiskFunctions.r
Modified:
pkg/PerformanceAnalytics/DESCRIPTION
pkg/PerformanceAnalytics/NAMESPACE
pkg/PerformanceAnalytics/NEWS
pkg/PerformanceAnalytics/R/VaR.R
pkg/PerformanceAnalytics/man/ActivePremium.Rd
pkg/PerformanceAnalytics/man/AdjustedSharpeRatio.Rd
pkg/PerformanceAnalytics/man/AppraisalRatio.Rd
pkg/PerformanceAnalytics/man/AverageDrawdown.Rd
pkg/PerformanceAnalytics/man/BernardoLedoitRatio.Rd
pkg/PerformanceAnalytics/man/BetaCoMoments.Rd
pkg/PerformanceAnalytics/man/BurkeRatio.Rd
pkg/PerformanceAnalytics/man/CAPM.RiskPremium.Rd
pkg/PerformanceAnalytics/man/CAPM.alpha.Rd
pkg/PerformanceAnalytics/man/CAPM.beta.Rd
pkg/PerformanceAnalytics/man/CAPM.dynamic.Rd
pkg/PerformanceAnalytics/man/CAPM.epsilon.Rd
pkg/PerformanceAnalytics/man/CAPM.jensenAlpha.Rd
pkg/PerformanceAnalytics/man/CDD.Rd
pkg/PerformanceAnalytics/man/CalmarRatio.Rd
pkg/PerformanceAnalytics/man/CoMoments.Rd
pkg/PerformanceAnalytics/man/DRatio.Rd
pkg/PerformanceAnalytics/man/DownsideDeviation.Rd
pkg/PerformanceAnalytics/man/DownsideFrequency.Rd
pkg/PerformanceAnalytics/man/DrawdownDeviation.Rd
pkg/PerformanceAnalytics/man/DrawdownPeak.Rd
pkg/PerformanceAnalytics/man/ES.Rd
pkg/PerformanceAnalytics/man/FamaBeta.Rd
pkg/PerformanceAnalytics/man/Frequency.Rd
pkg/PerformanceAnalytics/man/HurstIndex.Rd
pkg/PerformanceAnalytics/man/InformationRatio.Rd
pkg/PerformanceAnalytics/man/Kappa.Rd
pkg/PerformanceAnalytics/man/KellyRatio.Rd
pkg/PerformanceAnalytics/man/M2Sortino.Rd
pkg/PerformanceAnalytics/man/MSquared.Rd
pkg/PerformanceAnalytics/man/MSquaredExcess.Rd
pkg/PerformanceAnalytics/man/MarketTiming.Rd
pkg/PerformanceAnalytics/man/MartinRatio.Rd
pkg/PerformanceAnalytics/man/MeanAbsoluteDeviation.Rd
pkg/PerformanceAnalytics/man/Modigliani.Rd
pkg/PerformanceAnalytics/man/NetSelectivity.Rd
pkg/PerformanceAnalytics/man/Omega.Rd
pkg/PerformanceAnalytics/man/OmegaExcessReturn.Rd
pkg/PerformanceAnalytics/man/OmegaSharpeRatio.Rd
pkg/PerformanceAnalytics/man/PainIndex.Rd
pkg/PerformanceAnalytics/man/PainRatio.Rd
pkg/PerformanceAnalytics/man/ProspectRatio.Rd
pkg/PerformanceAnalytics/man/Return.Geltner.Rd
pkg/PerformanceAnalytics/man/Return.annualized.Rd
pkg/PerformanceAnalytics/man/Return.annualized.excess.Rd
pkg/PerformanceAnalytics/man/Return.calculate.Rd
pkg/PerformanceAnalytics/man/Return.clean.Rd
pkg/PerformanceAnalytics/man/Return.cumulative.Rd
pkg/PerformanceAnalytics/man/Return.excess.Rd
pkg/PerformanceAnalytics/man/Return.portfolio.Rd
pkg/PerformanceAnalytics/man/Return.read.Rd
pkg/PerformanceAnalytics/man/Return.relative.Rd
pkg/PerformanceAnalytics/man/Selectivity.Rd
pkg/PerformanceAnalytics/man/SharpeRatio.Rd
pkg/PerformanceAnalytics/man/SharpeRatio.annualized.Rd
pkg/PerformanceAnalytics/man/SkewnessKurtosisRatio.Rd
pkg/PerformanceAnalytics/man/SmoothingIndex.Rd
pkg/PerformanceAnalytics/man/SortinoRatio.Rd
pkg/PerformanceAnalytics/man/SpecificRisk.Rd
pkg/PerformanceAnalytics/man/StdDev.Rd
pkg/PerformanceAnalytics/man/StdDev.annualized.Rd
pkg/PerformanceAnalytics/man/SystematicRisk.Rd
pkg/PerformanceAnalytics/man/TotalRisk.Rd
pkg/PerformanceAnalytics/man/TrackingError.Rd
pkg/PerformanceAnalytics/man/TreynorRatio.Rd
pkg/PerformanceAnalytics/man/UlcerIndex.Rd
pkg/PerformanceAnalytics/man/UpDownRatios.Rd
pkg/PerformanceAnalytics/man/UpsideFrequency.Rd
pkg/PerformanceAnalytics/man/UpsidePotentialRatio.Rd
pkg/PerformanceAnalytics/man/UpsideRisk.Rd
pkg/PerformanceAnalytics/man/VaR.Rd
pkg/PerformanceAnalytics/man/VolatilitySkewness.Rd
pkg/PerformanceAnalytics/man/apply.fromstart.Rd
pkg/PerformanceAnalytics/man/apply.rolling.Rd
pkg/PerformanceAnalytics/man/centeredmoments.Rd
pkg/PerformanceAnalytics/man/chart.ACF.Rd
pkg/PerformanceAnalytics/man/chart.Bar.Rd
pkg/PerformanceAnalytics/man/chart.BarVaR.Rd
pkg/PerformanceAnalytics/man/chart.Boxplot.Rd
pkg/PerformanceAnalytics/man/chart.CaptureRatios.Rd
pkg/PerformanceAnalytics/man/chart.Correlation.Rd
pkg/PerformanceAnalytics/man/chart.CumReturns.Rd
pkg/PerformanceAnalytics/man/chart.Drawdown.Rd
pkg/PerformanceAnalytics/man/chart.ECDF.Rd
pkg/PerformanceAnalytics/man/chart.Events.Rd
pkg/PerformanceAnalytics/man/chart.Histogram.Rd
pkg/PerformanceAnalytics/man/chart.QQPlot.Rd
pkg/PerformanceAnalytics/man/chart.Regression.Rd
pkg/PerformanceAnalytics/man/chart.RelativePerformance.Rd
pkg/PerformanceAnalytics/man/chart.RiskReturnScatter.Rd
pkg/PerformanceAnalytics/man/chart.RollingCorrelation.Rd
pkg/PerformanceAnalytics/man/chart.RollingMean.Rd
pkg/PerformanceAnalytics/man/chart.RollingPerformance.Rd
pkg/PerformanceAnalytics/man/chart.RollingRegression.Rd
pkg/PerformanceAnalytics/man/chart.Scatter.Rd
pkg/PerformanceAnalytics/man/chart.SnailTrail.Rd
pkg/PerformanceAnalytics/man/chart.StackedBar.Rd
pkg/PerformanceAnalytics/man/chart.TimeSeries.Rd
pkg/PerformanceAnalytics/man/chart.VaRSensitivity.Rd
pkg/PerformanceAnalytics/man/charts.PerformanceSummary.Rd
pkg/PerformanceAnalytics/man/charts.RollingPerformance.Rd
pkg/PerformanceAnalytics/man/checkData.Rd
pkg/PerformanceAnalytics/man/clean.boudt.Rd
pkg/PerformanceAnalytics/man/findDrawdowns.Rd
pkg/PerformanceAnalytics/man/kurtosis.Rd
pkg/PerformanceAnalytics/man/legend.Rd
pkg/PerformanceAnalytics/man/lpm.Rd
pkg/PerformanceAnalytics/man/maxDrawdown.Rd
pkg/PerformanceAnalytics/man/mean.geometric.Rd
pkg/PerformanceAnalytics/man/skewness.Rd
pkg/PerformanceAnalytics/man/sortDrawdowns.Rd
pkg/PerformanceAnalytics/man/table.AnnualizedReturns.Rd
pkg/PerformanceAnalytics/man/table.Arbitrary.Rd
pkg/PerformanceAnalytics/man/table.Autocorrelation.Rd
pkg/PerformanceAnalytics/man/table.CAPM.Rd
pkg/PerformanceAnalytics/man/table.CalendarReturns.Rd
pkg/PerformanceAnalytics/man/table.CaptureRatios.Rd
pkg/PerformanceAnalytics/man/table.Correlation.Rd
pkg/PerformanceAnalytics/man/table.Distributions.Rd
pkg/PerformanceAnalytics/man/table.DownsideRisk.Rd
pkg/PerformanceAnalytics/man/table.DownsideRiskRatio.Rd
pkg/PerformanceAnalytics/man/table.Drawdowns.Rd
pkg/PerformanceAnalytics/man/table.DrawdownsRatio.Rd
pkg/PerformanceAnalytics/man/table.HigherMoments.Rd
pkg/PerformanceAnalytics/man/table.InformationRatio.Rd
pkg/PerformanceAnalytics/man/table.MonthlyReturns.Rd
pkg/PerformanceAnalytics/man/table.ProbOutPerformance.Rd
pkg/PerformanceAnalytics/man/table.RollingPeriods.Rd
pkg/PerformanceAnalytics/man/table.SpecificRisk.Rd
pkg/PerformanceAnalytics/man/table.Variability.Rd
pkg/PerformanceAnalytics/man/textplot.Rd
pkg/PerformanceAnalytics/man/zerofill.Rd
pkg/PerformanceAnalytics/tests/Examples/PerformanceAnalytics-Ex.Rout.save
Log:
- move bootstrap files to sandbox, will clean up for next release
- update docs to latest roxygen2
- update NEWS
Modified: pkg/PerformanceAnalytics/DESCRIPTION
===================================================================
--- pkg/PerformanceAnalytics/DESCRIPTION 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/DESCRIPTION 2014-09-04 10:06:12 UTC (rev 3523)
@@ -1,7 +1,7 @@
Package: PerformanceAnalytics
Type: Package
Title: Econometric tools for performance and risk analysis.
-Version: 1.1.3522
+Version: 1.4.3523
Date: $Date$
Author: Peter Carl, Brian G. Peterson
Maintainer: Brian G. Peterson <brian at braverock.com>
Modified: pkg/PerformanceAnalytics/NAMESPACE
===================================================================
--- pkg/PerformanceAnalytics/NAMESPACE 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/NAMESPACE 2014-09-04 10:06:12 UTC (rev 3523)
@@ -1,4 +1,4 @@
-# Generated by roxygen2 (4.0.1): do not edit by hand
+# Generated by roxygen2 (4.0.2): do not edit by hand
S3method(textplot,character)
S3method(textplot,data.frame)
Modified: pkg/PerformanceAnalytics/NEWS
===================================================================
--- pkg/PerformanceAnalytics/NEWS 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/NEWS 2014-09-04 10:06:12 UTC (rev 3523)
@@ -1,3 +1,32 @@
+PerformanceAnalytics 1.4 Release Notes
+
+This release mostly adds some functionality from Bacon, and
+makes visible some features that were previously internal to
+the package.
+
+We've also moved to using roxygen2 to generate the NAMESPACE file,
+which required touching basically every single generated .Rd file.
+
+
+
+Most of the commits leading up to release are the result of continued
+tightening and drift in CRAN standards, most should not have user-visible
+results.
+
+... and the usual minor things that get changed and updated in the general
+course of maintenance.
+
+PerformanceAnalytics 1.1 Release Notes
+
+Another release almost exclusively based on R core changes ahead of R 3.0.0.
+
+Most of the commits leading up to release are the result of continued
+tightening and drift in CRAN standards, most should not have user-visible
+results.
+
+... and the usual minor things that get changed and updated in the general
+course of maintenance.
+
PerformanceAnalytics 1.0.4.3 Release Notes
(sent to CRAN 2012-03-29)
Modified: pkg/PerformanceAnalytics/R/VaR.R
===================================================================
--- pkg/PerformanceAnalytics/R/VaR.R 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/R/VaR.R 2014-09-04 10:06:12 UTC (rev 3523)
@@ -104,6 +104,19 @@
#'
#' Rockafellar, Terry and Uryasev, Stanislav. Optimization of Conditional VaR.
#' The Journal of Risk, 2000, vol. 2, 21-41.
+#'
+#' Dowd, Kevin. Measuring Market Risk, John Wiley and Sons, 2010.
+#'
+#' Jorian, Phillippe. Value at Risk, the new benchmark for managing financial risk.
+#' 3rd Edition, McGraw Hill, 2006.
+#'
+#' Hallerback, John. "Decomposing Portfolio Value-at-Risk: A General Analysis",
+#' 2003. The Journal of Risk vol 5/2.
+#'
+#' Yamai and Yoshiba (2002). "Comparative Analyses of Expected Shortfall and
+#' Value-at-Risk: Their Estimation Error, Decomposition, and Optimization",
+#' Bank of Japan.
+#'
#' @keywords ts multivariate distribution models
#' @examples
#'
Deleted: pkg/PerformanceAnalytics/R/decomposeMVaR.R
===================================================================
--- pkg/PerformanceAnalytics/R/decomposeMVaR.R 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/R/decomposeMVaR.R 2014-09-04 10:06:12 UTC (rev 3523)
@@ -1,106 +0,0 @@
-
-
-portm2 = function(w,sigma)
-{
- return(t(w)%*%sigma%*%w)
-}
-portm2
-
-portm3 = function(w,M3)
-{
- return(t(w)%*%M3%*%(w%x%w))
-}
-portm3
-
-table.VaR.CornishFisher.portfolio =
-function (p, w, mu, sigma, M3, M4 , names)
-{
- w = matrix( w , ncol = 1 )
- alpha = .setalphaprob(p)
- p = alpha
- z = qnorm(alpha)
- location = t(w) %*% mu
- pm2 = portm2(w, sigma)
- dpm2 = as.vector(derportm2(w, sigma))
- pm3 = portm3(w, M3)
- dpm3 = as.vector(derportm3(w, M3))
- pm4 = portm4(w, M4)
- dpm4 = as.vector(derportm4(w, M4))
- skew = pm3/pm2^(3/2)
- exkurt = pm4/pm2^(2) - 3
- derskew = (2 * (pm2^(3/2)) * dpm3 - 3 * pm3 * sqrt(pm2) *
- dpm2)/(2 * pm2^3)
- derexkurt = ((pm2) * dpm4 - 2 * pm4 * dpm2)/(pm2^3)
- h = z + (1/6) * (z^2 - 1) * skew
- h = h + (1/24) * (z^3 - 3 * z) * exkurt - (1/36) * (2 * z^3 -
- 5 * z) * skew^2
- MVaR = -location - h * sqrt(pm2)
- derGausVaR = -as.vector(mu) - qnorm(alpha) * (0.5 * as.vector(dpm2))/sqrt(pm2)
- derMVaR_skew = (0.5 * dpm2/sqrt(pm2)) * (-(1/6) *
- (z^2 - 1) * skew + (1/36) * (2 * z^3 - 5 * z) * skew^2)
- derMVaR_skew = derMVaR_skew + sqrt(pm2) * (
- -(1/6) * (z^2 - 1) * derskew + (1/36) * (2 * z^3 -
- 5 * z) * 2 * skew * derskew)
- derMVaR_kurt = (0.5 * dpm2/sqrt(pm2)) * ( - (1/24) * (z^3 - 3 * z) * exkurt )
- derMVaR_kurt = derMVaR_kurt + sqrt(pm2) * ( - (1/24) * (z^3 - 3 * z) * derexkurt)
-
-
- # derMVaR = derGausVaR + (0.5 * dpm2/sqrt(pm2)) * (-(1/6) *
- # (z^2 - 1) * skew - (1/24) * (z^3 - 3 * z) * exkurt +
- # (1/36) * (2 * z^3 - 5 * z) * skew^2)
- # derMVaR = derMVaR + sqrt(pm2) * (-(1/6) * (z^2 - 1) * derskew -
- # (1/24) * (z^3 - 3 * z) * derexkurt + (1/36) * (2 * z^3 -
- # 5 * z) * 2 * skew * derskew)
-
- derMVaR = derGausVaR + derMVaR_skew + derMVaR_kurt
-
- contrib = as.vector(w) * as.vector(derMVaR)
- contrib_gaus = as.vector(w) * as.vector(derGausVaR)
- contrib_skew = as.vector(w) * as.vector(derMVaR_skew)
- contrib_kurt = as.vector(w) * as.vector(derMVaR_kurt)
- pct_contrib = contrib/MVaR
- pct_contrib_gaus = contrib_gaus/MVaR
- pct_contrib_skew = contrib_skew/MVaR
- pct_contrib_kurt = contrib_kurt/MVaR
-
- names(contrib_gaus) = names(contrib_skew) = names(contrib_kurt) = names(contrib) <- names(w);
- names(pct_contrib_gaus) = names(pct_contrib_skew) = names(pct_contrib_kurt) = names(pct_contrib) <- names(w)
-
- out = cbind( contrib, contrib_gaus , contrib_skew , contrib_kurt ,
- pct_contrib , pct_contrib_gaus , pct_contrib_skew , pct_contrib_kurt )
- out = rbind( out , apply( out , 2 , 'sum' ) )
- rownames(out) = c(names,"sum")
- colnames(out)= c( "total comp" , "Gaussian comp" , "skew comp" , "kurt comp",
- "Perc total" , "Perc. Gaussian comp" , "Perc. skew comp" , "Perc. kurt comp")
- print( round(out, 3 ) )
- ret = (list(MVaR, contrib, contrib_gaus , contrib_skew , contrib_kurt ,
- pct_contrib , pct_contrib_gaus , pct_contrib_skew , pct_contrib_kurt ))
- names(ret) = c("MVaR", "contribution", "contribution_gaus" , "contribution_skew" , "contribution_kurt" ,
- "pct_contrib","pct_contrib_gaus","pct_contrib_skew","pct_contrib_kurt" )
- return(ret)
-
-}
-
-#data(edhec)
-#N = 4
-#edhec = edhec[,1:N]
-#mu = apply( edhec , 2 , 'mean' )
-#sigma = cov(edhec)
-#m3 = PerformanceAnalytics:::M3.MM( edhec )
-#m4 = PerformanceAnalytics:::M4.MM( edhec )
-#out = Table.VaR.CornishFisher.portfolio ( p = 0.95 , w = rep(1/N,N) , mu = mu , sigma = sigma , M3 = m3 , M4 = m4 , names = colnames(edhec) )
-
-
-
-
-###############################################################################
-# R (http://r-project.org/) Econometrics for Performance and Risk Analysis
-#
-# Copyright (c) 2004-2014 Peter Carl and Brian G. Peterson
-#
-# This R package is distributed under the terms of the GNU Public License (GPL)
-# for full details see the file COPYING
-#
-# $Id$
-#
-###############################################################################
Deleted: pkg/PerformanceAnalytics/R/expectedShortFallFunctions.r
===================================================================
--- pkg/PerformanceAnalytics/R/expectedShortFallFunctions.r 2014-09-02 21:09:55 UTC (rev 3522)
+++ pkg/PerformanceAnalytics/R/expectedShortFallFunctions.r 2014-09-04 10:06:12 UTC (rev 3523)
@@ -1,684 +0,0 @@
-## expectedShortFallFunctions.r
-## author: Eric Zivot
-## created: April 29, 2009
-## update history:
-## December 2, 2009
-## Fixed bootstrapIncrementalES to work with one asset portfolio
-##
-## purpose: functions for computing expected shortfall
-##
-## Functions:
-## normalES
-## normalPortfolioES
-## normalMarginalES Not done
-## normalComponentES Not done
-## normalIncrementalES
-## normalESreport Not done
-## modifiedES Not done
-## modifiedPortfolioES Not done
-## modifiedMarginalES Not done
-## modifiedComponentES Not done
-## modifiedIncrementalES Not done
-## modifiedESreport Not done
-## bootstrapES
-## bootstrapPortfolioES
-## bootstrapIncrementalES
-## bootstrapMarginalES
-## bootstrapComponentES
-## bootstrapESreport
-##
-## References:
-## 1. Dowd, K. (2002). Measuring Market Risk, John Wiley and Sons
-## 2. Boudt, Peterson and Croux (2008), "Estimation and Decomposition of Downside
-## Risk for Portfolios with Non-Normal Returns," Journal of Risk.
-## 3. Yamai and Yoshiba (2002). "Comparative Analyses of Expected Shortfall and
-## Value-at-Risk: Their Estimation Error, Decomposition, and Optimization
-## Bank of Japan.
-
-##
-## 1. ES functions based on the normal distribution
-##
-
-normalES <- function(mu, sigma, tail.prob = 0.01) {
-## compute normal ES for collection of assets given mean and sd vector
-## inputs:
-## mu n x 1 vector of expected returns
-## sigma n x 1 vector of standard deviations
-## tail.prob scalar tail probability
-## output:
-## ES n x 1 vector of left tail average returns reported as a positive number
- mu = as.matrix(mu)
- sigma = as.matrix(sigma)
- if ( nrow(mu) != nrow(sigma) )
- stop("mu and sigma must have same number of elements")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
-
-
-
-#' calculates Expected Shortfall(ES) (or Conditional Value-at-Risk(CVaR) for
-#' univariate and component, using a variety of analytical methods.
-#'
-#' Calculates Expected Shortfall(ES) (also known as) Conditional Value at
-#' Risk(CVaR) for univariate, component, and marginal cases using a variety of
-#' analytical methods.
-#'
-#'
-#' @aliases ES CVaR ETL
-#' @param R a vector, matrix, data frame, timeSeries or zoo object of asset
-#' returns
-#' @param p confidence level for calculation, default p=.95
-#' @param method one of "modified","gaussian","historical", "kernel", see
-#' Details.
-#' @param clean method for data cleaning through \code{\link{Return.clean}}.
-#' Current options are "none", "boudt", or "geltner".
-#' @param portfolio_method one of "single","component","marginal" defining
-#' whether to do univariate, component, or marginal calc, see Details.
-#' @param weights portfolio weighting vector, default NULL, see Details
-#' @param mu If univariate, mu is the mean of the series. Otherwise mu is the
-#' vector of means of the return series , default NULL, , see Details
-#' @param sigma If univariate, sigma is the variance of the series. Otherwise
-#' sigma is the covariance matrix of the return series , default NULL, see
-#' Details
-#' @param m3 If univariate, m3 is the skewness of the series. Otherwise m3 is
-#' the coskewness matrix of the returns series, default NULL, see Details
-#' @param m4 If univariate, m4 is the excess kurtosis of the series. Otherwise
-#' m4 is the cokurtosis matrix of the return series, default NULL, see Details
-#' @param invert TRUE/FALSE whether to invert the VaR measure. see Details.
-#' @param operational TRUE/FALSE, default TRUE, see Details.
-#' @param \dots any other passthru parameters
-#' @note The option to \code{invert} the ES measure should appease both
-#' academics and practitioners. The mathematical definition of ES as the
-#' negative value of extreme losses will (usually) produce a positive number.
-#' Practitioners will argue that ES denotes a loss, and should be internally
-#' consistent with the quantile (a negative number). For tables and charts,
-#' different preferences may apply for clarity and compactness. As such, we
-#' provide the option, and set the default to TRUE to keep the return
-#' consistent with prior versions of PerformanceAnalytics, but make no value
-#' judgement on which approach is preferable.
-#' @section Background: This function provides several estimation methods for
-#' the Expected Shortfall (ES) (also called Expected Tail Loss (ETL)
-#' orConditional Value at Risk (CVaR)) of a return series and the Component ES
-#' (ETL/CVaR) of a portfolio.
-#'
-#' At a preset probability level denoted \eqn{c}, which typically is between 1
-#' and 5 per cent, the ES of a return series is the negative value of the
-#' expected value of the return when the return is less than its
-#' \eqn{c}-quantile. Unlike value-at-risk, conditional value-at-risk has all
-#' the properties a risk measure should have to be coherent and is a convex
-#' function of the portfolio weights (Pflug, 2000). With a sufficiently large
-#' data set, you may choose to estimate ES with the sample average of all
-#' returns that are below the \eqn{c} empirical quantile. More efficient
-#' estimates of VaR are obtained if a (correct) assumption is made on the
-#' return distribution, such as the normal distribution. If your return series
-#' is skewed and/or has excess kurtosis, Cornish-Fisher estimates of ES can be
-#' more appropriate. For the ES of a portfolio, it is also of interest to
-#' decompose total portfolio ES into the risk contributions of each of the
-#' portfolio components. For the above mentioned ES estimators, such a
-#' decomposition is possible in a financially meaningful way.
-#' @author Brian G. Peterson and Kris Boudt
-#' @seealso \code{\link{VaR}} \cr \code{\link{SharpeRatio.modified}} \cr
-#' \code{\link{chart.VaRSensitivity}} \cr \code{\link{Return.clean}}
-#' @references Boudt, Kris, Peterson, Brian, and Christophe Croux. 2008.
-#' Estimation and decomposition of downside risk for portfolios with non-normal
-#' returns. 2008. The Journal of Risk, vol. 11, 79-103.
-#'
-#' Cont, Rama, Deguest, Romain and Giacomo Scandolo. Robustness and sensitivity
-#' analysis of risk measurement procedures. Financial Engineering Report No.
-#' 2007-06, Columbia University Center for Financial Engineering.
-#'
-#' Laurent Favre and Jose-Antonio Galeano. Mean-Modified Value-at-Risk
-#' Optimization with Hedge Funds. Journal of Alternative Investment, Fall 2002,
-#' v 5.
-#'
-#' Martellini, Lionel, and Volker Ziemann. Improved Forecasts of Higher-Order
-#' Comoments and Implications for Portfolio Selection. 2007. EDHEC Risk and
-#' Asset Management Research Centre working paper.
-#'
-#' Pflug, G. Ch. Some remarks on the value-at-risk and the conditional
-#' value-at-risk. In S. Uryasev, ed., Probabilistic Constrained Optimization:
-#' Methodology and Applications, Dordrecht: Kluwer, 2000, 272-281.
-#'
-#' Scaillet, Olivier. Nonparametric estimation and sensitivity analysis of
-#' expected shortfall. Mathematical Finance, 2002, vol. 14, 74-86.
-#' @keywords ts multivariate distribution models
-#' @examples
-#'
-#' data(edhec)
-#'
-#' # first do normal ES calc
-#' ES(edhec, p=.95, method="historical")
-#'
-#' # now use Gaussian
-#' ES(edhec, p=.95, method="gaussian")
-#'
-#' # now use modified Cornish Fisher calc to take non-normal distribution into account
-#' ES(edhec, p=.95, method="modified")
-#'
-#' # now use p=.99
-#' ES(edhec, p=.99)
-#' # or the equivalent alpha=.01
-#' ES(edhec, p=.01)
-#'
-#' # now with outliers squished
-#' ES(edhec, clean="boudt")
-#'
-#' # add Component ES for the equal weighted portfolio
-#' ES(edhec, clean="boudt", portfolio_method="component")
-#'
- ES = mu - sigma*dnorm(qnorm(tail.prob))/tail.prob
- return(-ES)
-}
-
-normalPortfolioES <- function(mu, Sigma, w, tail.prob = 0.01) {
-## compute normal portfolio ES given portfolio weights, mean vector and
-## covariance matrix
-## inputs:
-## mu n x 1 vector of expected returns
-## Sigma n x n return covariance matrix
-## w n x 1 vector of portfolio weights
-## tail.prob scalar tail probability
-## output:
-## pES scalar left tail average return of normal portfolio distn returned
-## as a positive number.
- mu = as.matrix(mu)
- Sigma = as.matrix(Sigma)
- w = as.matrix(w)
- if ( nrow(mu) != nrow(Sigma) )
- stop("mu and Sigma must have same number of rows")
- if ( nrow(mu) != nrow(w) )
- stop("mu and w must have same number of elements")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
- pES = crossprod(w, mu) + sqrt( t(w) %*% Sigma %*% w )*dnorm(qnorm(tail.prob))/tail.prob
- return(as.numeric(pES))
-}
-
-normalIncrementalES <- function(mu, Sigma, w, tail.prob = 0.01) {
-## compute normal incremental ES given portfolio weights, mean vector and
-## covariance matrix
-## Incremental ES is defined as the change in portfolio ES that occurs
-## when an asset is removed from the portfolio
-## inputs:
-## mu n x 1 vector of expected returns
-## Sigma n x n return covariance matrix
-## w n x 1 vector of portfolio weights
-## tail.prob scalar tail probability
-## output:
-## iES n x 1 vector of incremental ES values
-## References:
-## Jorian, P. (2007). Value at Risk, pg. 168.
- mu = as.matrix(mu)
- Sigma = as.matrix(Sigma)
- w = as.matrix(w)
- if ( nrow(mu) != nrow(Sigma) )
- stop("mu and Sigma must have same number of rows")
- if ( nrow(mu) != nrow(w) )
- stop("mu and w must have same number of elements")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
-
- n.w = nrow(mu)
- ## portfolio ES with all assets
- pES = normalPortfolioES(mu, Sigma, w, tail.prob)
- temp.w = w
- iES = matrix(0, n.w, 1)
- for (i in 1:n.w) {
- ## set weight for asset i to zero and renormalize
- temp.w[i,1] = 0
- temp.w = temp.w/sum(temp.w)
- pES.new = normalPortfolioES(mu, Sigma, temp.w, tail.prob)
- iES[i,1] = pES.new - pES
- ## reset weight
- temp.w = w
- }
- return(iES)
-}
-
-
-##
-## 2. ES functions based on simulated (bootstrapped) data
-##
-
-bootstrapES <- function(bootData, tail.prob = 0.01,
- method=c("HS", "CornishFisher")) {
-## Compute ES given bootstrap data. ES is computed as the sample mean beyond
-## VaR where VaR is computed either as the sample quantile or the quantile using
-## the Cornish-Fisher expansion
-## inputs:
-## bootData B x n matrix of B bootstrap returns on n assets in portfolio
-## tail.prob scalar tail probability
-## method character, method for computing VaR. Valid choices are "HS" for
-## historical simulation (empirical quantile); "CornishFisher" for
-## modified VaR based on Cornish-Fisher quantile estimate. Cornish-Fisher
-## computation is done with the VaR.CornishFisher in the PerformanceAnalytics
-## package
-## output:
-## ES n x 1 matrix of ES values for each asset
-## References:
- require(ff,quietly=TRUE)
- method = method[1]
- if (!is.ff(bootData))
- bootData = as.matrix(bootData)
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
- n.assets = ncol(bootData)
- ES = matrix(0, n.assets, 1)
- rownames(ES) = colnames(bootData)
- colnames(ES) = "ES"
- ## loop over all assets and compute ES
- for (i in 1:n.assets) {
- if (method == "HS") {
- ## use empirical quantile
- VaR = quantile(bootData[,i], probs=tail.prob)
- } else {
- ## use Cornish-Fisher quantile
- VaR = -VaR.CornishFisher(bootData[,i], p=(1-tail.prob))
- }
- idx = which(bootData[,i] <= VaR)
- ES[i,1] = mean(bootData[idx, i])
- }
-return(-ES)
-}
-
-bootstrapPortfolioES <- function(bootData, w, tail.prob = 0.01,
- method=c("HS", "CornishFisher")) {
-## Compute portfolio ES given bootstrap data and portfolio weights. VaR is computed
-## either as the sample quantile or as an estimated quantile using the
-## Cornish-Fisher expansion
-## inputs:
-## bootData B x n matrix of bootstrap returns on n assets in portfolio
-## w n x 1 vector of portfolio weights
-## tail.prob scalar tail probability
-## method character, method for computing VaR. Valid choices are "HS" for
-## historical simulation (empirical quantile); "CornishFisher" for
-## modified VaR based on Cornish-Fisher quantile estimate. Cornish-Fisher
-## computation is done with the VaR.CornishFisher in the PerformanceAnalytics
-## package
-## output:
-## pES scalar, portfolio ES value
-## References:
-## Yamai and Yoshiba (2002). "Comparative Analyses of Expected Shortfall and
-## Value-at-Risk: Their Estimation Error, Decomposition, and Optimization
-## Bank of Japan.
-
- require(ff, quietly=TRUE)
- method = method[1]
- if (!is.ff(bootData))
- bootData = as.matrix(bootData)
- w = as.matrix(w)
- if ( ncol(bootData) != nrow(w) )
- stop("Columns of bootData and rows of w do not match")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
- if (is.ff(bootData)) {
- ## use on disk ff objects
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- r.p = ffrowapply( bootData[i1:i2, ]%*%w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- ## use in RAM objects
- r.p = bootData %*% w
- }
- if (method == "HS") {
- ## use empirical quantile
- pVaR = quantile(r.p[], prob=tail.prob)
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- } else {
- ## use Cornish-Fisher expansion
- pVaR = -VaR.CornishFisher(r.p[], p=(1-tail.prob))
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- }
-return(pES)
-}
-
-
-bootstrapIncrementalES <- function(bootData, w, tail.prob = 0.01,
- method=c("HS", "CornishFisher")) {
-## Compute incremental ES given bootstrap data and portfolio weights.
-## Incremental ES is defined as the change in portfolio ES that occurs
-## when an asset is removed from the portfolio and allocation is spread equally
-## among remaining assets. VaR used in ES computation is computed either as the
-## sample quantile or as an estimated quantile using the Cornish-Fisher expansion
-## inputs:
-## bootData B x n matrix of B bootstrap returns on n assets in portfolio
-## w n x 1 vector of portfolio weights
-## tail.prob scalar tail probability
-## method character, method for computing VaR. Valid choices are "HS" for
-## historical simulation (empirical quantile); "CornishFisher" for
-## modified VaR based on Cornish-Fisher quantile estimate. Cornish-Fisher
-## computation is done with the VaR.CornishFisher in the PerformanceAnalytics
-## package
-## output:
-## iES n x 1 matrix of incremental VaR values for each asset
-## References:
-## Jorian, P. (2007). Value-at-Risk, pg. 168.
-##
- require(PerformanceAnalytics)
- require(ff)
- method = method[1]
- if (!is.ff(bootData))
- bootData = as.matrix(bootData)
- w = as.matrix(w)
- if ( ncol(bootData) != nrow(w) )
- stop("Columns of bootData and rows of w do not match")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
-
- if (ncol(bootData) == 1) {
- ## EZ: Added 12/2/2009
- ## one asset portfolio so iES = 0 by construction
- iES = 0
- } else {
-
- n.w = nrow(w)
- ## portfolio VaR with all assets
- if (is.ff(bootData)) {
- ## use on disk ff object
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- r.p = ffrowapply( bootData[i1:i2, ]%*%w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- ## use in RAM object
- r.p = bootData %*% w
- }
- if (method == "HS") {
- ## use empirical quantile to compute VaR
- pVaR = quantile(r.p[], prob=tail.prob)
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- temp.w = w
- iES = matrix(0, n.w, 1)
- rownames(iES) = colnames(bootData)
- colnames(iES) = "i.ES"
- for (i in 1:n.w) {
- ## set weight for asset i to zero and renormalize
- temp.w[i,1] = 0
- temp.w = temp.w/sum(temp.w)
- if (is.ff(bootData)) {
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- temp.r.p = ffrowapply( bootData[i1:i2, ]%*%temp.w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- temp.r.p = bootData %*% temp.w
- }
- pVaR.new = quantile(temp.r.p[], prob=tail.prob)
- idx = which(temp.r.p[] <= pVaR.new)
- pES.new = -mean(temp.r.p[idx])
- iES[i,1] = pES.new - pES
- ## reset weight
- temp.w = w
- }
- } else {
- ## use Cornish-Fisher VaR
- pVaR = -VaR.CornishFisher(r.p[], p=(1-tail.prob))
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- temp.w = w
- iES = matrix(0, n.w, 1)
- rownames(iES) = colnames(bootData)
- colnames(iES) = "i.ES"
- for (i in 1:n.w) {
- ## set weight for asset i to zero and renormalize
- temp.w[i,1] = 0
- temp.w = temp.w/sum(temp.w)
- if (is.ff(bootData)) {
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- temp.r.p = ffrowapply( bootData[i1:i2, ]%*%temp.w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- temp.r.p = bootData %*% temp.w
- }
- pVaR.new = -VaR.CornishFisher(temp.r.p[], p=(1-tail.prob))
- idx = which(temp.r.p[] <= pVaR.new)
- pES.new = -mean(temp.r.p[idx])
- iES[i,1] = pES.new - pES
- ## reset weight
- temp.w = w
- }
- }
- }
- return(iES)
-}
-
-bootstrapMarginalES <- function(bootData, w, delta.w = 0.001, tail.prob = 0.01,
- method=c("derivative", "average"),
- VaR.method=c("HS", "CornishFisher")) {
-## Compute marginal ES given bootstrap data and portfolio weights.
-## Marginal ES is computed either as the numerical derivative of ES wrt portfolio weight or
-## as the expected fund return given portfolio return is less than or equal to portfolio VaR
-## VaR is compute either as the sample quantile or as an estimated quantile
-## using the Cornish-Fisher expansion
-## inputs:
-## bootData B x n matrix of B bootstrap returns on assets in portfolio.
-## w n x 1 vector of portfolio weights
-## delta.w scalar, change in portfolio weight for computing numerical derivative
-## tail.prob scalar tail probability
-## method character, method for computing marginal ES. Valid choices are
-## "derivative" for numerical computation of the derivative of portfolio
-## ES wrt fund portfolio weight; "average" for approximating E[Ri | Rp<=VaR]
-## VaR.method character, method for computing VaR. Valid choices are "HS" for
-## historical simulation (empirical quantile); "CornishFisher" for
-## modified VaR based on Cornish-Fisher quantile estimate. Cornish-Fisher
-## computation is done with the VaR.CornishFisher in the PerformanceAnalytics
-## package
-## output:
-## mES n x 1 matrix of marginal ES values for each fund
-## References:
-## 1. Hallerback (2003), "Decomposing Portfolio Value-at-Risk: A General Analysis",
-## The Journal of Risk 5/2.
-## 2. Yamai and Yoshiba (2002). "Comparative Analyses of Expected Shortfall and
-## Value-at-Risk: Their Estimation Error, Decomposition, and Optimization
-## Bank of Japan.
- require(PerformanceAnalytics)
- require(ff)
- method = method[1]
- VaR.method = VaR.method[1]
- if (!is.ff(bootData))
- bootData = as.matrix(bootData)
- w = as.matrix(w)
- if ( ncol(bootData) != nrow(w) )
- stop("Columns of bootData and rows of w do not match")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
-
- n.w = nrow(w)
-
- ## portfolio VaR and ES with all assets
- if (is.ff(bootData)) {
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- r.p = ffrowapply( bootData[i1:i2, ]%*%w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- r.p = bootData %*% w
- }
- if (VaR.method == "HS") {
- pVaR = quantile(r.p[], prob=tail.prob)
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- } else {
- pVaR = -VaR.CornishFisher(r.p[], p=(1-tail.prob))
- idx = which(r.p[] <= pVaR)
- pES = -mean(r.p[idx])
- }
- ##
- ## compute marginal ES
- ##
- if (method=="derivative") {
- ## compute marginal ES as derivative wrt portfolio weight
- temp.w = w
- mES = matrix(0, n.w, 1)
- for (i in 1:n.w) {
- ## increment weight for asset i by delta.w
- temp.w[i,1] = w[i,1] + delta.w
- if (is.ff(bootData)) {
- dummy<-TRUE; if(!dummy){i1=1;i2=2} #fake assign to make R CMD check happy
- temp.r.p = ffrowapply( bootData[i1:i2, ]%*%temp.w, X=bootData, RETURN=TRUE, RETCOL=1)
- } else {
- temp.r.p = bootData %*% temp.w
- }
- if (VaR.method == "HS") {
- pVaR.new = quantile(temp.r.p[], prob=tail.prob)
- idx = which(temp.r.p[] <= pVaR.new)
- pES.new = -mean(temp.r.p[idx])
- } else {
- pVaR.new = -VaR.CornishFisher(temp.r.p[], p=(1-tail.prob))
- idx = which(temp.r.p[] <= pVaR.new)
- pES.new = -mean(temp.r.p[idx])
- }
- mES[i,1] = (pES.new - pES)/delta.w
- ## reset weight
- temp.w = w
- }
- } else {
- ## compute marginal ES as expected value of fund return given portfolio
- ## return is less than or equal to portfolio VaR
- mES = -as.matrix(colMeans(bootData[idx,,drop=FALSE]))
- }
-## compute correction factor so that sum of weighted marginal ES adds to portfolio VaR
-cf = as.numeric( pES / sum(mES*w) )
-return(cf*mES)
-}
-
-bootstrapComponentES <- function(bootData, w, delta.w = 0.001, tail.prob = 0.01,
- method=c("derivative", "average"),
- VaR.method=c("HS", "CornishFisher")) {
-## Compute component ES given bootstrap data and portfolio weights.
-## Marginal ES is computed either as the derivative of ES wrt portfolio weight or
-## as the expected fund return given portfolio return is less than or equal to
-## portfolio VaR. VaR is compute either as the sample quantile or as an estimated
-## quantile using the Cornish-Fisher expansion
-## inputs:
-## bootData B x n matrix of bootstrap returns on assets in portfolio.
-## w n x 1 vector of portfolio weights
-## delta.w scalar, change in portfolio weight for computing numerical derivative
-## tail.prob scalar tail probability
-## method character, method for computing marginal ES. Valid choices are
-## "derivative" for numerical computation of the derivative of portfolio
-## ES wrt fund portfolio weight; "average" for approximating E[Ri | Rp<=VaR]
-## VaR.method character, method for computing VaR. Valid choices are "HS" for
-## historical simulation (empirical quantile); "CornishFisher" for
-## modified VaR based on Cornish-Fisher quantile estimate. Cornish-Fisher
-## computation is done with the VaR.CornishFisher in the PerformanceAnalytics
-## package
-## output:
-## cES n x 1 matrix of component VaR values for each fund
-## References:
-## 1. Hallerback (2003), "Decomposing Portfolio Value-at-Risk: A General Analysis",
-## The Journal of Risk 5/2.
-## 2. Yamai and Yoshiba (2002). "Comparative Analyses of Expected Shortfall and
-## Value-at-Risk: Their Estimation Error, Decomposition, and Optimization
-## Bank of Japan.
- require(PerformanceAnalytics)
- require(ff)
- method = method[1]
- VaR.method = VaR.method[1]
- if (!is.ff(bootData))
- bootData = as.matrix(bootData)
- w = as.matrix(w)
-
- if ( ncol(bootData) != nrow(w) )
- stop("Columns of bootData and rows of w do not match")
- if ( tail.prob < 0 || tail.prob > 1)
- stop("tail.prob must be between 0 and 1")
-
- mES = bootstrapMarginalES(bootData, w, delta.w, tail.prob,
- method, VaR.method)
- cES = mES * w
- return(cES)
-}
-
-bootstrapESreport <- function(bootData, w, delta.w = 0.001, tail.prob = 0.01,
- method=c("derivative", "average"),
- VaR.method=c("HS", "CornishFisher"),
- nav, nav.p, fundName, fundStrategy) {
-## compute ES report for collection of assets in a portfolio given
-## simulated (bootstrapped) return data
-## Report format follows that of Excel VaR report
-## inputs:
-## bootData B x n matrix of B bootstrap returns on assets in portfolio.
-## w n x 1 vector of portfolio weights
-## delta.w scalar, change in portfolio weight for computing numerical derivative.
-## Default value is 0.01.
-## tail.prob scalar tail probability
-## method character, method for computing marginal ES. Valid choices are
-## "derivative" for numerical computation of the derivative of portfolio
-## ES wrt fund portfolio weight; "average" for approximating E[Ri | Rp<=VaR]
-## VaR.method character, method for computing VaR. Valid choices are "HS" for
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/returnanalytics -r 3523
More information about the Returnanalytics-commits
mailing list