[Returnanalytics-commits] r3650 - in pkg/Dowd: . R man tests/testthat
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue May 26 21:53:33 CEST 2015
Author: dacharya
Date: 2015-05-26 21:53:32 +0200 (Tue, 26 May 2015)
New Revision: 3650
Added:
pkg/Dowd/R/JarqueBeraBacktest.R
pkg/Dowd/R/KSTestStat.R
pkg/Dowd/R/KuiperTestStat.R
pkg/Dowd/man/Dowd-package.Rd
pkg/Dowd/man/JarqueBeraBacktest.Rd
pkg/Dowd/man/KSTestStat.Rd
pkg/Dowd/man/KuiperTestStat.Rd
pkg/Dowd/tests/testthat/testJarqueBeraBacktest.R
Modified:
pkg/Dowd/DESCRIPTION
pkg/Dowd/NAMESPACE
pkg/Dowd/R/ADTestStat.R
pkg/Dowd/man/ADTestStat.Rd
Log:
KSTestStat: source and documentation.
KuiperTestStat: source and documentation.
JarqueBeraBacktest: source, documentation and test.
Modified: pkg/Dowd/DESCRIPTION
===================================================================
--- pkg/Dowd/DESCRIPTION 2015-05-25 20:30:10 UTC (rev 3649)
+++ pkg/Dowd/DESCRIPTION 2015-05-26 19:53:32 UTC (rev 3650)
@@ -1,7 +1,7 @@
Package: Dowd
Type: Package
Title: R-version of Matlab Toolbox offered in Kevin Dowd's book Measuring Market Risk
-Version: 0.0.1
+Version: 0.1
Date: 2015-05-24
Author: Dinesh Acharya <dines.acharya at gmail.com>
Maintainer: Dinesh Acharya <dines.acharya at gmail.com>
Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE 2015-05-25 20:30:10 UTC (rev 3649)
+++ pkg/Dowd/NAMESPACE 2015-05-26 19:53:32 UTC (rev 3650)
@@ -2,3 +2,6 @@
export(ADTestStat)
export(BinomialBacktest)
+export(JarqueBeraBacktest)
+export(KSTestStat)
+export(KuiperTestStat)
Modified: pkg/Dowd/R/ADTestStat.R
===================================================================
--- pkg/Dowd/R/ADTestStat.R 2015-05-25 20:30:10 UTC (rev 3649)
+++ pkg/Dowd/R/ADTestStat.R 2015-05-26 19:53:32 UTC (rev 3650)
@@ -1,17 +1,17 @@
-#' Plots cumulative density for Anderson-Darling test and computes confidence
-#' interval Anderson-Darling test stat.
+#' Plots cumulative density for AD test and computes confidence
+#' interval for AD test stat.
#'
-#' AD test can be used to carry out distribution equality test and is
+#' 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 can be simplified to
+#' 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, Kevin. Measuring Market Risk, Wiley, 2007.
+#' @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
@@ -45,10 +45,10 @@
AD.test.stat <- double(m)
# Compute AD test statistic
- for (i in 1:m){
+ for (i in 1:m) {
trial.sample <- data[i, ]
ordered.trial.sample <- sort(trial.sample)
- for (j in 1:n){
+ 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)));
}
@@ -59,7 +59,7 @@
# 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],
+ 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)
@@ -67,6 +67,6 @@
main="Cumulative density for AD test statistic",
xlab="AD test statistic", ylab="Cumulative probability")
- return(confidence.interval.for.KS.test.stat)
+ return(confidence.interval.for.AD.test.stat)
}
Added: pkg/Dowd/R/JarqueBeraBacktest.R
===================================================================
--- pkg/Dowd/R/JarqueBeraBacktest.R (rev 0)
+++ pkg/Dowd/R/JarqueBeraBacktest.R 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,36 @@
+#' Jarque-Bera backtest for normality.
+#'
+#' Jarque-Bera (JB) is a backtest to test whether the skewness and kurtosis of a
+#' given sample matches that of normal distribution. JB test statistic is
+#' defined as \deqn{JB=\frac{n}{6}\left(s^2+\frac{(k-3)^2}{4}\right)} where
+#' \eqn{n} is sample size, \eqn{s} and \eqn{k} are coefficients of sample
+#' skewness and kurtosis.
+#'
+#' @param sample.skewness Coefficient of Skewness of the sample
+#' @param sample.kurtosis Coefficient of Kurtosis of the sample
+#' @param n Number of observations
+#' @return Probability of null hypothesis H0
+#'
+#' @references Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+#'
+#' Jarque, C. M. and Bera, A. K. A test for normality of observations and
+#' regression residuals, International Statistical Review, 55(2): 163-172.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # JB test statistic for sample with 500 observations with sample
+#' skewness and kurtosis of -0.075 and 2.888
+#' JarqueBeraBacktest(-0.075,2.888,500)
+#'
+#' @export
+JarqueBeraBacktest <- function(sample.skewness, sample.kurtosis, n){
+ s <- sample.skewness
+ k <- sample.kurtosis
+ jb.test.stat <- (n/6)*(s^2+((k-3)^2)/4)
+ # Corresponding cdf-value for a chi-squared distribution with two degrees of
+ # freedom
+ prob.value.of.null <- 1-pchisq(jb.test.stat, 2)
+ return(prob.value.of.null)
+}
\ No newline at end of file
Added: pkg/Dowd/R/KSTestStat.R
===================================================================
--- pkg/Dowd/R/KSTestStat.R (rev 0)
+++ pkg/Dowd/R/KSTestStat.R 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,66 @@
+#' Plots cumulative density for KS test and computes confidence interval for
+#' KS test stat.
+#'
+#' Kolmogorov-Smirnov (KS) test statistic is a non parametric test for
+#' distribution equality and measures the maximum distance between two cdfs.
+#' Formally, the KS test statistic is : \deqn{D=\max_i|F(X_i)-\hat{F}(X_i)|}
+#'
+#' @param number.trials Number of trials
+#' @param sample.size Sizes of the trial samples
+#' @param confidence.interval Confidence interval expressed as a fraction of 1
+#' @return Confidence Interval for KS test stat
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' Chakravarti, I. M., Laha, R. G. and Roy, J. Handbook of Methods of #' Applied Statistics, Volume 1, Wiley, 1967.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # Plots the cdf for KS Test statistic and returns KS confidence interval
+#' for 100 trials with 1000 sample size and 0.95 confidence interval
+#' KSTestStat(100, 1000, 0.95)
+#'
+#' @export
+KSTestStat <- function(number.trials, sample.size, confidence.interval){
+
+ if (confidence.interval>1){
+ stop("Confidence Interval should be less than 1.")
+ }
+
+ # Read back input parameters
+ m <- number.trials
+ n <- sample.size
+ # Random number generator
+ data <- matrix(rnorm(m*n), m, n)
+
+ # Initialize vectors
+ cdf.diff <- double(n)
+ max.diff <- double(m)
+
+ # Compute KS Test Statistic
+ for (i in 1:m) {
+ trial.sample <- data[i, ]
+ ordered.trial.sample <- sort(trial.sample)
+ for (j in 1:n) {
+ cdf.diff[j] <- j/n-pnorm(ordered.trial.sample[j],0,1)
+ }
+ max.diff[i] <- max(abs(cdf.diff))
+ }
+ max.diff <- sort(max.diff)
+
+ # 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(max.diff[lower.bound.index],
+ max.diff[upper.bound.index])
+
+ # Plot
+ cdf <- seq(1/m, 1, 1/m)
+ plot(max.diff, cdf, col="red", type="l",
+ main="Cumulative Density for KS test statistic",
+ xlab="KS test statistic", ylab="Cumulative probability")
+
+ return(confidence.interval.for.KS.test.stat)
+}
\ No newline at end of file
Added: pkg/Dowd/R/KuiperTestStat.R
===================================================================
--- pkg/Dowd/R/KuiperTestStat.R (rev 0)
+++ pkg/Dowd/R/KuiperTestStat.R 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,67 @@
+#' Plots cummulative density for Kuiper test and computes confidence interval
+#' for Kuiper test stat.
+#'
+#' Kuiper test statistic is a non parametric test for
+#' distribution equality and is closely related to KS test. Formally, the
+#' Kuiper test statistic is :
+#' \deqn{D*=\max_i\{F(X_i)-\hat{F(x_i)}+\max_i\{\hat{F}(X_i)-F(X_i)\}}
+#'
+#' @param number.trials Number of trials
+#' @param sample.size Sizes of the trial samples
+#' @param confidence.interval Confidence interval expressed as a fraction of 1
+#' @return Confidence Interval for KS test stat
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#'
+#' @author Dinesh Acharya
+#' @examples
+#'
+#' # Plots the cdf for Kuiper Test statistic and returns Kuiper confidence
+#' interval for 100 trials with 1000 sample size and 0.95 confidence
+#' interval.
+#' KuiperTestStat(100, 1000, 0.95)
+#'
+#' @export
+KuiperTestStat <- function(number.trials, sample.size, confidence.interval){
+
+ if (confidence.interval>1){
+ stop("Confidence Interval should be less than 1.")
+ }
+
+ # Read back input parameters
+ m <- number.trials
+ n <- sample.size
+
+ # Random number generator
+ data <- matrix(rnorm(m*n), m, n)
+
+ # Initialize vectors
+ cdf.diff <- double(n)
+ Kuiper.test.stat <- double(m)
+
+ # Compute KS Test Statistic
+ for (i in 1:m) {
+ trial.sample <- data[i, ]
+ ordered.trial.sample <- sort(trial.sample)
+ for (j in 1:n) {
+ cdf.diff[j] <- j/n-pnorm(ordered.trial.sample[j],0,1)
+ }
+ Kuiper.test.stat[i] <- max(cdf.diff)-min(cdf.diff)
+ }
+ Kuiper.test.stat <- sort(Kuiper.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.Kuiper.test.stat <- c(Kuiper.test.stat[lower.bound.index],
+ Kuiper.test.stat[upper.bound.index])
+
+ # Plot
+ cdf <- seq(1/m, 1, 1/m)
+ plot(Kuiper.test.stat, cdf, col="red", type="l",
+ main="Cumulative density for Kuiper test statistic",
+ xlab="Kuiper test statistic", ylab="Cumulative probability")
+
+ return(confidence.interval.for.Kuiper.test.stat)
+}
\ No newline at end of file
Modified: pkg/Dowd/man/ADTestStat.Rd
===================================================================
--- pkg/Dowd/man/ADTestStat.Rd 2015-05-25 20:30:10 UTC (rev 3649)
+++ pkg/Dowd/man/ADTestStat.Rd 2015-05-26 19:53:32 UTC (rev 3650)
@@ -2,8 +2,8 @@
% 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.}
+\title{Plots cumulative density for AD test and computes confidence
+interval for AD test stat.}
\usage{
ADTestStat(number.trials, sample.size, confidence.interval)
}
@@ -18,10 +18,10 @@
Confidence Interval for AD test statistic
}
\description{
-AD test can be used to carry out distribution equality test and is
+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 can be simplified to
+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}))]}
}
\examples{
@@ -33,7 +33,7 @@
Dinesh Acharya
}
\references{
-Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+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
Added: pkg/Dowd/man/Dowd-package.Rd
===================================================================
--- pkg/Dowd/man/Dowd-package.Rd (rev 0)
+++ pkg/Dowd/man/Dowd-package.Rd 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,29 @@
+\name{Dowd-package}
+\alias{Dowd-package}
+\docType{package}
+\title{
+R-version of Kevin Dowd's MATLAB Toolbox from book "Measuring Market Risk".
+}
+
+\description{
+\kbd{Dowd} Kevin Dowd's book "Measuring Market Risk" gives overview of risk measurement procedures with focus on Value at Risk (VaR) and Expected Shortfall (ES).
+}
+\author{
+Dinesh Acharya \cr
+
+Maintainer: Dinesh Acharya \email{dines.acharya at gmail.com}
+}
+
+\references{
+
+Dowd, K. \emph{Measuring Market Risk}. Wiley. 2005.
+
+}
+
+\section{Acknowledgments}{
+Without Kevin Dowd's book Measuring Market Risk and accompanying MATLAB toolbox, this project would not have been possible.
+
+Peter Carl and Brian G. Peterson deserve special acknowledgement for mentoring me on this project.
+}
+
+\keyword{ package }
\ No newline at end of file
Added: pkg/Dowd/man/JarqueBeraBacktest.Rd
===================================================================
--- pkg/Dowd/man/JarqueBeraBacktest.Rd (rev 0)
+++ pkg/Dowd/man/JarqueBeraBacktest.Rd 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,40 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/JarqueBeraBacktest.R
+\name{JarqueBeraBacktest}
+\alias{JarqueBeraBacktest}
+\title{Jarque-Bera backtest for normality.}
+\usage{
+JarqueBeraBacktest(sample.skewness, sample.kurtosis, n)
+}
+\arguments{
+\item{sample.skewness}{Coefficient of Skewness of the sample}
+
+\item{sample.kurtosis}{Coefficient of Kurtosis of the sample}
+
+\item{n}{Number of observations}
+}
+\value{
+Probability of null hypothesis H0
+}
+\description{
+Jarque-Bera (JB) is a backtest to test whether the skewness and kurtosis of a
+given sample matches that of normal distribution. JB test statistic is
+defined as \deqn{JB=\frac{n}{6}\left(s^2+\frac{(k-3)^2}{4}\right)} where
+\eqn{n} is sample size, \eqn{s} and \eqn{k} are coefficients of sample
+skewness and kurtosis.
+}
+\examples{
+# JB test statistic for sample with 500 observations with sample
+ skewness and kurtosis of -0.075 and 2.888
+ JarqueBeraBacktest(-0.075,2.888,500)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, Kevin. Measuring Market Risk, Wiley, 2007.
+
+Jarque, C. M. and Bera, A. K. A test for normality of observations and
+regression residuals, International Statistical Review, 55(2): 163-172.
+}
+
Added: pkg/Dowd/man/KSTestStat.Rd
===================================================================
--- pkg/Dowd/man/KSTestStat.Rd (rev 0)
+++ pkg/Dowd/man/KSTestStat.Rd 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,38 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/KSTestStat.R
+\name{KSTestStat}
+\alias{KSTestStat}
+\title{Plots cumulative density for KS test and computes confidence interval for
+KS test stat.}
+\usage{
+KSTestStat(number.trials, sample.size, confidence.interval)
+}
+\arguments{
+\item{number.trials}{Number of trials}
+
+\item{sample.size}{Sizes of the trial samples}
+
+\item{confidence.interval}{Confidence interval expressed as a fraction of 1}
+}
+\value{
+Confidence Interval for KS test stat
+}
+\description{
+Kolmogorov-Smirnov (KS) test statistic is a non parametric test for
+distribution equality and measures the maximum distance between two cdfs.
+Formally, the KS test statistic is : \deqn{D=\max_i|F(X_i)-\hat{F}(X_i)|}
+}
+\examples{
+# Plots the cdf for KS Test statistic and returns KS confidence interval
+ for 100 trials with 1000 sample size and 0.95 confidence interval
+ KSTestStat(100, 1000, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Chakravarti, I. M., Laha, R. G. and Roy, J. Handbook of Methods of #' Applied Statistics, Volume 1, Wiley, 1967.
+}
+
Added: pkg/Dowd/man/KuiperTestStat.Rd
===================================================================
--- pkg/Dowd/man/KuiperTestStat.Rd (rev 0)
+++ pkg/Dowd/man/KuiperTestStat.Rd 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,38 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/KuiperTestStat.R
+\name{KuiperTestStat}
+\alias{KuiperTestStat}
+\title{Plots cummulative density for Kuiper test and computes confidence interval
+for Kuiper test stat.}
+\usage{
+KuiperTestStat(number.trials, sample.size, confidence.interval)
+}
+\arguments{
+\item{number.trials}{Number of trials}
+
+\item{sample.size}{Sizes of the trial samples}
+
+\item{confidence.interval}{Confidence interval expressed as a fraction of 1}
+}
+\value{
+Confidence Interval for KS test stat
+}
+\description{
+Kuiper test statistic is a non parametric test for
+distribution equality and is closely related to KS test. Formally, the
+Kuiper test statistic is :
+ \deqn{D*=\max_i\{F(X_i)-\hat{F(x_i)}+\max_i\{\hat{F}(X_i)-F(X_i)\}}
+}
+\examples{
+# Plots the cdf for Kuiper Test statistic and returns Kuiper confidence
+ interval for 100 trials with 1000 sample size and 0.95 confidence
+ interval.
+ KuiperTestStat(100, 1000, 0.95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+
Added: pkg/Dowd/tests/testthat/testJarqueBeraBacktest.R
===================================================================
--- pkg/Dowd/tests/testthat/testJarqueBeraBacktest.R (rev 0)
+++ pkg/Dowd/tests/testthat/testJarqueBeraBacktest.R 2015-05-26 19:53:32 UTC (rev 3650)
@@ -0,0 +1,5 @@
+test_that("Binomial Backtest Works.",{
+ # Success
+ expect_equal(0.6942, JarqueBeraBacktest(-0.0758, 2.8888, 500), tolerance=0.01)
+ expect_equal(1, JarqueBeraBacktest(0, 3, 100), tolerance=0.01)
+})
\ No newline at end of file
More information about the Returnanalytics-commits
mailing list