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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Jun 1 23:12:22 CEST 2015


Author: dacharya
Date: 2015-06-01 23:12:22 +0200 (Mon, 01 Jun 2015)
New Revision: 3659

Added:
   pkg/Dowd/R/CdfOfSumUsingGumbelCopula.R
   pkg/Dowd/R/GumbelCopulaVaR.R
   pkg/Dowd/man/CdfOfSumUsingGumbelCopula.Rd
   pkg/Dowd/man/GumbelCopulaVaR.Rd
Modified:
   pkg/Dowd/NAMESPACE
   pkg/Dowd/R/BootstrapVaR.R
   pkg/Dowd/R/BootstrapVaRConfInterval.R
   pkg/Dowd/R/BootstrapVaRFigure.R
   pkg/Dowd/R/HSVaR.R
   pkg/Dowd/man/BootstrapESFigure.Rd
   pkg/Dowd/man/BootstrapVaR.Rd
   pkg/Dowd/man/BootstrapVarConfInterval.Rd
   pkg/Dowd/man/HSVaR.Rd
   pkg/Dowd/readme.txt
Log:
GumbelCopulaVaR and CdfOfSumUsingGumbelCopula: source and documentation. Some error exists. HSVaR and dependent functions (BootstrapVaR, BootstrapVaRFigure) are functional.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/NAMESPACE	2015-06-01 21:12:22 UTC (rev 3659)
@@ -7,9 +7,12 @@
 export(BootstrapESConfInterval)
 export(BootstrapESFigure)
 export(BootstrapVaR)
-export(BootstrapVarConfInterval)
+export(BootstrapVaRConfInterval)
+export(BootstrapVaRFigure)
+export(CdfOfSumUsingGumbelCopula)
 export(ChristoffersenBacktestForIndependence)
 export(ChristoffersenBacktestForUnconditionalCoverage)
+export(GumbelCopulaVaR)
 export(HSES)
 export(HSVaR)
 export(JarqueBeraBacktest)

Modified: pkg/Dowd/R/BootstrapVaR.R
===================================================================
--- pkg/Dowd/R/BootstrapVaR.R	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/R/BootstrapVaR.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -18,7 +18,7 @@
 #'    BootstrapES(a, 50, 0.95)
 #'
 #' @export
