[Returnanalytics-commits] r3684 - pkg/Dowd/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sat Jun 20 13:03:20 CEST 2015


Author: dacharya
Date: 2015-06-20 13:03:19 +0200 (Sat, 20 Jun 2015)
New Revision: 3684

Modified:
   pkg/Dowd/R/ADTestStat.R
   pkg/Dowd/R/BinomialBacktest.R
Log:
Condition requirement for confidence level made more precise

Modified: pkg/Dowd/R/ADTestStat.R
===================================================================
--- pkg/Dowd/R/ADTestStat.R	2015-06-20 10:44:55 UTC (rev 3683)
+++ pkg/Dowd/R/ADTestStat.R	2015-06-20 11:03:19 UTC (rev 3684)
@@ -1,72 +1,75 @@
-#' Plots cumulative density for AD test and computes confidence 
-#' interval for AD test stat.
-#' 
-#' Anderson-Darling(AD) test can be used to carry out distribution equality test and is 
-#' similar to Kolmogorov-Smirnov test. AD test statistic is defined as:
-#' \deqn{A^2=n\int_{-\infty}^{\infty}\frac{[\hat{F}(x)-F(x)]^2}{F(x)[1-F(x)]}dF(x)}
-#' which is equivalent to
-#' \deqn{=-n-\frac{1}{n}\sum_{i=1}^n(2i-1)[\ln F(X_i)+\ln(1-F(X_{n+1-i}))]}
-#' 
-#' @param number.trials Number of trials
-#' @param sample.size Sample size
-#' @param confidence.interval Confidence Interval
-#' @return Confidence Interval for AD test statistic
-#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
-#' 
-#' Anderson, T.W. and Darling, D.A. Asymptotic Theory of Certain Goodness of
-#' Fit Criteria Based on Stochastic Processes, The Annals of Mathematical
-#' Statistics, 23(2), 1952, p. 193-212.
-#' 
-#' Kvam, P.H. and Vidakovic, B. Nonparametric Statistics with Applications to
-#' Science and Engineering, Wiley, 2007.
-#' 
-#' @author Dinesh Acharya
-#' @examples
-#' 
-#'    # Probability that the VaR model is correct for 3 failures, 100 number
-#'    # observations and  95% confidence level
-#'    ADTestStat(1000, 100, 0.95)
-#'
-#' @export
-ADTestStat <- function(number.trials, sample.size, confidence.interval){
-  
-  if (confidence.interval>1){
-    stop("Confidence Interval should be less than 1.")
-  }
-  
-  m <- number.trials
-  n <- sample.size
-  
-  # Random Number Generation
-  data <- matrix(rnorm(m*n), m, n)
-  
-  # Initialize vectors
-  term <- double(n)
-  AD.test.stat <- double(m)
-  
-  # Compute AD test statistic
-  for (i in 1:m) {
-    trial.sample <- data[i, ]
-    ordered.trial.sample <- sort(trial.sample)
-    for (j in 1:n) {
-      term[j] <- (2*j-1)*(log(pnorm(ordered.trial.sample[j],0,1))-
-                            log(1-pnorm(ordered.trial.sample[n+1-j],0,1)));
-    }
-    AD.test.stat[i] <- -n-mean(term)
-  }
-  AD.test.stat <- sort(AD.test.stat)
-  
-  # Obtain confidence interval
-  lower.bound.index <- round(m*(1-confidence.interval)/2)
-  upper.bound.index <- round(m* (confidence.interval+(1-confidence.interval)/2))
-  confidence.interval.for.AD.test.stat <- c(AD.test.stat[lower.bound.index], 
-                                            AD.test.stat[upper.bound.index])
-  # Plot the graph
-  cdf <- seq(1/m, 1, 1/m)
-  plot(AD.test.stat, cdf, col="red", type="l", 
-       main="Cumulative density for AD test statistic", 
-       xlab="AD test statistic", ylab="Cumulative probability")
-  
-  return(confidence.interval.for.AD.test.stat)
-  
-}
+#' Plots cumulative density for AD test and computes confidence 
+#' interval for AD test stat.
+#' 
+#' Anderson-Darling(AD) test can be used to carry out distribution equality test and is 
+#' similar to Kolmogorov-Smirnov test. AD test statistic is defined as:
+#' \deqn{A^2=n\int_{-\infty}^{\infty}\frac{[\hat{F}(x)-F(x)]^2}{F(x)[1-F(x)]}dF(x)}
+#' which is equivalent to
+#' \deqn{=-n-\frac{1}{n}\sum_{i=1}^n(2i-1)[\ln F(X_i)+\ln(1-F(X_{n+1-i}))]}
+#' 
+#' @param number.trials Number of trials
+#' @param sample.size Sample size
+#' @param confidence.interval Confidence Interval
+#' @return Confidence Interval for AD test statistic
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Anderson, T.W. and Darling, D.A. Asymptotic Theory of Certain Goodness of
+#' Fit Criteria Based on Stochastic Processes, The Annals of Mathematical
+#' Statistics, 23(2), 1952, p. 193-212.
+#' 
+#' Kvam, P.H. and Vidakovic, B. Nonparametric Statistics with Applications to
+#' Science and Engineering, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Probability that the VaR model is correct for 3 failures, 100 number
+#'    # observations and  95% confidence level
+#'    ADTestStat(1000, 100, 0.95)
+#'
+#' @export
+ADTestStat <- function(number.trials, sample.size, confidence.interval){
+  
+  if (confidence.interval >= 1){
+    stop("Confidence Interval should be less than 1.")
+  }
+  if (confidence.interval <= 1){
+    stop("Confidence Interval should be positive.")
+  }
+  
+  m <- number.trials
+  n <- sample.size
+  
+  # Random Number Generation
+  data <- matrix(rnorm(m*n), m, n)
+  
+  # Initialize vectors
+  term <- double(n)
+  AD.test.stat <- double(m)
+  
+  # Compute AD test statistic
+  for (i in 1:m) {
+    trial.sample <- data[i, ]
+    ordered.trial.sample <- sort(trial.sample)
+    for (j in 1:n) {
+      term[j] <- (2*j-1)*(log(pnorm(ordered.trial.sample[j],0,1))-
+                            log(1-pnorm(ordered.trial.sample[n+1-j],0,1)));
+    }
+    AD.test.stat[i] <- -n-mean(term)
+  }
+  AD.test.stat <- sort(AD.test.stat)
+  
+  # Obtain confidence interval
+  lower.bound.index <- round(m*(1-confidence.interval)/2)
+  upper.bound.index <- round(m* (confidence.interval+(1-confidence.interval)/2))
+  confidence.interval.for.AD.test.stat <- c(AD.test.stat[lower.bound.index], 
+                                            AD.test.stat[upper.bound.index])
+  # Plot the graph
+  cdf <- seq(1/m, 1, 1/m)
+  plot(AD.test.stat, cdf, col="red", type="l", 
+       main="Cumulative density for AD test statistic", 
+       xlab="AD test statistic", ylab="Cumulative probability")
+  
+  return(confidence.interval.for.AD.test.stat)
+  
+}

Modified: pkg/Dowd/R/BinomialBacktest.R
===================================================================
--- pkg/Dowd/R/BinomialBacktest.R	2015-06-20 10:44:55 UTC (rev 3683)
+++ pkg/Dowd/R/BinomialBacktest.R	2015-06-20 11:03:19 UTC (rev 3684)
@@ -27,9 +27,8 @@
 #' @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) 
+  if(x > n | cl >= 1 | cl <= 0){
+    stop ("Incorrect parameter list. Make sure that x>n and 0<cl<1")
   }
   
   p <- 1 - cl # Probability of a failure each observation occurs



More information about the Returnanalytics-commits mailing list