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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu May 28 23:14:29 CEST 2015


Author: dacharya
Date: 2015-05-28 23:14:29 +0200 (Thu, 28 May 2015)
New Revision: 3656

Added:
   pkg/Dowd/R/BootstrapES.R
   pkg/Dowd/R/HSES.R
   pkg/Dowd/R/HSVaR.R
   pkg/Dowd/man/BootstrapES.Rd
   pkg/Dowd/man/HSES.Rd
   pkg/Dowd/man/HSVaR.Rd
Modified:
   pkg/Dowd/NAMESPACE
   pkg/Dowd/readme.txt
Log:
BootstrapES, HSES and HSVaR: source and documentation

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-05-27 19:05:08 UTC (rev 3655)
+++ pkg/Dowd/NAMESPACE	2015-05-28 21:14:29 UTC (rev 3656)
@@ -3,8 +3,11 @@
 export(ADTestStat)
 export(BinomialBacktest)
 export(BlancoIhleBacktest)
+export(BootstrapES)
 export(ChristoffersenBacktestForIndependence)
 export(ChristoffersenBacktestForUnconditionalCoverage)
+export(HSES)
+export(HSVaR)
 export(JarqueBeraBacktest)
 export(KSTestStat)
 export(KuiperTestStat)

Added: pkg/Dowd/R/BootstrapES.R
===================================================================
--- pkg/Dowd/R/BootstrapES.R	                        (rev 0)
+++ pkg/Dowd/R/BootstrapES.R	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,60 @@
+#' Bootstrapped ES for specified confidence level
+#'
+#' Estimates the bootstrapped ES for confidence level and holding period
+#' implied by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param number.sample Number of samples to be taken in bootstrap procedure
+#' @return cl Number corresponding to Value at Risk confidence level
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Estimates bootstrapped Es for given parameters
+#'    a <- rnorm(100) # generate a random profit/loss vector
+#'    BootstrappedES(a, 50, 0.95)
+#'
+#' @export
+BootstrapES <- function(Ra, number.sample, cl){
+  
+  if (nargs() < 3){
+    error("Too few arguments")
+  }
+  if (nargs() > 3){
+    error("Too many arguments")
+  }
+  
+  profit.loss.data <- as.vector(Ra)
+  # Preprocess data
+  unsorted.loss.data <- -profit.loss.data
+  losses.data <- sort(unsorted.loss.data)
+  n <- length(losses.data)
+  
+  # Check that inputs have correct dimensions
+  if (length(cl) != 1) {
+    error("Confidence level must be a scalar")
+  }
+  if (length(number.samples) != 1){
+    error("Number of resamples must be a scalar");
+  }
+  
+  # Check that inputs obey sign and value restrictions
+  if (cl >= 1){
+    stop("Confidence level must be less that 1")
+  }
+  if (number.resamples <= 0){
+    stop("Number of resamples must be at least 0")
+  }
+  
+  #############################################
+  # suitable alternative to bootstrp in R is still to be explored.
+  #############################################
+  # ES estimation
+  #
+  # es <- bootstrp(number.resamples, "hses", losses.data, cl)
+  # y <- mean(es)
+  # return (y)
+}
\ No newline at end of file