-BootstrapVaR <- function(Ra, number.sample, cl){
+BootstrapVaR <- function(Ra, number.resamples, cl){
   
   if (nargs() < 3){
     error("Too few arguments")
@@ -37,7 +37,7 @@
   if (length(cl) != 1) {
     error("Confidence level must be a scalar")
   }
-  if (length(number.samples) != 1){
+  if (length(number.resamples) != 1){
     error("Number of resamples must be a scalar");
   }
   

Modified: pkg/Dowd/R/BootstrapVaRConfInterval.R
===================================================================
--- pkg/Dowd/R/BootstrapVaRConfInterval.R	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/R/BootstrapVaRConfInterval.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -21,7 +21,7 @@
 #'    BootstrapVarConfInterval(Ra, 500, 0.95)
 #'
 #' @export
-BootstrapVarConfInterval <- function(Ra, number.resamples, cl){
+BootstrapVaRConfInterval <- function(Ra, number.resamples, cl){
   
   # Determine if there are three arguments
   if (nargs() < 3){

Modified: pkg/Dowd/R/BootstrapVaRFigure.R
===================================================================
--- pkg/Dowd/R/BootstrapVaRFigure.R	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/R/BootstrapVaRFigure.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -20,7 +20,7 @@
 #'    BootstrapESFigure(Ra, 500, 0.95)
 #'
 #' @export
-BootstrapESFigure <- function(Ra, number.resamples, cl){
+BootstrapVaRFigure <- function(Ra, number.resamples, cl){
   
   # Determine if there are three arguments
   if (nargs() < 3){
@@ -67,6 +67,6 @@
   
   # Histogram
   cl.for.label <- 100*cl
-  hist(VaR[1], 30, xlab="VaR", ylab="Frequency", main=paste("Bootstrapped Historical Simulation VaR at", cl, "% Confidence Level"))
+  hist(VaR, 30, xlab="VaR", ylab="Frequency", main=paste("Bootstrapped Historical Simulation VaR at", cl, "% Confidence Level"))
   
 }
\ No newline at end of file

Added: pkg/Dowd/R/CdfOfSumUsingGumbelCopula.R
===================================================================
--- pkg/Dowd/R/CdfOfSumUsingGumbelCopula.R	                        (rev 0)
+++ pkg/Dowd/R/CdfOfSumUsingGumbelCopula.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -0,0 +1,73 @@
+#' Derives prob ( X + Y < quantile) using Gumbel copula
+#' 
+#' If X and Y are position P/Ls, then the VaR is equal to minus quantile. In
+#' such cases, we insert the negative of the VaR as the quantile, and the
+#' function gives us the value of 1 minus VaR confidence level. In other
+#' words, if X and Y are position P/Ls, the quantile is the negative of the
+#' VaR, and the output is 1 minus the VaR confidence level.
+#' 
+#' @param quantile Portfolio quantile (or negative of Var, if X, Y are position P/Ls)
+#' @param mu1 Mean of Profit/Loss on first position 
+#' @param mu2 Mean of Profit/Loss on second position
+#' @param sigma1 Standard Deviation of Profit/Loss on first position
+#' @param sigma2 Standard Deviation of Profit/Loss on second position
+#' @param beta Gumber copula parameter (greater than 1)
+#' @return Probability of X + Y being less than quantile
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Dowd, K. and Fackler, P. Estimating VaR with copulas. Financial Engineering
+#' News, 2004.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Prob ( X + Y < q ) using Gumbel Copula for X with mean 2.3 and std. .2
+#'    # CdfOfSumUsingGumbelCopula(0.9, 2.3, 4.5, 1.2, 1.5, 1.2)
+#'
+#' @export
+CdfOfSumUsingGumbelCopula <- function(quantile, mu1, mu2, sigma1, sigma2, beta){
+  
+  if (beta <= 1) {
+    stop("Beta must be bigger than 1")
+  }
+  # Define w variable
+  w.min <- 0.001
+  w.max <- 0.999 # Specify min and max of w
+  dw <- 0.001 # Specify incremental change in w
+  w <- seq(w.min, w.max, dw) # Define w-variable going from min to max in units of size dw
+  
+  # Obtain 'first' and 'second' sets of copula values corresponding to 
+  # values of w (NB: The difference between the 'first' and 'second' goes 
+  # into the calculation of the approximate copula differential)
+  first.copula <- double(length(w)) # initialize variables
+  second.copula <- double(length(w))
+  approximate.copula.differential <- double(length(w))
+  for (i in 1:length(w)) {
+    first.copula[i] <- GumbelCopula(w[i], 
+                                    pnorm(quantile - qnorm(w[i], mu1, sigma1), mu2, sigma2), 
+                                    beta)
+  }
+  for (i in 2:length(w)) {
+    first.copula[i] <- GumbelCopula(w[i] - dw, 
+                                    pnorm(quantile - qnorm(w[i], mu1, sigma1), mu2, sigma2), 
+                                    beta)
+  }
+  
+  # Obtain approximate copula differentials from the above
+  for (i in 2:length(w)){
+    approximate.copula.differential[i] <- (first.copula[i]-second.copula[i])/dw
+  }
+  
+  y<- sum(approximate.copula.differential) * dw
+  
+}
+
+GumbelCopula <- function(u, v, beta){
+  # Derives value of Gumbel Copula
+  # u is value of first marginal for random variable X
+  # v is value of second marginal for random variable Y
+  # beta parameter for Gumbel Copula
+  y <- exp(-((-log(u))^beta + (-log(v))^beta))^(1/beta);
+  return(y)
+  
+}
\ No newline at end of file

Added: pkg/Dowd/R/GumbelCopulaVaR.R
===================================================================
--- pkg/Dowd/R/GumbelCopulaVaR.R	                        (rev 0)
+++ pkg/Dowd/R/GumbelCopulaVaR.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -0,0 +1,68 @@
+#' Bivariate Gumbel Copule VaR
+#' 
+#' Derives VaR using bivariate Gumbel or logistic copula with specified inputs 
+#' for normal marginals.
+#' 
+#' @param mu1 Mean of Profit/Loss on first position 
+#' @param mu2 Mean of Profit/Loss on second position
+#' @param sigma1 Standard Deviation of Profit/Loss on first position
+#' @param sigma2 Standard Deviation of Profit/Loss on second position
+#' @param beta Gumber copula parameter (greater than 1)
+#' @param cl VaR onfidece level
+#' @return Copula based VaR
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' Dowd, K. and Fackler, P. Estimating VaR with copulas. Financial Engineering
+#' News, 2004.
+#'
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # VaR using bivariate Gumbel for X and Y with given parameters:
+#'    GumbelCopulaVaR(2.3, 4.1, 1.2, 1.5, 1.2, .95)
+#'
+#' @export
+GumbelCopulaVaR <- function(mu1, mu2, sigma1, sigma2, beta, cl){
+  
+  if (beta <= 1) {
+    stop("Beta must be bigger than 1")
+  }
+  
+  p <- 1 - cl # p is tail probability or cdf
+  
+  # Compute portfolio mean and sigma (NB: These are used here to help compute
+  # initial bounds automatically)
+  portfolio.mu <- mu1 + mu2
+  portfolio.variance <- sigma1^2+sigma2^2
+  portfolio.sigma <- sqrt(portfolio.variance)
+  
+  # Specify bounds arbitrarily (NB: Would need to change manually if these were
+  # inappropriate)
+  L <- -portfolio.mu - 5 * portfolio.sigma
+  fL <- CdfOfSumUsingGumbelCopula(L, mu1, mu2, sigma1, sigma2, beta) - p
+  sign.fL <- sign(fL)
+  U <- -portfolio.mu + 5 *  portfolio.sigma
+  fU <- CdfOfSumUsingGumbelCopula(U, mu1, mu2, sigma1, sigma2, beta) - p
+  sign.fU <- sign(fU)
+  if (sign.fL == sign.fU){
+    stop("Assumed bounds do not include answer")
+  }
+  
+  # Bisection Algorithm
+  tol <- 0.0001 # Tolerance level (NM: change manually if desired)
+  while (U - L > tol){
+    x <- (L + U) / 2 # Bisection carried out in terms of P/L quantiles or minus VaR
+    cum.prob <- CdfOfSumUsingGumbelCopula(x, mu1, mu2, sigma1, sigma2, beta)
+    fx <- cum.prob - p
+    if (sign(fx) == sign(fL)){
+      L <- x
+      fL <- fx
+    } else {
+      U <- x
+      fU <- fx
+    }
+  }
+  y <- -x # VaR is negative of terminal x-value or P/L quantile
+  
+}
\ No newline at end of file

Modified: pkg/Dowd/R/HSVaR.R
===================================================================
--- pkg/Dowd/R/HSVaR.R	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/R/HSVaR.R	2015-06-01 21:12:22 UTC (rev 3659)
@@ -5,7 +5,7 @@
 #' period implies by data frequency.
 #'
 #' @param Ra Vector corresponding to profit and loss distribution
-#' @param Rb Vector corresponding to VaR confidence levels.
+#' @param Rb Scalar corresponding to VaR confidence levels.
 #' @return Value at Risk of the portfolio
 #' 
 #' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
@@ -25,7 +25,9 @@
 #' @author Dinesh Acharya
 #' @examples
 #' 
-#'    # To be added.
+#'    # To be added
+#'    a <- rnorm(1000) # Payoffs of random portfolio
+#'    HSVaR(a, .95)
 #'
 #' @export
 HSVaR <- function(Ra, Rb){
@@ -40,7 +42,6 @@
     stop("Too many arguments")
   }
   
-  
   if (nargs() == 2) {
     profit.loss.data <- as.vector(Ra)
     cl <- as.vector(Rb)
@@ -50,11 +51,10 @@
   }
   
   # Check that inputs have correct dimensions
-  if (is.vector(Rb)) {
+  if (!is.vector(cl)) {
     stop("Confidence level must be a vector")
   }
   
-  
   # Check that inputs obey sign and value restrictions
   if (max(cl) >= 1) {
     stop("Confidence level must be less than 1.")
@@ -68,7 +68,7 @@
   
   # If index value is an integer, VaR follows immediately
   y <- double(length(i))
-  if (index-round[index] == 0){
+  if (index-round(index) == 0){
     y[i] <- losses.data[index]
   }
   

Modified: pkg/Dowd/man/BootstrapESFigure.Rd
===================================================================
--- pkg/Dowd/man/BootstrapESFigure.Rd	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/man/BootstrapESFigure.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -1,12 +1,10 @@
 % Generated by roxygen2 (4.1.1): do not edit by hand
-% Please edit documentation in R/BootstrapESFigure.R, R/BootstrapVaRFigure.R
+% Please edit documentation in R/BootstrapESFigure.R
 \name{BootstrapESFigure}
 \alias{BootstrapESFigure}
 \title{Plots figure of bootstrapped ES}
 \usage{
 BootstrapESFigure(Ra, number.resamples, cl)
-
-BootstrapESFigure(Ra, number.resamples, cl)
 }
 \arguments{
 \item{Ra}{Vector corresponding to profit and loss distribution}
@@ -14,19 +12,10 @@
 \item{cl}{Number corresponding to Expected Shortfall confidence level}
 
 \item{number.resample}{Number of samples to be taken in bootstrap procedure}
-
-\item{Ra}{Vector corresponding to profit and loss distribution}
-
-\item{number.sample}{Number of samples to be taken in bootstrap procedure}
-
-\item{cl}{Number corresponding to Value at Risk confidence level}
 }
 \description{
 Plots figure for the bootstrapped ES, for confidence
 level and holding period implied by data frequency.
-
-Plots figure for the bootstrapped VaR, for confidence
-level and holding period implied by data frequency.
 }
 \examples{
 # To be modified with appropriate data.
@@ -34,20 +23,11 @@
    # confidence interval
    Ra <- rnorm(1000)
    BootstrapESFigure(Ra, 500, 0.95)
-# To be modified with appropriate data.
-   # Estimates 90\% confidence interval for bootstrapped VaR for 95\%
-   # confidence interval
-   Ra <- rnorm(1000)
-   BootstrapESFigure(Ra, 500, 0.95)
 }
 \author{
 Dinesh Acharya
-
-Dinesh Acharya
 }
 \references{
 Dowd, K. Measuring Market Risk, Wiley, 2007.
-
-Dowd, K. Measuring Market Risk, Wiley, 2007.
 }
 

Modified: pkg/Dowd/man/BootstrapVaR.Rd
===================================================================
--- pkg/Dowd/man/BootstrapVaR.Rd	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/man/BootstrapVaR.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -4,7 +4,7 @@
 \alias{BootstrapVaR}
 \title{Bootstrapped VaR for specified confidence level}
 \usage{
-BootstrapVaR(Ra, number.sample, cl)
+BootstrapVaR(Ra, number.resamples, cl)
 }
 \arguments{
 \item{Ra}{Vector corresponding to profit and loss distribution}

Modified: pkg/Dowd/man/BootstrapVarConfInterval.Rd
===================================================================
--- pkg/Dowd/man/BootstrapVarConfInterval.Rd	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/man/BootstrapVarConfInterval.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -1,10 +1,10 @@
 % Generated by roxygen2 (4.1.1): do not edit by hand
 % Please edit documentation in R/BootstrapVaRConfInterval.R
-\name{BootstrapVarConfInterval}
-\alias{BootstrapVarConfInterval}
+\name{BootstrapVaRConfInterval}
+\alias{BootstrapVaRConfInterval}
 \title{Bootstrapped VaR Confidence Interval}
 \usage{
-BootstrapVarConfInterval(Ra, number.resamples, cl)
+BootstrapVaRConfInterval(Ra, number.resamples, cl)
 }
 \arguments{
 \item{Ra}{Vector corresponding to profit and loss distribution}

Added: pkg/Dowd/man/CdfOfSumUsingGumbelCopula.Rd
===================================================================
--- pkg/Dowd/man/CdfOfSumUsingGumbelCopula.Rd	                        (rev 0)
+++ pkg/Dowd/man/CdfOfSumUsingGumbelCopula.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -0,0 +1,45 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/CdfOfSumUsingGumbelCopula.R
+\name{CdfOfSumUsingGumbelCopula}
+\alias{CdfOfSumUsingGumbelCopula}
+\title{Derives prob ( X + Y < quantile) using Gumbel copula}
+\usage{
+CdfOfSumUsingGumbelCopula(quantile, mu1, mu2, sigma1, sigma2, beta)
+}
+\arguments{
+\item{quantile}{Portfolio quantile (or negative of Var, if X, Y are position P/Ls)}
+
+\item{mu1}{Mean of Profit/Loss on first position}
+
+\item{mu2}{Mean of Profit/Loss on second position}
+
+\item{sigma1}{Standard Deviation of Profit/Loss on first position}
+
+\item{sigma2}{Standard Deviation of Profit/Loss on second position}
+
+\item{beta}{Gumber copula parameter (greater than 1)}
+}
+\value{
+Probability of X + Y being less than quantile
+}
+\description{
+If X and Y are position P/Ls, then the VaR is equal to minus quantile. In
+such cases, we insert the negative of the VaR as the quantile, and the
+function gives us the value of 1 minus VaR confidence level. In other
+words, if X and Y are position P/Ls, the quantile is the negative of the
+VaR, and the output is 1 minus the VaR confidence level.
+}
+\examples{
+# Prob ( X + Y < q ) using Gumbel Copula for X with mean 2.3 and std. .2
+   # CdfOfSumUsingGumbelCopula(0.9, 2.3, 4.5, 1.2, 1.5, 1.2)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Dowd, K. and Fackler, P. Estimating VaR with copulas. Financial Engineering
+News, 2004.
+}
+

Added: pkg/Dowd/man/GumbelCopulaVaR.Rd
===================================================================
--- pkg/Dowd/man/GumbelCopulaVaR.Rd	                        (rev 0)
+++ pkg/Dowd/man/GumbelCopulaVaR.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -0,0 +1,42 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/GumbelCopulaVaR.R
+\name{GumbelCopulaVaR}
+\alias{GumbelCopulaVaR}
+\title{Bivariate Gumbel Copule VaR}
+\usage{
+GumbelCopulaVaR(mu1, mu2, sigma1, sigma2, beta, cl)
+}
+\arguments{
+\item{mu1}{Mean of Profit/Loss on first position}
+
+\item{mu2}{Mean of Profit/Loss on second position}
+
+\item{sigma1}{Standard Deviation of Profit/Loss on first position}
+
+\item{sigma2}{Standard Deviation of Profit/Loss on second position}
+
+\item{beta}{Gumber copula parameter (greater than 1)}
+
+\item{cl}{VaR onfidece level}
+}
+\value{
+Copula based VaR
+}
+\description{
+Derives VaR using bivariate Gumbel or logistic copula with specified inputs
+for normal marginals.
+}
+\examples{
+# VaR using bivariate Gumbel for X and Y with given parameters:
+   GumbelCopulaVaR(2.3, 4.1, 1.2, 1.5, 1.2, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+
+Dowd, K. and Fackler, P. Estimating VaR with copulas. Financial Engineering
+News, 2004.
+}
+

Modified: pkg/Dowd/man/HSVaR.Rd
===================================================================
--- pkg/Dowd/man/HSVaR.Rd	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/man/HSVaR.Rd	2015-06-01 21:12:22 UTC (rev 3659)
@@ -9,7 +9,7 @@
 \arguments{
 \item{Ra}{Vector corresponding to profit and loss distribution}
 
-\item{Rb}{Vector corresponding to VaR confidence levels.}
+\item{Rb}{Scalar corresponding to VaR confidence levels.}
 }
 \value{
 Value at Risk of the portfolio
@@ -20,7 +20,9 @@
 period implies by data frequency.
 }
 \examples{
-# To be added.
+# To be added
+   a <- rnorm(1000) # Payoffs of random portfolio
+   HSVaR(a, .95)
 }
 \author{
 Dinesh Acharya

Modified: pkg/Dowd/readme.txt
===================================================================
--- pkg/Dowd/readme.txt	2015-05-29 21:20:41 UTC (rev 3658)
+++ pkg/Dowd/readme.txt	2015-06-01 21:12:22 UTC (rev 3659)
@@ -1,9 +1,12 @@
 #***************************************************************
-# Bootstrap is almost complete. It is still to be tested/debugged. Tests still remaining.
+# Some error is present in GumbelCopulaVaR and needs correction
 #***************************************************************
+# Bootstrap is functional (but HSVaR still does not accept matrix P/L
+# and only still accepts vectors, its needs to be modified)
+#***************************************************************
 # Jarque-Bera Test:
 # It has to be checked Probability of null (H0) or (H1).
-# (http://stats.stackexchange.com/questions/130368/why-do-i-get-this-p-value-doing-the-jarque-bera-test-in-r)
+# 
 #***************************************************************
 # Christofferson Backtest for Independence:
 # VaR(excess_loss<=0)=[]; Does not make sense. It is still to be checked if it is as intended.



More information about the Returnanalytics-commits mailing list