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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Thu Jul 9 16:20:34 CEST 2015


Author: dacharya
Date: 2015-07-09 16:20:34 +0200 (Thu, 09 Jul 2015)
New Revision: 3798

Added:
   pkg/Dowd/R/LogtVaRDFPerc.R
   pkg/Dowd/man/LogtVaRDFPerc.Rd
Log:
Function LogtVaRDFPerc added.

Added: pkg/Dowd/R/LogtVaRDFPerc.R
===================================================================
--- pkg/Dowd/R/LogtVaRDFPerc.R	                        (rev 0)
+++ pkg/Dowd/R/LogtVaRDFPerc.R	2015-07-09 14:20:34 UTC (rev 3798)
@@ -0,0 +1,154 @@
+#' Percentiles of VaR distribution function for Student-t
+#' 
+#' Plots the VaR of a portfolio against confidence level assuming that geometric returns are 
+#' Student t distributed, for specified confidence level and holding period.
+#' 
+#' @param returns Vector of daily geometric return data
+#' @param mu Mean of daily geometric return data
+#' @param sigma Standard deviation of daily geometric return data
+#' @param n Sample size
+#' @param investment Size of investment
+#' @param perc Desired percentile
+#' @param df Number of degrees of freedom in the t distribution
+#' @param cl VaR confidence level and must be a scalar
+#' @param hp VaR holding period and must be a a scalar
+#' @return Percentiles of VaR distribution function
+#' @note The input arguments contain either return data or else mean and 
+#' standard deviation data. Accordingly, number of input arguments is either 5 
+#' or 6. In case there 5 input arguments, the mean and standard deviation of 
+#' data is computed from return data. See examples for details.
+#'  
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Estimates Percentiles of VaR distribution
+#'    data <- runif(5, min = 0, max = .2)
+#'    LogtVaRDFPerc(returns = data, investment = 5, perc = .7, df = 6, cl = .95, hp = 60)
+#'    
+#'    # Computes v given mean and standard deviation of return data
+#'    LogtVaRDFPerc(mu = .012, sigma = .03, n= 10, investment = 5, perc = .8, df = 6, cl = .99, hp = 40)
+#'
+#'
+#' @export
+LogtVaRDFPerc <- function(...){
+  if (nargs() < 6) {
+    stop("Too few arguments")
+  }
+  if (nargs() == 7) {
+    stop("Incorrect number of arguments")
+  }
+  if (nargs() > 8) {
+    stop("Too many arguments")
+  }
+  args <- list(...)
+  if (nargs() == 8) {
+    mu <- args$mu
+    investment <- args$investment
+    df <- args$df
+    cl <- args$cl
+    perc <- args$sigma
+    n <- args$n
+    sigma <- args$sigma
+    hp <- args$hp
+  }
+  if (nargs() == 6) {
+    mu <- mean(args$returns)
+    investment <- args$investment
+    df <- args$df
+    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 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 (cl > 1){
+    stop("Chosen percentile must not exceed 1")
+  }
+  if (cl <= 0){
+    stop("Confidence level must be positive")
+  }
+  if (cl >= 1){
+    stop("Confidence level(s) 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 estimation
+  y <- investment - exp( ((df-2)/df) * sigma[1,1] * sqrt(hp) %*% qt(1 - cl, df)  + mu[1,1] * hp %*% matrix(1,cl.row,cl.col) + log(investment)) # VaR
+  
+  return(y)
+}

Added: pkg/Dowd/man/LogtVaRDFPerc.Rd
===================================================================
--- pkg/Dowd/man/LogtVaRDFPerc.Rd	                        (rev 0)
+++ pkg/Dowd/man/LogtVaRDFPerc.Rd	2015-07-09 14:20:34 UTC (rev 3798)
@@ -0,0 +1,55 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/LogtVaRDFPerc.R
+\name{LogtVaRDFPerc}
+\alias{LogtVaRDFPerc}
+\title{Percentiles of VaR distribution function for Student-t}
+\usage{
+LogtVaRDFPerc(...)
+}
+\arguments{
+\item{returns}{Vector of daily geometric return data}
+
+\item{mu}{Mean of daily geometric return data}
+
+\item{sigma}{Standard deviation of daily geometric return data}
+
+\item{n}{Sample size}
+
+\item{investment}{Size of investment}
+
+\item{perc}{Desired percentile}
+
+\item{df}{Number of degrees of freedom in the t distribution}
+
+\item{cl}{VaR confidence level and must be a scalar}
+
+\item{hp}{VaR holding period and must be a a scalar}
+}
+\value{
+Percentiles of VaR distribution function
+}
+\description{
+Plots the VaR of a portfolio against confidence level assuming that geometric returns are
+Student t distributed, for specified confidence level and holding period.
+}
+\note{
+The input arguments contain either return data or else mean and
+standard deviation data. Accordingly, number of input arguments is either 5
+or 6. In case there 5 input arguments, the mean and standard deviation of
+data is computed from return data. See examples for details.
+}
+\examples{
+# Estimates Percentiles of VaR distribution
+   data <- runif(5, min = 0, max = .2)
+   LogtVaRDFPerc(returns = data, investment = 5, perc = .7, df = 6, cl = .95, hp = 60)
+
+   # Computes v given mean and standard deviation of return data
+   LogtVaRDFPerc(mu = .012, sigma = .03, n= 10, investment = 5, perc = .8, df = 6, cl = .99, hp = 40)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list