Added: pkg/Dowd/R/HSES.R
===================================================================
--- pkg/Dowd/R/HSES.R	                        (rev 0)
+++ pkg/Dowd/R/HSES.R	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,108 @@
+#' Expected Shortfall of a portfolio using Historical Estimator
+#'
+#' Estimates the Expected Shortfall (aka. Average Value at Risk or Conditional 
+#' Value at Risk) using historical estimator approach for the specified
+#' confidence level and the holding period implies by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param cl Number between 0 and 1 corresponding to confidence level
+#' @return Expected Shortfall of the portfolio
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Cont, R., Deguest, R. and Scandolo, G. Robustness and sensitivity analysis
+#' of risk measurement procedures. Quantitative Finance, 10(6), 2010, 593-606.
+#' 
+#' Acerbi, C. and Tasche, D. On the coherence of Expected Shortfall. Journal
+#' of Banking and Finance, 26(7), 2002, 1487-1503
+#' 
+#' Artzner, P., Delbaen, F., Eber, J.M. and Heath, D. Coherent Risk Measures 
+#' of Risk. Mathematical Finance 9(3), 1999, 203.
+#' 
+#' Föllmer, H. and Scheid, A. Stochastic Finance: An Introduction in Discrete 
+#' Time. De Gryuter, 2011.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Computes Historical Expected Shortfall for a given profit/loss
+#'    # distribution and confidence level
+#'    a <- rnorm(100) # generate a random profit/loss vector
+#'    HSES(a, 0.95)
+#'
+#' @export
+HSES <- function(Ra, cl){
+  
+  if (nargs() < 2) {
+    stop("Too few arguments")
+  }
+  
+  if (nargs() > 2) {
+    stop("Too many arguments")
+  }
+  
+  
+  if (nargs() == 2) {
+    profit.loss.data <- as.vector(Ra)
+    unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L
+    losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
+    n <- length(losses.data)
+  }
+  
+  # Check that inputs have correct dimensions
+  if (length(cl) != 1) {
+    stop('Confidence level must be scalar (length-1 vector in R)')
+  }
+  
+  
+  # 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 positive")
+  }
+  
+  index <- n*cl # This putative index value may or may not be an integer
+  
+  # Each case needs to be considered in turn
+  # If index value is an integegr, VaR follows immediately and then we
+  # estimate ES
+  if (index-round(index)==0){
+    var <- losses.data[index] # Historical Value at Risk
+    k <- which[var <= losses.data] # Finds indices of tail loss data
+    tail.losses <- losses.data[k] # Creates data set of tail loss observations
+    es <- mean(tail.losses) # Expected Shortfall
+    y <- es    
+  }
+  
+  # If index not an integer, take VaR as linear interpolation of loss
+  # observationsjust above and "below" true VaR and take Expected Shortfall
+  # as linear interpolation of corresponding upper and lower Expected Shortfall
+  if (index-round(index) != 0){
+    # Deal with loss
+    upper.index <- ceiling(index)
+    upper.var <- losses.data(upper.index) # Upper VaR
+    upper.k <- which(upper.var<=losses.data) # Finds indices of upper tail loss data
+    upper.tail.losses <- losses.data(upper.k) # Creates data set of upper tail loss obs.
+    lower.es <- mean(lower.tail.losses) # Lower Expected Shortfall (ES)
+    # If lower and upper indices are the same, ES is upper ES
+    if (upper.index == lower.index){
+      y <- upper.es
+    }
+    # If lower and upper indices are different, ES is weighted average of 
+    # upper and lower ESs
+    if (upper.index!=lower.index) {
+      # Weights attached to upper and lower ESs
+      lower.weight <- (upper.index-index)/(upper.index-lower.index)
+      upper.weight <- (index-lower.index)/(upper.index-lower.index)
+      # Finally, the weighted, ES as a linear interpolation of upper and lower
+      # ESs
+      y <- lower.weight*lower.es+upper.weight*upper.es
+  
+    }
+    return(y)
+  }
+  
+}
+

