[Returnanalytics-commits] r3653 - in pkg/Dowd: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed May 27 17:49:03 CEST 2015
Author: dacharya
Date: 2015-05-27 17:49:03 +0200 (Wed, 27 May 2015)
New Revision: 3653
Added:
pkg/Dowd/R/ChristoffersenBacktestForIndependence.R
pkg/Dowd/R/ChristoffersenBacktestForUnconditionalCoverage.R
pkg/Dowd/man/ChristoffersenBacktestForUnconditionalCoverage.Rd
Log:
Christoffersen Backtests: source and documentation.
Added: pkg/Dowd/R/ChristoffersenBacktestForIndependence.R
===================================================================
--- pkg/Dowd/R/ChristoffersenBacktestForIndependence.R (rev 0)
+++ pkg/Dowd/R/ChristoffersenBacktestForIndependence.R 2015-05-27 15:49:03 UTC (rev 3653)
@@ -0,0 +1,67 @@
+#' Christoffersen Backtest for Independence
+#'
+#' Carries out the Christoffersen backtest of independence for a VaR risk
+#' measurement model, for specified VaR confidence level.
+#'
+#' @param Ra Vector of portfolio profit and loss observations
+#' @param Rb Vector of corresponding VaR forecasts
+#' @param cl Confidence interval for
+#' @return Probability that given the data set, the null hypothesis
+#' (i.e. independence) is correct.
+#'
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' Christoffersen, P. Evaluating Interval Forecasts. International Economic
+#' Review, 39(4), 1992, 841-862.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' # To be added
+#'
+#' @export
+BlancoIhleBacktest <- function(Ra, Rb, cl){
+
+ profit.loss <- as.vector(Ra)
+ VaR <- as.vector(Ra)
+
+ n <- length(profit.loss)
+ p <- 1-cl
+ excess.loss <- -profit.loss-VaR # Derives excess loss
+ excess.loss <- excess.loss[excess.loss>0] # Gets rid of negative or zeros
+ ##########################################
+ # Read Documentation/Alternative Implementation.
+ # VaR <- VaR[excess.loss>0]
+ ##########################################
+ t00 <- 0
+ t01 <- 0
+ t10 <- 0
+ t11 <- 0
+ for (i in 2:n){
+ if(excess.loss[i]<=0){
+ if(excess.loss[i-1]<=0){
+ t00 <- t00+1;
+ } else {
+ t10 <- t10+1
+ }
+ } else {
+ if(excess.loss[i-1]<=0){
+ t01 <- t01+1
+ } else {
+ t11 <- t11+1
+ }
+ }
+ }
+
+ # Recover pie terms
+ pie0 <- t01/(t00+t01)
+ pie1 <- t11/(t10+t11)
+
+ # Likelihood ratio test statistic
+ LR=-2*log((((1-p)^(T00+T10))*(p^(T01+T11)))+2*log((((1-pie0)^T00))*(pie0^T01)*((1-pie1)^(T10))*pie1^T11))
+
+ # Probability that null hypothesis (independence is correct)
+ y <- 1-pchisq(LR,1)
+ return(y)
+
+}
\ No newline at end of file
Added: pkg/Dowd/R/ChristoffersenBacktestForUnconditionalCoverage.R
===================================================================
--- pkg/Dowd/R/ChristoffersenBacktestForUnconditionalCoverage.R (rev 0)
+++ pkg/Dowd/R/ChristoffersenBacktestForUnconditionalCoverage.R 2015-05-27 15:49:03 UTC (rev 3653)
@@ -0,0 +1,37 @@
+#' Christoffersen Backtest for Unconditional Coverage
+#'
+#' Carries out the Christiffersen backtest for unconditional coverage for a
+#' VaR risk measurement model, for specified VaR confidence level.
+#'
+#' @param Ra Vector of portfolio profit and loss observations
+#' @param Rb Vector of VaR forecasts corresponding to PandL observations
+#' @param cl Confidence level for VaR
+#' @return Probability, given the data set, that the null hypothesis (i.e.
+#' a correct model) is correct.
+#'
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' Christoffersen, P. Evaluating interval forecasts. International Economic
+#' Review, 39(4), 1998, 841-862.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' # To be added
+#'
+#' @export
+ChristoffersenBacktestForUnconditionalCoverage <- function(Ra, Rb, cl){
+
+ profit.loss <- as.vector(Ra)
+ VaR <- as.vector(Rb)
+
+ n <- length(profit.loss) # Number of observations
+ p <- 1-cl # Probability of failure under null hypothesis
+ v <- length(which(VaR+profit.loss<0)) # Frequency of failures
+
+ # Likelihood ratio test statistic
+ LR <- -2*log(((p^x)*(1-p)^(n-x))/((phat^x)*((1-phat)^(n-x))))
+
+ # Probability that null hypothesis (model) is correct
+ return(1-pchisq(LR,1))
+
+}
\ No newline at end of file
Added: pkg/Dowd/man/ChristoffersenBacktestForUnconditionalCoverage.Rd
===================================================================
--- pkg/Dowd/man/ChristoffersenBacktestForUnconditionalCoverage.Rd (rev 0)
+++ pkg/Dowd/man/ChristoffersenBacktestForUnconditionalCoverage.Rd 2015-05-27 15:49:03 UTC (rev 3653)
@@ -0,0 +1,36 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/ChristoffersenBacktestForUnconditionalCoverage.R
+\name{ChristoffersenBacktestForUnconditionalCoverage}
+\alias{ChristoffersenBacktestForUnconditionalCoverage}
+\title{Christoffersen Backtest for Unconditional Coverage}
+\usage{
+ChristoffersenBacktestForUnconditionalCoverage(Ra, Rb, cl)
+}
+\arguments{
+\item{Ra}{Vector of portfolio profit and loss observations}
+
+\item{Rb}{Vector of VaR forecasts corresponding to PandL observations}
+
+\item{cl}{Confidence level for VaR}
+}
+\value{
+Probability, given the data set, that the null hypothesis (i.e.
+a correct model) is correct.
+}
+\description{
+Carries out the Christiffersen backtest for unconditional coverage for a
+VaR risk measurement model, for specified VaR confidence level.
+}
+\examples{
+# To be added
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Christoffersen, P. Evaluating interval forecasts. International Economic
+Review, 39(4), 1998, 841-862.
+}
+
More information about the Returnanalytics-commits
mailing list