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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 9 23:12:48 CEST 2015


Author: dacharya
Date: 2015-06-09 23:12:47 +0200 (Tue, 09 Jun 2015)
New Revision: 3666

Added:
   pkg/Dowd/R/MEFPlot.R
   pkg/Dowd/R/NormalQQPlot.R
   pkg/Dowd/man/MEFPlot.Rd
   pkg/Dowd/man/NormalQQPlot.Rd
Modified:
   pkg/Dowd/NAMESPACE
Log:
MEFPlot and NormalQQPlot: source and documentation.

Modified: pkg/Dowd/NAMESPACE
===================================================================
--- pkg/Dowd/NAMESPACE	2015-06-09 09:52:29 UTC (rev 3665)
+++ pkg/Dowd/NAMESPACE	2015-06-09 21:12:47 UTC (rev 3666)
@@ -27,6 +27,8 @@
 export(KSTestStat)
 export(KuiperTestStat)
 export(LopezBacktest)
+export(MEFPlot)
+export(NormalQQPlot)
 export(PickandsEstimator)
 export(PickandsPlot)
 export(ProductCopulaVaR)

Added: pkg/Dowd/R/MEFPlot.R
===================================================================
--- pkg/Dowd/R/MEFPlot.R	                        (rev 0)
+++ pkg/Dowd/R/MEFPlot.R	2015-06-09 21:12:47 UTC (rev 3666)
@@ -0,0 +1,33 @@
+#' Mean Excess Function Plot
+#'
+#' Plots mean-excess function values of the data set.
+#'
+#' @param Ra Vector data
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Plots 
+#'    Ra <- rnorm(1000)
+#'    MEFPlot(Ra)
+#'
+#' @export
+MEFPlot <- function(Ra){
+  data <- as.vector(Ra)
+  if (!is.vector(data)) {
+    stop("Input should be a vector data.")
+  }
+  u <- data
+  n <- length(u)
+  mef <- 
+  for (i in 1:n - 1) {
+    data <- data[which(data > u[i])]
+    mef[i] <- mean(data) - u[i]
+  }
+  u <- u[!u==max(u)]
+  plot(u, mef, type = "l", xlab = "Threshold", ylab = "e(u)", 
+       main = "Empirical Mean Excess Function")
+  
+} 
\ No newline at end of file

