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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jun 18 01:16:50 CEST 2015


Author: dacharya
Date: 2015-06-18 01:16:50 +0200 (Thu, 18 Jun 2015)
New Revision: 3678

Added:
   pkg/Dowd/R/HSVaRDFPerc.R
   pkg/Dowd/man/HSVaRDFPerc.Rd
Modified:
   pkg/Dowd/NAMESPACE
   pkg/Dowd/R/GumbelESPlot2DCl.R
   pkg/Dowd/R/GumbelVaRPlot2DCl.R
   pkg/Dowd/man/GumbelESPlot2DCl.Rd
Log:
HSVaRDFPerc : source and documentation

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-16 15:39:42 UTC (rev 3677)
+++ pkg/Dowd/NAMESPACE	2015-06-17 23:16:50 UTC (rev 3678)
@@ -34,6 +34,7 @@
 export(GumbelVaRPlot2DCl)
 export(HSES)
 export(HSVaR)
+export(HSVaRDFPerc)
 export(HillEstimator)
 export(HillPlot)
 export(HillQuantileEstimator)

Modified: pkg/Dowd/R/GumbelESPlot2DCl.R
===================================================================
--- pkg/Dowd/R/GumbelESPlot2DCl.R	2015-06-16 15:39:42 UTC (rev 3677)
+++ pkg/Dowd/R/GumbelESPlot2DCl.R	2015-06-17 23:16:50 UTC (rev 3678)
@@ -15,7 +15,7 @@
 #' @examples
 #' 
 #'    # Plots ES against Cl
-#'    GumbelESPlot2DCl(0, 1.2, 100, c(.9,.88, .85, .8), 280)
+#'    GumbelESPlot2DCl(0, 1.2, 100, seq(0.8,0.99,0.02), 280)
 #'
 #' @export
 GumbelESPlot2DCl<- function(mu, sigma, n, cl, hp){
@@ -68,20 +68,10 @@
   
   # Plotting
   plot(cl0, v, xlab = "Confidence Level", ylab = "ES", type = "l")
-  
-  text(mean(cl0), 
-       max(v) - .1*(max(v) - min(v)),
-       'Input parameters')
-  text(mean(cl0), 
-       max(v)-.2*(max(v)-min(v)),
-       paste('Location parameter for daily L/P = ', mu))
-  text(mean(cl0), 
-       max(v) - .3 * (max(v) - min(v)),
-       paste('Scale parameter for daily L/P = ', sigma))
-  text(mean(cl0), 
-       max(v) - .4 * (max(v) - min(v)),
-       paste('Holding period = ', hp, ' days'))
-  
+  legend("topleft",c('Input parameters',
+                     paste('Location parameter for daily L/P = ', mu),
+                     paste('Scale parameter for daily L/P = ', sigma),
+                     paste('Holding period = ', hp, ' days')), bty="n")
   title("Gumbel ES against confidence level")
   
 } 
\ No newline at end of file

Modified: pkg/Dowd/R/GumbelVaRPlot2DCl.R
===================================================================
--- pkg/Dowd/R/GumbelVaRPlot2DCl.R	2015-06-16 15:39:42 UTC (rev 3677)
+++ pkg/Dowd/R/GumbelVaRPlot2DCl.R	2015-06-17 23:16:50 UTC (rev 3678)
@@ -10,7 +10,6 @@
 #' 
 #' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
 #' 
-#' 
 #' @author Dinesh Acharya
 #' @examples
 #' 
@@ -56,19 +55,10 @@
   
   # Plotting
   plot(cl, VaR, xlab = "Confidence Level", ylab = "VaR", type = "l")
-  text(mean(cl), 
-       max(VaR) - .1*(max(VaR) - min(VaR)),
-       'Input parameters')
-  text(mean(cl), 
-       max(VaR)-.2*(max(VaR)-min(VaR)),
-       paste('Location parameter for daily L/P = ', mu))
-  text(mean(cl), 
-       max(VaR) - .3 * (max(VaR) - min(VaR)),
-       paste('Scale parameter for daily L/P = ', sigma))
-  text(mean(cl), 
-       max(VaR) - .4 * (max(VaR) - min(VaR)),
-       paste('Holding period = ', hp, ' days'))
-  
+  legend("topleft",c('Input parameters',
+                     paste('Location parameter for daily L/P = ', mu),
+                     paste('Scale parameter for daily L/P = ', sigma),
+                     paste('Holding period = ', hp, ' days')), bty="n")
   title("Gumbel VaR against confidence level")
   
 } 
\ No newline at end of file

