[Returnanalytics-commits] r3838 - in pkg/Dowd: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jul 21 23:05:47 CEST 2015
Author: dacharya
Date: 2015-07-21 23:05:46 +0200 (Tue, 21 Jul 2015)
New Revision: 3838
Added:
pkg/Dowd/R/NormalESDFPerc.R
pkg/Dowd/man/NormalESDFPerc.Rd
Log:
Function NormalESDFPerc added.
Added: pkg/Dowd/R/NormalESDFPerc.R
===================================================================
--- pkg/Dowd/R/NormalESDFPerc.R (rev 0)
+++ pkg/Dowd/R/NormalESDFPerc.R 2015-07-21 21:05:46 UTC (rev 3838)
@@ -0,0 +1,158 @@
+#' Percentiles of ES distribution function for normally distributed P/L data
+#'
+#' Estimates the percentiles of ES distribution for normally distributed P/L data, for specified confidence level and holding period using the theory of order statistics.
+#'
+#' @param ... The input arguments contain either return data or else mean and
+#' standard deviation data. Accordingly, number of input arguments is either 4 or 6. In case there 4 input arguments, the mean, standard deviation and number of samples is computed from return data. See examples for details.
+#'
+#' returns Vector of daily geometric return data
+#'
+#' mu Mean of daily geometric return data
+#'
+#' sigma Standard deviation of daily geometric return data
+#'
+#' n Sample size
+#'
+#' perc Desired percentile
+#'
+#' cl ES confidence level and must be a scalar
+#'
+#' hp ES holding period and must be a a scalar
+#'
+#' @return Percentiles of ES distribution function
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # Estimates Percentiles of ES distribution
+#' data <- runif(5, min = 0, max = .2)
+#' NormalESDFPerc(returns = data, perc = .7, cl = .95, hp = 60)
+#'
+#' # Estimates Percentiles given mean, standard deviation and number of sambles of return data
+#' NormalESDFPerc(mu = .012, sigma = .03, n= 10, perc = .8, cl = .99, hp = 40)
+#'
+#'
+#' @export
+NormalESDFPerc <- function(...){
+ # Determine if there are five or seven arguments, and ensure that arguments are read as intended
+ if (nargs() < 4) {
+ stop("Too few arguments")
+ }
+ if (nargs() == 5) {
+ stop("Incorrect number of arguments")
+ }
+ if (nargs() > 6) {
+ stop("Too many arguments")
+ }
+ args <- list(...)
+ if (nargs() == 6) {
+ mu <- args$mu
+ cl <- args$cl
+ perc <- args$sigma
+ n <- args$n
+ sigma <- args$sigma
+ hp <- args$hp
+ }
+ if (nargs() == 4) {
+ mu <- mean(args$returns)
+ n <- max(dim(as.matrix(args$returns)))
+ perc <- args$perc
+ cl <- args$cl
+ sigma <- sd(args$returns)
+ hp <- args$hp
+ }
+
+ # Check that inputs have correct dimensions
+ mu <- as.matrix(mu)
+ mu.row <- dim(mu)[1]
+ mu.col <- dim(mu)[2]
+ if (max(mu.row, mu.col) > 1) {
+ stop("Mean must be a scalar")
+ }
+ sigma <- as.matrix(sigma)
+ sigma.row <- dim(sigma)[1]
+ sigma.col <- dim(sigma)[2]
+ if (max(sigma.row, sigma.col) > 1) {
+ stop("Standard deviation must be a scalar")
+ }
+ n <- as.matrix(n)
+ n.row <- dim(n)[1]
+ n.col <- dim(n)[2]
+ if (max(n.row, n.col) > 1) {
+ stop("Number of observations in a sample must be a scalar")
+ }
+ if (n %% 1 != 0) {
+ stop("Number of observations in a sample must be an integer.")
+ }
+ perc <- as.matrix(perc)
+ perc.row <- dim(perc)[1]
+ perc.col <- dim(perc)[2]
+ if (max(perc.row, perc.col) > 1) {
+ stop("Chosen percentile of the distribution must be a scalar")
+ }
+ cl <- as.matrix(cl)
+ cl.row <- dim(cl)[1]
+ cl.col <- dim(cl)[2]
+ if (max(cl.row, cl.col) > 1) {
+ stop("Confidence level must be a scalar")
+ }
+ hp <- as.matrix(hp)
+ hp.row <- dim(hp)[1]
+ hp.col <- dim(hp)[2]
+ if (max(hp.row, hp.col) > 1) {
+ stop("Holding period must be a scalar")
+ }
+
+ # Check that inputs obey sign and value restrictions
+ if (sigma < 0) {
+ stop("Standard deviation must be non-negative")
+ }
+ if (n < 0) {
+ stop("Number of observations must be non-negative")
+ }
+ if (perc > 1){
+ stop("Chosen percentile must not exceed 1")
+ }
+ if (perc <= 0){
+ stop("Chosen percentile must be positive")
+ }
+ 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("Honding period must be greater than 0")
+ }
+
+ # Derive order statistic and ensure it is an integer
+ w <- n * cl # Derive r-th order statistic
+ 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
+ }
+ }
+
+ VaR.perc <- qnorm(x, -mu * hp, sigma * sqrt(hp)) # Value of VaR percentile; note normal VaR formula
+
+ # ES estimation
+ ES <- sigma[1,1] * sqrt(hp) %*% dnorm(VaR.perc, 0, 1) / (1 - cl) - mu[1,1] * hp %*% matrix(1,cl.row,cl.col) # Value of ES percentile
+
+ return(ES)
+}
\ No newline at end of file
Added: pkg/Dowd/man/NormalESDFPerc.Rd
===================================================================
--- pkg/Dowd/man/NormalESDFPerc.Rd (rev 0)
+++ pkg/Dowd/man/NormalESDFPerc.Rd 2015-07-21 21:05:46 UTC (rev 3838)
@@ -0,0 +1,47 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/NormalESDFPerc.R
+\name{NormalESDFPerc}
+\alias{NormalESDFPerc}
+\title{Percentiles of ES distribution function for normally distributed P/L data}
+\usage{
+NormalESDFPerc(...)
+}
+\arguments{
+\item{...}{The input arguments contain either return data or else mean and
+standard deviation data. Accordingly, number of input arguments is either 4 or 6. In case there 4 input arguments, the mean, standard deviation and number of samples is computed from return data. See examples for details.
+
+returns Vector of daily geometric return data
+
+mu Mean of daily geometric return data
+
+sigma Standard deviation of daily geometric return data
+
+n Sample size
+
+perc Desired percentile
+
+cl ES confidence level and must be a scalar
+
+hp ES holding period and must be a a scalar}
+}
+\value{
+Percentiles of ES distribution function
+}
+\description{
+Estimates the percentiles of ES distribution for normally distributed P/L data, for specified confidence level and holding period using the theory of order statistics.
+}
+\examples{
+# Estimates Percentiles of ES distribution
+ data <- runif(5, min = 0, max = .2)
+ NormalESDFPerc(returns = data, perc = .7, cl = .95, hp = 60)
+
+ # Estimates Percentiles given mean, standard deviation and number of sambles of return data
+ NormalESDFPerc(mu = .012, sigma = .03, n= 10, perc = .8, cl = .99, hp = 40)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+
More information about the Returnanalytics-commits
mailing list