[Returnanalytics-commits] r3674 - in pkg/Dowd: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 15 12:05:50 CEST 2015


Author: dacharya
Date: 2015-06-15 12:05:49 +0200 (Mon, 15 Jun 2015)
New Revision: 3674

Added:
   pkg/Dowd/R/AdjustedNormalESHotspots.R
   pkg/Dowd/R/AdjustedNormalVaRHotspots.R
   pkg/Dowd/man/AdjustedNormalESHotspots.Rd
   pkg/Dowd/man/AdjustedNormalVaRHotspots.Rd
Modified:
   pkg/Dowd/NAMESPACE
   pkg/Dowd/R/FrechetVaRPlot2DCl.R
   pkg/Dowd/man/FrechetVaRPlot2DCl.Rd
Log:
AdjustedNormalESHotspots, AdjustedNormalVaRHotspots: source and documentation

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-12 16:43:18 UTC (rev 3673)
+++ pkg/Dowd/NAMESPACE	2015-06-15 10:05:49 UTC (rev 3674)
@@ -1,6 +1,8 @@
 # Generated by roxygen2 (4.1.1): do not edit by hand
 
 export(ADTestStat)
+export(AdjustedNormalESHotspots)
+export(AdjustedNormalVaRHotspots)
 export(BinomialBacktest)
 export(BlancoIhleBacktest)
 export(BootstrapES)

Added: pkg/Dowd/R/AdjustedNormalESHotspots.R
===================================================================
--- pkg/Dowd/R/AdjustedNormalESHotspots.R	                        (rev 0)
+++ pkg/Dowd/R/AdjustedNormalESHotspots.R	2015-06-15 10:05:49 UTC (rev 3674)
@@ -0,0 +1,114 @@
+#' @title Hotspots for ES adjusted by Cornish-Fisher correction
+#' 
+#' @description Estimates the ES hotspots (or vector of incremental ESs) for a 
+#' portfolio with portfolio return adjusted for non-normality by Cornish-Fisher 
+#' corerction, for specified confidence level and holding period.
+#' 
+#' @param vc.matrix Variance covariance matrix for returns
+#' @param mu Vector of expected position returns
+#' @param skew Return skew
+#' @param kurtisos Return kurtosis
+#' @param positions Vector of positions
+#' @param cl Confidence level and is scalar
+#' @param hp Holding period and is scalar
+#' 
+#' @references Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' 
+#' @examples
+#' 
+#'    # Hotspots for ES for randomly generated portfolio
+#'    vc.matrix <- matrix(rnorm(16),4,4)
+#'    return <- rnorm(4)
+#'    skew <- .5
+#'    kurtosis <- 1.2
+#'    positions <- c(5,2,6,10)
+#'    cl <- .95
+#'    hp <- 280
+#'    AdjustedNormalESHotsopts(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+#' 
+#' @export
+AdjustedNormalESHotspots <- function(vc.matrix, mu, skew, kurtosis, positions,
+                                    cl, hp){
+  
+  # Check that positions vector read as a scalar or row vector
+  positions <- as.matrix(positions)
+  if (dim(positions)[1] > dim(positions)[2]){
+    positions <- t(positions)
+  }
+  
+  # Check that expected returns vector is read as a scalar or row vector
+  mu <- as.matrix(mu)
+  if (dim(mu)[1] > dim(mu)[2]){
+    mu <- t(mu)
+  }
+  
+  # Check that dimensions are correct
+  if (max(dim(mu)) != max(dim(positions))){
+    stop("Positions vector and expected returns vector must have same size")
+  }
+  if (max(dim(vc.matrix)) != max(dim(positions))){
+    stop("Positions vector and expected returns vector must have same size")
+  }
+  
+  # Check that inputs obey sign and value restrictions
+  if (cl >= 1){
+    stop("Confidence level must be less than 1")
+  }
+  if (cl <= 0){
+    stop("Confidence level must be greater than 0");
+  }
+  if (hp <= 0){
+    stop("Holding period must be greater than 0");
+  }
+  
+  # VaR and ES estimation
+  # Begin with portfolio ES
+  z <- qnorm(1 - cl, 0 ,1)
+  sigma <- positions %*% vc.matrix %*% t(positions)/(sum(positions)^2) # Initial 
+  # standard deviation of portfolio returns
+  adjustment <- (1 / 6) * (z ^ 2 - 1) * skew + (1 / 24) * (z ^ 3 - 3 * z) * 
+    (kurtosis - 3) - (1 / 36) * (2 * z ^ 3 - 5 * z) * skew ^ 2
+  VaR <- - mu %*% t(positions) * hp - (z + adjustment) * sigma * 
+    (sum(positions)^2) * sqrt(hp) # Initial VaR
+  n <- 1000 # Number of slives into which tail is divided
+  cl0 <- cl # Initial confidence level
+  term <- VaR
+  delta.cl <- (1 - cl) / n # Increment to confidence level
+  for (k in 1:(n - 1)) {
+    cl <- cl0 + k * delta.cl # Revised cl
+    z <- qnorm(1 - cl, 0, 1)
+    adjustment=(1 / 6) * (z ^ 2 - 1) * skew + (1 / 24) * (z ^ 3 - 3 * z) * 
+      (kurtosis - 3) - (1 / 36) * (2 * z ^ 3 - 5 * z) * skew ^ 2
+    term <- term - mu %*% t(positions) * hp - (z + adjustment) * sigma * 
+      (sum(positions)^2) * sqrt(hp)
+  }
+  portfolio.ES <- term/n
+  
+  # Portfolio ES
+  es <- double(length(positions))
+  ies <- double(length(positions))
+  for (j in range(1: length(positions))) {
+    x <- positions
+    x[j] <- 0
+    sigma <- x %*% vc.matrix %*% t(x) / (sum(x)^2)
+    term[j] <- - mu %*% t(x) * hp - qnorm(1-cl, 0, 1) * x %*% 
+      vc.matrix %*% t(x) * sqrt(hp)
+    
+    for (k in 1:(n - 1)){
+      cl <- cl0 + k * delta.cl # Revised cl
+      z <- qnorm(1-cl, 0, 1)
+      adjustment=(1 / 6) * (z ^ 2 - 1) * skew + (1 / 24) * (z ^ 3 - 3 * z) * 
+        (kurtosis - 3) - (1 / 36) * (2 * z ^ 3 - 5 * z) * skew ^ 2
+      term[j] <- term[j] - mu %*% t(positions) * hp - (z + adjustment) * 
+        sigma * (sum(positions)^2) * sqrt(hp)
+    }
+    es[j] <- term[j]/n # ES on portfolio minus position j
+    ies [j] <- portfolio.ES - es[j] # Incremental ES
+    
+  }
+  y <- ies
+  return(ies)
+  
+}