Added: pkg/Dowd/R/HSVaR.R
===================================================================
--- pkg/Dowd/R/HSVaR.R	                        (rev 0)
+++ pkg/Dowd/R/HSVaR.R	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,106 @@
+#' Value at Risk of a portfolio using Historical Estimator
+#'
+#' Estimates the Value at Risk (VaR) using historical estimator 
+#' approach for the specified range of confidence levels and the holding
+#' period implies by data frequency.
+#'
+#' @param Ra Vector corresponding to profit and loss distribution
+#' @param Rb Vector corresponding to VaR confidence levels.
+#' @return Value at Risk of the portfolio
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Jorion, P. Value at Risk: The New Benchmark for Managing Financial Risk. 
+#' McGraw-Hill, 2006
+#' 
+#' Cont, R., Deguest, R. and Scandolo, G. Robustness and sensitivity analysis
+#' of risk measurement procedures. Quantitative Finance, 10(6), 2010, 593-606.
+#' 
+#' Artzner, P., Delbaen, F., Eber, J.M. and Heath, D. Coherent Risk Measures 
+#' of Risk. Mathematical Finance 9(3), 1999, 203.
+#' 
+#' Föllmer, H. and Scheid, A. Stochastic Finance: An Introduction in Discrete 
+#' Time. De Gryuter, 2011.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # To be added.
+#'
+#' @export
+HSVaR <- function(Ra, Rb){
+  
+  # Determine if there are two arguments and ensure that they are read as
+  # intended
+  if (nargs() < 2) {
+    stop("Too few arguments")
+  }
+  
+  if (nargs() > 2) {
+    stop("Too many arguments")
+  }
+  
+  
+  if (nargs() == 2) {
+    profit.loss.data <- as.vector(Ra)
+    cl <- as.vector(Rb)
+    unsorted.loss.data <- -profit.loss.data # Derives L/P data from input P/L
+    losses.data <- sort(unsorted.loss.data) # Puts losses in ascending order
+    n <- length(losses.data)
+  }
+  
+  # Check that inputs have correct dimensions
+  if (is.vector(Rb)) {
+    stop("Confidence level must be a vector")
+  }
+  
+  
+  # Check that inputs obey sign and value restrictions
+  if (max(cl) >= 1) {
+    stop("Confidence level must be less than 1.")
+  }
+  if (max(cl) <= 0) {
+    stop("Confidence level must be positive")
+  }
+  
+  i <- 1:length(cl)
+  index <- cl*n # This putative index value may or may not be an integer
+  
+  # If index value is an integer, VaR follows immediately
+  y <- double(length(i))
+  if (index-round[index] == 0){
+    y[i] <- losses.data[index]
+  }
+  
+  # If index not an integer, take VaR as linear interpolation of loss
+  # observations just above and below "true" VaR
+  
+  if (index-round(index)!=0){
+    # Deal with loss observation just above VaR
+    upper.index <- ceiling(index)
+    upper.var <- losses.data[upper.index] # Loss observation just above VaR or upper VaR
+    
+    # Deal with loss observation just below VaR
+    lower.index <- floor(index)
+    lower.var <- losses.data[lower.index] # Loss observation just below VaR or lower VaR
+    
+    # If lower and upper indices are the same ,VaR is upper VaR
+    if (upper.index==lower.index){
+      y <- upper.var
+    }
+    
+    # If lower and upper indices different, VaR is weighted average of upper
+    # and lower VaRs
+    if (upper.index!=lower.index){
+      # Weights attached to upper and lower VaRs
+      lower.weight <- (upper.index-index)/(upper.index-lower.index) # weight on upper.var
+      
+      # Finally, the weighted, VaR as a linear interpolation of upper and lower VaRs
+      
+      y <- lower.weight * lower.var + upper.weight * upper.var
+      
+    }
+    
+  }
+    return(y)
+}
\ No newline at end of file

