[Returnanalytics-commits] r3748 - in pkg/Dowd: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Jun 26 20:47:29 CEST 2015
Author: dacharya
Date: 2015-06-26 20:47:29 +0200 (Fri, 26 Jun 2015)
New Revision: 3748
Added:
pkg/Dowd/R/HSESDFPerc.R
pkg/Dowd/man/HSESDFPerc.Rd
Log:
HSESDFPerc added.
Added: pkg/Dowd/R/HSESDFPerc.R
===================================================================
--- pkg/Dowd/R/HSESDFPerc.R (rev 0)
+++ pkg/Dowd/R/HSESDFPerc.R 2015-06-26 18:47:29 UTC (rev 3748)
@@ -0,0 +1,82 @@
+#' @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)
+#' HSESDFPerc(Ra, .75, .95)
+#'
+#' @export
+HSESDFPerc <- 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)
+ VaR <- data[i] # Value of percentile of VaR distribution function
+ j <- i:n
+ y <- mean(data[j]) # Value of percentile of ES distribution function
+ return(y)
+
+}
\ No newline at end of file
Added: pkg/Dowd/man/HSESDFPerc.Rd
===================================================================
--- pkg/Dowd/man/HSESDFPerc.Rd (rev 0)
+++ pkg/Dowd/man/HSESDFPerc.Rd 2015-06-26 18:47:29 UTC (rev 3748)
@@ -0,0 +1,35 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/HSESDFPerc.R
+\name{HSESDFPerc}
+\alias{HSESDFPerc}
+\title{Percentile of historical simulation VaR distribution function}
+\usage{
+HSESDFPerc(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)
+ HSESDFPerc(Ra, .75, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+
More information about the Returnanalytics-commits
mailing list