Added: pkg/Dowd/R/AdjustedNormalVaRHotspots.R
===================================================================
--- pkg/Dowd/R/AdjustedNormalVaRHotspots.R	                        (rev 0)
+++ pkg/Dowd/R/AdjustedNormalVaRHotspots.R	2015-06-15 10:05:49 UTC (rev 3674)
@@ -0,0 +1,85 @@
+#' @title Hotspots for VaR adjusted by Cornish-Fisher correction
+#' 
+#' @description Estimates the VaR hotspots (or vector of incremental VaRs) for a
+#' portfolio with portfolio return adjusted for non-normality by Cornish-Fisher 
+#' corerction, for specified confidence level and holding period.
+#' 
+#' @param vc.matrix Variance covariance matrix for returns
+#' @param mu Vector of expected position returns
+#' @param skew Return skew
+#' @param kurtisos Return kurtosis
+#' @param positions Vector of positions
+#' @param cl Confidence level and is scalar
+#' @param hp Holding period and is scalar
+#' 
+#' @references Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' 
+#' @examples
+#' 
+#'    # Hotspots for ES for randomly generated portfolio
+#'    vc.matrix <- matrix(rnorm(16),4,4)
+#'    return <- rnorm(4)
+#'    skew <- .5
+#'    kurtosis <- 1.2
+#'    positions <- c(5,2,6,10)
+#'    cl <- .95
+#'    hp <- 280
+#'    AdjustedNormalESHotsopts(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+#'    
+#' @export
+AdjustedNormalVaRHotspots <- function(vc.matrix, mu, skew, kurtosis, positions, cl, hp){
+  
+  # Check that positions vector read as a scalar or row vector
+  positions <- as.matrix(positions)
+  if (dim(positions)[1] > dim(positions)[2]){
+    positions <- t(positions)
+  }
+  
+  # Check that expected returns vector is read as a scalar or row vector
+  mu <- as.matrix(mu)
+  if (dim(mu)[1] > dim(mu)[2]){
+    mu <- t(mu)
+  }
+  
+  # Check that dimensions are correct
+  if (max(dim(mu)) != max(dim(positions))){
+    stop("Positions vector and expected returns vector must have same size")
+  }
+  vc.matrix <- as.matrix(vc.matrix)
+  if (max(dim(vc.matrix)) != max(dim(positions))){
+    stop("Positions vector and expected returns vector must have same size")
+  }
+  
+  # Check that inputs obey sign and value restrictions
+  if (cl >= 1){
+    stop("Confidence level must be less than 1")
+  }
+  if (cl <= 0){
+    stop("Confidence level must be greater than 0");
+  }
+  if (hp <= 0){
+    stop("Holding period must be greater than 0");
+  }
+  
+  # VaR and ES estimation
+  z <- qnorm(1 - cl, 0 ,1)
+  sigma <- positions %*% vc.matrix %*% t(positions)/(sum(positions)^2) # Initial standard deviation of portfolio returns
+  adjustment <- (1 / 6) * (z ^ 2 - 1) * skew + (1 / 24) * (z ^ 3 - 3 * z) * (kurtosis - 3) - (1 / 36) * (2 * z ^ 3 - 5 * z) * skew ^ 2
+  VaR <- - mu %*% t(positions) * hp - (z + adjustment) * sigma * (sum(positions)^2) * sqrt(hp)
+  
+  # VaR
+  x <- double(length(positions))
+  sigma <- double(length(positions))
+  iVaR <- double(length(positions))
+  for (i in 1:length(positions)){
+    x <- positions
+    x[i] <- 0
+    sigma[i] <- x %*% vc.matrix %*% t(x)/sum(x)^2 # standard deviation of portfolio returns
+    iVaR[i] <- VaR + mu %*% t(x) %*% hp + (z + adjustment) * sigma[i] * (sum(x))^2 * sqrt(hp) # Incremental VaR
+  }
+  y <- iVaR
+  return(y)
+  
+}
\ No newline at end of file

