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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon May 25 22:30:10 CEST 2015


Author: dacharya
Date: 2015-05-25 22:30:10 +0200 (Mon, 25 May 2015)
New Revision: 3649

Added:
   pkg/Dowd/R/ADTestStat.R
   pkg/Dowd/man/ADTestStat.Rd
Modified:
   pkg/Dowd/NAMESPACE
Log:
ADTestStat: code and documentation.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-05-25 12:39:03 UTC (rev 3648)
+++ pkg/Dowd/NAMESPACE	2015-05-25 20:30:10 UTC (rev 3649)
@@ -1,3 +1,4 @@
 # Generated by roxygen2 (4.1.1): do not edit by hand
 
+export(ADTestStat)
 export(BinomialBacktest)

Added: pkg/Dowd/R/ADTestStat.R
===================================================================
--- pkg/Dowd/R/ADTestStat.R	                        (rev 0)
+++ pkg/Dowd/R/ADTestStat.R	2015-05-25 20:30:10 UTC (rev 3649)
@@ -0,0 +1,72 @@
+#' Plots cumulative density for Anderson-Darling test and computes confidence 
+#' interval Anderson-Darling test stat.
+#' 
+#' 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 can be simplified 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 
+#' @param sample.size
+#' @param confidence.interval
+#' @return Confidence Interval for AD test statistic
+#' @references Dowd, Kevin. 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.KS.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.KS.test.stat)
+  
+}

Added: pkg/Dowd/man/ADTestStat.Rd
===================================================================
--- pkg/Dowd/man/ADTestStat.Rd	                        (rev 0)
+++ pkg/Dowd/man/ADTestStat.Rd	2015-05-25 20:30:10 UTC (rev 3649)
@@ -0,0 +1,45 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/ADTestStat.R
+\name{ADTestStat}
+\alias{ADTestStat}
+\title{Plots cumulative density for Anderson-Darling test and computes confidence
+interval Anderson-Darling test stat.}
+\usage{
+ADTestStat(number.trials, sample.size, confidence.interval)
+}
+\arguments{
+\item{number.trials}{}
+
+\item{sample.size}{}
+
+\item{confidence.interval}{}
+}
+\value{
+Confidence Interval for AD test statistic
+}
+\description{
+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 can be simplified to
+\deqn{=-n-\frac{1}{n}\sum_{i=1}^n(2i-1)[\ln F(X_i)+\ln(1-F(X_{n+1-i}))]}
+}
+\examples{
+# Probability that the VaR model is correct for 3 failures, 100 number
+   observations and  95\% confidence level
+   ADTestStat(1000, 100, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, Kevin. 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.
+}
+



More information about the Returnanalytics-commits mailing list