Added: pkg/Dowd/R/HSVaRDFPerc.R
===================================================================
--- pkg/Dowd/R/HSVaRDFPerc.R	                        (rev 0)
+++ pkg/Dowd/R/HSVaRDFPerc.R	2015-06-17 23:16:50 UTC (rev 3678)
@@ -0,0 +1,80 @@
+#' @title Percentile of historical simulation VaR distribution function
+#'
+#' @description Estimates percentiles of historical simulation VaR distribution 
+#' function, using theory of order statistics, for specified confidence level.
+#'
+#' @param Ra Vector of daily P/L data
+#' @param perc Desired percentile and is scalar
+#' @param cl VaR confidence level and is scalar
+#' @return Value of percentile of VaR distribution function
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Estimates Percentiles for random standard normal returns and given perc 
+#'    # and cl
+#'    Ra <- rnorm(100)
+#'    HSVaRDFPerc(Ra, .75, .95)
+#'
+#' @export
+HSVaRDFPerc <- function(Ra, perc, cl){
+  
+  # Determine if there are three arguments, and ensure that arguments are read as intended
+  if (nargs() < 3) {
+    stop("Too few arguments.")
+  }
+  if (nargs() > 3) {
+    stop("Too many arguments")
+  }
+  if (nargs() == 3) {
+    profit.loss <- as.vector(Ra)
+    data <- sort(profit.loss)
+    n <- length(data)
+  }
+  
+  # Check that inputs obey sign and value restrictions
+  if (n < 0) {
+    stop("Number of observations must be greater than zero.")
+  }
+  if (perc <= 0) {
+    stop("Chosen percentile must be positive.")
+  }
+  if (perc > 1) {
+    stop("Chosen percentile must not exceed 1")
+  }
+  if (cl >= 1) {
+    stop("Confidence level must be less than 1.")
+  }
+  if (cl <= 0) {
+    stop("Confidence level must positive.")
+  }
+  
+  # Derive order statistics and ensure it is an integer
+  w <- n * cl # Derive rth order statistics
+  r <- round(w) # Round r to nearest integer
+  
+  # Bisection routine
+  a <- 0
+  fa <- -Inf
+  b <- 1
+  fb <- Inf
+  eps <- .Machine$double.eps
+  while (b - a > eps * b) {
+    x <- (a + b) / 2
+    fx <- 1 - pbinom(r - 1, n, x) - perc
+    if (sign(fx) == sign(fa)){
+      a = x
+      fa = fx
+    } else {
+      b = x
+      fb = fx
+    }
+  }
+  i <- round(n * x)
+  y <- data[i] # Value of percentile of VaR distribution function
+  return(y)
+  
+} 
\ No newline at end of file

Modified: pkg/Dowd/man/GumbelESPlot2DCl.Rd
===================================================================
--- pkg/Dowd/man/GumbelESPlot2DCl.Rd	2015-06-16 15:39:42 UTC (rev 3677)
+++ pkg/Dowd/man/GumbelESPlot2DCl.Rd	2015-06-17 23:16:50 UTC (rev 3678)
@@ -22,7 +22,7 @@
 }
 \examples{
 # Plots ES against Cl
-   GumbelESPlot2DCl(0, 1.2, 100, c(.9,.88, .85, .8), 280)
+   GumbelESPlot2DCl(0, 1.2, 100, seq(0.8,0.99,0.02), 280)
 }
 \author{
 Dinesh Acharya

Added: pkg/Dowd/man/HSVaRDFPerc.Rd
===================================================================
--- pkg/Dowd/man/HSVaRDFPerc.Rd	                        (rev 0)
+++ pkg/Dowd/man/HSVaRDFPerc.Rd	2015-06-17 23:16:50 UTC (rev 3678)
@@ -0,0 +1,35 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/HSVaRDFPerc.R
+\name{HSVaRDFPerc}
+\alias{HSVaRDFPerc}
+\title{Percentile of historical simulation VaR distribution function}
+\usage{
+HSVaRDFPerc(Ra, perc, cl)
+}
+\arguments{
+\item{Ra}{Vector of daily P/L data}
+
+\item{perc}{Desired percentile and is scalar}
+
+\item{cl}{VaR confidence level and is scalar}
+}
+\value{
+Value of percentile of VaR distribution function
+}
+\description{
+Estimates percentiles of historical simulation VaR distribution
+function, using theory of order statistics, for specified confidence level.
+}
+\examples{
+# Estimates Percentiles for random standard normal returns and given perc
+   # and cl
+   Ra <- rnorm(100)
+   HSVaRDFPerc(Ra, .75, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list