Modified: pkg/Dowd/R/FrechetVaRPlot2DCl.R
===================================================================
--- pkg/Dowd/R/FrechetVaRPlot2DCl.R	2015-06-12 16:43:18 UTC (rev 3673)
+++ pkg/Dowd/R/FrechetVaRPlot2DCl.R	2015-06-15 10:05:49 UTC (rev 3674)
@@ -1,6 +1,6 @@
-#' Plots Frechet Value at Risk against Cl
+#' @title Plots Frechet Value at Risk against Cl
 #'
-#' Plots the VaR of a portfolio against confidence level assuming extreme losses
+#' @description Plots the VaR of a portfolio against confidence level assuming extreme losses
 #' are Frechet distributed, for specified range of confidence level and a given 
 #' holding period.
 #'
@@ -14,7 +14,7 @@
 #' @param cl Confidence level and should be a vector
 #' @param hp Holding period and should be a scalar
 #' 
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' @references Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
 #' 
 #' Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
 #' Insurance and Finance. Springer, Berlin, 1997, p. 324.
@@ -24,6 +24,7 @@
 #' 15-18.
 #' 
 #' @author Dinesh Acharya
+#' 
 #' @examples
 #' 
 #'    # Plots VaR against vector of cl assuming Frechet Distribution for given parameters