Added: pkg/Dowd/R/NormalQQPlot.R
===================================================================
--- pkg/Dowd/R/NormalQQPlot.R	                        (rev 0)
+++ pkg/Dowd/R/NormalQQPlot.R	2015-06-09 21:12:47 UTC (rev 3666)
@@ -0,0 +1,126 @@
+#' Normal Quantile Quantile Plot
+#'
+#' Produces an emperical QQ-Plot of the quantiles of the data set 'Ra' versus 
+#' the quantiles of a normal distribution. The purpose of the quantile-quantile plot is to determine whether the sample in 'Ra' is drawn from a normal (i.e., Gaussian) distribution.
+#' 
+#' @param Ra Vector data set
+#' 
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#' 
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # Description of example
+#'    Example
+#'
+#' @export
+NormalQQPlot <- function(Ra){
+  
+  x <- as.vector(Ra)
+  if (!is.vector(x)) {
+    stop("Input should be a vector.")
+  }
+  mu <- mean(x)
+  sigma <- sd(x)
+  y <- sort(x)
+  a <- PlotPos(y)
+  x <- a$pp
+  n <- a$n
+  x <- qnorm(x, mu, sigma) # This is theoretical normal quantile
+  xx <- x
+  yy <- y
+  
+  # Dowd's code has following details but since his code does not work for 
+  # matrices with rows, columns > 1, it is not efficient to have it here.
+  # if(((dim(x)[1] == n) | (dim(x)[1] == 1 & dim(x)[2] == n)) & ~any(is.nan(x))){
+  #  xx <- sort(x)
+  # } else {
+  #   xx <- quantile(x, pvec)[[1]]
+  # }
+  #
+  # if(((dim(y)[1] == n) | (dim(y)[1] == 1 & dim(y)[2] == n)) & ~any(is.nan(y))){
+  #   yy <- sort(y)
+  # } else {
+  #   yy <- quantile(y, pvec)[[1]]
+  # }
+  xx <- sort(x)
+  yy <- sort(y)
+  
+  q1x <- quantile(x, .25)[[1]]
+  q3x <- quantile(x, .75)[[1]]
+  q1y <- quantile(y, .25)[[1]]
+  q3y <- quantile(y, .75)[[1]]
+  qx <- matrix(c(q1x,q3x), 2, length(q1x))
+  qy <- matrix(c(q1y,q3y), 2, length(q1y))
+  
+  dx <- q3x - q1x
+  dy <- q3y - q1y
+  slope <- dy/dx
+  centerx <- (q1x + q3x)/2
+  centery <- (q1y + q3y)/2
+  maxx <- max(x)
+  minx <- min(x)
+  maxy <- centery + slope * (maxx - centerx)
+  miny <- centery - slope * (centerx - minx)
+  
+  mx <- matrix(c(minx,maxx), 2, length(minx))
+  my <- matrix(c(miny,maxy), 2, length(miny))
+  
+  xmin <- min(xx, qx, mx)
+  xmax <- max(xx, qx, mx)
+  ymin <- min(yy, qy, my)
+  ymax <- max(yy, qy, my)
+  
+  plot(xx, yy, type = "p", pch=3, col="red", xlab = "Normal Quantiles", 
+       ylab = "Quantiles of Input Sample", 
+       main = "QQ Plot of Sample Data versus Normal",
+       xlim = c(xmin, xmax), ylim = c(ymin, ymax))
+  par(new = TRUE)
+  plot(qx, qy, type = "l", col="blue", xlab = "Normal Quantiles", 
+       ylab = "Quantiles of Input Sample", 
+       main = "QQ Plot of Sample Data versus Normal",
+       xlim = c(xmin, xmax), ylim = c(ymin, ymax))
+  par(new = TRUE)
+  plot(mx, my, type = "l", xlab = "Normal Quantiles", 
+       ylab = "Quantiles of Input Sample", 
+       main = "QQ Plot of Sample Data versus Normal",
+       xlim = c(xmin, xmax), ylim = c(ymin, ymax))
+  
+}
+
+# Helper Functions
+
+# Position PLot
+PlotPos <- function(Ra){
+  # 
+  sx <- as.matrix(Ra)
+  if (!is.matrix(sx)) {
+    stop("Input should be a matrix.")
+  }
+  n <- dim(sx)[1]
+  m <- dim(sx)[2]
+  if (n == 1){
+    sx <- t(sx)
+    n = m
+    m = 1
+  }
+  nvec <- sum(!is.nan(sx))
+  pp <- RepMat(as.matrix(1:n), 1, m)
+  pp <- (pp - .5)/ RepMat(nvec, n, 1)
+  pp[is.nan(sx)] <- NaN
+  
+  if (nargs() > 1){
+    n <- max(nvec)
+  }
+  return(list("pp" = pp, "n" = n))
+  
+}
+
+# Implementation of repmat from matlab in R
+RepMat <- function(X,m,n){
+  X <- as.matrix(X)
+  mx <- dim(X)[1]
+  nx <- dim(X)[2]
+  a <- matrix(t(matrix(X, mx, nx * n)), mx * m, nx * n, byrow = T)
+  return(a)
+}
\ No newline at end of file

Added: pkg/Dowd/man/MEFPlot.Rd
===================================================================
--- pkg/Dowd/man/MEFPlot.Rd	                        (rev 0)
+++ pkg/Dowd/man/MEFPlot.Rd	2015-06-09 21:12:47 UTC (rev 3666)
@@ -0,0 +1,26 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/MEFPlot.R
+\name{MEFPlot}
+\alias{MEFPlot}
+\title{Mean Excess Function Plot}
+\usage{
+MEFPlot(Ra)
+}
+\arguments{
+\item{Ra}{Vector data}
+}
+\description{
+Plots mean-excess function values of the data set.
+}
+\examples{
+# Plots
+   Ra <- rnorm(1000)
+   MEFPlot(Ra)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+

Added: pkg/Dowd/man/NormalQQPlot.Rd
===================================================================
--- pkg/Dowd/man/NormalQQPlot.Rd	                        (rev 0)
+++ pkg/Dowd/man/NormalQQPlot.Rd	2015-06-09 21:12:47 UTC (rev 3666)
@@ -0,0 +1,26 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/NormalQQPlot.R
+\name{NormalQQPlot}
+\alias{NormalQQPlot}
+\title{Normal Quantile Quantile Plot}
+\usage{
+NormalQQPlot(Ra)
+}
+\arguments{
+\item{Ra}{Vector data set}
+}
+\description{
+Produces an emperical QQ-Plot of the quantiles of the data set 'Ra' versus
+the quantiles of a normal distribution. The purpose of the quantile-quantile plot is to determine whether the sample in 'Ra' is drawn from a normal (i.e., Gaussian) distribution.
+}
+\examples{
+# Description of example
+   Example
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list