Added: pkg/Dowd/man/BootstrapES.Rd
===================================================================
--- pkg/Dowd/man/BootstrapES.Rd	                        (rev 0)
+++ pkg/Dowd/man/BootstrapES.Rd	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,32 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/BootstrapES.R
+\name{BootstrapES}
+\alias{BootstrapES}
+\title{Bootstrapped ES for specified confidence level}
+\usage{
+BootstrapES(Ra, number.sample, cl)
+}
+\arguments{
+\item{Ra}{Vector corresponding to profit and loss distribution}
+
+\item{number.sample}{Number of samples to be taken in bootstrap procedure}
+}
+\value{
+cl Number corresponding to Value at Risk confidence level
+}
+\description{
+Estimates the bootstrapped ES for confidence level and holding period
+implied by data frequency.
+}
+\examples{
+# Estimates bootstrapped Es for given parameters
+   a <- rnorm(100) # generate a random profit/loss vector
+   BootstrappedES(a, 50, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+

Added: pkg/Dowd/man/HSES.Rd
===================================================================
--- pkg/Dowd/man/HSES.Rd	                        (rev 0)
+++ pkg/Dowd/man/HSES.Rd	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,46 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/HSES.R
+\name{HSES}
+\alias{HSES}
+\title{Expected Shortfall of a portfolio using Historical Estimator}
+\usage{
+HSES(Ra, cl)
+}
+\arguments{
+\item{Ra}{Vector corresponding to profit and loss distribution}
+
+\item{cl}{Number between 0 and 1 corresponding to confidence level}
+}
+\value{
+Expected Shortfall of the portfolio
+}
+\description{
+Estimates the Expected Shortfall (aka. Average Value at Risk or Conditional
+Value at Risk) using historical estimator approach for the specified
+confidence level and the holding period implies by data frequency.
+}
+\examples{
+# Computes Historical Expected Shortfall for a given profit/loss
+   # distribution and confidence level
+   a <- rnorm(100) # generate a random profit/loss vector
+   HSES(a, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Cont, R., Deguest, R. and Scandolo, G. Robustness and sensitivity analysis
+of risk measurement procedures. Quantitative Finance, 10(6), 2010, 593-606.
+
+Acerbi, C. and Tasche, D. On the coherence of Expected Shortfall. Journal
+of Banking and Finance, 26(7), 2002, 1487-1503
+
+Artzner, P., Delbaen, F., Eber, J.M. and Heath, D. Coherent Risk Measures
+of Risk. Mathematical Finance 9(3), 1999, 203.
+
+Föllmer, H. and Scheid, A. Stochastic Finance: An Introduction in Discrete
+Time. De Gryuter, 2011.
+}
+

Added: pkg/Dowd/man/HSVaR.Rd
===================================================================
--- pkg/Dowd/man/HSVaR.Rd	                        (rev 0)
+++ pkg/Dowd/man/HSVaR.Rd	2015-05-28 21:14:29 UTC (rev 3656)
@@ -0,0 +1,43 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/HSVaR.R
+\name{HSVaR}
+\alias{HSVaR}
+\title{Value at Risk of a portfolio using Historical Estimator}
+\usage{
+HSVaR(Ra, Rb)
+}
+\arguments{
+\item{Ra}{Vector corresponding to profit and loss distribution}
+
+\item{Rb}{Vector corresponding to VaR confidence levels.}
+}
+\value{
+Value at Risk of the portfolio
+}
+\description{
+Estimates the Value at Risk (VaR) using historical estimator
+approach for the specified range of confidence levels and the holding
+period implies by data frequency.
+}
+\examples{
+# To be added.
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Jorion, P. Value at Risk: The New Benchmark for Managing Financial Risk.
+McGraw-Hill, 2006
+
+Cont, R., Deguest, R. and Scandolo, G. Robustness and sensitivity analysis
+of risk measurement procedures. Quantitative Finance, 10(6), 2010, 593-606.
+
+Artzner, P., Delbaen, F., Eber, J.M. and Heath, D. Coherent Risk Measures
+of Risk. Mathematical Finance 9(3), 1999, 203.
+
+Föllmer, H. and Scheid, A. Stochastic Finance: An Introduction in Discrete
+Time. De Gryuter, 2011.
+}
+

Modified: pkg/Dowd/readme.txt
===================================================================
--- pkg/Dowd/readme.txt	2015-05-27 19:05:08 UTC (rev 3655)
+++ pkg/Dowd/readme.txt	2015-05-28 21:14:29 UTC (rev 3656)
@@ -1,4 +1,8 @@
 #***************************************************************
+# most suitable function similar to bootsrtp in matlab is still to be checked
+# original bootstrp VaR so that still needs to be checked.
+# Other functions depending on bootstrp are still only half complete.
+#***************************************************************
 # Jarque-Bera Test:
 # It has to be checked Probability of null (H0) or (H1).
 # (http://stats.stackexchange.com/questions/130368/why-do-i-get-this-p-value-doing-the-jarque-bera-test-in-r)



More information about the Returnanalytics-commits mailing list