Added: pkg/Dowd/man/AdjustedNormalESHotspots.Rd
===================================================================
--- pkg/Dowd/man/AdjustedNormalESHotspots.Rd	                        (rev 0)
+++ pkg/Dowd/man/AdjustedNormalESHotspots.Rd	2015-06-15 10:05:49 UTC (rev 3674)
@@ -0,0 +1,46 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/AdjustedNormalESHotspots.R
+\name{AdjustedNormalESHotspots}
+\alias{AdjustedNormalESHotspots}
+\title{Hotspots for ES adjusted by Cornish-Fisher correction}
+\usage{
+AdjustedNormalESHotspots(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+}
+\arguments{
+\item{vc.matrix}{Variance covariance matrix for returns}
+
+\item{mu}{Vector of expected position returns}
+
+\item{skew}{Return skew}
+
+\item{positions}{Vector of positions}
+
+\item{cl}{Confidence level and is scalar}
+
+\item{hp}{Holding period and is scalar}
+
+\item{kurtisos}{Return kurtosis}
+}
+\description{
+Estimates the ES hotspots (or vector of incremental ESs) for a
+portfolio with portfolio return adjusted for non-normality by Cornish-Fisher
+corerction, for specified confidence level and holding period.
+}
+\examples{
+# Hotspots for ES for randomly generated portfolio
+   vc.matrix <- matrix(rnorm(16),4,4)
+   return <- rnorm(4)
+   skew <- .5
+   kurtosis <- 1.2
+   positions <- c(5,2,6,10)
+   cl <- .95
+   hp <- 280
+   AdjustedNormalESHotsopts(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
+}
+

Added: pkg/Dowd/man/AdjustedNormalVaRHotspots.Rd
===================================================================
--- pkg/Dowd/man/AdjustedNormalVaRHotspots.Rd	                        (rev 0)
+++ pkg/Dowd/man/AdjustedNormalVaRHotspots.Rd	2015-06-15 10:05:49 UTC (rev 3674)
@@ -0,0 +1,46 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/AdjustedNormalVaRHotspots.R
+\name{AdjustedNormalVaRHotspots}
+\alias{AdjustedNormalVaRHotspots}
+\title{Hotspots for VaR adjusted by Cornish-Fisher correction}
+\usage{
+AdjustedNormalVaRHotspots(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+}
+\arguments{
+\item{vc.matrix}{Variance covariance matrix for returns}
+
+\item{mu}{Vector of expected position returns}
+
+\item{skew}{Return skew}
+
+\item{positions}{Vector of positions}
+
+\item{cl}{Confidence level and is scalar}
+
+\item{hp}{Holding period and is scalar}
+
+\item{kurtisos}{Return kurtosis}
+}
+\description{
+Estimates the VaR hotspots (or vector of incremental VaRs) for a
+portfolio with portfolio return adjusted for non-normality by Cornish-Fisher
+corerction, for specified confidence level and holding period.
+}
+\examples{
+# Hotspots for ES for randomly generated portfolio
+   vc.matrix <- matrix(rnorm(16),4,4)
+   return <- rnorm(4)
+   skew <- .5
+   kurtosis <- 1.2
+   positions <- c(5,2,6,10)
+   cl <- .95
+   hp <- 280
+   AdjustedNormalESHotsopts(vc.matrix, mu, skew, kurtosis, positions, cl, hp)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
+}
+

Modified: pkg/Dowd/man/FrechetVaRPlot2DCl.Rd
===================================================================
--- pkg/Dowd/man/FrechetVaRPlot2DCl.Rd	2015-06-12 16:43:18 UTC (rev 3673)
+++ pkg/Dowd/man/FrechetVaRPlot2DCl.Rd	2015-06-15 10:05:49 UTC (rev 3674)
@@ -23,8 +23,7 @@
 Plots the VaR of a portfolio against confidence level assuming extreme losses
 are Frechet distributed, for specified range of confidence level and a given
 holding period.
-}
-\details{
+
 Note that the long-right-hand tail is fitted to losses, not profits.
 }
 \examples{
@@ -36,7 +35,7 @@
 Dinesh Acharya
 }
 \references{
-Dowd, K. Measuring Market Risk, Wiley, 2007.
+Dowd, K. Measurh  ing Market Risk, Wiley, 2007.
 
 Embrechts, P., Kluppelberg, C. and Mikosch, T., Modelling Extremal Events for
 Insurance and Finance. Springer, Berlin, 1997, p. 324.



More information about the Returnanalytics-commits mailing list