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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Jul 5 22:32:33 CEST 2015


Author: dacharya
Date: 2015-07-05 22:32:33 +0200 (Sun, 05 Jul 2015)
New Revision: 3778

Added:
   pkg/Dowd/R/KernelVaRBoxKernel.R
   pkg/Dowd/man/KernelVaRBoxKernel.Rd
Log:
KernelVaRBoxKernel added.

Added: pkg/Dowd/R/KernelVaRBoxKernel.R
===================================================================
--- pkg/Dowd/R/KernelVaRBoxKernel.R	                        (rev 0)
+++ pkg/Dowd/R/KernelVaRBoxKernel.R	2015-07-05 20:32:33 UTC (rev 3778)
@@ -0,0 +1,50 @@
+#' Calculates VaR using box kernel approach
+#' 
+#' The output consists of a scalar VaR for specified confidence level.
+#' 
+#' @param Ra Profit and Loss data set
+#' @param cl VaR confidence level
+#' @return Scalar VaR
+#' @references Dowd, K. Measuring Market Risk, Wiley, 2007.
+#'
+#' @author Dinesh Acharya
+#' @examples
+#' 
+#'    # VaR for specified confidence level using box kernel approach
+#'    Ra <- rnorm(30)
+#'    KernelVaRBoxKernel(Ra, .95)
+#'
+#' @export
+KernelVaRBoxKernel <- function(Ra, cl) {
+  PandL <- as.vector(Ra)
+  mu <- mean(PandL)
+  sigma <- sd(PandL)
+  
+  # Obtain pdf values
+  kernel.data <- density(PandL, kernel = "rectangular", from = mu - 4 * sigma, to = mu + 4 * sigma, n = 1000, bw = "nrd")
+  kernel.pdf <- kernel.data$y
+  x.values <- kernel.data$x
+  delta.x <- x.values[2]-x.values[1]
+  n <- 1000 # = length(x.values)
+  
+  # Obtain cdf values
+  cdf <- double(n)
+  cdf[1] <- kernel.pdf[1] * delta.x
+  for (i in 2:n) {
+    cdf[i] <- kernel.pdf[i] * delta.x + cdf[i - 1]
+  }
+  plot(x.values, kernel.pdf, type="l", main = "Constructed Pdf")
+  
+  # Derivation of required percentile
+  cdf.indices.less.than.prob <- which(cdf<cl)
+  # Gives vector of indices for all cdf-values less than probability
+  max.cdf.index.less.than.prob <- length(cdf.indices.less.than.prob)
+  # Gives index of cdf-value just less than probability
+  lower.x.bound <- x.values[max.cdf.index.less.than.prob]
+  # Gives x-value just below specified probability
+  upper.x.bound <- x.values[max.cdf.index.less.than.prob + 1]
+  # Gives x-value just above specified probability
+  VaR <- (lower.x.bound + upper.x.bound) / 2 # Desired percentile, ie, answer.
+  return(VaR)
+  
+}

Added: pkg/Dowd/man/KernelVaRBoxKernel.Rd
===================================================================
--- pkg/Dowd/man/KernelVaRBoxKernel.Rd	                        (rev 0)
+++ pkg/Dowd/man/KernelVaRBoxKernel.Rd	2015-07-05 20:32:33 UTC (rev 3778)
@@ -0,0 +1,31 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/KernelVaRBoxKernel.R
+\name{KernelVaRBoxKernel}
+\alias{KernelVaRBoxKernel}
+\title{Calculates VaR using box kernel approach}
+\usage{
+KernelVaRBoxKernel(Ra, cl)
+}
+\arguments{
+\item{Ra}{Profit and Loss data set}
+
+\item{cl}{VaR confidence level}
+}
+\value{
+Scalar VaR
+}
+\description{
+The output consists of a scalar VaR for specified confidence level.
+}
+\examples{
+# VaR for specified confidence level using box kernel approach
+   Ra <- rnorm(30)
+   KernelVaRBoxKernel(Ra, .95)
+}
+\author{
+Dinesh Acharya
+}
+\references{
+Dowd, K. Measuring Market Risk, Wiley, 2007.
+}
+



More information about the Returnanalytics-commits mailing list