[Returnanalytics-commits] r3648 - in pkg: . Dowd Dowd/R Dowd/man Dowd/tests Dowd/tests/testthat

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 25 14:39:03 CEST 2015


Author: dacharya
Date: 2015-05-25 14:39:03 +0200 (Mon, 25 May 2015)
New Revision: 3648

Added:
   pkg/Dowd/
   pkg/Dowd/DESCRIPTION
   pkg/Dowd/NAMESPACE
   pkg/Dowd/R/
   pkg/Dowd/R/BinomialBacktest.R
   pkg/Dowd/man/
   pkg/Dowd/man/BinomialBacktest.Rd
   pkg/Dowd/tests/
   pkg/Dowd/tests/testthat.R
   pkg/Dowd/tests/testthat/
   pkg/Dowd/tests/testthat/testBinomialBacktest.R
Log:
code, documentation and tests for BinomialBacktest

Added: pkg/Dowd/DESCRIPTION
===================================================================
--- pkg/Dowd/DESCRIPTION	                        (rev 0)
+++ pkg/Dowd/DESCRIPTION	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,12 @@
+Package: Dowd
+Type: Package
+Title: R-version of Matlab Toolbox offered in Kevin Dowd's book Measuring Market Risk
+Version: 0.0.1
+Date: 2015-05-24
+Author: Dinesh Acharya <dines.acharya at gmail.com>
+Maintainer: Dinesh Acharya <dines.acharya at gmail.com>
+Description: 
+Depends: R (>= 2.14.0)
+Suggests: PerformanceAnalytics,
+    testthat
+License: GNU Public License

Added: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	                        (rev 0)
+++ pkg/Dowd/NAMESPACE	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,3 @@
+# Generated by roxygen2 (4.1.1): do not edit by hand
+
+export(BinomialBacktest)

Added: pkg/Dowd/R/BinomialBacktest.R
===================================================================
--- pkg/Dowd/R/BinomialBacktest.R	                        (rev 0)
+++ pkg/Dowd/R/BinomialBacktest.R	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,43 @@
+#' Carries out the binomial backtest for a VaR risk measurement model.
+#' 
+#' The basic idea behind binomial backtest (also called basic frequency 
+#' test) is to test whether the observed frequency of losses that exceed VaR is
+#' consistent with the frequency of tail losses predicted by the mode. Binomial
+#' Backtest carries out the binomial backtest for a VaR risk measurement model 
+#' for specified VaR confidence level and for a one-sided alternative 
+#' hypothesis (H1).
+#' 
+#' @param x Number of failures
+#' @param n Number of observations
+#' @param cl Confidence level for VaR
+#' @return Probability that the VaR model is correct
+#' 
+#' @references Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Kupiec, Paul. Techniques for verifying the accuracy of risk measurement
+#' models, Journal of Derivatives, Winter 1995, p. 79. 
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Probability that the VaR model is correct for 3 failures, 100 number
+#'    observations and  95% confidence level
+#'    BinomialBacktest(55, 1000, 0.95)
+#'
+#' @export
+BinomialBacktest <- function (x, n, cl){
+  # Give warning if x>n or cl>100%
+  if(x > n | cl > 1){
+    warning ("Incorrect parameter list. Make sure that x>n and cl<=100%")
+    return (NaN) 
+  }
+  
+  p <- 1 - cl # Probability of a failure each observation occurs
+  
+  if (x >= n*p){
+    probability.model.is.correct <- 1-pbinom(x-1, n, p)
+  } else {
+    probability.model.is.correct <- pbinom(x, n, p)
+  }
+  return (probability.model.is.correct)
+}

Added: pkg/Dowd/man/BinomialBacktest.Rd
===================================================================
--- pkg/Dowd/man/BinomialBacktest.Rd	                        (rev 0)
+++ pkg/Dowd/man/BinomialBacktest.Rd	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,41 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/BinomialBacktest.R
+\name{BinomialBacktest}
+\alias{BinomialBacktest}
+\title{Carries out the binomial backtest for a VaR risk measurement model.}
+\usage{
+BinomialBacktest(x, n, cl)
+}
+\arguments{
+\item{x}{Number of failures}
+
+\item{n}{Number of observations}
+
+\item{cl}{Confidence level for VaR}
+}
+\value{
+Probability that the VaR model is correct
+}
+\description{
+The basic idea behind binomial backtest (also called basic frequency
+test) is to test whether the observed frequency of losses that exceed VaR is
+consistent with the frequency of tail losses predicted by the mode. Binomial
+Backtest carries out the binomial backtest for a VaR risk measurement model
+for specified VaR confidence level and for a one-sided alternative
+hypothesis (H1).
+}
+\examples{
+# Probability that the VaR model is correct for 3 failures, 100 number
+   observations and  95\% confidence level
+   BinomialBacktest(55, 1000, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+
+Kupiec, Paul. Techniques for verifying the accuracy of risk measurement
+models, Journal of Derivatives, Winter 1995, p. 79.
+}
+

Added: pkg/Dowd/tests/testthat/testBinomialBacktest.R
===================================================================
--- pkg/Dowd/tests/testthat/testBinomialBacktest.R	                        (rev 0)
+++ pkg/Dowd/tests/testthat/testBinomialBacktest.R	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,11 @@
+test_that("Binomial Backtest Works.",{
+  # Success
+  expect_equal(0.7358, BinomialBacktest(1, 100, 0.99), tolerance=0.001)
+  expect_equal(0.2529, BinomialBacktest(55, 1000, 0.95), tolerance=0.001)
+  
+  # Warnings
+  expect_warning(val <- BinomialBacktest(35,30,0.95))
+  expect_true(is.nan(val))
+  expect_warning(val <- BinomialBacktest(5,30,1.5))
+  expect_true(is.nan(val))
+})
\ No newline at end of file

Added: pkg/Dowd/tests/testthat.R
===================================================================
--- pkg/Dowd/tests/testthat.R	                        (rev 0)
+++ pkg/Dowd/tests/testthat.R	2015-05-25 12:39:03 UTC (rev 3648)
@@ -0,0 +1,4 @@
+library(testthat)
+library(Dowd)
+
+test_check("Dowd")



More information about the Returnanalytics-commits mailing list