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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 29 14:38:34 CEST 2015


Author: dacharya
Date: 2015-06-29 14:38:33 +0200 (Mon, 29 Jun 2015)
New Revision: 3755

Added:
   pkg/Dowd/R/HSESFigure.R
   pkg/Dowd/man/HSESFigure.Rd
Modified:
   pkg/Dowd/NAMESPACE
Log:
HSESFigure added.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-29 12:36:04 UTC (rev 3754)
+++ pkg/Dowd/NAMESPACE	2015-06-29 12:38:33 UTC (rev 3755)
@@ -36,6 +36,7 @@
 export(GumbelVaRPlot2DCl)
 export(HSES)
 export(HSESDFPerc)
+export(HSESFigure)
 export(HSESPlot2DCl)
 export(HSVaR)
 export(HSVaRDFPerc)

Added: pkg/Dowd/R/HSESFigure.R
===================================================================
--- pkg/Dowd/R/HSESFigure.R	                        (rev 0)
+++ pkg/Dowd/R/HSESFigure.R	2015-06-29 12:38:33 UTC (rev 3755)
@@ -0,0 +1,71 @@
+#' @title Figure of Historical SImulation VaR and ES and histogram of L/P
+#'
+#' @description Plots figure showing the historical simulation VaR and ES and histogram
+#'  of L/P for specified confidence level and holding period implied by data 
+#'  frequency.
+#'
+#' @param Ra Vector of profit loss data
+#' @param cl VaR confidence level
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Plots figure showing VaR and histogram of P/L data
+#'    Ra <- rnorm(100)
+#'    HSESFigure(Ra, .95)
+#'
+#' @export
+HSESFigure<- function(Ra, cl){
+  # Determine if there are two arguments and ensure that arguments are read as intended
+  if (nargs() < 2) {
+    stop("Too few arguments")
+  }
+  if (nargs() > 2){
+    stop("Too many arguments")
+  }
+  if (nargs() == 2){
+    profit.loss <- as.vector(Ra)
+    n <- length(profit.loss)
+  }
+  
+  # Check that inputs have correct dimensions
+  if (length(cl) != 1) {
+    stop("Confidence level must be a scalar")
+  }
+  
+  if ( cl >= 1){
+    stop("Confidence level must be less than 1")
+  }
+  if ( cl <= 0){
+    stop("Confidence level must be positive.")
+  }
+  # VaR and ES estimation
+  VaR <- HSVaR(profit.loss, cl) # HS VaR
+  ES <- HSES(profit.loss, cl) # HS ES
+  # Histogram
+  n <- hist(-profit.loss, main = "Historical Simulation VaR", breaks = 50, col = 4,
+            xlab = "Loss(+) / Profit(-)", ylab = "Frequency")
+
+  # Insert line showing VaR
+  v <- c(0, .625 * max(n$counts)) # Coordinates for VaR line
+  u <- VaR * matrix(1, length(v),1) # Coordinates for VaR line
+  lines(u, v, type = "l", col = "black") # VaR line
+
+  # Insert line showing ES
+  w <- c(0, .45 * max(n$counts)) # Coordinates for ES
+  z <- ES * matrix(1, length(w), 1) # Coordinates for ES line
+  lines(z, w, type = "l", col = "black") # ES line
+  cl.for.label <- 100 * cl # Input to confidence level label
+  
+
+  # VaR line label
+  text(VaR, .75*max(n$counts), paste('VaR at', cl.for.label, "%Cl"))
+  text(VaR, .65*max(n$counts), c(round(VaR, digits = 2)))
+  
+  # ES line label
+  text(ES, .55*max(n$counts), c('ES ='))
+  text(ES, .45*max(n$counts), paste(round(ES, digits = 2)))
+  
+} 

Added: pkg/Dowd/man/HSESFigure.Rd
===================================================================
--- pkg/Dowd/man/HSESFigure.Rd	                        (rev 0)
+++ pkg/Dowd/man/HSESFigure.Rd	2015-06-29 12:38:33 UTC (rev 3755)
@@ -0,0 +1,30 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/HSESFigure.R
+\name{HSESFigure}
+\alias{HSESFigure}
+\title{Figure of Historical SImulation VaR and ES and histogram of L/P}
+\usage{
+HSESFigure(Ra, cl)
+}
+\arguments{
+\item{Ra}{Vector of profit loss data}
+
+\item{cl}{VaR confidence level}
+}
+\description{
+Plots figure showing the historical simulation VaR and ES and histogram
+ of L/P for specified confidence level and holding period implied by data
+ frequency.
+}
+\examples{
+# Plots figure showing VaR and histogram of P/L data
+   Ra <- rnorm(100)
+   HSESFigure(Ra, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list