[Returnanalytics-commits] r3668 - in pkg/Dowd: . R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 10 15:06:29 CEST 2015
Author: peter_carl
Date: 2015-06-10 15:06:29 +0200 (Wed, 10 Jun 2015)
New Revision: 3668
Modified:
pkg/Dowd/
pkg/Dowd/R/ADTestStat.R
Log:
- fixed comment that caused error during check
Property changes on: pkg/Dowd
___________________________________________________________________
Added: svn:ignore
+ .Rproj.user
.Rhistory
.RData
Modified: pkg/Dowd/R/ADTestStat.R
===================================================================
--- pkg/Dowd/R/ADTestStat.R 2015-06-09 21:16:46 UTC (rev 3667)
+++ pkg/Dowd/R/ADTestStat.R 2015-06-10 13:06:29 UTC (rev 3668)
@@ -1,72 +1,72 @@
-#' 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
-#' @param sample.size
-#' @param 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
+#' @param sample.size
+#' @param 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)
+
+}
More information about the Returnanalytics-commits
mailing list